Completed
Push — add/sync-action ( b1c6ef...74b5a9 )
by
unknown
23:25 queued 15:01
created

Jetpack_Likes::module_toggle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 3
nop 0
1
<?php
2
/**
3
 * Module Name: Likes
4
 * Module Description: Give visitors an easy way to show their appreciation for your content.
5
 * First Introduced: 2.2
6
 * Sort Order: 23
7
 * Requires Connection: Yes
8
 * Auto Activate: No
9
 * Module Tags: Social
10
 * Additional Search Queries: like, likes, wordpress.com
11
 */
12
13
Jetpack::dns_prefetch( array(
14
	'//widgets.wp.com',
15
	'//s0.wp.com',
16
	'//0.gravatar.com',
17
	'//1.gravatar.com',
18
	'//2.gravatar.com',
19
) );
20
21
class Jetpack_Likes {
22
	public $version = '20151215';
23
24
	public static function init() {
25
		static $instance = NULL;
26
27
		if ( ! $instance ) {
28
			$instance = new Jetpack_Likes;
29
		}
30
31
		return $instance;
32
	}
33
34
	function __construct() {
35
		$this->in_jetpack = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? false : true;
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...
36
37
		add_action( 'init', array( &$this, 'action_init' ) );
38
		add_action( 'admin_init', array( $this, 'admin_init' ) );
39
40
		if ( $this->in_jetpack ) {
41
			add_action( 'jetpack_activate_module_likes',   array( $this, 'maybe_sync_content' ) );
42
			add_action( 'jetpack_activate_module_likes',   array( $this, 'set_social_notifications_like' ) );
43
			add_action( 'jetpack_deactivate_module_likes', array( $this, 'delete_social_notifications_like' ) );
44
45
			Jetpack::enable_module_configurable( __FILE__ );
46
			Jetpack::module_configuration_load( __FILE__, array( $this, 'configuration_redirect' ) );
47
48
			add_action('admin_print_scripts-settings_page_sharing', array( &$this, 'load_jp_css' ) );
49
			add_filter( 'sharing_show_buttons_on_row_start', array( $this, 'configuration_target_area' ) );
50
51
			$active = Jetpack::get_active_modules();
52
53 View Code Duplication
			if ( ! in_array( 'sharedaddy', $active ) && ! in_array( 'publicize', $active ) ) {
54
				add_action( 'admin_menu', array( $this, 'sharing_menu' ) );	// we don't have a sharing page yet
55
			}
56
57
			if ( in_array( 'publicize', $active ) && ! in_array( 'sharedaddy', $active ) ) {
58
				add_action( 'pre_admin_screen_sharing', array( $this, 'sharing_block' ), 20 ); // we have a sharing page but not the global options area
59
				add_action( 'pre_admin_screen_sharing', array( $this, 'updated_message' ), -10 );
60
			}
61
62
			if( ! in_array( 'sharedaddy', $active ) ) {
63
				add_action( 'admin_init', array( $this, 'process_update_requests_if_sharedaddy_not_loaded' ) );
64
				add_action( 'sharing_global_options', array( $this, 'admin_settings_showbuttonon_init' ), 19 );
65
				add_action( 'sharing_admin_update', array( $this, 'admin_settings_showbuttonon_callback' ), 19 );
66
				add_action( 'admin_init', array( $this, 'add_meta_box' ) );
67
			} else {
68
				add_filter( 'sharing_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) );
69
				add_action( 'start_sharing_meta_box_content', array( $this, 'meta_box_content' ) );
70
			}
71
		} else { // wpcom
72
			add_action( 'wpmu_new_blog', array( $this, 'enable_comment_likes' ), 10, 1 );
73
			add_action( 'admin_init', array( $this, 'add_meta_box' ) );
74
			add_action( 'end_likes_meta_box_content', array( $this, 'sharing_meta_box_content' ) );
75
			add_filter( 'likes_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) );
76
		}
77
78
		add_action( 'admin_init', array( $this, 'admin_discussion_likes_settings_init' ) ); // Likes notifications
79
80
		add_action( 'admin_bar_menu', array( $this, 'admin_bar_likes' ), 60 );
81
82
		add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) );
83
84
		add_action( 'save_post', array( $this, 'meta_box_save' ) );
85
		add_action( 'edit_attachment', array( $this, 'meta_box_save' ) );
86
		add_action( 'sharing_global_options', array( $this, 'admin_settings_init' ), 20 );
87
		add_action( 'sharing_admin_update',   array( $this, 'admin_settings_callback' ), 20 );
88
	}
89
90
	function maybe_sync_content() {
91
		if ( Jetpack::init()->sync->reindex_needed() ) {
92
			Jetpack::init()->sync->reindex_trigger();
93
		}
94
	}
95
96
	/**
97
	 * Set the social_notifications_like option to `on` when the Likes module is activated.
98
	 *
99
	 * @since 3.7.0
100
	 *
101
	 * @return null
102
	 */
103
	function set_social_notifications_like() {
104
		update_option( 'social_notifications_like', 'on' );
105
	}
106
107
	/**
108
	 * Delete the social_notifications_like option that was set to `on` on module activation.
109
	 *
110
	 * @since 3.7.0
111
	 *
112
	 * @return null
113
	 */
114
	function delete_social_notifications_like() {
115
		delete_option( 'social_notifications_like' );
116
	}
117
118
	/**
119
	 * Redirects to the likes section of the sharing page.
120
	 */
121
	function configuration_redirect() {
122
		wp_safe_redirect( admin_url( 'options-general.php?page=sharing#likes' ) );
123
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method configuration_redirect() 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...
124
	}
125
126
	/**
127
	 * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable
128
	 */
129
	function load_jp_css() {
130
		// Do we really need `admin_styles`? With the new admin UI, it's breaking some bits.
131
		// Jetpack::init()->admin_styles();
132
	}
133
	/**
134
	 * Load style on the front end.
135
	 * @return null
136
	 */
137
	function load_styles_register_scripts() {
138
139
		wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION );
140
		if( $this->in_jetpack ) {
141
			$this->register_scripts();
142
		}
143
	}
144
145
	/**
146
	 * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box
147
	 * @param  string $html row heading for the sharedaddy "which page" setting
148
	 * @return string       html with the jetpack-targetable class and likes id. tbody gets closed after the like settings
149
	 */
150
	function configuration_target_area( $html = '' ) {
151
		$html = "<tbody id='likes' class='jetpack-targetable'>" . $html;
152
		return $html;
153
	}
154
155
	/**
156
	 * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares"
157
	 * @param string $title The current title of the metabox, not needed/used.
158
	 */
159
	function add_likes_to_sharing_meta_box_title( $title ) {
0 ignored issues
show
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
		return __( 'Likes and Shares', 'jetpack' );
161
	}
162
163
	/**
164
	 * Adds a metabox to the post screen if the sharing one doesn't currently exist.
165
	 */
166
	function add_meta_box() {
167
		if (
168
			/**
169
			 * Allow disabling of the Likes metabox on the post editor screen.
170
			 *
171
			 * @module likes
172
			 *
173
			 * @since 2.2.0
174
			 *
175
			 * @param bool false Should the Likes metabox be disabled? Default to false.
176
			 */
177
			apply_filters( 'post_flair_disable', false )
178
		) {
179
			return;
180
		}
181
182
		$post_types = get_post_types( array( 'public' => true ) );
183
		/**
184
		 * Filters the Likes metabox title.
185
		 *
186
		 * @module likes
187
		 *
188
		 * @since 2.2.0
189
		 *
190
		 * @param string Likes metabox title. Default to "Likes".
191
		 */
192
		$title = apply_filters( 'likes_meta_box_title', __( 'Likes', 'jetpack' ) );
193
		foreach( $post_types as $post_type ) {
194
			add_meta_box( 'likes_meta', $title, array( $this, 'meta_box_content' ), $post_type, 'advanced', 'high' );
195
		}
196
	}
197
198
	function meta_box_save( $post_id ) {
199
		if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
200
			return $post_id;
201
202
		if ( empty( $_POST['wpl_like_status_hidden'] ) )
203
			return $post_id;
204
205
		// Record sharing disable. Only needs to be done for WPCOM
206
		if ( ! $this->in_jetpack ) {
207
			if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], get_post_types( array( 'public' => true ) ) ) ) {
208 View Code Duplication
				if ( ! isset( $_POST['wpl_enable_post_sharing'] ) ) {
209
					update_post_meta( $post_id, 'sharing_disabled', 1 );
210
				} else {
211
					delete_post_meta( $post_id, 'sharing_disabled' );
212
				}
213
			}
214
		}
215
216
		if ( 'post' == $_POST['post_type'] ) {
217
			if ( !current_user_can( 'edit_post', $post_id ) ) {
218
				return $post_id;
219
			}
220
		}
221
222
		// Record a change in like status for this post - only if it contradicts the
223
		// site like setting.
224
		if ( ( $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) || ( ! $this->is_enabled_sitewide() && !empty( $_POST['wpl_enable_post_likes'] ) ) ) {
225
			update_post_meta( $post_id, 'switch_like_status', 1 );
226
			//$g_gif = file_get_contents( 'http://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=switched_post_like_status' ); @todo stat
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
227
		} else {
228
			delete_post_meta( $post_id, 'switch_like_status' );
229
		}
230
231
		return $post_id;
232
	}
233
234
	/**
235
	 * Shows the likes option in the post screen metabox.
236
	 */
237
	function meta_box_content( $post ) {
238
		$post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
239
		$checked         = true;
240
		$disabled        = ! $this->is_enabled_sitewide();
241
		$switched_status = get_post_meta( $post_id, 'switch_like_status', true );
242
243
		if ( $disabled && empty( $switched_status ) || false == $disabled && !empty( $switched_status ) )
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...
244
			$checked = false;
245
246
		/**
247
		 * Fires before the Likes meta box content in the post editor.
248
		 *
249
		 * @module likes
250
		 *
251
		 * @since 2.2.0
252
		 *
253
		 * @param WP_Post|array|null $post Post data.
254
		 */
255
		do_action( 'start_likes_meta_box_content', $post );
256
		?>
257
258
		<p>
259
			<label for="wpl_enable_post_likes">
260
				<input type="checkbox" name="wpl_enable_post_likes" id="wpl_enable_post_likes" value="1" <?php checked( $checked ); ?>>
261
				<?php esc_html_e( 'Show likes.', 'jetpack' ); ?>
262
			</label>
263
			<input type="hidden" name="wpl_like_status_hidden" value="1" />
264
		</p> <?php
265
		/**
266
		 * Fires after the Likes meta box content in the post editor.
267
		 *
268
		 * @module likes
269
		 *
270
		 * @since 2.2.0
271
		 *
272
		 * @param WP_Post|array|null $post Post data.
273
		 */
274
		do_action( 'end_likes_meta_box_content', $post );
275
	}
276
277
	/**
278
	 * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog)
279
	 */
280
	function sharing_meta_box_content( $post ) {
281
		$post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
282
		$disabled = get_post_meta( $post_id, 'sharing_disabled', true ); ?>
283
		<p>
284
			<label for="wpl_enable_post_sharing">
285
				<input type="checkbox" name="wpl_enable_post_sharing" id="wpl_enable_post_sharing" value="1" <?php checked( !$disabled ); ?>>
286
				<?php _e( 'Show sharing buttons.', 'jetpack' ); ?>
287
			</label>
288
			<input type="hidden" name="wpl_sharing_status_hidden" value="1" />
289
		</p> <?php
290
	}
291
292
	/**
293
	  * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page)
294
	  */
295
296 View Code Duplication
	function admin_discussion_likes_settings_init() {
297
		// Add a temporary section, until we can move the setting out of there and with the rest of the email notification settings
298
		add_settings_section( 'likes-notifications', __( 'Likes Notifications', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_section' ), 'discussion' );
299
		add_settings_field( 'social-notifications', __( 'Email me whenever', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_field' ), 'discussion', 'likes-notifications' );
300
		// Register the setting
301
		register_setting( 'discussion', 'social_notifications_like', array( $this, 'admin_discussion_likes_settings_validate' ) );
302
	}
303
304
	function admin_discussion_likes_settings_section() {
305
		// Atypical usage here.  We emit jquery to move likes notification checkbox to be with the rest of the email notification settings
306
?>
307
	<script type="text/javascript">
308
	jQuery( function( $ )  {
309
		var table = $( '#social_notifications_like' ).parents( 'table:first' ),
310
			header = table.prevAll( 'h3:first' ),
311
			newParent = $( '#moderation_notify' ).parent( 'label' ).parent();
312
313
		if ( !table.size() || !header.size() || !newParent.size() ) {
314
			return;
315
		}
316
317
		newParent.append( '<br/>' ).append( table.end().parent( 'label' ).siblings().andSelf() );
318
		header.remove();
319
		table.remove();
320
	} );
321
	</script>
322
<?php
323
	}
324
325
	function admin_likes_get_option( $option ) {
326
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
327
			$option_setting = get_blog_option( get_current_blog_id(), $option, 'on' );
328
		} else {
329
			$option_setting = get_option( $option, 'on' );
330
		}
331
332
		return intval( 'on' == $option_setting );
333
	}
334
335
	function admin_discussion_likes_settings_field() {
336
		$like = $this->admin_likes_get_option( 'social_notifications_like' );
337
?>
338
		<label><input type="checkbox" id="social_notifications_like" name="social_notifications_like" value="1" <?php checked( $like ); ?> /> <?php esc_html_e( 'Someone likes one of my posts', 'jetpack' ); ?></label>
339
<?php
340
	}
341
342
	function admin_discussion_likes_settings_validate( $input ) {
343
		// If it's not set (was unchecked during form submission) or was set to off (during option update), return 'off'.
344
		if ( !$input || 'off' == $input )
345
			return 'off';
346
347
		// Otherwise, return 'on'.
348
		return 'on';
349
	}
350
351
	/**
352
	 * The actual options block to be inserted into the sharing page.
353
	 */
354
	function admin_settings_init() { ?>
355
		<tr>
356
			<th scope="row">
357
				<label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label>
358
			</th>
359
			<td>
360
				<div>
361
					<label>
362
						<input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> />
363
						<?php esc_html_e( 'On for all posts', 'jetpack' ); ?>
364
					</label>
365
				</div>
366
				<div>
367
					<label>
368
						<input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> />
369
						<?php esc_html_e( 'Turned on per post', 'jetpack' ); ?>
370
					</label>
371
				<div>
372
			</td>
373
		</tr>
374
		<?php if ( ! $this->in_jetpack ) : ?>
375
		<tr>
376
			<th scope="row">
377
				<label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack' ); ?></label>
378
			</th>
379
			<td>
380
				<div>
381
					<label>
382
						<input type="radio" class="code" name="jetpack_reblogs_enabled" value="on" <?php checked( $this->reblogs_enabled_sitewide(), true ); ?> />
383
						<?php esc_html_e( 'Show the Reblog button on posts', 'jetpack' ); ?>
384
					</label>
385
				</div>
386
				<div>
387
					<label>
388
						<input type="radio" class="code" name="jetpack_reblogs_enabled" value="off" <?php checked( $this->reblogs_enabled_sitewide(), false ); ?> />
389
						<?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack' ); ?>
390
					</label>
391
				<div>
392
			</td>
393
		</tr>
394
		<tr>
395
			<th scope="row">
396
				<label><?php esc_html_e( 'Comment Likes are', 'jetpack' ); ?></label>
397
			</th>
398
			<td>
399
				<div>
400
					<label>
401
						<input type="checkbox" class="code" name="jetpack_comment_likes_enabled" value="1" <?php checked( $this->is_comments_enabled(), true ); ?> />
402
						<?php esc_html_e( 'On for all comments', 'jetpack' ); ?>
403
					</label>
404
				</div>
405
			</td>
406
		</tr>
407
		<?php endif; ?>
408
		</tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?>
409
	<?php }
410
411
	/**
412
	 * 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.
413
	 */
414
	function admin_settings_showbuttonon_init() { ?>
415
		<?php
416
			/** This action is documented in modules/sharedaddy/sharing.php */
417
			echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' );
418
		?>
419
		<th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th>
420
		<td>
421
			<?php
422
				$br = false;
423
				$shows = array_values( get_post_types( array( 'public' => true ) ) );
424
				array_unshift( $shows, 'index' );
425
				$global = $this->get_options();
426 View Code Duplication
				foreach ( $shows as $show ) :
427
					if ( 'index' == $show ) {
428
						$label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
429
					} else {
430
						$post_type_object = get_post_type_object( $show );
431
						$label = $post_type_object->labels->name;
432
					}
433
			?>
434
				<?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>
435
			<?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...
436
		</td>
437
		<?php
438
			/** This action is documented in modules/sharedaddy/sharing.php */
439
			echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' );
440
		?>
441
	<?php }
442
443
444
	/**
445
	 * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option.
446
	 */
447
	function admin_settings_showbuttonon_callback() {
448
		$options = get_option( 'sharing-options' );
449
		if ( !is_array( $options ) )
450
			$options = array();
451
452
		$shows = array_values( get_post_types( array( 'public' => true ) ) );
453
		$shows[] = 'index';
454
		$data = $_POST;
455
456
		if ( isset( $data['show'] ) ) {
457 View Code Duplication
			if ( is_scalar( $data['show'] ) ) {
458
				switch ( $data['show'] ) {
459
					case 'posts' :
460
						$data['show'] = array( 'post', 'page' );
461
					break;
462
					case 'index' :
463
						$data['show'] = array( 'index' );
464
					break;
465
					case 'posts-index' :
466
						$data['show'] = array( 'post', 'page', 'index' );
467
					break;
468
				}
469
			}
470
471 View Code Duplication
			if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
472
				$options['global']['show'] = $data['show'];
473
			}
474
		} else {
475
			$options['global']['show'] = array();
476
		}
477
478
		update_option( 'sharing-options', $options );
479
	}
480
481
	/**
482
	 * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled.
483
	 */
484
	function process_update_requests_if_sharedaddy_not_loaded() {
485
		if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) {
486
			if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) {
487
				/** This action is documented in modules/sharedaddy/sharing.php */
488
				do_action( 'sharing_admin_update' );
489
				wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
490
				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...
491
			}
492
		}
493
	}
494
495
	/**
496
	 * Saves the setting in the database, bumps a stat on WordPress.com
497
	 */
498
	function admin_settings_callback() {
499
		// We're looking for these, and doing a dance to set some stats and save
500
		// them together in array option.
501
		$new_state = !empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on';
502
		$db_state  = $this->is_enabled_sitewide();
503
504
		$reblogs_new_state = !empty( $_POST['jetpack_reblogs_enabled'] ) ? $_POST['jetpack_reblogs_enabled'] : 'on';
505
		$reblogs_db_state = $this->reblogs_enabled_sitewide();
506
		/** Default State *********************************************************/
507
508
		// Checked (enabled)
509 View Code Duplication
		switch( $new_state ) {
510
			case 'off' :
511
				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...
512
					$g_gif = file_get_contents( 'http://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...
513
				}
514
				update_option( 'disabled_likes', 1 );
515
				break;
516
			case 'on'  :
517
			default:
518
				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...
519
					$g_gif = file_get_contents( 'http://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...
520
				}
521
				delete_option( 'disabled_likes' );
522
				break;
523
		}
524
525 View Code Duplication
		switch( $reblogs_new_state ) {
526
			case 'off' :
527
				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...
528
					$g_gif = file_get_contents( 'http://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...
529
				}
530
				update_option( 'disabled_reblogs', 1 );
531
				break;
532
			case 'on'  :
533
			default:
534
				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...
535
					$g_gif = file_get_contents( 'http://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...
536
				}
537
				delete_option( 'disabled_reblogs' );
538
				break;
539
		}
540
541
		// comment setting
542
		$new_comments_state = !empty( $_POST['jetpack_comment_likes_enabled'] ) ? $_POST['jetpack_comment_likes_enabled'] : false;
543
		switch( (bool) $new_comments_state ) {
544
			case true:
545
				update_option( 'jetpack_comment_likes_enabled', 1 );
546
			break;
547
			case false:
548
			default:
549
				update_option( 'jetpack_comment_likes_enabled', 0 );
550
			break;
551
		}
552
	}
553
554
	/**
555
	 * Force comment likes on for a blog
556
	 * Used when a new blog is created
557
	 */
558
	function enable_comment_likes( $blog_id ) {
559
		switch_to_blog( $blog_id );
560
		update_option( 'jetpack_comment_likes_enabled', 1 );
561
		restore_current_blog();
562
	}
563
564
	/**
565
	 * Adds the 'sharing' menu to the settings menu.
566
	 * Only ran if sharedaddy and publicize are not already active.
567
	 */
568
	function sharing_menu() {
569
		add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) );
570
	}
571
572
	/**
573
	 * Provides a sharing page with the sharing_global_options hook
574
	 * so we can display the setting.
575
	 * Only ran if sharedaddy and publicize are not already active.
576
	 */
577
	function sharing_page() {
578
		$this->updated_message(); ?>
579
		<div class="wrap">
580
			<div class="icon32" id="icon-options-general"><br /></div>
581
			<h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1>
582
			<?php
583
				/** This action is documented in modules/sharedaddy/sharing.php */
584
				do_action( 'pre_admin_screen_sharing' );
585
			?>
586
			<?php $this->sharing_block(); ?>
587
		</div> <?php
588
	}
589
590
	/**
591
	 * Returns the settings have been saved message.
592
	 */
593
	function updated_message() {
594
		if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' )
595
			echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>';
596
	}
597
598
	/**
599
	 * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts
600
	 */
601
	function sharing_block() { ?>
602
		<h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2>
603
		<form method="post" action="">
604
		<table class="form-table">
605
		<tbody>
606
			<?php
607
			/** This action is documented in modules/sharedaddy/sharing.php */
608
			do_action( 'sharing_global_options' );
609
			?>
610
		</tbody>
611
		</table>
612
613
		<p class="submit">
614
			<input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" />
615
		</p>
616
617
		<input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
618
		</form> <?php
619
	}
620
621
	function admin_init() {
622
		add_filter( 'manage_posts_columns', array( $this, 'add_like_count_column' ) );
623
		add_filter( 'manage_pages_columns', array( $this, 'add_like_count_column' ) );
624
		add_action( 'manage_posts_custom_column', array( $this, 'likes_edit_column' ), 10, 2 );
625
		add_action( 'manage_pages_custom_column', array( $this, 'likes_edit_column' ), 10, 2 );
626
		add_action( 'admin_print_styles-edit.php', array( $this, 'load_admin_css' ) );
627
		add_action( "admin_print_scripts-edit.php", array( $this, 'enqueue_admin_scripts' ) );
628
	}
629
630
	function action_init() {
631
		if ( is_admin() ) {
632
			return;
633
		}
634
635
		if ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ||
636
			 ( defined( 'APP_REQUEST' ) && APP_REQUEST ) ||
637
			 ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
638
			 ( defined( 'COOKIE_AUTH_REQUEST' ) && COOKIE_AUTH_REQUEST ) ||
639
			 ( defined( 'JABBER_SERVER' ) && JABBER_SERVER ) ) {
640
			return;
641
		}
642
643
		// Comment Likes widget has been disabled, pending performance improvements.
644
		// add_filter( 'comment_text', array( &$this, 'comment_likes' ), 10, 2 );
645
646
		if ( $this->in_jetpack ) {
647
			add_filter( 'the_content', array( &$this, 'post_likes' ), 30, 1 );
648
			add_filter( 'the_excerpt', array( &$this, 'post_likes' ), 30, 1 );
649
650
		} else {
651
			add_filter( 'post_flair', array( &$this, 'post_likes' ), 30, 1 );
652
			add_filter( 'post_flair_block_css', array( $this, 'post_flair_service_enabled_like' ) );
653
654
			wp_enqueue_script( 'postmessage', '/wp-content/js/postmessage.js', array( 'jquery' ), JETPACK__VERSION, false );
655
			wp_enqueue_script( 'jquery_inview', '/wp-content/js/jquery/jquery.inview.js', array( 'jquery' ), JETPACK__VERSION, false );
656
			wp_enqueue_script( 'jetpack_resize', '/wp-content/js/jquery/jquery.jetpack-resize.js', array( 'jquery' ), JETPACK__VERSION, false );
657
			wp_enqueue_style( 'jetpack_likes', plugins_url( 'jetpack-likes.css', __FILE__ ), array(), JETPACK__VERSION );
658
		}
659
	}
660
661
	/**
662
	* Register scripts
663
	*/
664
	function register_scripts() {
665
		// Lets register all the sciprts
666
		wp_register_script( 'postmessage', plugins_url( '_inc/postmessage.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false );
667
		wp_register_script( 'jquery_inview', plugins_url( '_inc/jquery.inview.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false );
668
		wp_register_script( 'jetpack_resize', plugins_url( '_inc/jquery.jetpack-resize.js' , dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false );
669
		wp_register_script( 'jetpack_likes_queuehandler', plugins_url( 'likes/queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize', 'jquery_inview' ), JETPACK__VERSION, true );
670
	}
671
672
	/**
673
	* Load the CSS needed for the wp-admin area.
674
	*/
675
	function load_admin_css() {
676
		?>
677
		<?php if ( version_compare( $GLOBALS['wp_version'], '4.3-alpha', '>=' ) ) : ?>
678
			<style type="text/css">
679
				.vers img { display: none; }
680
				.metabox-prefs .vers img { display: inline; }
681
				.fixed .column-likes { width: 5.5em; padding: 8px 0; text-align: left; }
682
				.fixed .column-stats { width: 5em; }
683
				.fixed .column-likes .post-com-count {
684
					-webkit-box-sizing: border-box;
685
					-moz-box-sizing: border-box;
686
					box-sizing: border-box;
687
					display: inline-block;
688
					padding: 0 8px;
689
					height: 2em;
690
					margin-top: 5px;
691
					-webkit-border-radius: 5px;
692
					border-radius: 5px;
693
					background-color: #72777C;
694
					color: #FFF;
695
					font-size: 11px;
696
					line-height: 21px;
697
				}
698
				.fixed .column-likes .post-com-count::after { border: none !important; }
699
				.fixed .column-likes .post-com-count:hover { background-color: #0073AA; }
700
				.fixed .column-likes .vers:before {
701
					font: normal 20px/1 dashicons;
702
					content: '\f155';
703
					speak: none;
704
					-webkit-font-smoothing: antialiased;
705
					-moz-osx-font-smoothing: grayscale;
706
				}
707
				@media screen and (max-width: 782px) {
708
					.fixed .column-likes {
709
						display: none;
710
					}
711
				}
712
			</style>
713
		<?php else : // @todo Remove when 4.3 is minimum ?>
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
714
			<style type="text/css">
715
				.fixed .column-likes { width: 5em; padding-top: 8px; text-align: center !important; }
716
				.fixed .column-stats { width: 5em; }
717
				.fixed .column-likes .post-com-count { background-image: none; }
718
				.fixed .column-likes .post-com-count::after { border: none !important; }
719
				.fixed .column-likes .comment-count { background-color: #bbb; }
720
				.fixed .column-likes .comment-count:hover { background-color: #2ea2cc; }
721
				.fixed .column-likes .vers img { display: none; }
722
				.fixed .column-likes .vers:before {
723
					font: normal 20px/1 dashicons;
724
					content: '\f155';
725
					speak: none;
726
					-webkit-font-smoothing: antialiased;
727
					-moz-osx-font-smoothing: grayscale;
728
				}
729
				@media screen and (max-width: 782px) {
730
					.fixed .column-likes {
731
						display: none;
732
					}
733
				}
734
			</style>
735
		<?php endif; ?>
736
		<?php
737
	}
738
739
	/**
740
	* Load the JS required for loading the like counts.
741
	*/
742
	function enqueue_admin_scripts() {
743
		if ( empty( $_GET['post_type'] ) || 'post' == $_GET['post_type'] || 'page' == $_GET['post_type'] ) {
744
			if ( $this->in_jetpack ) {
745
				wp_enqueue_script( 'likes-post-count', plugins_url( 'modules/likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION );
746
				wp_enqueue_script( 'likes-post-count-jetpack', plugins_url( 'modules/likes/post-count-jetpack.js', dirname( __FILE__ ) ), array( 'likes-post-count' ), JETPACK__VERSION );
747
			} else {
748
				wp_enqueue_script( 'jquery.wpcom-proxy-request', "/wp-content/js/jquery/jquery.wpcom-proxy-request.js", array('jquery'), NULL, true );
749
				wp_enqueue_script( 'likes-post-count', plugins_url( 'likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION );
750
				wp_enqueue_script( 'likes-post-count-wpcom', plugins_url( 'likes/post-count-wpcom.js', dirname( __FILE__ ) ), array( 'likes-post-count', 'jquery.wpcom-proxy-request' ), JETPACK__VERSION );
751
			}
752
		}
753
	}
754
755
	/**
756
	* Add "Likes" column data to the post edit table in wp-admin.
757
	*
758
	* @param string $column_name
759
	* @param int $post_id
760
	*/
761
	function likes_edit_column( $column_name, $post_id ) {
762
		if ( 'likes' == $column_name ) {
763
764
			if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
765
				$blog_id = get_current_blog_id();
766
			} else {
767
				$blog_id = Jetpack_Options::get_option( 'id' );
768
			}
769
770
			$permalink = get_permalink( get_the_ID() ); ?>
771
			<a title="" data-post-id="<?php echo (int) $post_id; ?>" class="post-com-count post-like-count" id="post-like-count-<?php echo (int) $post_id; ?>" data-blog-id="<?php echo (int) $blog_id; ?>" href="<?php echo esc_url( $permalink ); ?>#like-<?php echo (int) $post_id; ?>">
772
				<span class="comment-count">0</span>
773
			</a>
774
			<?php
775
		}
776
	}
777
778
	/**
779
	* Add a "Likes" column header to the post edit table in wp-admin.
780
	*
781
	* @param array $columns
782
	* @return array
783
	*/
784
	function add_like_count_column( $columns ) {
785
		$date = $columns['date'];
786
		unset( $columns['date'] );
787
788
		$columns['likes'] = '<span class="vers"><img title="' . esc_attr__( 'Likes', 'jetpack' ) . '" alt="' . esc_attr__( 'Likes', 'jetpack' ) . '" src="//s0.wordpress.com/i/like-grey-icon.png" /></span>';
789
		$columns['date'] = $date;
790
791
		return $columns;
792
	}
793
794
	function post_likes( $content ) {
795
		global $post;
796
797
		if ( ! $this->is_likes_visible() )
798
			return $content;
799
800 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
801
			$blog_id = get_current_blog_id();
802
			$bloginfo = get_blog_details( (int) $blog_id );
803
			$domain = $bloginfo->domain;
804
		} else {
805
			$blog_id = Jetpack_Options::get_option( 'id' );
806
			$url = home_url();
807
			$url_parts = parse_url( $url );
808
			$domain = $url_parts['host'];
809
		}
810
		// make sure to include the scripts before the iframe otherwise weird things happen
811
		add_action( 'wp_footer', array( $this, 'likes_master' ), 21 );
812
813
		/**
814
		* if the same post appears more then once on a page the page goes crazy
815
		* we need a slightly more unique id / name for the widget wrapper.
816
		*/
817
		$uniqid = uniqid();
818
819
		$src = sprintf( '//widgets.wp.com/likes/#blog_id=%1$d&amp;post_id=%2$d&amp;origin=%3$s&amp;obj_id=%1$d-%2$d-%4$s', $blog_id, $post->ID, $domain, $uniqid );
820
		$name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post->ID, $uniqid );
821
		$wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post->ID, $uniqid );
822
823
		$html  = "<div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper' data-src='$src' data-name='$name'><h3 class='sd-title'>" . esc_html__( 'Like this:', 'jetpack' ) . '</h3>';
824
		$html .= "<div class='likes-widget-placeholder post-likes-widget-placeholder' style='height:55px'><span class='button'><span>" . esc_html__( 'Like', 'jetpack' ) . '</span></span> <span class="loading">' . esc_html__( 'Loading...', 'jetpack' ) . '</span></div>';
825
		$html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>";
826
		$html .= '</div>';
827
828
		// Lets make sure that the script is enqued
829
		wp_enqueue_script( 'jetpack_likes_queuehandler' );
830
831
		return $content . $html;
832
	}
833
834
	function comment_likes( $content, $comment = null ) {
835
		if ( empty( $comment ) )
836
			return $content;
837
838
		if ( ! $this->is_comments_enabled() )
839
			return $content;
840
841
		$protocol = 'http';
842
		if ( is_ssl() )
843
			$protocol = 'https';
844
845 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
846
			$blog_id = get_current_blog_id();
847
			$bloginfo = get_blog_details( (int) $blog_id );
848
			$domain = $bloginfo->domain;
849
		} else {
850
			$blog_id = Jetpack_Options::get_option( 'id' );
851
			$url = home_url();
852
			$url_parts = parse_url( $url );
853
			$domain = $url_parts['host'];
854
		}
855
		// make sure to include the scripts before the iframe otherwise weird things happen
856
		add_action( 'wp_footer', array( $this, 'likes_master' ), 21 );
857
858
		$src = sprintf( '%1$s://widgets.wp.com/likes/#blog_id=%2$d&amp;comment_id=%3$d&amp;origin=%1$s://%4$s', $protocol, $blog_id, $comment->comment_ID, $domain );
859
		$name = sprintf( 'like-comment-frame-%1$d-%2$d', $blog_id, $comment->comment_ID );
860
		$wrapper = sprintf( 'like-comment-wrapper-%1$d-%2$d', $blog_id, $comment->comment_ID );
861
862
		$html  = "<div><div class='jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper'>";
863
		$html .= "<iframe class='comment-likes-widget jetpack-likes-widget' name='$name' height='16px' width='100%' data='$src'></iframe>";
864
		$html .= '</div></div>';
865
		return $content . $html;
866
	}
867
868
	function post_flair_service_enabled_like( $classes ) {
869
		$classes[] = 'sd-like-enabled';
870
		return $classes;
871
	}
872
873
	function admin_bar_likes() {
874
		global $wp_admin_bar, $post;
875
876
		if ( ! $this->is_admin_bar_button_visible() ) {
877
			return;
878
		}
879
880
		$protocol = 'http';
881
		if ( is_ssl() )
882
			$protocol = 'https';
883
884 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
885
			$blog_id = get_current_blog_id();
886
			$bloginfo = get_blog_details( (int) $blog_id );
887
			$domain = $bloginfo->domain;
888
		} else {
889
			$blog_id = Jetpack_Options::get_option( 'id' );
890
			$url = home_url();
891
			$url_parts = parse_url( $url );
892
			$domain = $url_parts['host'];
893
		}
894
		// make sure to include the scripts before the iframe otherwise weird things happen
895
		add_action( 'wp_footer', array( $this, 'likes_master' ), 21 );
896
897
		$src = sprintf( '%1$s://widgets.wp.com/likes/#blog_id=%2$d&amp;post_id=%3$d&amp;origin=%1$s://%4$s', $protocol, $blog_id, $post->ID, $domain );
898
899
		$html = "<iframe class='admin-bar-likes-widget jetpack-likes-widget' scrolling='no' frameBorder='0' name='admin-bar-likes-widget' src='$src'></iframe>";
900
901
		$node = array(
902
				'id'   => 'admin-bar-likes-widget',
903
				'meta' => array(
904
							'html' => $html
905
				)
906
		);
907
908
		$wp_admin_bar->add_node( $node );
909
	}
910
911
	/**
912
	 * This function needs to get loaded after the scripts get added to the page.
913
	 *
914
	 */
915
	function likes_master() {
916
		$protocol = 'http';
917
		if ( is_ssl() )
918
			$protocol = 'https';
919
920
		$_locale = get_locale();
921
922
		// We have to account for w.org vs WP.com locale divergence
923
		if ( $this->in_jetpack ) {
924
			if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
925
				return false;
926
			}
927
928
			require_once JETPACK__GLOTPRESS_LOCALES_PATH;
929
930
			$gp_locale = GP_Locales::by_field( 'wp_locale', $_locale );
931
			$_locale = isset( $gp_locale->slug ) ? $gp_locale->slug : '';
932
		}
933
934
		$likes_locale = ( '' == $_locale || 'en' == $_locale ) ? '' : '&amp;lang=' . strtolower( $_locale );
935
936
		$src = sprintf(
937
			'%1$s://widgets.wp.com/likes/master.html?ver=%2$s#ver=%2$s%3$s',
938
			$protocol,
939
			$this->version,
940
			$likes_locale
941
		);
942
943
		$likersText = wp_kses( __( '<span>%d</span> bloggers like this:', 'jetpack' ), array( 'span' => array() ) );
944
		?>
945
		<iframe src='<?php echo $src; ?>' scrolling='no' id='likes-master' name='likes-master' style='display:none;'></iframe>
946
		<div id='likes-other-gravatars'><div class="likes-text"><?php echo $likersText; ?></div><ul class="wpl-avatars sd-like-gravatars"></ul></div>
947
		<?php
948
	}
949
950
	/**
951
	 * Get the 'disabled_likes' option from the DB of the current blog.
952
	 *
953
	 * @return array
954
	 */
955
	function get_options() {
956
		$setting             = array();
957
		$setting['disabled'] = get_option( 'disabled_likes'  );
958
		$sharing             = get_option( 'sharing-options' );
959
960
		// Default visibility settings
961
		if ( ! isset( $sharing['global']['show'] ) ) {
962
			$sharing['global']['show'] = array( 'post', 'page' );
963
964
		// Scalar check
965
		} elseif ( is_scalar( $sharing['global']['show'] ) ) {
966
			switch ( $sharing['global']['show'] ) {
967
				case 'posts' :
968
					$sharing['global']['show'] = array( 'post', 'page' );
969
					break;
970
				case 'index' :
971
					$sharing['global']['show'] = array( 'index' );
972
					break;
973
				case 'posts-index' :
974
					$sharing['global']['show'] = array( 'post', 'page', 'index' );
975
					break;
976
			}
977
		}
978
979
		// Ensure it's always an array (even if not previously empty or scalar)
980
		$setting['show'] = !empty( $sharing['global']['show'] ) ? (array) $sharing['global']['show'] : array();
981
982
		/**
983
		 * Filters where the Likes are displayed.
984
		 *
985
		 * @module likes
986
		 *
987
		 * @since 2.2.0
988
		 *
989
		 * @param array $setting Array of Likes display settings.
990
		 */
991
		return apply_filters( 'wpl_get_options', $setting );
992
	}
993
994
	/** _is_ functions ************************************************************/
995
996
	/**
997
	 * Are likes visible in this context?
998
	 *
999
	 * Some of this code was taken and modified from sharing_display() to ensure
1000
	 * similar logic and filters apply here, too.
1001
	 */
1002
	function is_likes_visible() {
1003
1004
		global $post, $wp_current_filter;              // Used to apply 'sharing_show' filter
1005
		// @todo: Remove this block when 4.5 is the minimum
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
1006
		global $wp_version;
1007
		$comment_popup = false;
1008
		if ( version_compare( $wp_version, '4.5-alpha', '<=' ) ) {
1009
			$comment_popup = is_comments_popup();
1010
		}
1011
		// End 4.5 conditional block.
1012
1013
		// Never show on feeds or previews
1014
		if ( is_feed() || is_preview() || $comment_popup ) { // @todo: Remove $comment_popup when 4.5 is minimum.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
1015
			$enabled = false;
1016
1017
		// Not a feed or preview, so what is it?
1018
		} else {
1019
1020
			if ( in_the_loop() ) {
1021
				// If in the loop, check if the current post is likeable
1022
				$enabled = $this->is_post_likeable();
1023
			} else {
1024
				// Otherwise, check and see if likes are enabled sitewide
1025
				$enabled = $this->is_enabled_sitewide();
1026
			}
1027
1028
			if ( post_password_required() )
1029
				$enabled = false;
1030
1031
			if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
1032
				$enabled = false;
1033
			}
1034
1035
			// Sharing Setting Overrides ****************************************
1036
1037
			// Single post including custom post types
1038
			if ( is_single() ) {
1039
				if ( ! $this->is_single_post_enabled( $post->post_type ) ) {
1040
					$enabled = false;
1041
				}
1042
1043
			// Single page
1044
			} elseif ( is_page() && ! is_front_page() ) {
1045
				if ( ! $this->is_single_page_enabled() ) {
1046
					$enabled = false;
1047
				}
1048
1049
			// Attachment
1050
			} elseif ( is_attachment() ) {
1051
				if ( ! $this->is_attachment_enabled() ) {
1052
					$enabled = false;
1053
				}
1054
1055
			// All other loops
1056
			} elseif ( ! $this->is_index_enabled() ) {
1057
				$enabled = false;
1058
			}
1059
		}
1060
1061
		if( is_object( $post ) ) {
1062
			// Check that the post is a public, published post.
1063
			if ( 'attachment' == $post->post_type ) {
1064
				$post_status = get_post_status( $post->post_parent );
1065
			} else {
1066
				$post_status = $post->post_status;
1067
			}
1068
			if ( 'publish' != $post_status ) {
1069
				$enabled = false;
1070
			}
1071
		}
1072
1073
		// Run through the sharing filters
1074
		/** This filter is documented in modules/sharedaddy/sharing-service.php */
1075
		$enabled = apply_filters( 'sharing_show', $enabled, $post );
1076
1077
		/**
1078
		 * Filters whether the Likes should be visible or not.
1079
		 * Allows overwriting the options set in Settings > Sharing.
1080
		 *
1081
		 * @module likes
1082
		 *
1083
		 * @since 2.2.0
1084
		 *
1085
		 * @param bool $enabled Should the Likes be visible?
1086
		 */
1087
		return (bool) apply_filters( 'wpl_is_likes_visible', $enabled );
1088
	}
1089
1090
	/**
1091
	 * Returns the current state of the "WordPress.com Likes are" option.
1092
	 * @return boolean true if enabled sitewide, false if not
1093
	 */
1094
	function is_enabled_sitewide() {
1095
		/**
1096
		 * Filters whether Likes are enabled by default on all posts.
1097
		 * true if enabled sitewide, false if not.
1098
		 *
1099
		 * @module likes
1100
		 *
1101
		 * @since 2.2.0
1102
		 *
1103
		 * @param bool $option Are Likes enabled sitewide.
1104
		 */
1105
		return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
1106
	}
1107
1108
	/**
1109
	 * Returns the current state of the "WordPress.com Reblogs are" option.
1110
	 * @return boolean true if enabled sitewide, false if not
1111
	 */
1112
	function reblogs_enabled_sitewide() {
1113
		/**
1114
		 * Filters whether Reblogs are enabled by default on all posts.
1115
		 * true if enabled sitewide, false if not.
1116
		 *
1117
		 * @module likes
1118
		 *
1119
		 * @since 3.0.0
1120
		 *
1121
		 * @param bool $option Are Reblogs enabled sitewide.
1122
		 */
1123
		return (bool) apply_filters( 'wpl_reblogging_enabled_sitewide', ! get_option( 'disabled_reblogs' ) );
1124
	}
1125
1126
	/**
1127
	 * Returns if comment likes are enabled. Defaults to 'off'
1128
	 * @todo decide what the default should be
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
1129
	 * @return boolean true if we should show comment likes, false if not
1130
	 */
1131
	function is_comments_enabled() {
1132
		/**
1133
		 * Filters whether Comment Likes are enabled.
1134
		 * true if enabled, false if not.
1135
		 *
1136
		 * @module likes
1137
		 *
1138
		 * @since 2.2.0
1139
		 *
1140
		 * @param bool $option Are Comment Likes enabled sitewide.
1141
		 */
1142
		return (bool) apply_filters( 'jetpack_comment_likes_enabled', get_option( 'jetpack_comment_likes_enabled', false ) );
1143
	}
1144
1145
	function is_admin_bar_button_visible() {
1146
		global $wp_admin_bar;
1147
1148
		if ( ! is_object( $wp_admin_bar ) )
1149
			return false;
1150
1151
		if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) )
1152
			return false;
1153
1154
		if ( ! $this->is_likes_visible() )
1155
			return false;
1156
1157
		if ( ! $this->is_post_likeable() )
1158
			return false;
1159
1160
		/**
1161
		 * Filters whether the Like button is enabled in the admin bar.
1162
		 *
1163
		 * @module likes
1164
		 *
1165
		 * @since 2.2.0
1166
		 *
1167
		 * @param bool true Should the Like button be visible in the Admin bar. Default to true.
1168
		 */
1169
		return (bool) apply_filters( 'jetpack_admin_bar_likes_enabled', true );
1170
	}
1171
1172
	/**
1173
	 * Are likes enabled for this post?
1174
	 *
1175
	 * @param int $post_id
1176
	 * @retun bool
1177
	 */
1178
	function is_post_likeable( $post_id = 0 ) {
1179
		$post = get_post( $post_id );
1180
		if ( !$post || is_wp_error( $post ) ) {
1181
			return false;
1182
		}
1183
1184
		$sitewide_likes_enabled = (bool) Jetpack_Likes::is_enabled_sitewide();
1185
		$post_likes_switched    = (bool) get_post_meta( $post->ID, 'switch_like_status', true );
1186
1187
		$post_likes_enabled = $sitewide_likes_enabled;
1188
		if ( $post_likes_switched ) {
1189
			$post_likes_enabled = ! $post_likes_enabled;
1190
		}
1191
1192
		return $post_likes_enabled;
1193
	}
1194
1195
	/**
1196
	 * Are Post Likes enabled on archive/front/search pages?
1197
	 *
1198
	 * @return bool
1199
	 */
1200
	function is_index_enabled() {
1201
		$options = $this->get_options();
1202
		/**
1203
		 * Filters whether Likes should be enabled on archive/front/search pages.
1204
		 *
1205
		 * @module likes
1206
		 *
1207
		 * @since 2.2.0
1208
		 *
1209
		 * @param bool $enabled Are Post Likes enabled on archive/front/search pages?
1210
		 */
1211
		return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'] ) );
1212
	}
1213
1214
	/**
1215
	 * Are Post Likes enabled on single posts?
1216
	 *
1217
	 * @param String $post_type custom post type identifier
1218
	 * @return bool
1219
	 */
1220 View Code Duplication
	function is_single_post_enabled( $post_type = 'post' ) {
1221
		$options = $this->get_options();
1222
		return (bool) apply_filters(
1223
			/**
1224
			 * Filters whether Likes should be enabled on single posts.
1225
			 *
1226
			 * The dynamic part of the filter, {$post_type}, allows you to specific the post type where Likes should be enabled.
1227
			 *
1228
			 * @module likes
1229
			 *
1230
			 * @since 2.2.0
1231
			 *
1232
			 * @param bool $enabled Are Post Likes enabled on single posts?
1233
			 */
1234
			"wpl_is_single_{$post_type}_disabled",
1235
			(bool) in_array( $post_type, $options['show'] )
1236
		);
1237
	}
1238
1239
	/**
1240
	 * Are Post Likes enabled on single pages?
1241
	 *
1242
	 * @return bool
1243
	 */
1244 View Code Duplication
	function is_single_page_enabled() {
1245
		$options = $this->get_options();
1246
		/**
1247
		 * Filters whether Likes should be enabled on single pages.
1248
		 *
1249
		 * @module likes
1250
		 *
1251
		 * @since 2.2.0
1252
		 *
1253
		 * @param bool $enabled Are Post Likes enabled on single pages?
1254
		 */
1255
		return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'] ) );
1256
	}
1257
1258
	/**
1259
	 * Are Media Likes enabled on single pages?
1260
	 *
1261
	 * @return bool
1262
	 */
1263
	function is_attachment_enabled() {
1264
		$options = $this->get_options();
1265
		/**
1266
		 * Filters whether Likes should be enabled on attachment pages.
1267
		 *
1268
		 * @module likes
1269
		 *
1270
		 * @since 2.2.0
1271
		 *
1272
		 * @param bool $enabled Are Post Likes enabled on attachment pages?
1273
		 */
1274
		return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'] ) );
1275
	}
1276
}
1277
1278
Jetpack_Likes::init();
1279