Completed
Push — fix/slideshow-dependencies ( a328c8...25d99e )
by
unknown
12:50 queued 06:11
created

Jetpack_Likes_Settings::admin_settings_init()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 61
rs 8.8509
c 0
b 0
f 0

How to fix   Long Method   

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
		return $post_likes_switched || ( $sitewide_likes_enabled && $post_likes_switched !== '0' );
248
	}
249
250
	/**
251
	 * Are likes visible in this context?
252
	 *
253
	 * Some of this code was taken and modified from sharing_display() to ensure
254
	 * similar logic and filters apply here, too.
255
	 */
256
	function is_likes_visible() {
257
		require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php';
258
		if ( Jetpack_Sync_Settings::is_syncing() ) {
259
			return false;
260
		}
261
262
		global $wp_current_filter; // Used to apply 'sharing_show' filter
263
264
		$post = get_post();
265
266
		// Never show on feeds or previews
267
		if ( is_feed() || is_preview() ) {
268
			$enabled = false;
269
270
			// Not a feed or preview, so what is it?
271
		} else {
272
273
			if ( in_the_loop() ) {
274
				// If in the loop, check if the current post is likeable
275
				$enabled = $this->is_post_likeable();
276
			} else {
277
				// Otherwise, check and see if likes are enabled sitewide
278
				$enabled = $this->is_enabled_sitewide();
279
			}
280
281
			if ( post_password_required() )
282
				$enabled = false;
283
284
			if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
285
				$enabled = false;
286
			}
287
288
			// Sharing Setting Overrides ****************************************
289
290
			// Single post including custom post types
291
			if ( is_single() ) {
292
				if ( ! $this->is_single_post_enabled( $post->post_type ) ) {
293
					$enabled = false;
294
				}
295
296
				// Single page
297
			} elseif ( is_page() && ! is_front_page() ) {
298
				if ( ! $this->is_single_page_enabled() ) {
299
					$enabled = false;
300
				}
301
302
				// Attachment
303
			} elseif ( is_attachment() ) {
304
				if ( ! $this->is_attachment_enabled() ) {
305
					$enabled = false;
306
				}
307
308
				// All other loops
309
			} elseif ( ! $this->is_index_enabled() ) {
310
				$enabled = false;
311
			}
312
		}
313
314
		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...
315
			// Check that the post is a public, published post.
316 View Code Duplication
			if ( 'attachment' == $post->post_type ) {
317
				$post_status = get_post_status( $post->post_parent );
318
			} else {
319
				$post_status = $post->post_status;
320
			}
321
			if ( 'publish' != $post_status ) {
322
				$enabled = false;
323
			}
324
		}
325
326
		// Run through the sharing filters
327
		/** This filter is documented in modules/sharedaddy/sharing-service.php */
328
		$enabled = apply_filters( 'sharing_show', $enabled, $post );
329
330
		/**
331
		 * Filters whether the Likes should be visible or not.
332
		 * Allows overwriting the options set in Settings > Sharing.
333
		 *
334
		 * @module likes
335
		 *
336
		 * @since 2.2.0
337
		 *
338
		 * @param bool $enabled Should the Likes be visible?
339
		 */
340
		return (bool) apply_filters( 'wpl_is_likes_visible', $enabled );
341
	}
342
343
	/**
344
	 * Are Post Likes enabled on single posts?
345
	 *
346
	 * @param String $post_type custom post type identifier
347
	 * @return bool
348
	 */
349 View Code Duplication
	function is_single_post_enabled( $post_type = 'post' ) {
350
		$options = $this->get_options();
351
		return (bool) apply_filters(
352
		/**
353
		 * Filters whether Likes should be enabled on single posts.
354
		 *
355
		 * The dynamic part of the filter, {$post_type}, allows you to specific the post type where Likes should be enabled.
356
		 *
357
		 * @module likes
358
		 *
359
		 * @since 2.2.0
360
		 *
361
		 * @param bool $enabled Are Post Likes enabled on single posts?
362
		 */
363
			"wpl_is_single_{$post_type}_disabled",
364
			(bool) in_array( $post_type, $options['show'] )
365
		);
366
	}
367
368
	/**
369
	 * Get the 'disabled_likes' option from the DB of the current blog.
370
	 *
371
	 * @return array
372
	 */
373
	function get_options() {
374
		$setting             = array();
375
		$setting['disabled'] = get_option( 'disabled_likes'  );
376
		$sharing             = get_option( 'sharing-options' );
377
378
		// Default visibility settings
379
		if ( ! isset( $sharing['global']['show'] ) ) {
380
			$sharing['global']['show'] = array( 'post', 'page' );
381
382
			// Scalar check
383
		} elseif ( is_scalar( $sharing['global']['show'] ) ) {
384
			switch ( $sharing['global']['show'] ) {
385
				case 'posts' :
386
					$sharing['global']['show'] = array( 'post', 'page' );
387
					break;
388
				case 'index' :
389
					$sharing['global']['show'] = array( 'index' );
390
					break;
391
				case 'posts-index' :
392
					$sharing['global']['show'] = array( 'post', 'page', 'index' );
393
					break;
394
			}
395
		}
396
397
		// Ensure it's always an array (even if not previously empty or scalar)
398
		$setting['show'] = ! empty( $sharing['global']['show'] ) ? (array) $sharing['global']['show'] : array();
399
400
		/**
401
		 * Filters where the Likes are displayed.
402
		 *
403
		 * @module likes
404
		 *
405
		 * @since 2.2.0
406
		 *
407
		 * @param array $setting Array of Likes display settings.
408
		 */
409
		return apply_filters( 'wpl_get_options', $setting );
410
	}
411
412
	/**
413
	 * Are Post Likes enabled on archive/front/search pages?
414
	 *
415
	 * @return bool
416
	 */
417
	function is_index_enabled() {
418
		$options = $this->get_options();
419
		/**
420
		 * Filters whether Likes should be enabled on archive/front/search pages.
421
		 *
422
		 * @module likes
423
		 *
424
		 * @since 2.2.0
425
		 *
426
		 * @param bool $enabled Are Post Likes enabled on archive/front/search pages?
427
		 */
428
		return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'] ) );
429
	}
430
431
	/**
432
	 * Are Post Likes enabled on single pages?
433
	 *
434
	 * @return bool
435
	 */
436 View Code Duplication
	function is_single_page_enabled() {
437
		$options = $this->get_options();
438
		/**
439
		 * Filters whether Likes should be enabled on single pages.
440
		 *
441
		 * @module likes
442
		 *
443
		 * @since 2.2.0
444
		 *
445
		 * @param bool $enabled Are Post Likes enabled on single pages?
446
		 */
447
		return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'] ) );
448
	}
449
450
	/**
451
	 * Are Media Likes enabled on single pages?
452
	 *
453
	 * @return bool
454
	 */
455
	function is_attachment_enabled() {
456
		$options = $this->get_options();
457
		/**
458
		 * Filters whether Likes should be enabled on attachment pages.
459
		 *
460
		 * @module likes
461
		 *
462
		 * @since 2.2.0
463
		 *
464
		 * @param bool $enabled Are Post Likes enabled on attachment pages?
465
		 */
466
		return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'] ) );
467
	}
468
469
	/**
470
	 * The actual options block to be inserted into the sharing page.
471
	 */
472
	function admin_settings_init() {
473
		?>
474
		<tr>
475
			<th scope="row">
476
				<label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label>
477
			</th>
478
			<td>
479
				<div>
480
					<label>
481
						<input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> />
482
						<?php esc_html_e( 'On for all posts', 'jetpack' ); ?>
483
					</label>
484
				</div>
485
				<div>
486
					<label>
487
						<input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> />
488
						<?php esc_html_e( 'Turned on per post', 'jetpack' ); ?>
489
					</label>
490
					<div>
491
			</td>
492
		</tr>
493
		<?php if ( ! $this->in_jetpack ) : ?>
494
			<tr>
495
				<th scope="row">
496
					<label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack' ); ?></label>
497
				</th>
498
				<td>
499
					<div>
500
						<label>
501
							<input type="radio" class="code" name="jetpack_reblogs_enabled" value="on" <?php checked( $this->reblogs_enabled_sitewide(), true ); ?> />
502
							<?php esc_html_e( 'Show the Reblog button on posts', 'jetpack' ); ?>
503
						</label>
504
					</div>
505
					<div>
506
						<label>
507
							<input type="radio" class="code" name="jetpack_reblogs_enabled" value="off" <?php checked( $this->reblogs_enabled_sitewide(), false ); ?> />
508
							<?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack' ); ?>
509
						</label>
510
					</div>
511
				</td>
512
			</tr>
513
			<!-- WPCOM only: Comment Likes -->
514
			<?php if ( ! $this->in_jetpack ) : ?>
515
				<tr>
516
					<th scope="row">
517
						<label><?php esc_html_e( 'Comment Likes are', 'jetpack' ); ?></label>
518
					</th>
519
					<td>
520
						<div>
521
							<label>
522
								<input type="checkbox" class="code" name="jetpack_comment_likes_enabled" value="1" <?php checked( $this->is_comments_enabled(), true ); ?> />
523
								<?php esc_html_e( 'On for all comments', 'jetpack' ); ?>
524
							</label>
525
						</div>
526
					</td>
527
				</tr>
528
			<?php endif; ?>
529
		<?php endif; ?>
530
		</tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?>
531
	<?php
532
	}
533
534
	/**
535
	 * Returns the current state of the "WordPress.com Reblogs are" option.
536
	 * @return boolean true if enabled sitewide, false if not
537
	 */
538
	function reblogs_enabled_sitewide() {
539
		/**
540
		 * Filters whether Reblogs are enabled by default on all posts.
541
		 * true if enabled sitewide, false if not.
542
		 *
543
		 * @module likes
544
		 *
545
		 * @since 3.0.0
546
		 *
547
		 * @param bool $option Are Reblogs enabled sitewide.
548
		 */
549
		return (bool) apply_filters( 'wpl_reblogging_enabled_sitewide', ! get_option( 'disabled_reblogs' ) );
550
	}
551
552
	/**
553
	 * Used for WPCOM ONLY. Comment likes are in their own module in Jetpack.
554
	 * Returns if comment likes are enabled. Defaults to 'off'
555
	 * @return boolean true if we should show comment likes, false if not
556
	 */
557
	function is_comments_enabled() {
558
		/**
559
		 * Filters whether Comment Likes are enabled.
560
		 * true if enabled, false if not.
561
		 *
562
		 * @module comment-likes
563
		 *
564
		 * @since 2.2.0
565
		 *
566
		 * @param bool $option Are Comment Likes enabled sitewide.
567
		 */
568
		return (bool) apply_filters( 'jetpack_comment_likes_enabled', get_option( 'jetpack_comment_likes_enabled', false ) );
569
	}
570
571
	/**
572
	 * Saves the setting in the database, bumps a stat on WordPress.com
573
	 */
574
	function admin_settings_callback() {
575
		// We're looking for these, and doing a dance to set some stats and save
576
		// them together in array option.
577
		$new_state = ! empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on';
578
		$db_state  = $this->is_enabled_sitewide();
579
580
		$reblogs_new_state = ! empty( $_POST['jetpack_reblogs_enabled'] ) ? $_POST['jetpack_reblogs_enabled'] : 'on';
581
		$reblogs_db_state = $this->reblogs_enabled_sitewide();
582
		/** Default State *********************************************************/
583
584
		// Checked (enabled)
585 View Code Duplication
		switch( $new_state ) {
586
			case 'off' :
587
				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...
588
					$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...
589
				}
590
				update_option( 'disabled_likes', 1 );
591
				break;
592
			case 'on'  :
593
			default:
594
				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...
595
					$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...
596
				}
597
				delete_option( 'disabled_likes' );
598
				break;
599
		}
600
601 View Code Duplication
		switch( $reblogs_new_state ) {
602
			case 'off' :
603
				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...
604
					$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...
605
				}
606
				update_option( 'disabled_reblogs', 1 );
607
				break;
608
			case 'on'  :
609
			default:
610
				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...
611
					$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...
612
				}
613
				delete_option( 'disabled_reblogs' );
614
				break;
615
		}
616
617
		// WPCOM only: Comment Likes
618
		if ( ! $this->in_jetpack ) {
619
			$new_comments_state = ! empty( $_POST['jetpack_comment_likes_enabled'] ) ? $_POST['jetpack_comment_likes_enabled'] : false;
620
			switch( (bool) $new_comments_state ) {
621
				case true:
622
					update_option( 'jetpack_comment_likes_enabled', 1 );
623
				break;
624
				case false:
625
				default:
626
					update_option( 'jetpack_comment_likes_enabled', 0 );
627
				break;
628
			}
629
		}
630
	}
631
632
	/**
633
	 * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled.
634
	 */
635
	function process_update_requests_if_sharedaddy_not_loaded() {
636
		if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) {
637
			if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) {
638
				/** This action is documented in modules/sharedaddy/sharing.php */
639
				do_action( 'sharing_admin_update' );
640
				wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
641
				die();
642
			}
643
		}
644
	}
645
646
	/**
647
	 * 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.
648
	 */
649
	function admin_settings_showbuttonon_init() {
650
		/** This action is documented in modules/sharedaddy/sharing.php */
651
		echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' );
652
		?>
653
		<th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th>
654
		<td>
655
			<?php
656
			$br = false;
657
			$shows = array_values( get_post_types( array( 'public' => true ) ) );
658
			array_unshift( $shows, 'index' );
659
			$global = $this->get_options();
660 View Code Duplication
			foreach ( $shows as $show ) :
661
				if ( 'index' == $show ) {
662
					$label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
663
				} else {
664
					$post_type_object = get_post_type_object( $show );
665
					$label = $post_type_object->labels->name;
666
				}
667
				?>
668
				<?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>
669
				<?php	$br = true; endforeach; ?>
670
		</td>
671
		<?php
672
		/** This action is documented in modules/sharedaddy/sharing.php */
673
		echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' );
674
		?>
675
	<?php
676
	}
677
678
	/**
679
	 * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option.
680
	 */
681
	function admin_settings_showbuttonon_callback() {
682
		$options = get_option( 'sharing-options' );
683
		if ( !is_array( $options ) )
684
			$options = array();
685
686
		$shows = array_values( get_post_types( array( 'public' => true ) ) );
687
		$shows[] = 'index';
688
		$data = $_POST;
689
690
		if ( isset( $data['show'] ) ) {
691 View Code Duplication
			if ( is_scalar( $data['show'] ) ) {
692
				switch ( $data['show'] ) {
693
					case 'posts' :
694
						$data['show'] = array( 'post', 'page' );
695
						break;
696
					case 'index' :
697
						$data['show'] = array( 'index' );
698
						break;
699
					case 'posts-index' :
700
						$data['show'] = array( 'post', 'page', 'index' );
701
						break;
702
				}
703
			}
704
705 View Code Duplication
			if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
706
				$options['global']['show'] = $data['show'];
707
			}
708
		} else {
709
			$options['global']['show'] = array();
710
		}
711
712
		update_option( 'sharing-options', $options );
713
	}
714
}
715