Completed
Push — branch-4.2 ( f12916...1c7f7d )
by Jeremy
11:38
created

Jetpack_Likes::updated_message()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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