Completed
Push — fix/typo-pro-promo ( e5832e...fc698e )
by
unknown
29:20
created

modules/likes/jetpack-likes-settings.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class Jetpack_Likes_Settings {
4
	function __construct() {
5
		$this->in_jetpack = ! ( defined( 'IS_WPCOM' ) && IS_WPCOM );
6
	}
7
8
	/**
9
	 * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares"
10
	 */
11
	public function add_likes_to_sharing_meta_box_title() {
12
		return __( 'Likes and Shares', 'jetpack' );
13
	}
14
15
	/**
16
	 * Adds a metabox to the post screen if the sharing one doesn't currently exist.
17
	 */
18
	public function add_meta_box() {
19
		if (
20
			/**
21
			 * Allow disabling of the Likes metabox on the post editor screen.
22
			 *
23
			 * @module likes
24
			 *
25
			 * @since 2.2.0
26
			 *
27
			 * @param bool false Should the Likes metabox be disabled? Default to false.
28
			 */
29
		apply_filters( 'post_flair_disable', false )
30
		) {
31
			return;
32
		}
33
34
		$post_types = get_post_types( array( 'public' => true ) );
35
		/**
36
		 * Filters the Likes metabox title.
37
		 *
38
		 * @module likes
39
		 *
40
		 * @since 2.2.0
41
		 *
42
		 * @param string Likes metabox title. Default to "Likes".
43
		 */
44
		$title = apply_filters( 'likes_meta_box_title', __( 'Likes', 'jetpack' ) );
45
		foreach( $post_types as $post_type ) {
46
			add_meta_box( 'likes_meta', $title, array( $this, 'meta_box_content' ), $post_type, 'side', 'default' );
47
		}
48
	}
49
50
	/**
51
	 * Shows the likes option in the post screen metabox.
52
	 */
53
	public function meta_box_content( $post ) {
54
		$post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
55
		$checked         = true;
56
		$disabled        = ! $this->is_enabled_sitewide();
57
		$switched_status = get_post_meta( $post_id, 'switch_like_status', true );
58
59
		if ( $disabled && empty( $switched_status ) || false == $disabled && !empty( $switched_status ) )
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
60
			$checked = false;
61
62
		/**
63
		 * Fires before the Likes meta box content in the post editor.
64
		 *
65
		 * @module likes
66
		 *
67
		 * @since 2.2.0
68
		 *
69
		 * @param WP_Post|array|null $post Post data.
70
		 */
71
		do_action( 'start_likes_meta_box_content', $post );
72
		?>
73
74
		<p>
75
			<label for="wpl_enable_post_likes">
76
				<input type="checkbox" name="wpl_enable_post_likes" id="wpl_enable_post_likes" value="1" <?php checked( $checked ); ?>>
77
				<?php esc_html_e( 'Show likes.', 'jetpack' ); ?>
78
			</label>
79
			<input type="hidden" name="wpl_like_status_hidden" value="1" />
80
		</p> <?php
81
		/**
82
		 * Fires after the Likes meta box content in the post editor.
83
		 *
84
		 * @module likes
85
		 *
86
		 * @since 2.2.0
87
		 *
88
		 * @param WP_Post|array|null $post Post data.
89
		 */
90
		do_action( 'end_likes_meta_box_content', $post );
91
	}
92
93
	/**
94
	 * Returns the current state of the "WordPress.com Likes are" option.
95
	 * @return boolean true if enabled sitewide, false if not
96
	 */
97
	public function is_enabled_sitewide() {
98
		/**
99
		 * Filters whether Likes are enabled by default on all posts.
100
		 * true if enabled sitewide, false if not.
101
		 *
102
		 * @module likes
103
		 *
104
		 * @since 2.2.0
105
		 *
106
		 * @param bool $option Are Likes enabled sitewide.
107
		 */
108
		return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! Jetpack_Options::get_option_and_ensure_autoload( 'disabled_likes', 0 ) );
109
	}
110
111
	public function meta_box_save( $post_id ) {
112
		if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
113
			return $post_id;
114
		}
115
116
		if ( empty( $_POST['wpl_like_status_hidden'] ) ) {
117
			return $post_id;
118
		}
119
120
		// Record sharing disable. Only needs to be done for WPCOM
121
		if ( ! $this->in_jetpack ) {
122
			if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], get_post_types( array( 'public' => true ) ) ) ) {
123 View Code Duplication
				if ( ! isset( $_POST['wpl_enable_post_sharing'] ) ) {
124
					update_post_meta( $post_id, 'sharing_disabled', 1 );
125
				} else {
126
					delete_post_meta( $post_id, 'sharing_disabled' );
127
				}
128
			}
129
		}
130
131
		if ( 'post' == $_POST['post_type'] ) {
132
			if ( !current_user_can( 'edit_post', $post_id ) ) {
133
				return $post_id;
134
			}
135
		}
136
137
		// Record a change in like status for this post - only if it contradicts the
138
		// site like setting.
139
		if ( ( $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) || ( ! $this->is_enabled_sitewide() && ! empty( $_POST['wpl_enable_post_likes'] ) ) ) {
140
			update_post_meta( $post_id, 'switch_like_status', 1 );
141
		} else {
142
			delete_post_meta( $post_id, 'switch_like_status' );
143
		}
144
145
		return $post_id;
146
	}
147
148
	/**
149
	 * Adds the 'sharing' menu to the settings menu.
150
	 * Only ran if sharedaddy and publicize are not already active.
151
	 */
152
	function sharing_menu() {
153
		add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) );
154
	}
155
156
	/**
157
	 * Provides a sharing page with the sharing_global_options hook
158
	 * so we can display the setting.
159
	 * Only ran if sharedaddy and publicize are not already active.
160
	 */
161
	function sharing_page() {
162
		$this->updated_message(); ?>
163
		<div class="wrap">
164
			<div class="icon32" id="icon-options-general"><br /></div>
165
			<h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1>
166
			<?php
167
			/** This action is documented in modules/sharedaddy/sharing.php */
168
			do_action( 'pre_admin_screen_sharing' );
169
			?>
170
			<?php $this->sharing_block(); ?>
171
		</div> <?php
172
	}
173
174
	/**
175
	 * Returns the settings have been saved message.
176
	 */
177
	function updated_message() {
178
		if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' ){
179
			echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>';
180
		}
181
	}
182
183
	/**
184
	 * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts
185
	 */
186
	function sharing_block() { ?>
187
		<h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2>
188
		<form method="post" action="">
189
			<table class="form-table">
190
				<tbody>
191
				<?php
192
				/** This action is documented in modules/sharedaddy/sharing.php */
193
				do_action( 'sharing_global_options' );
194
				?>
195
				</tbody>
196
			</table>
197
198
			<p class="submit">
199
				<input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" />
200
			</p>
201
202
			<input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
203
		</form> <?php
204
	}
205
206
	/**
207
	 * Are likes enabled for this post?
208
	 *
209
	 * @param int $post_id
210
	 * @return bool
211
	 */
212
	function is_post_likeable( $post_id = 0 ) {
213
		$post = get_post( $post_id );
214
		if ( !$post || is_wp_error( $post ) ) {
215
			return false;
216
		}
217
218
		$sitewide_likes_enabled = (bool) $this->is_enabled_sitewide();
219
		$post_likes_switched    = (bool) get_post_meta( $post->ID, 'switch_like_status', true );
220
221
		return $post_likes_switched xor $sitewide_likes_enabled;
222
	}
223
224
	/**
225
	 * Are likes visible in this context?
226
	 *
227
	 * Some of this code was taken and modified from sharing_display() to ensure
228
	 * similar logic and filters apply here, too.
229
	 */
230
	function is_likes_visible() {
231
		require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php';
232
		if ( Jetpack_Sync_Settings::is_syncing() ) {
233
			return false;
234
		}
235
236
		global $wp_current_filter; // Used to apply 'sharing_show' filter
237
238
		$post = get_post();
239
240
		// Never show on feeds or previews
241
		if ( is_feed() || is_preview() ) {
242
			$enabled = false;
243
244
			// Not a feed or preview, so what is it?
245
		} else {
246
247
			if ( in_the_loop() ) {
248
				// If in the loop, check if the current post is likeable
249
				$enabled = $this->is_post_likeable();
250
			} else {
251
				// Otherwise, check and see if likes are enabled sitewide
252
				$enabled = $this->is_enabled_sitewide();
253
			}
254
255
			if ( post_password_required() )
256
				$enabled = false;
257
258
			if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
259
				$enabled = false;
260
			}
261
262
			// Sharing Setting Overrides ****************************************
263
264
			// Single post including custom post types
265
			if ( is_single() ) {
266
				if ( ! $this->is_single_post_enabled( $post->post_type ) ) {
267
					$enabled = false;
268
				}
269
270
				// Single page
271
			} elseif ( is_page() && ! is_front_page() ) {
272
				if ( ! $this->is_single_page_enabled() ) {
273
					$enabled = false;
274
				}
275
276
				// Attachment
277
			} elseif ( is_attachment() ) {
278
				if ( ! $this->is_attachment_enabled() ) {
279
					$enabled = false;
280
				}
281
282
				// All other loops
283
			} elseif ( ! $this->is_index_enabled() ) {
284
				$enabled = false;
285
			}
286
		}
287
288
		if ( $post instanceof WP_Post ) {
289
			// Check that the post is a public, published post.
290 View Code Duplication
			if ( 'attachment' == $post->post_type ) {
291
				$post_status = get_post_status( $post->post_parent );
292
			} else {
293
				$post_status = $post->post_status;
294
			}
295
			if ( 'publish' != $post_status ) {
296
				$enabled = false;
297
			}
298
		}
299
300
		// Run through the sharing filters
301
		/** This filter is documented in modules/sharedaddy/sharing-service.php */
302
		$enabled = apply_filters( 'sharing_show', $enabled, $post );
303
304
		/**
305
		 * Filters whether the Likes should be visible or not.
306
		 * Allows overwriting the options set in Settings > Sharing.
307
		 *
308
		 * @module likes
309
		 *
310
		 * @since 2.2.0
311
		 *
312
		 * @param bool $enabled Should the Likes be visible?
313
		 */
314
		return (bool) apply_filters( 'wpl_is_likes_visible', $enabled );
315
	}
316
317
	/**
318
	 * Are Post Likes enabled on single posts?
319
	 *
320
	 * @param String $post_type custom post type identifier
321
	 * @return bool
322
	 */
323 View Code Duplication
	function is_single_post_enabled( $post_type = 'post' ) {
324
		$options = $this->get_options();
325
		return (bool) apply_filters(
326
		/**
327
		 * Filters whether Likes should be enabled on single posts.
328
		 *
329
		 * The dynamic part of the filter, {$post_type}, allows you to specific the post type where Likes should be enabled.
330
		 *
331
		 * @module likes
332
		 *
333
		 * @since 2.2.0
334
		 *
335
		 * @param bool $enabled Are Post Likes enabled on single posts?
336
		 */
337
			"wpl_is_single_{$post_type}_disabled",
338
			(bool) in_array( $post_type, $options['show'] )
339
		);
340
	}
341
342
	/**
343
	 * Get the 'disabled_likes' option from the DB of the current blog.
344
	 *
345
	 * @return array
346
	 */
347
	function get_options() {
348
		$setting             = array();
349
		$setting['disabled'] = get_option( 'disabled_likes'  );
350
		$sharing             = get_option( 'sharing-options' );
351
352
		// Default visibility settings
353
		if ( ! isset( $sharing['global']['show'] ) ) {
354
			$sharing['global']['show'] = array( 'post', 'page' );
355
356
			// Scalar check
357
		} elseif ( is_scalar( $sharing['global']['show'] ) ) {
358
			switch ( $sharing['global']['show'] ) {
359
				case 'posts' :
360
					$sharing['global']['show'] = array( 'post', 'page' );
361
					break;
362
				case 'index' :
363
					$sharing['global']['show'] = array( 'index' );
364
					break;
365
				case 'posts-index' :
366
					$sharing['global']['show'] = array( 'post', 'page', 'index' );
367
					break;
368
			}
369
		}
370
371
		// Ensure it's always an array (even if not previously empty or scalar)
372
		$setting['show'] = !empty( $sharing['global']['show'] ) ? (array) $sharing['global']['show'] : array();
373
374
		/**
375
		 * Filters where the Likes are displayed.
376
		 *
377
		 * @module likes
378
		 *
379
		 * @since 2.2.0
380
		 *
381
		 * @param array $setting Array of Likes display settings.
382
		 */
383
		return apply_filters( 'wpl_get_options', $setting );
384
	}
385
386
	/**
387
	 * Are Post Likes enabled on archive/front/search pages?
388
	 *
389
	 * @return bool
390
	 */
391
	function is_index_enabled() {
392
		$options = $this->get_options();
393
		/**
394
		 * Filters whether Likes should be enabled on archive/front/search pages.
395
		 *
396
		 * @module likes
397
		 *
398
		 * @since 2.2.0
399
		 *
400
		 * @param bool $enabled Are Post Likes enabled on archive/front/search pages?
401
		 */
402
		return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'] ) );
403
	}
404
405
	/**
406
	 * Are Post Likes enabled on single pages?
407
	 *
408
	 * @return bool
409
	 */
410 View Code Duplication
	function is_single_page_enabled() {
411
		$options = $this->get_options();
412
		/**
413
		 * Filters whether Likes should be enabled on single pages.
414
		 *
415
		 * @module likes
416
		 *
417
		 * @since 2.2.0
418
		 *
419
		 * @param bool $enabled Are Post Likes enabled on single pages?
420
		 */
421
		return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'] ) );
422
	}
423
424
	/**
425
	 * Are Media Likes enabled on single pages?
426
	 *
427
	 * @return bool
428
	 */
429
	function is_attachment_enabled() {
430
		$options = $this->get_options();
431
		/**
432
		 * Filters whether Likes should be enabled on attachment pages.
433
		 *
434
		 * @module likes
435
		 *
436
		 * @since 2.2.0
437
		 *
438
		 * @param bool $enabled Are Post Likes enabled on attachment pages?
439
		 */
440
		return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'] ) );
441
	}
442
443
	/**
444
	 * The actual options block to be inserted into the sharing page.
445
	 */
446
	function admin_settings_init() {
447
		?>
448
		<tr>
449
			<th scope="row">
450
				<label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label>
451
			</th>
452
			<td>
453
				<div>
454
					<label>
455
						<input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> />
456
						<?php esc_html_e( 'On for all posts', 'jetpack' ); ?>
457
					</label>
458
				</div>
459
				<div>
460
					<label>
461
						<input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> />
462
						<?php esc_html_e( 'Turned on per post', 'jetpack' ); ?>
463
					</label>
464
					<div>
465
			</td>
466
		</tr>
467
		<?php if ( ! $this->in_jetpack ) : ?>
468
			<tr>
469
				<th scope="row">
470
					<label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack' ); ?></label>
471
				</th>
472
				<td>
473
					<div>
474
						<label>
475
							<input type="radio" class="code" name="jetpack_reblogs_enabled" value="on" <?php checked( $this->reblogs_enabled_sitewide(), true ); ?> />
476
							<?php esc_html_e( 'Show the Reblog button on posts', 'jetpack' ); ?>
477
						</label>
478
					</div>
479
					<div>
480
						<label>
481
							<input type="radio" class="code" name="jetpack_reblogs_enabled" value="off" <?php checked( $this->reblogs_enabled_sitewide(), false ); ?> />
482
							<?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack' ); ?>
483
						</label>
484
						<div>
485
				</td>
486
			</tr>
487
		<?php endif; ?>
488
		</tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?>
489
	<?php
490
	}
491
492
	/**
493
	 * Returns the current state of the "WordPress.com Reblogs are" option.
494
	 * @return boolean true if enabled sitewide, false if not
495
	 */
496
	function reblogs_enabled_sitewide() {
497
		/**
498
		 * Filters whether Reblogs are enabled by default on all posts.
499
		 * true if enabled sitewide, false if not.
500
		 *
501
		 * @module likes
502
		 *
503
		 * @since 3.0.0
504
		 *
505
		 * @param bool $option Are Reblogs enabled sitewide.
506
		 */
507
		return (bool) apply_filters( 'wpl_reblogging_enabled_sitewide', ! get_option( 'disabled_reblogs' ) );
508
	}
509
510
	/**
511
	 * Saves the setting in the database, bumps a stat on WordPress.com
512
	 */
513
	function admin_settings_callback() {
514
		// We're looking for these, and doing a dance to set some stats and save
515
		// them together in array option.
516
		$new_state = !empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on';
517
		$db_state  = $this->is_enabled_sitewide();
518
519
		$reblogs_new_state = !empty( $_POST['jetpack_reblogs_enabled'] ) ? $_POST['jetpack_reblogs_enabled'] : 'on';
520
		$reblogs_db_state = $this->reblogs_enabled_sitewide();
521
		/** Default State *********************************************************/
522
523
		// Checked (enabled)
524 View Code Duplication
		switch( $new_state ) {
525
			case 'off' :
526
				if ( true == $db_state && ! $this->in_jetpack ) {
527
					$g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=disabled_likes' );
528
				}
529
				update_option( 'disabled_likes', 1 );
530
				break;
531
			case 'on'  :
532
			default:
533
				if ( false == $db_state && ! $this->in_jetpack ) {
534
					$g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=reenabled_likes' );
535
				}
536
				delete_option( 'disabled_likes' );
537
				break;
538
		}
539
540 View Code Duplication
		switch( $reblogs_new_state ) {
541
			case 'off' :
542
				if ( true == $reblogs_db_state && ! $this->in_jetpack ) {
543
					$g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=disabled_reblogs' );
544
				}
545
				update_option( 'disabled_reblogs', 1 );
546
				break;
547
			case 'on'  :
548
			default:
549
				if ( false == $reblogs_db_state && ! $this->in_jetpack ) {
550
					$g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=reenabled_reblogs' );
551
				}
552
				delete_option( 'disabled_reblogs' );
553
				break;
554
		}
555
	}
556
557
	/**
558
	 * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled.
559
	 */
560
	function process_update_requests_if_sharedaddy_not_loaded() {
561
		if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) {
562
			if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) {
563
				/** This action is documented in modules/sharedaddy/sharing.php */
564
				do_action( 'sharing_admin_update' );
565
				wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
566
				die();
567
			}
568
		}
569
	}
570
571
	/**
572
	 * If sharedaddy is not loaded, we don't have the "Show buttons on" yet, so we need to add that since it affects likes too.
573
	 */
574
	function admin_settings_showbuttonon_init() {
575
		?>
576
		<?php
577
		/** This action is documented in modules/sharedaddy/sharing.php */
578
		echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' );
579
		?>
580
		<th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th>
581
		<td>
582
			<?php
583
			$br = false;
584
			$shows = array_values( get_post_types( array( 'public' => true ) ) );
585
			array_unshift( $shows, 'index' );
586
			$global = $this->get_options();
587 View Code Duplication
			foreach ( $shows as $show ) :
588
				if ( 'index' == $show ) {
589
					$label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
590
				} else {
591
					$post_type_object = get_post_type_object( $show );
592
					$label = $post_type_object->labels->name;
593
				}
594
				?>
595
				<?php if ( $br ) echo '<br />'; ?><label><input type="checkbox"<?php checked( in_array( $show, $global['show'] ) ); ?> name="show[]" value="<?php echo esc_attr( $show ); ?>" /> <?php echo esc_html( $label ); ?></label>
596
				<?php	$br = true; endforeach; ?>
597
		</td>
598
		<?php
599
		/** This action is documented in modules/sharedaddy/sharing.php */
600
		echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' );
601
		?>
602
	<?php
603
	}
604
605
	/**
606
	 * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option.
607
	 */
608
	function admin_settings_showbuttonon_callback() {
609
		$options = get_option( 'sharing-options' );
610
		if ( !is_array( $options ) )
611
			$options = array();
612
613
		$shows = array_values( get_post_types( array( 'public' => true ) ) );
614
		$shows[] = 'index';
615
		$data = $_POST;
616
617
		if ( isset( $data['show'] ) ) {
618 View Code Duplication
			if ( is_scalar( $data['show'] ) ) {
619
				switch ( $data['show'] ) {
620
					case 'posts' :
621
						$data['show'] = array( 'post', 'page' );
622
						break;
623
					case 'index' :
624
						$data['show'] = array( 'index' );
625
						break;
626
					case 'posts-index' :
627
						$data['show'] = array( 'post', 'page', 'index' );
628
						break;
629
				}
630
			}
631
632 View Code Duplication
			if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
633
				$options['global']['show'] = $data['show'];
634
			}
635
		} else {
636
			$options['global']['show'] = array();
637
		}
638
639
		update_option( 'sharing-options', $options );
640
	}
641
}
642