Completed
Push — fix/jetpack-state-messages ( 62bcb3 )
by
unknown
27:29 queued 18:26
created

Jetpack_Likes   D

Complexity

Total Complexity 193

Size/Duplication

Total Lines 1249
Duplicated Lines 10.57 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 132
loc 1249
rs 4.4102
c 0
b 0
f 0
wmc 193
lcom 1
cbo 3

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 load_styles_register_scripts() 0 7 2
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
C meta_box_save() 5 35 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 30 13
A register_scripts() 0 7 1
A load_admin_css() 0 63 2
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 they appreciate your content.
5
 * First Introduced: 2.2
6
 * Sort Order: 23
7
 * Requires Connection: Yes
8
 * Auto Activate: No
9
 * Module Tags: Social
10
 * Feature: Engagement
11
 * Additional Search Queries: like, likes, wordpress.com
12
 */
13
14
Jetpack::dns_prefetch( array(
15
	'//widgets.wp.com',
16
	'//s0.wp.com',
17
	'//0.gravatar.com',
18
	'//1.gravatar.com',
19
	'//2.gravatar.com',
20
) );
21
22
class Jetpack_Likes {
23
	public $version = '20151215';
24
25
	public static function init() {
26
		static $instance = NULL;
27
28
		if ( ! $instance ) {
29
			$instance = new Jetpack_Likes;
30
		}
31
32
		return $instance;
33
	}
34
35
	function __construct() {
36
		$this->in_jetpack = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? false : true;
0 ignored issues
show
Bug introduced by
The property in_jetpack does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
38
		add_action( 'init', array( &$this, 'action_init' ) );
39
		add_action( 'admin_init', array( $this, 'admin_init' ) );
40
41
		if ( $this->in_jetpack ) {
42
			add_action( 'jetpack_activate_module_likes',   array( $this, '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
	 * Load style on the front end.
129
	 * @return null
130
	 */
131
	function load_styles_register_scripts() {
132
133
		wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION );
134
		if( $this->in_jetpack ) {
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;
146
		return $html;
147
	}
148
149
	/**
150
	 * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares"
151
	 * @param string $title The current title of the metabox, not needed/used.
152
	 */
153
	function add_likes_to_sharing_meta_box_title( $title ) {
0 ignored issues
show
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
1123
	 * @return boolean true if we should show comment likes, false if not
1124
	 */
1125
	function is_comments_enabled() {
1126
		/**
1127
		 * Filters whether Comment Likes are enabled.
1128
		 * true if enabled, false if not.
1129
		 *
1130
		 * @module likes
1131
		 *
1132
		 * @since 2.2.0
1133
		 *
1134
		 * @param bool $option Are Comment Likes enabled sitewide.
1135
		 */
1136
		return (bool) apply_filters( 'jetpack_comment_likes_enabled', get_option( 'jetpack_comment_likes_enabled', false ) );
1137
	}
1138
1139
	function is_admin_bar_button_visible() {
1140
		global $wp_admin_bar;
1141
1142
		if ( ! is_object( $wp_admin_bar ) )
1143
			return false;
1144
1145
		if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) )
1146
			return false;
1147
1148
		if ( ! $this->is_likes_visible() )
1149
			return false;
1150
1151
		if ( ! $this->is_post_likeable() )
1152
			return false;
1153
1154
		/**
1155
		 * Filters whether the Like button is enabled in the admin bar.
1156
		 *
1157
		 * @module likes
1158
		 *
1159
		 * @since 2.2.0
1160
		 *
1161
		 * @param bool true Should the Like button be visible in the Admin bar. Default to true.
1162
		 */
1163
		return (bool) apply_filters( 'jetpack_admin_bar_likes_enabled', true );
1164
	}
1165
1166
	/**
1167
	 * Are likes enabled for this post?
1168
	 *
1169
	 * @param int $post_id
1170
	 * @retun bool
1171
	 */
1172
	function is_post_likeable( $post_id = 0 ) {
1173
		$post = get_post( $post_id );
1174
		if ( !$post || is_wp_error( $post ) ) {
1175
			return false;
1176
		}
1177
1178
		$sitewide_likes_enabled = (bool) Jetpack_Likes::is_enabled_sitewide();
1179
		$post_likes_switched    = (bool) get_post_meta( $post->ID, 'switch_like_status', true );
1180
1181
		$post_likes_enabled = $sitewide_likes_enabled;
1182
		if ( $post_likes_switched ) {
1183
			$post_likes_enabled = ! $post_likes_enabled;
1184
		}
1185
1186
		return $post_likes_enabled;
1187
	}
1188
1189
	/**
1190
	 * Are Post Likes enabled on archive/front/search pages?
1191
	 *
1192
	 * @return bool
1193
	 */
1194
	function is_index_enabled() {
1195
		$options = $this->get_options();
1196
		/**
1197
		 * Filters whether Likes should be enabled on archive/front/search pages.
1198
		 *
1199
		 * @module likes
1200
		 *
1201
		 * @since 2.2.0
1202
		 *
1203
		 * @param bool $enabled Are Post Likes enabled on archive/front/search pages?
1204
		 */
1205
		return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'] ) );
1206
	}
1207
1208
	/**
1209
	 * Are Post Likes enabled on single posts?
1210
	 *
1211
	 * @param String $post_type custom post type identifier
1212
	 * @return bool
1213
	 */
1214 View Code Duplication
	function is_single_post_enabled( $post_type = 'post' ) {
1215
		$options = $this->get_options();
1216
		return (bool) apply_filters(
1217
			/**
1218
			 * Filters whether Likes should be enabled on single posts.
1219
			 *
1220
			 * The dynamic part of the filter, {$post_type}, allows you to specific the post type where Likes should be enabled.
1221
			 *
1222
			 * @module likes
1223
			 *
1224
			 * @since 2.2.0
1225
			 *
1226
			 * @param bool $enabled Are Post Likes enabled on single posts?
1227
			 */
1228
			"wpl_is_single_{$post_type}_disabled",
1229
			(bool) in_array( $post_type, $options['show'] )
1230
		);
1231
	}
1232
1233
	/**
1234
	 * Are Post Likes enabled on single pages?
1235
	 *
1236
	 * @return bool
1237
	 */
1238 View Code Duplication
	function is_single_page_enabled() {
1239
		$options = $this->get_options();
1240
		/**
1241
		 * Filters whether Likes should be enabled on single pages.
1242
		 *
1243
		 * @module likes
1244
		 *
1245
		 * @since 2.2.0
1246
		 *
1247
		 * @param bool $enabled Are Post Likes enabled on single pages?
1248
		 */
1249
		return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'] ) );
1250
	}
1251
1252
	/**
1253
	 * Are Media Likes enabled on single pages?
1254
	 *
1255
	 * @return bool
1256
	 */
1257
	function is_attachment_enabled() {
1258
		$options = $this->get_options();
1259
		/**
1260
		 * Filters whether Likes should be enabled on attachment pages.
1261
		 *
1262
		 * @module likes
1263
		 *
1264
		 * @since 2.2.0
1265
		 *
1266
		 * @param bool $enabled Are Post Likes enabled on attachment pages?
1267
		 */
1268
		return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'] ) );
1269
	}
1270
}
1271
1272
Jetpack_Likes::init();
1273