Completed
Push — fix/gulp-env ( ec4107...cf0b47 )
by
unknown
133:51 queued 124:05
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 0
Metric Value
wmc 192
lcom 1
cbo 3
dl 132
loc 1222
rs 4.4102
c 0
b 0
f 0

51 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 9 2
B __construct() 3 54 9
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
A add_likes_to_sharing_meta_box_title() 0 3 1
B add_meta_box() 0 31 3
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
A add_like_count_column() 0 9 1
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
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
A load_styles_register_scripts() 0 6 2
C meta_box_save() 5 34 14
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
B post_likes() 10 39 4
B comment_likes() 10 33 6
C is_admin_bar_button_visible() 0 26 7

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