Completed
Push — add/geo-location-support ( 007f25...6296b5 )
by Brad
29:34 queued 17:46
created

Jetpack_Likes_Settings::sharing_meta_box_content()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 11
rs 9.4285
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
	 * 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
		<?php endif; ?>
514
		</tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?>
515
	<?php
516
	}
517
518
	/**
519
	 * Returns the current state of the "WordPress.com Reblogs are" option.
520
	 * @return boolean true if enabled sitewide, false if not
521
	 */
522
	function reblogs_enabled_sitewide() {
523
		/**
524
		 * Filters whether Reblogs are enabled by default on all posts.
525
		 * true if enabled sitewide, false if not.
526
		 *
527
		 * @module likes
528
		 *
529
		 * @since 3.0.0
530
		 *
531
		 * @param bool $option Are Reblogs enabled sitewide.
532
		 */
533
		return (bool) apply_filters( 'wpl_reblogging_enabled_sitewide', ! get_option( 'disabled_reblogs' ) );
534
	}
535
536
	/**
537
	 * Saves the setting in the database, bumps a stat on WordPress.com
538
	 */
539
	function admin_settings_callback() {
540
		// We're looking for these, and doing a dance to set some stats and save
541
		// them together in array option.
542
		$new_state = !empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on';
543
		$db_state  = $this->is_enabled_sitewide();
544
545
		$reblogs_new_state = !empty( $_POST['jetpack_reblogs_enabled'] ) ? $_POST['jetpack_reblogs_enabled'] : 'on';
546
		$reblogs_db_state = $this->reblogs_enabled_sitewide();
547
		/** Default State *********************************************************/
548
549
		// Checked (enabled)
550 View Code Duplication
		switch( $new_state ) {
551
			case 'off' :
552
				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...
553
					$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...
554
				}
555
				update_option( 'disabled_likes', 1 );
556
				break;
557
			case 'on'  :
558
			default:
559
				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...
560
					$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...
561
				}
562
				delete_option( 'disabled_likes' );
563
				break;
564
		}
565
566 View Code Duplication
		switch( $reblogs_new_state ) {
567
			case 'off' :
568
				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...
569
					$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...
570
				}
571
				update_option( 'disabled_reblogs', 1 );
572
				break;
573
			case 'on'  :
574
			default:
575
				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...
576
					$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...
577
				}
578
				delete_option( 'disabled_reblogs' );
579
				break;
580
		}
581
	}
582
583
	/**
584
	 * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled.
585
	 */
586
	function process_update_requests_if_sharedaddy_not_loaded() {
587
		if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) {
588
			if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) {
589
				/** This action is documented in modules/sharedaddy/sharing.php */
590
				do_action( 'sharing_admin_update' );
591
				wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
592
				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...
593
			}
594
		}
595
	}
596
597
	/**
598
	 * 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.
599
	 */
600
	function admin_settings_showbuttonon_init() {
601
		?>
602
		<?php
603
		/** This action is documented in modules/sharedaddy/sharing.php */
604
		echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' );
605
		?>
606
		<th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th>
607
		<td>
608
			<?php
609
			$br = false;
610
			$shows = array_values( get_post_types( array( 'public' => true ) ) );
611
			array_unshift( $shows, 'index' );
612
			$global = $this->get_options();
613 View Code Duplication
			foreach ( $shows as $show ) :
614
				if ( 'index' == $show ) {
615
					$label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
616
				} else {
617
					$post_type_object = get_post_type_object( $show );
618
					$label = $post_type_object->labels->name;
619
				}
620
				?>
621
				<?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>
622
				<?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...
623
		</td>
624
		<?php
625
		/** This action is documented in modules/sharedaddy/sharing.php */
626
		echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' );
627
		?>
628
	<?php
629
	}
630
631
	/**
632
	 * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option.
633
	 */
634
	function admin_settings_showbuttonon_callback() {
635
		$options = get_option( 'sharing-options' );
636
		if ( !is_array( $options ) )
637
			$options = array();
638
639
		$shows = array_values( get_post_types( array( 'public' => true ) ) );
640
		$shows[] = 'index';
641
		$data = $_POST;
642
643
		if ( isset( $data['show'] ) ) {
644 View Code Duplication
			if ( is_scalar( $data['show'] ) ) {
645
				switch ( $data['show'] ) {
646
					case 'posts' :
647
						$data['show'] = array( 'post', 'page' );
648
						break;
649
					case 'index' :
650
						$data['show'] = array( 'index' );
651
						break;
652
					case 'posts-index' :
653
						$data['show'] = array( 'post', 'page', 'index' );
654
						break;
655
				}
656
			}
657
658 View Code Duplication
			if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
659
				$options['global']['show'] = $data['show'];
660
			}
661
		} else {
662
			$options['global']['show'] = array();
663
		}
664
665
		update_option( 'sharing-options', $options );
666
	}
667
}
668