Completed
Push — feature/videopress-uploader ( c50b88...58b703 )
by
unknown
41:05 queued 31:04
created

Jetpack_Likes::sharing_block()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

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