Completed
Push — update/likes-style-admin-merge ( edeb0b )
by Jeremy
11:07
created

Jetpack_Likes::is_enabled_sitewide()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 1
eloc 2
nc 1
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 = '20160301';
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, 'module_toggle' ) );
43
			add_action( 'jetpack_deactivate_module_likes', array( $this, 'module_toggle' ) );
44
			add_action( 'jetpack_activate_module_likes',   array( $this, 'set_social_notifications_like' ) );
45
			add_action( 'jetpack_deactivate_module_likes', array( $this, 'delete_social_notifications_like' ) );
46
47
			Jetpack::enable_module_configurable( __FILE__ );
48
			Jetpack::module_configuration_load( __FILE__, array( $this, 'configuration_redirect' ) );
49
50
			add_action('admin_print_scripts-settings_page_sharing', array( &$this, 'load_jp_css' ) );
51
			add_filter( 'sharing_show_buttons_on_row_start', array( $this, 'configuration_target_area' ) );
52
53
			$active = Jetpack::get_active_modules();
54
55 View Code Duplication
			if ( ! in_array( 'sharedaddy', $active ) && ! in_array( 'publicize', $active ) ) {
56
				add_action( 'admin_menu', array( $this, 'sharing_menu' ) );	// we don't have a sharing page yet
57
			}
58
59
			if ( in_array( 'publicize', $active ) && ! in_array( 'sharedaddy', $active ) ) {
60
				add_action( 'pre_admin_screen_sharing', array( $this, 'sharing_block' ), 20 ); // we have a sharing page but not the global options area
61
				add_action( 'pre_admin_screen_sharing', array( $this, 'updated_message' ), -10 );
62
			}
63
64
			if( ! in_array( 'sharedaddy', $active ) ) {
65
				add_action( 'admin_init', array( $this, 'process_update_requests_if_sharedaddy_not_loaded' ) );
66
				add_action( 'sharing_global_options', array( $this, 'admin_settings_showbuttonon_init' ), 19 );
67
				add_action( 'sharing_admin_update', array( $this, 'admin_settings_showbuttonon_callback' ), 19 );
68
				add_action( 'admin_init', array( $this, 'add_meta_box' ) );
69
			} else {
70
				add_filter( 'sharing_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) );
71
				add_action( 'start_sharing_meta_box_content', array( $this, 'meta_box_content' ) );
72
			}
73
74
			Jetpack_Sync::sync_options( __FILE__, 'social_notifications_like' );
75
76
		} else { // wpcom
77
			add_action( 'wpmu_new_blog', array( $this, 'enable_comment_likes' ), 10, 1 );
78
			add_action( 'admin_init', array( $this, 'add_meta_box' ) );
79
			add_action( 'end_likes_meta_box_content', array( $this, 'sharing_meta_box_content' ) );
80
			add_filter( 'likes_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) );
81
		}
82
83
		add_action( 'admin_init', array( $this, 'admin_discussion_likes_settings_init' ) ); // Likes notifications
84
85
		add_action( 'admin_bar_menu', array( $this, 'admin_bar_likes' ), 60 );
86
87
		add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) );
88
89
		add_action( 'save_post', array( $this, 'meta_box_save' ) );
90
		add_action( 'edit_attachment', array( $this, 'meta_box_save' ) );
91
		add_action( 'sharing_global_options', array( $this, 'admin_settings_init' ), 20 );
92
		add_action( 'sharing_admin_update',   array( $this, 'admin_settings_callback' ), 20 );
93
	}
94
95
	function maybe_sync_content() {
96
		if ( Jetpack::init()->sync->reindex_needed() ) {
97
			Jetpack::init()->sync->reindex_trigger();
98
		}
99
	}
100
101
	function module_toggle() {
102
		$jetpack = Jetpack::init();
103
		$jetpack->sync->register( 'noop' );
104
	}
105
106
	/**
107
	 * Set the social_notifications_like option to `on` when the Likes module is activated.
108
	 *
109
	 * @since 3.7.0
110
	 *
111
	 * @return null
112
	 */
113
	function set_social_notifications_like() {
114
		update_option( 'social_notifications_like', 'on' );
115
	}
116
117
	/**
118
	 * Delete the social_notifications_like option that was set to `on` on module activation.
119
	 *
120
	 * @since 3.7.0
121
	 *
122
	 * @return null
123
	 */
124
	function delete_social_notifications_like() {
125
		delete_option( 'social_notifications_like' );
126
	}
127
128
	/**
129
	 * Redirects to the likes section of the sharing page.
130
	 */
131
	function configuration_redirect() {
132
		wp_safe_redirect( admin_url( 'options-general.php?page=sharing#likes' ) );
133
		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...
134
	}
135
136
	/**
137
	 * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable
138
	 */
139
	function load_jp_css() {
140
		// Do we really need `admin_styles`? With the new admin UI, it's breaking some bits.
141
		// Jetpack::init()->admin_styles();
142
	}
143
	/**
144
	 * Load style on the front end.
145
	 * @return null
146
	 */
147
	function load_styles_register_scripts() {
148
149
		wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION );
150
		if( $this->in_jetpack ) {
151
			$this->register_scripts();
152
		}
153
	}
154
155
	/**
156
	 * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box
157
	 * @param  string $html row heading for the sharedaddy "which page" setting
158
	 * @return string       html with the jetpack-targetable class and likes id. tbody gets closed after the like settings
159
	 */
160
	function configuration_target_area( $html = '' ) {
161
		$html = "<tbody id='likes' class='jetpack-targetable'>" . $html;
162
		return $html;
163
	}
164
165
	/**
166
	 * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares"
167
	 * @param string $title The current title of the metabox, not needed/used.
168
	 */
169
	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...
170
		return __( 'Likes and Shares', 'jetpack' );
171
	}
172
173
	/**
174
	 * Adds a metabox to the post screen if the sharing one doesn't currently exist.
175
	 */
176
	function add_meta_box() {
177
		if (
178
			/**
179
			 * Allow disabling of the Likes metabox on the post editor screen.
180
			 *
181
			 * @module likes
182
			 *
183
			 * @since 2.2.0
184
			 *
185
			 * @param bool false Should the Likes metabox be disabled? Default to false.
186
			 */
187
			apply_filters( 'post_flair_disable', false )
188
		) {
189
			return;
190
		}
191
192
		$post_types = get_post_types( array( 'public' => true ) );
193
		/**
194
		 * Filters the Likes metabox title.
195
		 *
196
		 * @module likes
197
		 *
198
		 * @since 2.2.0
199
		 *
200
		 * @param string Likes metabox title. Default to "Likes".
201
		 */
202
		$title = apply_filters( 'likes_meta_box_title', __( 'Likes', 'jetpack' ) );
203
		foreach( $post_types as $post_type ) {
204
			add_meta_box( 'likes_meta', $title, array( $this, 'meta_box_content' ), $post_type, 'advanced', 'high' );
205
		}
206
	}
207
208
	function meta_box_save( $post_id ) {
209
		if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
210
			return $post_id;
211
212
		if ( empty( $_POST['wpl_like_status_hidden'] ) )
213
			return $post_id;
214
215
		// Record sharing disable. Only needs to be done for WPCOM
216
		if ( ! $this->in_jetpack ) {
217
			if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], get_post_types( array( 'public' => true ) ) ) ) {
218 View Code Duplication
				if ( ! isset( $_POST['wpl_enable_post_sharing'] ) ) {
219
					update_post_meta( $post_id, 'sharing_disabled', 1 );
220
				} else {
221
					delete_post_meta( $post_id, 'sharing_disabled' );
222
				}
223
			}
224
		}
225
226
		if ( 'post' == $_POST['post_type'] ) {
227
			if ( !current_user_can( 'edit_post', $post_id ) ) {
228
				return $post_id;
229
			}
230
		}
231
232
		// Record a change in like status for this post - only if it contradicts the
233
		// site like setting.
234
		if ( ( $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) || ( ! $this->is_enabled_sitewide() && !empty( $_POST['wpl_enable_post_likes'] ) ) ) {
235
			update_post_meta( $post_id, 'switch_like_status', 1 );
236
			//$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...
237
		} else {
238
			delete_post_meta( $post_id, 'switch_like_status' );
239
		}
240
241
		return $post_id;
242
	}
243
244
	/**
245
	 * Shows the likes option in the post screen metabox.
246
	 */
247
	function meta_box_content( $post ) {
248
		$post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
249
		$checked         = true;
250
		$disabled        = ! $this->is_enabled_sitewide();
251
		$switched_status = get_post_meta( $post_id, 'switch_like_status', true );
252
253
		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...
254
			$checked = false;
255
256
		/**
257
		 * Fires before the Likes meta box content in the post editor.
258
		 *
259
		 * @module likes
260
		 *
261
		 * @since 2.2.0
262
		 *
263
		 * @param WP_Post|array|null $post Post data.
264
		 */
265
		do_action( 'start_likes_meta_box_content', $post );
266
		?>
267
268
		<p>
269
			<label for="wpl_enable_post_likes">
270
				<input type="checkbox" name="wpl_enable_post_likes" id="wpl_enable_post_likes" value="1" <?php checked( $checked ); ?>>
271
				<?php esc_html_e( 'Show likes.', 'jetpack' ); ?>
272
			</label>
273
			<input type="hidden" name="wpl_like_status_hidden" value="1" />
274
		</p> <?php
275
		/**
276
		 * Fires after the Likes meta box content in the post editor.
277
		 *
278
		 * @module likes
279
		 *
280
		 * @since 2.2.0
281
		 *
282
		 * @param WP_Post|array|null $post Post data.
283
		 */
284
		do_action( 'end_likes_meta_box_content', $post );
285
	}
286
287
	/**
288
	 * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog)
289
	 */
290
	function sharing_meta_box_content( $post ) {
291
		$post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
292
		$disabled = get_post_meta( $post_id, 'sharing_disabled', true ); ?>
293
		<p>
294
			<label for="wpl_enable_post_sharing">
295
				<input type="checkbox" name="wpl_enable_post_sharing" id="wpl_enable_post_sharing" value="1" <?php checked( !$disabled ); ?>>
296
				<?php _e( 'Show sharing buttons.', 'jetpack' ); ?>
297
			</label>
298
			<input type="hidden" name="wpl_sharing_status_hidden" value="1" />
299
		</p> <?php
300
	}
301
302
	/**
303
	  * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page)
304
	  */
305
306 View Code Duplication
	function admin_discussion_likes_settings_init() {
307
		// Add a temporary section, until we can move the setting out of there and with the rest of the email notification settings
308
		add_settings_section( 'likes-notifications', __( 'Likes Notifications', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_section' ), 'discussion' );
309
		add_settings_field( 'social-notifications', __( 'Email me whenever', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_field' ), 'discussion', 'likes-notifications' );
310
		// Register the setting
311
		register_setting( 'discussion', 'social_notifications_like', array( $this, 'admin_discussion_likes_settings_validate' ) );
312
	}
313
314
	function admin_discussion_likes_settings_section() {
315
		// Atypical usage here.  We emit jquery to move likes notification checkbox to be with the rest of the email notification settings
316
?>
317
	<script type="text/javascript">
318
	jQuery( function( $ )  {
319
		var table = $( '#social_notifications_like' ).parents( 'table:first' ),
320
			header = table.prevAll( 'h3:first' ),
321
			newParent = $( '#moderation_notify' ).parent( 'label' ).parent();
322
323
		if ( !table.size() || !header.size() || !newParent.size() ) {
324
			return;
325
		}
326
327
		newParent.append( '<br/>' ).append( table.end().parent( 'label' ).siblings().andSelf() );
328
		header.remove();
329
		table.remove();
330
	} );
331
	</script>
332
<?php
333
	}
334
335
	function admin_likes_get_option( $option ) {
336
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
337
			$option_setting = get_blog_option( get_current_blog_id(), $option, 'on' );
338
		} else {
339
			$option_setting = get_option( $option, 'on' );
340
		}
341
342
		return intval( 'on' == $option_setting );
343
	}
344
345
	function admin_discussion_likes_settings_field() {
346
		$like = $this->admin_likes_get_option( 'social_notifications_like' );
347
?>
348
		<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>
349
<?php
350
	}
351
352
	function admin_discussion_likes_settings_validate( $input ) {
353
		// If it's not set (was unchecked during form submission) or was set to off (during option update), return 'off'.
354
		if ( !$input || 'off' == $input )
355
			return 'off';
356
357
		// Otherwise, return 'on'.
358
		return 'on';
359
	}
360
361
	/**
362
	 * The actual options block to be inserted into the sharing page.
363
	 */
364
	function admin_settings_init() { ?>
365
		<tr>
366
			<th scope="row">
367
				<label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label>
368
			</th>
369
			<td>
370
				<div>
371
					<label>
372
						<input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> />
373
						<?php esc_html_e( 'On for all posts', 'jetpack' ); ?>
374
					</label>
375
				</div>
376
				<div>
377
					<label>
378
						<input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> />
379
						<?php esc_html_e( 'Turned on per post', 'jetpack' ); ?>
380
					</label>
381
				<div>
382
			</td>
383
		</tr>
384
		<?php if ( ! $this->in_jetpack ) : ?>
385
		<tr>
386
			<th scope="row">
387
				<label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack' ); ?></label>
388
			</th>
389
			<td>
390
				<div>
391
					<label>
392
						<input type="radio" class="code" name="jetpack_reblogs_enabled" value="on" <?php checked( $this->reblogs_enabled_sitewide(), true ); ?> />
393
						<?php esc_html_e( 'Show the Reblog button on posts', 'jetpack' ); ?>
394
					</label>
395
				</div>
396
				<div>
397
					<label>
398
						<input type="radio" class="code" name="jetpack_reblogs_enabled" value="off" <?php checked( $this->reblogs_enabled_sitewide(), false ); ?> />
399
						<?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack' ); ?>
400
					</label>
401
				<div>
402
			</td>
403
		</tr>
404
		<tr>
405
			<th scope="row">
406
				<label><?php esc_html_e( 'Comment Likes are', 'jetpack' ); ?></label>
407
			</th>
408
			<td>
409
				<div>
410
					<label>
411
						<input type="checkbox" class="code" name="jetpack_comment_likes_enabled" value="1" <?php checked( $this->is_comments_enabled(), true ); ?> />
412
						<?php esc_html_e( 'On for all comments', 'jetpack' ); ?>
413
					</label>
414
				</div>
415
			</td>
416
		</tr>
417
		<?php endif; ?>
418
		</tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?>
419
	<?php }
420
421
	/**
422
	 * 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.
423
	 */
424
	function admin_settings_showbuttonon_init() { ?>
425
		<?php
426
			/** This action is documented in modules/sharedaddy/sharing.php */
427
			echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' );
428
		?>
429
		<th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th>
430
		<td>
431
			<?php
432
				$br = false;
433
				$shows = array_values( get_post_types( array( 'public' => true ) ) );
434
				array_unshift( $shows, 'index' );
435
				$global = $this->get_options();
436 View Code Duplication
				foreach ( $shows as $show ) :
437
					if ( 'index' == $show ) {
438
						$label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
439
					} else {
440
						$post_type_object = get_post_type_object( $show );
441
						$label = $post_type_object->labels->name;
442
					}
443
			?>
444
				<?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>
445
			<?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...
446
		</td>
447
		<?php
448
			/** This action is documented in modules/sharedaddy/sharing.php */
449
			echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' );
450
		?>
451
	<?php }
452
453
454
	/**
455
	 * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option.
456
	 */
457
	function admin_settings_showbuttonon_callback() {
458
		$options = get_option( 'sharing-options' );
459
		if ( !is_array( $options ) )
460
			$options = array();
461
462
		$shows = array_values( get_post_types( array( 'public' => true ) ) );
463
		$shows[] = 'index';
464
		$data = $_POST;
465
466
		if ( isset( $data['show'] ) ) {
467 View Code Duplication
			if ( is_scalar( $data['show'] ) ) {
468
				switch ( $data['show'] ) {
469
					case 'posts' :
470
						$data['show'] = array( 'post', 'page' );
471
					break;
472
					case 'index' :
473
						$data['show'] = array( 'index' );
474
					break;
475
					case 'posts-index' :
476
						$data['show'] = array( 'post', 'page', 'index' );
477
					break;
478
				}
479
			}
480
481 View Code Duplication
			if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
482
				$options['global']['show'] = $data['show'];
483
			}
484
		} else {
485
			$options['global']['show'] = array();
486
		}
487
488
		update_option( 'sharing-options', $options );
489
	}
490
491
	/**
492
	 * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled.
493
	 */
494
	function process_update_requests_if_sharedaddy_not_loaded() {
495
		if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) {
496
			if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) {
497
				/** This action is documented in modules/sharedaddy/sharing.php */
498
				do_action( 'sharing_admin_update' );
499
				wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
500
				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...
501
			}
502
		}
503
	}
504
505
	/**
506
	 * Saves the setting in the database, bumps a stat on WordPress.com
507
	 */
508
	function admin_settings_callback() {
509
		// We're looking for these, and doing a dance to set some stats and save
510
		// them together in array option.
511
		$new_state = !empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on';
512
		$db_state  = $this->is_enabled_sitewide();
513
514
		$reblogs_new_state = !empty( $_POST['jetpack_reblogs_enabled'] ) ? $_POST['jetpack_reblogs_enabled'] : 'on';
515
		$reblogs_db_state = $this->reblogs_enabled_sitewide();
516
		/** Default State *********************************************************/
517
518
		// Checked (enabled)
519 View Code Duplication
		switch( $new_state ) {
520
			case 'off' :
521
				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...
522
					$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...
523
				}
524
				update_option( 'disabled_likes', 1 );
525
				break;
526
			case 'on'  :
527
			default:
528
				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...
529
					$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...
530
				}
531
				delete_option( 'disabled_likes' );
532
				break;
533
		}
534
535 View Code Duplication
		switch( $reblogs_new_state ) {
536
			case 'off' :
537
				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...
538
					$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...
539
				}
540
				update_option( 'disabled_reblogs', 1 );
541
				break;
542
			case 'on'  :
543
			default:
544
				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...
545
					$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...
546
				}
547
				delete_option( 'disabled_reblogs' );
548
				break;
549
		}
550
551
		// comment setting
552
		$new_comments_state = !empty( $_POST['jetpack_comment_likes_enabled'] ) ? $_POST['jetpack_comment_likes_enabled'] : false;
553
		switch( (bool) $new_comments_state ) {
554
			case true:
555
				update_option( 'jetpack_comment_likes_enabled', 1 );
556
			break;
557
			case false:
558
			default:
559
				update_option( 'jetpack_comment_likes_enabled', 0 );
560
			break;
561
		}
562
	}
563
564
	/**
565
	 * Force comment likes on for a blog
566
	 * Used when a new blog is created
567
	 */
568
	function enable_comment_likes( $blog_id ) {
569
		switch_to_blog( $blog_id );
570
		update_option( 'jetpack_comment_likes_enabled', 1 );
571
		restore_current_blog();
572
	}
573
574
	/**
575
	 * Adds the 'sharing' menu to the settings menu.
576
	 * Only ran if sharedaddy and publicize are not already active.
577
	 */
578
	function sharing_menu() {
579
		add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) );
580
	}
581
582
	/**
583
	 * Provides a sharing page with the sharing_global_options hook
584
	 * so we can display the setting.
585
	 * Only ran if sharedaddy and publicize are not already active.
586
	 */
587
	function sharing_page() {
588
		$this->updated_message(); ?>
589
		<div class="wrap">
590
			<div class="icon32" id="icon-options-general"><br /></div>
591
			<h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1>
592
			<?php
593
				/** This action is documented in modules/sharedaddy/sharing.php */
594
				do_action( 'pre_admin_screen_sharing' );
595
			?>
596
			<?php $this->sharing_block(); ?>
597
		</div> <?php
598
	}
599
600
	/**
601
	 * Returns the settings have been saved message.
602
	 */
603
	function updated_message() {
604
		if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' )
605
			echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>';
606
	}
607
608
	/**
609
	 * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts
610
	 */
611
	function sharing_block() { ?>
612
		<h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2>
613
		<form method="post" action="">
614
		<table class="form-table">
615
		<tbody>
616
			<?php
617
			/** This action is documented in modules/sharedaddy/sharing.php */
618
			do_action( 'sharing_global_options' );
619
			?>
620
		</tbody>
621
		</table>
622
623
		<p class="submit">
624
			<input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" />
625
		</p>
626
627
		<input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" />
628
		</form> <?php
629
	}
630
631
	function admin_init() {
632
		add_filter( 'manage_posts_columns', array( $this, 'add_like_count_column' ) );
633
		add_filter( 'manage_pages_columns', array( $this, 'add_like_count_column' ) );
634
		add_action( 'manage_posts_custom_column', array( $this, 'likes_edit_column' ), 10, 2 );
635
		add_action( 'manage_pages_custom_column', array( $this, 'likes_edit_column' ), 10, 2 );
636
		add_action( 'admin_print_styles-edit.php', array( $this, 'load_admin_css' ) );
637
		add_action( "admin_print_scripts-edit.php", array( $this, 'enqueue_admin_scripts' ) );
638
639
640
		if ( $this->in_jetpack ) {
641
			$post_stati = get_post_stati( array( 'public' => true ) ); // All public post stati
642
			$post_stati[] = 'private';                                 // Content from private stati will be redacted
643
			Jetpack_Sync::sync_posts( __FILE__, array(
644
				'post_types' => get_post_types( array( 'public' => true ) ),
645
				'post_stati' => $post_stati,
646
				) );
647
		}
648
	}
649
650
	function action_init() {
651
		if ( is_admin() ) {
652
			return;
653
		}
654
655
		if ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ||
656
			 ( defined( 'APP_REQUEST' ) && APP_REQUEST ) ||
657
			 ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
658
			 ( defined( 'COOKIE_AUTH_REQUEST' ) && COOKIE_AUTH_REQUEST ) ||
659
			 ( defined( 'JABBER_SERVER' ) && JABBER_SERVER ) ) {
660
			return;
661
		}
662
663
		// Comment Likes widget has been disabled, pending performance improvements.
664
		// add_filter( 'comment_text', array( &$this, 'comment_likes' ), 10, 2 );
665
666
		if ( $this->in_jetpack ) {
667
			add_filter( 'the_content', array( &$this, 'post_likes' ), 30, 1 );
668
			add_filter( 'the_excerpt', array( &$this, 'post_likes' ), 30, 1 );
669
670
		} else {
671
			add_filter( 'post_flair', array( &$this, 'post_likes' ), 30, 1 );
672
			add_filter( 'post_flair_block_css', array( $this, 'post_flair_service_enabled_like' ) );
673
674
			wp_enqueue_script( 'postmessage', '/wp-content/js/postmessage.js', array( 'jquery' ), JETPACK__VERSION, false );
675
			wp_enqueue_script( 'jquery_inview', '/wp-content/js/jquery/jquery.inview.js', array( 'jquery' ), JETPACK__VERSION, false );
676
			wp_enqueue_script( 'jetpack_resize', '/wp-content/js/jquery/jquery.jetpack-resize.js', array( 'jquery' ), JETPACK__VERSION, false );
677
			wp_enqueue_style( 'jetpack_likes', plugins_url( 'jetpack-likes.css', __FILE__ ), array(), JETPACK__VERSION );
678
		}
679
	}
680
681
	/**
682
	* Register scripts
683
	*/
684
	function register_scripts() {
685
		// Lets register all the sciprts
686
		wp_register_script( 'postmessage', plugins_url( '_inc/postmessage.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false );
687
		wp_register_script( 'jquery_inview', plugins_url( '_inc/jquery.inview.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false );
688
		wp_register_script( 'jetpack_resize', plugins_url( '_inc/jquery.jetpack-resize.js' , dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false );
689
		wp_register_script( 'jetpack_likes_queuehandler', plugins_url( 'likes/queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize', 'jquery_inview' ), JETPACK__VERSION, true );
690
	}
691
692
	/**
693
	* Load the CSS needed for the wp-admin area.
694
	*/
695
	function load_admin_css() {
696
		?>
697
		<style type="text/css">
698
			.fixed .column-likes { width: 5em; padding-top: 8px; text-align: center !important; }
699
			.fixed .column-stats { width: 5em; }
700
			.fixed .column-likes a .comment-count { background: none; color: #555; }
701
			.fixed .column-likes .post-com-count { background-image: none; }
702
		<?php if ( version_compare( $GLOBALS['wp_version'], '3.8-alpha', '>=' ) ) : ?>
703
			.fixed .column-likes .post-com-count::after { border: none !important; }
704
			.fixed .column-likes .vers img { display: none; }
705
			.fixed .column-likes .vers:before {
706
				font: normal 20px/1 dashicons;
707
				content: '\f155';
708
				speak: none;
709
				-webkit-font-smoothing: antialiased;
710
				-moz-osx-font-smoothing: grayscale;
711
			}
712
			@media screen and (max-width: 782px) {
713
				.fixed .column-likes {
714
					display: none;
715
				}
716
			}
717
		<?php endif; ?>
718
		</style>
719
		<?php
720
	}
721
722
	/**
723
	* Load the JS required for loading the like counts.
724
	*/
725
	function enqueue_admin_scripts() {
726
		if ( empty( $_GET['post_type'] ) || 'post' == $_GET['post_type'] || 'page' == $_GET['post_type'] ) {
727
			if ( $this->in_jetpack ) {
728
				wp_enqueue_script( 'likes-post-count', plugins_url( 'modules/likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION );
729
				wp_enqueue_script( 'likes-post-count-jetpack', plugins_url( 'modules/likes/post-count-jetpack.js', dirname( __FILE__ ) ), array( 'likes-post-count' ), JETPACK__VERSION );
730
			} else {
731
				wp_enqueue_script( 'jquery.wpcom-proxy-request', "/wp-content/js/jquery/jquery.wpcom-proxy-request.js", array('jquery'), NULL, true );
732
				wp_enqueue_script( 'likes-post-count', plugins_url( 'likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION );
733
				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 );
734
			}
735
		}
736
	}
737
738
	/**
739
	* Add "Likes" column data to the post edit table in wp-admin.
740
	*
741
	* @param string $column_name
742
	* @param int $post_id
743
	*/
744
	function likes_edit_column( $column_name, $post_id ) {
745
		if ( 'likes' == $column_name ) {
746
747
			if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
748
				$blog_id = get_current_blog_id();
749
			} else {
750
				$blog_id = Jetpack_Options::get_option( 'id' );
751
			}
752
753
			$permalink = get_permalink( get_the_ID() ); ?>
754
			<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; ?>">
755
				<span class="comment-count">—</span>
756
			</a>
757
			<?php
758
		}
759
	}
760
761
	/**
762
	* Add a "Likes" column header to the post edit table in wp-admin.
763
	*
764
	* @param array $columns
765
	* @return array
766
	*/
767
	function add_like_count_column( $columns ) {
768
		$date = $columns['date'];
769
		unset( $columns['date'] );
770
771
		$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>';
772
		$columns['date'] = $date;
773
774
		return $columns;
775
	}
776
777
	function post_likes( $content ) {
778
		global $post;
779
780
		if ( ! $this->is_likes_visible() )
781
			return $content;
782
783 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
784
			$blog_id = get_current_blog_id();
785
			$bloginfo = get_blog_details( (int) $blog_id );
786
			$domain = $bloginfo->domain;
787
		} else {
788
			$blog_id = Jetpack_Options::get_option( 'id' );
789
			$url = home_url();
790
			$url_parts = parse_url( $url );
791
			$domain = $url_parts['host'];
792
		}
793
		// make sure to include the scripts before the iframe otherwise weird things happen
794
		add_action( 'wp_footer', array( $this, 'likes_master' ), 21 );
795
796
		/**
797
		* if the same post appears more then once on a page the page goes crazy
798
		* we need a slightly more unique id / name for the widget wrapper.
799
		*/
800
		$uniqid = uniqid();
801
802
		$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 );
803
		$name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post->ID, $uniqid );
804
		$wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post->ID, $uniqid );
805
806
		$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>';
807
		$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>';
808
		$html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>";
809
		$html .= '</div>';
810
811
		// Lets make sure that the script is enqued
812
		wp_enqueue_script( 'jetpack_likes_queuehandler' );
813
814
		return $content . $html;
815
	}
816
817
	function comment_likes( $content, $comment = null ) {
818
		if ( empty( $comment ) )
819
			return $content;
820
821
		if ( ! $this->is_comments_enabled() )
822
			return $content;
823
824
		$protocol = 'http';
825
		if ( is_ssl() )
826
			$protocol = 'https';
827
828 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
829
			$blog_id = get_current_blog_id();
830
			$bloginfo = get_blog_details( (int) $blog_id );
831
			$domain = $bloginfo->domain;
832
		} else {
833
			$blog_id = Jetpack_Options::get_option( 'id' );
834
			$url = home_url();
835
			$url_parts = parse_url( $url );
836
			$domain = $url_parts['host'];
837
		}
838
		// make sure to include the scripts before the iframe otherwise weird things happen
839
		add_action( 'wp_footer', array( $this, 'likes_master' ), 21 );
840
841
		$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 );
842
		$name = sprintf( 'like-comment-frame-%1$d-%2$d', $blog_id, $comment->comment_ID );
843
		$wrapper = sprintf( 'like-comment-wrapper-%1$d-%2$d', $blog_id, $comment->comment_ID );
844
845
		$html  = "<div><div class='jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper'>";
846
		$html .= "<iframe class='comment-likes-widget jetpack-likes-widget' name='$name' height='16px' width='100%' data='$src'></iframe>";
847
		$html .= '</div></div>';
848
		return $content . $html;
849
	}
850
851
	function post_flair_service_enabled_like( $classes ) {
852
		$classes[] = 'sd-like-enabled';
853
		return $classes;
854
	}
855
856
	function admin_bar_likes() {
857
		global $wp_admin_bar, $post;
858
859
		if ( ! $this->is_admin_bar_button_visible() ) {
860
			return;
861
		}
862
863
		$protocol = 'http';
864
		if ( is_ssl() )
865
			$protocol = 'https';
866
867 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
868
			$blog_id = get_current_blog_id();
869
			$bloginfo = get_blog_details( (int) $blog_id );
870
			$domain = $bloginfo->domain;
871
		} else {
872
			$blog_id = Jetpack_Options::get_option( 'id' );
873
			$url = home_url();
874
			$url_parts = parse_url( $url );
875
			$domain = $url_parts['host'];
876
		}
877
		// make sure to include the scripts before the iframe otherwise weird things happen
878
		add_action( 'wp_footer', array( $this, 'likes_master' ), 21 );
879
880
		$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 );
881
882
		$html = "<iframe class='admin-bar-likes-widget jetpack-likes-widget' scrolling='no' frameBorder='0' name='admin-bar-likes-widget' src='$src'></iframe>";
883
884
		$node = array(
885
				'id'   => 'admin-bar-likes-widget',
886
				'meta' => array(
887
							'html' => $html
888
				)
889
		);
890
891
		$wp_admin_bar->add_node( $node );
892
	}
893
894
	/**
895
	 * This function needs to get loaded after the scripts get added to the page.
896
	 *
897
	 */
898
	function likes_master() {
899
		$protocol = 'http';
900
		if ( is_ssl() )
901
			$protocol = 'https';
902
903
		$_locale = get_locale();
904
905
		// We have to account for w.org vs WP.com locale divergence
906
		if ( $this->in_jetpack ) {
907
			if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
908
				return false;
909
			}
910
911
			require_once JETPACK__GLOTPRESS_LOCALES_PATH;
912
913
			$gp_locale = GP_Locales::by_field( 'wp_locale', $_locale );
914
			$_locale = isset( $gp_locale->slug ) ? $gp_locale->slug : '';
915
		}
916
917
		$likes_locale = ( '' == $_locale || 'en' == $_locale ) ? '' : '&amp;lang=' . strtolower( $_locale );
918
919
		$src = sprintf(
920
			'%1$s://widgets.wp.com/likes/master.html?ver=%2$s#ver=%2$s%3$s',
921
			$protocol,
922
			$this->version,
923
			$likes_locale
924
		);
925
926
		$likersText = wp_kses( __( '<span>%d</span> bloggers like this:', 'jetpack' ), array( 'span' => array() ) );
927
		?>
928
		<iframe src='<?php echo $src; ?>' scrolling='no' id='likes-master' name='likes-master' style='display:none;'></iframe>
929
		<div id='likes-other-gravatars'><div class="likes-text"><?php echo $likersText; ?></div><ul class="wpl-avatars sd-like-gravatars"></ul></div>
930
		<?php
931
	}
932
933
	/**
934
	 * Get the 'disabled_likes' option from the DB of the current blog.
935
	 *
936
	 * @return array
937
	 */
938
	function get_options() {
939
		$setting             = array();
940
		$setting['disabled'] = get_option( 'disabled_likes'  );
941
		$sharing             = get_option( 'sharing-options' );
942
943
		// Default visibility settings
944
		if ( ! isset( $sharing['global']['show'] ) ) {
945
			$sharing['global']['show'] = array( 'post', 'page' );
946
947
		// Scalar check
948
		} elseif ( is_scalar( $sharing['global']['show'] ) ) {
949
			switch ( $sharing['global']['show'] ) {
950
				case 'posts' :
951
					$sharing['global']['show'] = array( 'post', 'page' );
952
					break;
953
				case 'index' :
954
					$sharing['global']['show'] = array( 'index' );
955
					break;
956
				case 'posts-index' :
957
					$sharing['global']['show'] = array( 'post', 'page', 'index' );
958
					break;
959
			}
960
		}
961
962
		// Ensure it's always an array (even if not previously empty or scalar)
963
		$setting['show'] = !empty( $sharing['global']['show'] ) ? (array) $sharing['global']['show'] : array();
964
965
		/**
966
		 * Filters where the Likes are displayed.
967
		 *
968
		 * @module likes
969
		 *
970
		 * @since 2.2.0
971
		 *
972
		 * @param array $setting Array of Likes display settings.
973
		 */
974
		return apply_filters( 'wpl_get_options', $setting );
975
	}
976
977
	/** _is_ functions ************************************************************/
978
979
	/**
980
	 * Are likes visible in this context?
981
	 *
982
	 * Some of this code was taken and modified from sharing_display() to ensure
983
	 * similar logic and filters apply here, too.
984
	 */
985
	function is_likes_visible() {
986
987
		global $post, $wp_current_filter;              // Used to apply 'sharing_show' filter
988
		// @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...
989
		global $wp_version;
990
		$comment_popup = false;
991
		if ( version_compare( $wp_version, '4.5-alpha', '<=' ) ) {
992
			$comment_popup = is_comments_popup();
993
		}
994
		// End 4.5 conditional block.
995
996
		// Never show on feeds or previews
997
		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...
998
			$enabled = false;
999
1000
		// Not a feed or preview, so what is it?
1001
		} else {
1002
1003
			if ( in_the_loop() ) {
1004
				// If in the loop, check if the current post is likeable
1005
				$enabled = $this->is_post_likeable();
1006
			} else {
1007
				// Otherwise, check and see if likes are enabled sitewide
1008
				$enabled = $this->is_enabled_sitewide();
1009
			}
1010
1011
			if ( post_password_required() )
1012
				$enabled = false;
1013
1014
			if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
1015
				$enabled = false;
1016
			}
1017
1018
			// Sharing Setting Overrides ****************************************
1019
1020
			// Single post including custom post types
1021
			if ( is_single() ) {
1022
				if ( ! $this->is_single_post_enabled( $post->post_type ) ) {
1023
					$enabled = false;
1024
				}
1025
1026
			// Single page
1027
			} elseif ( is_page() && ! is_front_page() ) {
1028
				if ( ! $this->is_single_page_enabled() ) {
1029
					$enabled = false;
1030
				}
1031
1032
			// Attachment
1033
			} elseif ( is_attachment() ) {
1034
				if ( ! $this->is_attachment_enabled() ) {
1035
					$enabled = false;
1036
				}
1037
1038
			// All other loops
1039
			} elseif ( ! $this->is_index_enabled() ) {
1040
				$enabled = false;
1041
			}
1042
		}
1043
1044
		if( is_object( $post ) ) {
1045
			// Check that the post is a public, published post.
1046
			if ( 'attachment' == $post->post_type ) {
1047
				$post_status = get_post_status( $post->post_parent );
1048
			} else {
1049
				$post_status = $post->post_status;
1050
			}
1051
			if ( 'publish' != $post_status ) {
1052
				$enabled = false;
1053
			}
1054
		}
1055
1056
		// Run through the sharing filters
1057
		/** This filter is documented in modules/sharedaddy/sharing-service.php */
1058
		$enabled = apply_filters( 'sharing_show', $enabled, $post );
1059
1060
		/**
1061
		 * Filters whether the Likes should be visible or not.
1062
		 * Allows overwriting the options set in Settings > Sharing.
1063
		 *
1064
		 * @module likes
1065
		 *
1066
		 * @since 2.2.0
1067
		 *
1068
		 * @param bool $enabled Should the Likes be visible?
1069
		 */
1070
		return (bool) apply_filters( 'wpl_is_likes_visible', $enabled );
1071
	}
1072
1073
	/**
1074
	 * Returns the current state of the "WordPress.com Likes are" option.
1075
	 * @return boolean true if enabled sitewide, false if not
1076
	 */
1077
	function is_enabled_sitewide() {
1078
		/**
1079
		 * Filters whether Likes are enabled by default on all posts.
1080
		 * true if enabled sitewide, false if not.
1081
		 *
1082
		 * @module likes
1083
		 *
1084
		 * @since 2.2.0
1085
		 *
1086
		 * @param bool $option Are Likes enabled sitewide.
1087
		 */
1088
		return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
1089
	}
1090
1091
	/**
1092
	 * Returns the current state of the "WordPress.com Reblogs are" option.
1093
	 * @return boolean true if enabled sitewide, false if not
1094
	 */
1095
	function reblogs_enabled_sitewide() {
1096
		/**
1097
		 * Filters whether Reblogs are enabled by default on all posts.
1098
		 * true if enabled sitewide, false if not.
1099
		 *
1100
		 * @module likes
1101
		 *
1102
		 * @since 3.0.0
1103
		 *
1104
		 * @param bool $option Are Reblogs enabled sitewide.
1105
		 */
1106
		return (bool) apply_filters( 'wpl_reblogging_enabled_sitewide', ! get_option( 'disabled_reblogs' ) );
1107
	}
1108
1109
	/**
1110
	 * Returns if comment likes are enabled. Defaults to 'off'
1111
	 * @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...
1112
	 * @return boolean true if we should show comment likes, false if not
1113
	 */
1114
	function is_comments_enabled() {
1115
		/**
1116
		 * Filters whether Comment Likes are enabled.
1117
		 * true if enabled, false if not.
1118
		 *
1119
		 * @module likes
1120
		 *
1121
		 * @since 2.2.0
1122
		 *
1123
		 * @param bool $option Are Comment Likes enabled sitewide.
1124
		 */
1125
		return (bool) apply_filters( 'jetpack_comment_likes_enabled', get_option( 'jetpack_comment_likes_enabled', false ) );
1126
	}
1127
1128
	function is_admin_bar_button_visible() {
1129
		global $wp_admin_bar;
1130
1131
		if ( ! is_object( $wp_admin_bar ) )
1132
			return false;
1133
1134
		if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) )
1135
			return false;
1136
1137
		if ( ! $this->is_likes_visible() )
1138
			return false;
1139
1140
		if ( ! $this->is_post_likeable() )
1141
			return false;
1142
1143
		/**
1144
		 * Filters whether the Like button is enabled in the admin bar.
1145
		 *
1146
		 * @module likes
1147
		 *
1148
		 * @since 2.2.0
1149
		 *
1150
		 * @param bool true Should the Like button be visible in the Admin bar. Default to true.
1151
		 */
1152
		return (bool) apply_filters( 'jetpack_admin_bar_likes_enabled', true );
1153
	}
1154
1155
	/**
1156
	 * Are likes enabled for this post?
1157
	 *
1158
	 * @param int $post_id
1159
	 * @retun bool
1160
	 */
1161
	function is_post_likeable( $post_id = 0 ) {
1162
		$post = get_post( $post_id );
1163
		if ( !$post || is_wp_error( $post ) ) {
1164
			return false;
1165
		}
1166
1167
		$sitewide_likes_enabled = (bool) Jetpack_Likes::is_enabled_sitewide();
1168
		$post_likes_switched    = (bool) get_post_meta( $post->ID, 'switch_like_status', true );
1169
1170
		$post_likes_enabled = $sitewide_likes_enabled;
1171
		if ( $post_likes_switched ) {
1172
			$post_likes_enabled = ! $post_likes_enabled;
1173
		}
1174
1175
		return $post_likes_enabled;
1176
	}
1177
1178
	/**
1179
	 * Are Post Likes enabled on archive/front/search pages?
1180
	 *
1181
	 * @return bool
1182
	 */
1183
	function is_index_enabled() {
1184
		$options = $this->get_options();
1185
		/**
1186
		 * Filters whether Likes should be enabled on archive/front/search pages.
1187
		 *
1188
		 * @module likes
1189
		 *
1190
		 * @since 2.2.0
1191
		 *
1192
		 * @param bool $enabled Are Post Likes enabled on archive/front/search pages?
1193
		 */
1194
		return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'] ) );
1195
	}
1196
1197
	/**
1198
	 * Are Post Likes enabled on single posts?
1199
	 *
1200
	 * @param String $post_type custom post type identifier
1201
	 * @return bool
1202
	 */
1203 View Code Duplication
	function is_single_post_enabled( $post_type = 'post' ) {
1204
		$options = $this->get_options();
1205
		return (bool) apply_filters(
1206
			/**
1207
			 * Filters whether Likes should be enabled on single posts.
1208
			 *
1209
			 * The dynamic part of the filter, {$post_type}, allows you to specific the post type where Likes should be enabled.
1210
			 *
1211
			 * @module likes
1212
			 *
1213
			 * @since 2.2.0
1214
			 *
1215
			 * @param bool $enabled Are Post Likes enabled on single posts?
1216
			 */
1217
			"wpl_is_single_{$post_type}_disabled",
1218
			(bool) in_array( $post_type, $options['show'] )
1219
		);
1220
	}
1221
1222
	/**
1223
	 * Are Post Likes enabled on single pages?
1224
	 *
1225
	 * @return bool
1226
	 */
1227 View Code Duplication
	function is_single_page_enabled() {
1228
		$options = $this->get_options();
1229
		/**
1230
		 * Filters whether Likes should be enabled on single pages.
1231
		 *
1232
		 * @module likes
1233
		 *
1234
		 * @since 2.2.0
1235
		 *
1236
		 * @param bool $enabled Are Post Likes enabled on single pages?
1237
		 */
1238
		return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'] ) );
1239
	}
1240
1241
	/**
1242
	 * Are Media Likes enabled on single pages?
1243
	 *
1244
	 * @return bool
1245
	 */
1246
	function is_attachment_enabled() {
1247
		$options = $this->get_options();
1248
		/**
1249
		 * Filters whether Likes should be enabled on attachment pages.
1250
		 *
1251
		 * @module likes
1252
		 *
1253
		 * @since 2.2.0
1254
		 *
1255
		 * @param bool $enabled Are Post Likes enabled on attachment pages?
1256
		 */
1257
		return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'] ) );
1258
	}
1259
}
1260
1261
Jetpack_Likes::init();
1262