Completed
Push — update/security-widget-languag... ( 75ac2c...0fa1b8 )
by
unknown
126:04 queued 116:11
created

Jetpack_Likes_Settings::is_post_likeable()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 4
nop 1
dl 0
loc 11
rs 8.8571
c 0
b 0
f 0
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 behaviour 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
	 * Adds the 'sharing' menu to the settings menu.
161
	 * Only ran if sharedaddy and publicize are not already active.
162
	 */
163
	function sharing_menu() {
164
		add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) );
165
	}
166
167
	/**
168
	 * Provides a sharing page with the sharing_global_options hook
169
	 * so we can display the setting.
170
	 * Only ran if sharedaddy and publicize are not already active.
171
	 */
172
	function sharing_page() {
173
		$this->updated_message(); ?>
174
		<div class="wrap">
175
			<div class="icon32" id="icon-options-general"><br /></div>
176
			<h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1>
177
			<?php
178
			/** This action is documented in modules/sharedaddy/sharing.php */
179
			do_action( 'pre_admin_screen_sharing' );
180
			?>
181
			<?php $this->sharing_block(); ?>
182
		</div> <?php
183
	}
184
185
	/**
186
	 * Returns the settings have been saved message.
187
	 */
188
	function updated_message() {
189
		if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' ){
190
			echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>';
191
		}
192
	}
193
194
	/**
195
	 * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts
196
	 */
197
	function sharing_block() { ?>
198
		<h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2>
199
		<form method="post" action="">
200
			<table class="form-table">
201
				<tbody>
202
				<?php
203
				/** This action is documented in modules/sharedaddy/sharing.php */
204
				do_action( 'sharing_global_options' );
205
				?>
206
				</tbody>
207
			</table>
208
209
			<p class="submit">
210
				<input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" />
211
			</p>
212
213
			<input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
214
		</form> <?php
215
	}
216
217
	/**
218
	 * Are likes enabled for this post?
219
	 *
220
	 * @param int $post_id
221
	 * @return bool
222
	 */
223
	function is_post_likeable( $post_id = 0 ) {
224
		$post = get_post( $post_id );
225
		if ( !$post || is_wp_error( $post ) ) {
226
			return false;
227
		}
228
229
		$sitewide_likes_enabled = (bool) $this->is_enabled_sitewide();
230
		$post_likes_switched    = get_post_meta( $post->ID, 'switch_like_status', true );
231
232
		return $post_likes_switched || ( $sitewide_likes_enabled && $post_likes_switched !== '0' );
233
	}
234
235
	/**
236
	 * Are likes visible in this context?
237
	 *
238
	 * Some of this code was taken and modified from sharing_display() to ensure
239
	 * similar logic and filters apply here, too.
240
	 */
241
	function is_likes_visible() {
242
		require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php';
243
		if ( Jetpack_Sync_Settings::is_syncing() ) {
244
			return false;
245
		}
246
247
		global $wp_current_filter; // Used to apply 'sharing_show' filter
248
249
		$post = get_post();
250
251
		// Never show on feeds or previews
252
		if ( is_feed() || is_preview() ) {
253
			$enabled = false;
254
255
			// Not a feed or preview, so what is it?
256
		} else {
257
258
			if ( in_the_loop() ) {
259
				// If in the loop, check if the current post is likeable
260
				$enabled = $this->is_post_likeable();
261
			} else {
262
				// Otherwise, check and see if likes are enabled sitewide
263
				$enabled = $this->is_enabled_sitewide();
264
			}
265
266
			if ( post_password_required() )
267
				$enabled = false;
268
269
			if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
270
				$enabled = false;
271
			}
272
273
			// Sharing Setting Overrides ****************************************
274
275
			// Single post including custom post types
276
			if ( is_single() ) {
277
				if ( ! $this->is_single_post_enabled( $post->post_type ) ) {
278
					$enabled = false;
279
				}
280
281
				// Single page
282
			} elseif ( is_page() && ! is_front_page() ) {
283
				if ( ! $this->is_single_page_enabled() ) {
284
					$enabled = false;
285
				}
286
287
				// Attachment
288
			} elseif ( is_attachment() ) {
289
				if ( ! $this->is_attachment_enabled() ) {
290
					$enabled = false;
291
				}
292
293
				// All other loops
294
			} elseif ( ! $this->is_index_enabled() ) {
295
				$enabled = false;
296
			}
297
		}
298
299
		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...
300
			// Check that the post is a public, published post.
301 View Code Duplication
			if ( 'attachment' == $post->post_type ) {
302
				$post_status = get_post_status( $post->post_parent );
303
			} else {
304
				$post_status = $post->post_status;
305
			}
306
			if ( 'publish' != $post_status ) {
307
				$enabled = false;
308
			}
309
		}
310
311
		// Run through the sharing filters
312
		/** This filter is documented in modules/sharedaddy/sharing-service.php */
313
		$enabled = apply_filters( 'sharing_show', $enabled, $post );
314
315
		/**
316
		 * Filters whether the Likes should be visible or not.
317
		 * Allows overwriting the options set in Settings > Sharing.
318
		 *
319
		 * @module likes
320
		 *
321
		 * @since 2.2.0
322
		 *
323
		 * @param bool $enabled Should the Likes be visible?
324
		 */
325
		return (bool) apply_filters( 'wpl_is_likes_visible', $enabled );
326
	}
327
328
	/**
329
	 * Are Post Likes enabled on single posts?
330
	 *
331
	 * @param String $post_type custom post type identifier
332
	 * @return bool
333
	 */
334 View Code Duplication
	function is_single_post_enabled( $post_type = 'post' ) {
335
		$options = $this->get_options();
336
		return (bool) apply_filters(
337
		/**
338
		 * Filters whether Likes should be enabled on single posts.
339
		 *
340
		 * The dynamic part of the filter, {$post_type}, allows you to specific the post type where Likes should be enabled.
341
		 *
342
		 * @module likes
343
		 *
344
		 * @since 2.2.0
345
		 *
346
		 * @param bool $enabled Are Post Likes enabled on single posts?
347
		 */
348
			"wpl_is_single_{$post_type}_disabled",
349
			(bool) in_array( $post_type, $options['show'] )
350
		);
351
	}
352
353
	/**
354
	 * Get the 'disabled_likes' option from the DB of the current blog.
355
	 *
356
	 * @return array
357
	 */
358
	function get_options() {
359
		$setting             = array();
360
		$setting['disabled'] = get_option( 'disabled_likes'  );
361
		$sharing             = get_option( 'sharing-options' );
362
363
		// Default visibility settings
364
		if ( ! isset( $sharing['global']['show'] ) ) {
365
			$sharing['global']['show'] = array( 'post', 'page' );
366
367
			// Scalar check
368
		} elseif ( is_scalar( $sharing['global']['show'] ) ) {
369
			switch ( $sharing['global']['show'] ) {
370
				case 'posts' :
371
					$sharing['global']['show'] = array( 'post', 'page' );
372
					break;
373
				case 'index' :
374
					$sharing['global']['show'] = array( 'index' );
375
					break;
376
				case 'posts-index' :
377
					$sharing['global']['show'] = array( 'post', 'page', 'index' );
378
					break;
379
			}
380
		}
381
382
		// Ensure it's always an array (even if not previously empty or scalar)
383
		$setting['show'] = !empty( $sharing['global']['show'] ) ? (array) $sharing['global']['show'] : array();
384
385
		/**
386
		 * Filters where the Likes are displayed.
387
		 *
388
		 * @module likes
389
		 *
390
		 * @since 2.2.0
391
		 *
392
		 * @param array $setting Array of Likes display settings.
393
		 */
394
		return apply_filters( 'wpl_get_options', $setting );
395
	}
396
397
	/**
398
	 * Are Post Likes enabled on archive/front/search pages?
399
	 *
400
	 * @return bool
401
	 */
402
	function is_index_enabled() {
403
		$options = $this->get_options();
404
		/**
405
		 * Filters whether Likes should be enabled on archive/front/search pages.
406
		 *
407
		 * @module likes
408
		 *
409
		 * @since 2.2.0
410
		 *
411
		 * @param bool $enabled Are Post Likes enabled on archive/front/search pages?
412
		 */
413
		return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'] ) );
414
	}
415
416
	/**
417
	 * Are Post Likes enabled on single pages?
418
	 *
419
	 * @return bool
420
	 */
421 View Code Duplication
	function is_single_page_enabled() {
422
		$options = $this->get_options();
423
		/**
424
		 * Filters whether Likes should be enabled on single pages.
425
		 *
426
		 * @module likes
427
		 *
428
		 * @since 2.2.0
429
		 *
430
		 * @param bool $enabled Are Post Likes enabled on single pages?
431
		 */
432
		return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'] ) );
433
	}
434
435
	/**
436
	 * Are Media Likes enabled on single pages?
437
	 *
438
	 * @return bool
439
	 */
440
	function is_attachment_enabled() {
441
		$options = $this->get_options();
442
		/**
443
		 * Filters whether Likes should be enabled on attachment pages.
444
		 *
445
		 * @module likes
446
		 *
447
		 * @since 2.2.0
448
		 *
449
		 * @param bool $enabled Are Post Likes enabled on attachment pages?
450
		 */
451
		return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'] ) );
452
	}
453
454
	/**
455
	 * The actual options block to be inserted into the sharing page.
456
	 */
457
	function admin_settings_init() {
458
		?>
459
		<tr>
460
			<th scope="row">
461
				<label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label>
462
			</th>
463
			<td>
464
				<div>
465
					<label>
466
						<input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> />
467
						<?php esc_html_e( 'On for all posts', 'jetpack' ); ?>
468
					</label>
469
				</div>
470
				<div>
471
					<label>
472
						<input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> />
473
						<?php esc_html_e( 'Turned on per post', 'jetpack' ); ?>
474
					</label>
475
					<div>
476
			</td>
477
		</tr>
478
		<?php if ( ! $this->in_jetpack ) : ?>
479
			<tr>
480
				<th scope="row">
481
					<label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack' ); ?></label>
482
				</th>
483
				<td>
484
					<div>
485
						<label>
486
							<input type="radio" class="code" name="jetpack_reblogs_enabled" value="on" <?php checked( $this->reblogs_enabled_sitewide(), true ); ?> />
487
							<?php esc_html_e( 'Show the Reblog button on posts', 'jetpack' ); ?>
488
						</label>
489
					</div>
490
					<div>
491
						<label>
492
							<input type="radio" class="code" name="jetpack_reblogs_enabled" value="off" <?php checked( $this->reblogs_enabled_sitewide(), false ); ?> />
493
							<?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack' ); ?>
494
						</label>
495
						<div>
496
				</td>
497
			</tr>
498
		<?php endif; ?>
499
		</tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?>
500
	<?php
501
	}
502
503
	/**
504
	 * Returns the current state of the "WordPress.com Reblogs are" option.
505
	 * @return boolean true if enabled sitewide, false if not
506
	 */
507
	function reblogs_enabled_sitewide() {
508
		/**
509
		 * Filters whether Reblogs are enabled by default on all posts.
510
		 * true if enabled sitewide, false if not.
511
		 *
512
		 * @module likes
513
		 *
514
		 * @since 3.0.0
515
		 *
516
		 * @param bool $option Are Reblogs enabled sitewide.
517
		 */
518
		return (bool) apply_filters( 'wpl_reblogging_enabled_sitewide', ! get_option( 'disabled_reblogs' ) );
519
	}
520
521
	/**
522
	 * Saves the setting in the database, bumps a stat on WordPress.com
523
	 */
524
	function admin_settings_callback() {
525
		// We're looking for these, and doing a dance to set some stats and save
526
		// them together in array option.
527
		$new_state = !empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on';
528
		$db_state  = $this->is_enabled_sitewide();
529
530
		$reblogs_new_state = !empty( $_POST['jetpack_reblogs_enabled'] ) ? $_POST['jetpack_reblogs_enabled'] : 'on';
531
		$reblogs_db_state = $this->reblogs_enabled_sitewide();
532
		/** Default State *********************************************************/
533
534
		// Checked (enabled)
535 View Code Duplication
		switch( $new_state ) {
536
			case 'off' :
537
				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...
538
					$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...
539
				}
540
				update_option( 'disabled_likes', 1 );
541
				break;
542
			case 'on'  :
543
			default:
544
				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...
545
					$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...
546
				}
547
				delete_option( 'disabled_likes' );
548
				break;
549
		}
550
551 View Code Duplication
		switch( $reblogs_new_state ) {
552
			case 'off' :
553
				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...
554
					$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...
555
				}
556
				update_option( 'disabled_reblogs', 1 );
557
				break;
558
			case 'on'  :
559
			default:
560
				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...
561
					$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...
562
				}
563
				delete_option( 'disabled_reblogs' );
564
				break;
565
		}
566
	}
567
568
	/**
569
	 * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled.
570
	 */
571
	function process_update_requests_if_sharedaddy_not_loaded() {
572
		if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) {
573
			if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) {
574
				/** This action is documented in modules/sharedaddy/sharing.php */
575
				do_action( 'sharing_admin_update' );
576
				wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
577
				die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method process_update_requests_if_sharedaddy_not_loaded() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
578
			}
579
		}
580
	}
581
582
	/**
583
	 * 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.
584
	 */
585
	function admin_settings_showbuttonon_init() {
586
		?>
587
		<?php
588
		/** This action is documented in modules/sharedaddy/sharing.php */
589
		echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' );
590
		?>
591
		<th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th>
592
		<td>
593
			<?php
594
			$br = false;
595
			$shows = array_values( get_post_types( array( 'public' => true ) ) );
596
			array_unshift( $shows, 'index' );
597
			$global = $this->get_options();
598 View Code Duplication
			foreach ( $shows as $show ) :
599
				if ( 'index' == $show ) {
600
					$label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
601
				} else {
602
					$post_type_object = get_post_type_object( $show );
603
					$label = $post_type_object->labels->name;
604
				}
605
				?>
606
				<?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>
607
				<?php	$br = true; endforeach; ?>
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
608
		</td>
609
		<?php
610
		/** This action is documented in modules/sharedaddy/sharing.php */
611
		echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' );
612
		?>
613
	<?php
614
	}
615
616
	/**
617
	 * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option.
618
	 */
619
	function admin_settings_showbuttonon_callback() {
620
		$options = get_option( 'sharing-options' );
621
		if ( !is_array( $options ) )
622
			$options = array();
623
624
		$shows = array_values( get_post_types( array( 'public' => true ) ) );
625
		$shows[] = 'index';
626
		$data = $_POST;
627
628
		if ( isset( $data['show'] ) ) {
629 View Code Duplication
			if ( is_scalar( $data['show'] ) ) {
630
				switch ( $data['show'] ) {
631
					case 'posts' :
632
						$data['show'] = array( 'post', 'page' );
633
						break;
634
					case 'index' :
635
						$data['show'] = array( 'index' );
636
						break;
637
					case 'posts-index' :
638
						$data['show'] = array( 'post', 'page', 'index' );
639
						break;
640
				}
641
			}
642
643 View Code Duplication
			if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
644
				$options['global']['show'] = $data['show'];
645
			}
646
		} else {
647
			$options['global']['show'] = array();
648
		}
649
650
		update_option( 'sharing-options', $options );
651
	}
652
}
653