Completed
Push — update/ad-gutenblock-load ( 0c433b...289acb )
by
unknown
12:28 queued 05:20
created

Jetpack_Likes_Settings::meta_box_save()   D

Complexity

Conditions 18
Paths 38

Size

Total Lines 46

Duplication

Lines 5
Ratio 10.87 %

Importance

Changes 0
Metric Value
cc 18
nc 38
nop 1
dl 5
loc 46
rs 4.8666
c 0
b 0
f 0

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