Completed
Push — add/sync-action ( 450f6e...e6bb90 )
by
unknown
10:28
created

Jetpack_Likes   D

Complexity

Total Complexity 196

Size/Duplication

Total Lines 1263
Duplicated Lines 10.45 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

53 Methods

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

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

class MyClass { }

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

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

class MyClass {
    public $foo;
}

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