Completed
Push — branch-4.2 ( d657d9...0ad304 )
by Jeremy
25:42 queued 16:26
created

Jetpack_Likes   D

Complexity

Total Complexity 192

Size/Duplication

Total Lines 1222
Duplicated Lines 10.8 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 132
loc 1222
rs 4.4102
c 1
b 1
f 0
wmc 192
lcom 1
cbo 3

51 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 9 2
A set_social_notifications_like() 0 3 1
A delete_social_notifications_like() 0 3 1
A configuration_redirect() 0 4 1
A load_jp_css() 0 4 1
A configuration_target_area() 0 4 1
B __construct() 3 54 9
A load_styles_register_scripts() 0 6 2
A add_likes_to_sharing_meta_box_title() 0 3 1
B add_meta_box() 0 31 3
C meta_box_save() 5 34 14
B meta_box_content() 0 39 6
A sharing_meta_box_content() 0 11 2
A admin_discussion_likes_settings_init() 7 7 1
A admin_discussion_likes_settings_section() 0 20 1
A admin_likes_get_option() 0 9 3
A admin_discussion_likes_settings_field() 0 6 1
A admin_discussion_likes_settings_validate() 0 8 3
A admin_settings_init() 0 56 2
B admin_settings_showbuttonon_init() 10 28 4
C admin_settings_showbuttonon_callback() 16 33 8
B process_update_requests_if_sharedaddy_not_loaded() 0 10 6
F admin_settings_callback() 30 55 18
A enable_comment_likes() 0 5 1
A sharing_menu() 0 3 1
A sharing_page() 0 12 1
A updated_message() 0 4 3
A sharing_block() 0 19 1
A admin_init() 0 8 1
C action_init() 0 31 13
A register_scripts() 0 6 1
B load_admin_css() 0 39 1
B enqueue_admin_scripts() 0 12 5
A likes_edit_column() 0 16 4
A add_like_count_column() 0 9 1
B post_likes() 10 39 4
B comment_likes() 10 33 6
A post_flair_service_enabled_like() 0 4 1
B admin_bar_likes() 10 37 5
C likes_master() 0 34 8
C get_options() 0 38 7
F is_likes_visible() 0 87 19
A is_enabled_sitewide() 0 13 1
A reblogs_enabled_sitewide() 0 13 1
A is_comments_enabled() 0 13 1
C is_admin_bar_button_visible() 0 26 7
A is_post_likeable() 0 16 4
A is_index_enabled() 0 13 1
A is_single_post_enabled() 18 18 1
A is_single_page_enabled() 13 13 1
A is_attachment_enabled() 0 13 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Likes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Likes, and based on these observations, apply Extract Interface, too.

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