Completed
Push — add/stats-package ( c3aabb...99d182 )
by
unknown
148:14 queued 140:08
created

Jetpack_Likes_Settings::admin_settings_callback()   F

Complexity

Conditions 19
Paths 1008

Size

Total Lines 57

Duplication

Lines 30
Ratio 52.63 %

Importance

Changes 0
Metric Value
cc 19
nc 1008
nop 0
dl 30
loc 57
rs 0.3499
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Automattic\Jetpack\Sync\Settings;
4
5
class Jetpack_Likes_Settings {
6
	function __construct() {
7
		$this->in_jetpack = ! ( defined( 'IS_WPCOM' ) && IS_WPCOM );
0 ignored issues
show
Bug introduced by
The property in_jetpack does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
8
	}
9
10
	/**
11
	 * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares"
12
	 */
13
	public function add_likes_to_sharing_meta_box_title() {
14
		return __( 'Likes and Shares', 'jetpack' );
15
	}
16
17
	/**
18
	 * Adds a metabox to the post screen if the sharing one doesn't currently exist.
19
	 */
20
	public function add_meta_box() {
21
		if (
22
			/**
23
			 * Allow disabling of the Likes metabox on the post editor screen.
24
			 *
25
			 * @module likes
26
			 *
27
			 * @since 2.2.0
28
			 *
29
			 * @param bool false Should the Likes metabox be disabled? Default to false.
30
			 */
31
		apply_filters( 'post_flair_disable', false )
32
		) {
33
			return;
34
		}
35
36
		$post_types = get_post_types( array( 'public' => true ) );
37
		/**
38
		 * Filters the Likes metabox title.
39
		 *
40
		 * @module likes
41
		 *
42
		 * @since 2.2.0
43
		 *
44
		 * @param string Likes metabox title. Default to "Likes".
45
		 */
46
		$title = apply_filters( 'likes_meta_box_title', __( 'Likes', 'jetpack' ) );
47
		foreach( $post_types as $post_type ) {
48
			add_meta_box( 'likes_meta', $title, array( $this, 'meta_box_content' ), $post_type, 'side', 'default', array( '__back_compat_meta_box' => true ) );
49
		}
50
	}
51
52
	/**
53
	 * Shows the likes option in the post screen metabox.
54
	 */
55
	public function meta_box_content( $post ) {
56
		$post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
57
		$checked         = true;
58
		$disabled        = ! $this->is_enabled_sitewide();
59
		$switched_status = get_post_meta( $post_id, 'switch_like_status', true );
60
61
		if ( $disabled && empty( $switched_status ) || ! $disabled && $switched_status === '0' ) {
62
			$checked = false;
63
		}
64
65
		/**
66
		 * Fires before the Likes meta box content in the post editor.
67
		 *
68
		 * @module likes
69
		 *
70
		 * @since 2.2.0
71
		 *
72
		 * @param WP_Post|array|null $post Post data.
73
		 */
74
		do_action( 'start_likes_meta_box_content', $post );
75
		?>
76
77
		<p>
78
			<label for="wpl_enable_post_likes">
79
				<input type="checkbox" name="wpl_enable_post_likes" id="wpl_enable_post_likes" value="1" <?php checked( $checked ); ?>>
80
				<?php esc_html_e( 'Show likes.', 'jetpack' ); ?>
81
			</label>
82
			<input type="hidden" name="wpl_like_status_hidden" value="1" />
83
		</p> <?php
84
		/**
85
		 * Fires after the Likes meta box content in the post editor.
86
		 *
87
		 * @module likes
88
		 *
89
		 * @since 2.2.0
90
		 *
91
		 * @param WP_Post|array|null $post Post data.
92
		 */
93
		do_action( 'end_likes_meta_box_content', $post );
94
	}
95
96
	/**
97
	 * Returns the current state of the "WordPress.com Likes are" option.
98
	 * @return boolean true if enabled sitewide, false if not
99
	 */
100
	public function is_enabled_sitewide() {
101
		/**
102
		 * Filters whether Likes are enabled by default on all posts.
103
		 * true if enabled sitewide, false if not.
104
		 *
105
		 * @module likes
106
		 *
107
		 * @since 2.2.0
108
		 *
109
		 * @param bool $option Are Likes enabled sitewide.
110
		 */
111
		return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! Jetpack_Options::get_option_and_ensure_autoload( 'disabled_likes', 0 ) );
112
	}
113
114
	public function meta_box_save( $post_id ) {
115
		if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
116
			return $post_id;
117
		}
118
119
		if ( empty( $_POST['wpl_like_status_hidden'] ) ) {
120
			return $post_id;
121
		}
122
123
		// Record sharing disable. Only needs to be done for WPCOM
124
		if ( ! $this->in_jetpack ) {
125
			if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], get_post_types( array( 'public' => true ) ) ) ) {
126 View Code Duplication
				if ( ! isset( $_POST['wpl_enable_post_sharing'] ) ) {
127
					update_post_meta( $post_id, 'sharing_disabled', 1 );
128
				} else {
129
					delete_post_meta( $post_id, 'sharing_disabled' );
130
				}
131
			}
132
		}
133
134
		if ( 'post' == $_POST['post_type'] ) {
135
			if ( !current_user_can( 'edit_post', $post_id ) ) {
136
				return $post_id;
137
			}
138
		}
139
140
		// Record a change in like status for this post - only if it contradicts the
141
		// site like setting. If it doesn't contradict, then we delete the new individual status.
142
		if ( ! $this->is_enabled_sitewide() && ! empty( $_POST['wpl_enable_post_likes'] ) ) {
143
			// Likes turned on for individual posts. User wants to add the button to a single post
144
			update_post_meta( $post_id, 'switch_like_status', 1 );
145
		} else if ( $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) {
146
			// Likes turned on for all posts. User wants to remove the button from a single post
147
			update_post_meta( $post_id, 'switch_like_status', 0 );
148
		} else if (
149
			( ! $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) ||
150
			( $this->is_enabled_sitewide() && ! empty( $_POST['wpl_enable_post_likes'] ) )
151
		) {
152
			// User wants to update the likes button status for an individual post, but the new status
153
			// is the same as if they're asking for the default behavior according to the current Likes setting.
154
			// So we delete the meta.
155
			delete_post_meta( $post_id, 'switch_like_status' );
156
		}
157
158
		return $post_id;
159
	}
160
161
	/**
162
	 * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog)
163
	 */
164
	public function sharing_meta_box_content( $post ) {
165
		$post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
166
		$disabled = get_post_meta( $post_id, 'sharing_disabled', true ); ?>
167
		<p>
168
			<label for="wpl_enable_post_sharing">
169
				<input type="checkbox" name="wpl_enable_post_sharing" id="wpl_enable_post_sharing" value="1" <?php checked( ! $disabled ); ?>>
170
				<?php _e( 'Show sharing buttons.', 'jetpack' ); ?>
171
			</label>
172
			<input type="hidden" name="wpl_sharing_status_hidden" value="1" />
173
		</p> <?php
174
	}
175
176
	/**
177
	 * Adds the 'sharing' menu to the settings menu.
178
	 * Only ran if sharedaddy and publicize are not already active.
179
	 */
180
	function sharing_menu() {
181
		add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) );
182
	}
183
184
	/**
185
	 * Provides a sharing page with the sharing_global_options hook
186
	 * so we can display the setting.
187
	 * Only ran if sharedaddy and publicize are not already active.
188
	 */
189
	function sharing_page() {
190
		$this->updated_message(); ?>
191
		<div class="wrap">
192
			<div class="icon32" id="icon-options-general"><br /></div>
193
			<h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1>
194
			<?php
195
			/** This action is documented in modules/sharedaddy/sharing.php */
196
			do_action( 'pre_admin_screen_sharing' );
197
			?>
198
			<?php $this->sharing_block(); ?>
199
		</div> <?php
200
	}
201
202
	/**
203
	 * Returns the settings have been saved message.
204
	 */
205
	function updated_message() {
206
		if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' ){
207
			echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>';
208
		}
209
	}
210
211
	/**
212
	 * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts
213
	 */
214
	function sharing_block() { ?>
215
		<h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2>
216
		<form method="post" action="">
217
			<table class="form-table">
218
				<tbody>
219
				<?php
220
				/** This action is documented in modules/sharedaddy/sharing.php */
221
				do_action( 'sharing_global_options' );
222
				?>
223
				</tbody>
224
			</table>
225
226
			<p class="submit">
227
				<input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" />
228
			</p>
229
230
			<input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
231
		</form> <?php
232
	}
233
234
	/**
235
	 * Are likes enabled for this post?
236
	 *
237
	 * @param int $post_id
238
	 * @return bool
239
	 */
240
	function is_post_likeable( $post_id = 0 ) {
241
		$post = get_post( $post_id );
242
		if ( ! $post || is_wp_error( $post ) ) {
243
			return false;
244
		}
245
246
		$sitewide_likes_enabled = (bool) $this->is_enabled_sitewide();
247
		$post_likes_switched    = get_post_meta( $post->ID, 'switch_like_status', true );
248
249
		// on WPCOM, we need to look at post edit date so we don't break old posts
250
		// if post edit date predates this code, stick with the former (buggy) behavior
251
		// see: p7DVsv-64H-p2
252
		$last_modified_time = strtotime( $post->post_modified_gmt );
253
254
		$behavior_was_changed_at = strtotime( "2019-02-22 00:40:42" );
255
256
		if ( $this->in_jetpack || $last_modified_time > $behavior_was_changed_at ) {
257
			// the new and improved behavior on Jetpack and recent WPCOM posts:
258
			// $post_likes_switched is empty to follow site setting,
259
			// 0 if we want likes disabled, 1 if we want likes enabled
260
			return $post_likes_switched || ( $sitewide_likes_enabled && $post_likes_switched !== '0' );
261
		}
262
263
		// implicit else (old behavior): $post_likes_switched simply inverts the global setting
264
		return ( (bool) $post_likes_switched ) xor $sitewide_likes_enabled;
265
	}
266
267
	/**
268
	 * Are likes visible in this context?
269
	 *
270
	 * Some of this code was taken and modified from sharing_display() to ensure
271
	 * similar logic and filters apply here, too.
272
	 */
273
	function is_likes_visible() {
274
		if ( Settings::is_syncing() ) {
275
			return false;
276
		}
277
278
		global $wp_current_filter; // Used to apply 'sharing_show' filter
279
280
		$post = get_post();
281
282
		// Never show on feeds or previews
283
		if ( is_feed() || is_preview() ) {
284
			$enabled = false;
285
286
			// Not a feed or preview, so what is it?
287
		} else {
288
289
			if ( in_the_loop() ) {
290
				// If in the loop, check if the current post is likeable
291
				$enabled = $this->is_post_likeable();
292
			} else {
293
				// Otherwise, check and see if likes are enabled sitewide
294
				$enabled = $this->is_enabled_sitewide();
295
			}
296
297
			if ( post_password_required() )
298
				$enabled = false;
299
300
			if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
301
				$enabled = false;
302
			}
303
304
			// Sharing Setting Overrides ****************************************
305
306
			// Single post including custom post types
307
			if ( is_single() ) {
308
				if ( ! $this->is_single_post_enabled( ( $post instanceof WP_Post ) ? $post->post_type : 'post' ) ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
309
					$enabled = false;
310
				}
311
312
				// Single page
313
			} elseif ( is_page() && ! is_front_page() ) {
314
				if ( ! $this->is_single_page_enabled() ) {
315
					$enabled = false;
316
				}
317
318
				// Attachment
319
			} elseif ( is_attachment() ) {
320
				if ( ! $this->is_attachment_enabled() ) {
321
					$enabled = false;
322
				}
323
324
				// All other loops
325
			} elseif ( ! $this->is_index_enabled() ) {
326
				$enabled = false;
327
			}
328
		}
329
330
		if ( $post instanceof WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
331
			// Check that the post is a public, published post.
332 View Code Duplication
			if ( 'attachment' == $post->post_type ) {
333
				$post_status = get_post_status( $post->post_parent );
334
			} else {
335
				$post_status = $post->post_status;
336
			}
337
			if ( 'publish' != $post_status ) {
338
				$enabled = false;
339
			}
340
		}
341
342
		// Run through the sharing filters
343
		/** This filter is documented in modules/sharedaddy/sharing-service.php */
344
		$enabled = apply_filters( 'sharing_show', $enabled, $post );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $post.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
345
346
		/**
347
		 * Filters whether the Likes should be visible or not.
348
		 * Allows overwriting the options set in Settings > Sharing.
349
		 *
350
		 * @module likes
351
		 *
352
		 * @since 2.2.0
353
		 *
354
		 * @param bool $enabled Should the Likes be visible?
355
		 */
356
		return (bool) apply_filters( 'wpl_is_likes_visible', $enabled );
357
	}
358
359
	/**
360
	 * Are Post Likes enabled on single posts?
361
	 *
362
	 * @param String $post_type custom post type identifier
363
	 * @return bool
364
	 */
365 View Code Duplication
	function is_single_post_enabled( $post_type = 'post' ) {
366
		$options = $this->get_options();
367
		return (bool) apply_filters(
368
		/**
369
		 * Filters whether Likes should be enabled on single posts.
370
		 *
371
		 * The dynamic part of the filter, {$post_type}, allows you to specific the post type where Likes should be enabled.
372
		 *
373
		 * @module likes
374
		 *
375
		 * @since 2.2.0
376
		 *
377
		 * @param bool $enabled Are Post Likes enabled on single posts?
378
		 */
379
			"wpl_is_single_{$post_type}_disabled",
380
			(bool) in_array( $post_type, $options['show'] )
381
		);
382
	}
383
384
	/**
385
	 * Get the 'disabled_likes' option from the DB of the current blog.
386
	 *
387
	 * @return array
388
	 */
389
	function get_options() {
390
		$setting             = array();
391
		$setting['disabled'] = get_option( 'disabled_likes'  );
392
		$sharing             = get_option( 'sharing-options' );
393
394
		// Default visibility settings
395
		if ( ! isset( $sharing['global']['show'] ) ) {
396
			$sharing['global']['show'] = array( 'post', 'page' );
397
398
			// Scalar check
399
		} elseif ( is_scalar( $sharing['global']['show'] ) ) {
400
			switch ( $sharing['global']['show'] ) {
401
				case 'posts' :
402
					$sharing['global']['show'] = array( 'post', 'page' );
403
					break;
404
				case 'index' :
405
					$sharing['global']['show'] = array( 'index' );
406
					break;
407
				case 'posts-index' :
408
					$sharing['global']['show'] = array( 'post', 'page', 'index' );
409
					break;
410
			}
411
		}
412
413
		// Ensure it's always an array (even if not previously empty or scalar)
414
		$setting['show'] = ! empty( $sharing['global']['show'] ) ? (array) $sharing['global']['show'] : array();
415
416
		/**
417
		 * Filters where the Likes are displayed.
418
		 *
419
		 * @module likes
420
		 *
421
		 * @since 2.2.0
422
		 *
423
		 * @param array $setting Array of Likes display settings.
424
		 */
425
		return apply_filters( 'wpl_get_options', $setting );
426
	}
427
428
	/**
429
	 * Are Post Likes enabled on archive/front/search pages?
430
	 *
431
	 * @return bool
432
	 */
433
	function is_index_enabled() {
434
		$options = $this->get_options();
435
		/**
436
		 * Filters whether Likes should be enabled on archive/front/search pages.
437
		 *
438
		 * @module likes
439
		 *
440
		 * @since 2.2.0
441
		 *
442
		 * @param bool $enabled Are Post Likes enabled on archive/front/search pages?
443
		 */
444
		return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'] ) );
445
	}
446
447
	/**
448
	 * Are Post Likes enabled on single pages?
449
	 *
450
	 * @return bool
451
	 */
452 View Code Duplication
	function is_single_page_enabled() {
453
		$options = $this->get_options();
454
		/**
455
		 * Filters whether Likes should be enabled on single pages.
456
		 *
457
		 * @module likes
458
		 *
459
		 * @since 2.2.0
460
		 *
461
		 * @param bool $enabled Are Post Likes enabled on single pages?
462
		 */
463
		return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'] ) );
464
	}
465
466
	/**
467
	 * Are Media Likes enabled on single pages?
468
	 *
469
	 * @return bool
470
	 */
471
	function is_attachment_enabled() {
472
		$options = $this->get_options();
473
		/**
474
		 * Filters whether Likes should be enabled on attachment pages.
475
		 *
476
		 * @module likes
477
		 *
478
		 * @since 2.2.0
479
		 *
480
		 * @param bool $enabled Are Post Likes enabled on attachment pages?
481
		 */
482
		return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'] ) );
483
	}
484
485
	/**
486
	 * The actual options block to be inserted into the sharing page.
487
	 */
488
	function admin_settings_init() {
489
		?>
490
		<tr>
491
			<th scope="row">
492
				<label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label>
493
			</th>
494
			<td>
495
				<div>
496
					<label>
497
						<input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> />
498
						<?php esc_html_e( 'On for all posts', 'jetpack' ); ?>
499
					</label>
500
				</div>
501
				<div>
502
					<label>
503
						<input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> />
504
						<?php esc_html_e( 'Turned on per post', 'jetpack' ); ?>
505
					</label>
506
					<div>
507
			</td>
508
		</tr>
509
		<?php if ( ! $this->in_jetpack ) : ?>
510
			<tr>
511
				<th scope="row">
512
					<label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack' ); ?></label>
513
				</th>
514
				<td>
515
					<div>
516
						<label>
517
							<input type="radio" class="code" name="jetpack_reblogs_enabled" value="on" <?php checked( $this->reblogs_enabled_sitewide(), true ); ?> />
518
							<?php esc_html_e( 'Show the Reblog button on posts', 'jetpack' ); ?>
519
						</label>
520
					</div>
521
					<div>
522
						<label>
523
							<input type="radio" class="code" name="jetpack_reblogs_enabled" value="off" <?php checked( $this->reblogs_enabled_sitewide(), false ); ?> />
524
							<?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack' ); ?>
525
						</label>
526
					</div>
527
				</td>
528
			</tr>
529
			<!-- WPCOM only: Comment Likes -->
530
			<?php if ( ! $this->in_jetpack ) : ?>
531
				<tr>
532
					<th scope="row">
533
						<label><?php esc_html_e( 'Comment Likes are', 'jetpack' ); ?></label>
534
					</th>
535
					<td>
536
						<div>
537
							<label>
538
								<input type="checkbox" class="code" name="jetpack_comment_likes_enabled" value="1" <?php checked( $this->is_comments_enabled(), true ); ?> />
539
								<?php esc_html_e( 'On for all comments', 'jetpack' ); ?>
540
							</label>
541
						</div>
542
					</td>
543
				</tr>
544
			<?php endif; ?>
545
		<?php endif; ?>
546
		</tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?>
547
	<?php
548
	}
549
550
	/**
551
	 * Returns the current state of the "WordPress.com Reblogs are" option.
552
	 * @return boolean true if enabled sitewide, false if not
553
	 */
554
	function reblogs_enabled_sitewide() {
555
		/**
556
		 * Filters whether Reblogs are enabled by default on all posts.
557
		 * true if enabled sitewide, false if not.
558
		 *
559
		 * @module likes
560
		 *
561
		 * @since 3.0.0
562
		 *
563
		 * @param bool $option Are Reblogs enabled sitewide.
564
		 */
565
		return (bool) apply_filters( 'wpl_reblogging_enabled_sitewide', ! get_option( 'disabled_reblogs' ) );
566
	}
567
568
	/**
569
	 * Used for WPCOM ONLY. Comment likes are in their own module in Jetpack.
570
	 * Returns if comment likes are enabled. Defaults to 'off'
571
	 * @return boolean true if we should show comment likes, false if not
572
	 */
573
	function is_comments_enabled() {
574
		/**
575
		 * Filters whether Comment Likes are enabled.
576
		 * true if enabled, false if not.
577
		 *
578
		 * @module comment-likes
579
		 *
580
		 * @since 2.2.0
581
		 *
582
		 * @param bool $option Are Comment Likes enabled sitewide.
583
		 */
584
		return (bool) apply_filters( 'jetpack_comment_likes_enabled', get_option( 'jetpack_comment_likes_enabled', false ) );
585
	}
586
587
	/**
588
	 * Saves the setting in the database, bumps a stat on WordPress.com
589
	 */
590
	function admin_settings_callback() {
591
		// We're looking for these, and doing a dance to set some stats and save
592
		// them together in array option.
593
		$new_state = ! empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on';
594
		$db_state  = $this->is_enabled_sitewide();
595
596
		$reblogs_new_state = ! empty( $_POST['jetpack_reblogs_enabled'] ) ? $_POST['jetpack_reblogs_enabled'] : 'on';
597
		$reblogs_db_state = $this->reblogs_enabled_sitewide();
598
		/** Default State *********************************************************/
599
600
		// Checked (enabled)
601 View Code Duplication
		switch( $new_state ) {
602
			case 'off' :
603
				if ( true == $db_state && ! $this->in_jetpack ) {
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...
604
					$g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=disabled_likes' );
0 ignored issues
show
Unused Code introduced by
$g_gif is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
605
				}
606
				update_option( 'disabled_likes', 1 );
607
				break;
608
			case 'on'  :
609
			default:
610
				if ( false == $db_state && ! $this->in_jetpack ) {
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...
611
					$g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=reenabled_likes' );
0 ignored issues
show
Unused Code introduced by
$g_gif is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
612
				}
613
				delete_option( 'disabled_likes' );
614
				break;
615
		}
616
617 View Code Duplication
		switch( $reblogs_new_state ) {
618
			case 'off' :
619
				if ( true == $reblogs_db_state && ! $this->in_jetpack ) {
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...
620
					$g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=disabled_reblogs' );
0 ignored issues
show
Unused Code introduced by
$g_gif is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
621
				}
622
				update_option( 'disabled_reblogs', 1 );
623
				break;
624
			case 'on'  :
625
			default:
626
				if ( false == $reblogs_db_state && ! $this->in_jetpack ) {
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...
627
					$g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=reenabled_reblogs' );
0 ignored issues
show
Unused Code introduced by
$g_gif is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
628
				}
629
				delete_option( 'disabled_reblogs' );
630
				break;
631
		}
632
633
		// WPCOM only: Comment Likes
634
		if ( ! $this->in_jetpack ) {
635
			$new_comments_state = ! empty( $_POST['jetpack_comment_likes_enabled'] ) ? $_POST['jetpack_comment_likes_enabled'] : false;
636
			switch( (bool) $new_comments_state ) {
637
				case true:
638
					update_option( 'jetpack_comment_likes_enabled', 1 );
639
				break;
640
				case false:
641
				default:
642
					update_option( 'jetpack_comment_likes_enabled', 0 );
643
				break;
644
			}
645
		}
646
	}
647
648
	/**
649
	 * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled.
650
	 */
651
	function process_update_requests_if_sharedaddy_not_loaded() {
652
		if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) {
653
			if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) {
654
				/** This action is documented in modules/sharedaddy/sharing.php */
655
				do_action( 'sharing_admin_update' );
656
				wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
657
				die();
658
			}
659
		}
660
	}
661
662
	/**
663
	 * 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.
664
	 */
665
	function admin_settings_showbuttonon_init() {
666
		/** This action is documented in modules/sharedaddy/sharing.php */
667
		echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' );
668
		?>
669
		<th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th>
670
		<td>
671
			<?php
672
			$br = false;
673
			$shows = array_values( get_post_types( array( 'public' => true ) ) );
674
			array_unshift( $shows, 'index' );
675
			$global = $this->get_options();
676 View Code Duplication
			foreach ( $shows as $show ) :
677
				if ( 'index' == $show ) {
678
					$label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
679
				} else {
680
					$post_type_object = get_post_type_object( $show );
681
					$label = $post_type_object->labels->name;
682
				}
683
				?>
684
				<?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>
685
				<?php	$br = true; endforeach; ?>
686
		</td>
687
		<?php
688
		/** This action is documented in modules/sharedaddy/sharing.php */
689
		echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' );
690
		?>
691
	<?php
692
	}
693
694
	/**
695
	 * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option.
696
	 */
697
	function admin_settings_showbuttonon_callback() {
698
		$options = get_option( 'sharing-options' );
699
		if ( !is_array( $options ) )
700
			$options = array();
701
702
		$shows = array_values( get_post_types( array( 'public' => true ) ) );
703
		$shows[] = 'index';
704
		$data = $_POST;
705
706
		if ( isset( $data['show'] ) ) {
707
			if ( is_scalar( $data['show'] ) ) {
708
				switch ( $data['show'] ) {
709
					case 'posts' :
710
						$data['show'] = array( 'post', 'page' );
711
						break;
712
					case 'index' :
713
						$data['show'] = array( 'index' );
714
						break;
715
					case 'posts-index' :
716
						$data['show'] = array( 'post', 'page', 'index' );
717
						break;
718
				}
719
			}
720
721 View Code Duplication
			if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
722
				$options['global']['show'] = $data['show'];
723
			}
724
		} else {
725
			$options['global']['show'] = array();
726
		}
727
728
		update_option( 'sharing-options', $options );
729
	}
730
}
731