Completed
Push — fix/typo-pro-promo ( e5832e...fc698e )
by
unknown
29:20
created

Jetpack_Likes::action_init()   C

Complexity

Conditions 13
Paths 4

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 19
nc 4
nop 0
dl 0
loc 27
rs 5.1234
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Module Name: Likes
4
 * Module Description: Give visitors an easy way to show they appreciate your content.
5
 * Jumpstart Description: Give visitors an easy way to show they appreciate your content.
6
 * First Introduced: 2.2
7
 * Sort Order: 23
8
 * Requires Connection: Yes
9
 * Auto Activate: No
10
 * Module Tags: Social
11
 * Feature: Engagement, Jumpstart
12
 * Additional Search Queries: like, likes, wordpress.com
13
 */
14
15
Jetpack::dns_prefetch( array(
16
	'//widgets.wp.com',
17
	'//s0.wp.com',
18
	'//0.gravatar.com',
19
	'//1.gravatar.com',
20
	'//2.gravatar.com',
21
) );
22
23
include_once dirname( __FILE__ ) . '/likes/jetpack-likes-master-iframe.php';
24
include_once dirname( __FILE__ ) . '/likes/jetpack-likes-settings.php';
25
26
class Jetpack_Likes {
27
	public static function init() {
28
		static $instance = NULL;
29
30
		if ( ! $instance ) {
31
			$instance = new Jetpack_Likes;
32
		}
33
34
		return $instance;
35
	}
36
37
	function __construct() {
38
		$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...
39
		$this->settings = new Jetpack_Likes_Settings();
0 ignored issues
show
Bug introduced by
The property settings 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...
40
41
		add_action( 'init', array( &$this, 'action_init' ) );
42
		add_action( 'admin_init', array( $this, 'admin_init' ) );
43
44
		if ( $this->in_jetpack ) {
45
			add_action( 'jetpack_activate_module_likes',   array( $this, 'set_social_notifications_like' ) );
46
			add_action( 'jetpack_deactivate_module_likes', array( $this, 'delete_social_notifications_like' ) );
47
48
			Jetpack::enable_module_configurable( __FILE__ );
49
			Jetpack::module_configuration_load( __FILE__, array( $this, 'configuration_redirect' ) );
50
51
			add_action( 'admin_print_scripts-settings_page_sharing', array( &$this, 'load_jp_css' ) );
52
			add_filter( 'sharing_show_buttons_on_row_start', array( $this, 'configuration_target_area' ) );
53
54
			$active = Jetpack::get_active_modules();
55
56 View Code Duplication
			if ( ! in_array( 'sharedaddy', $active ) && ! in_array( 'publicize', $active ) ) {
57
				// we don't have a sharing page yet
58
				add_action( 'admin_menu', array( $this->settings, 'sharing_menu' ) );
59
			}
60
61 View Code Duplication
			if ( in_array( 'publicize', $active ) && ! in_array( 'sharedaddy', $active ) ) {
62
				// we have a sharing page but not the global options area
63
				add_action( 'pre_admin_screen_sharing', array( $this->settings, 'sharing_block' ), 20 );
64
				add_action( 'pre_admin_screen_sharing', array( $this->settings, 'updated_message' ), -10 );
65
			}
66
67 View Code Duplication
			if( ! in_array( 'sharedaddy', $active ) ) {
68
				add_action( 'admin_init', array( $this->settings, 'process_update_requests_if_sharedaddy_not_loaded' ) );
69
				add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_showbuttonon_init' ), 19 );
70
				add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_showbuttonon_callback' ), 19 );
71
				add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) );
72
			} else {
73
				add_filter( 'sharing_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) );
74
				add_action( 'start_sharing_meta_box_content', array( $this->settings, 'meta_box_content' ) );
75
			}
76
		} else { // wpcom
77
			add_action( 'wpmu_new_blog', array( $this, 'enable_comment_likes' ), 10, 1 );
78
			add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) );
79
			add_action( 'end_likes_meta_box_content', array( $this->settings, 'sharing_meta_box_content' ) );
80
			add_filter( 'likes_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) );
81
		}
82
83
		add_action( 'admin_init', array( $this, 'admin_discussion_likes_settings_init' ) ); // Likes notifications
84
85
		add_action( 'admin_bar_menu', array( $this, 'admin_bar_likes' ), 60 );
86
87
		add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) );
88
89
		add_action( 'save_post', array( $this->settings, 'meta_box_save' ) );
90
		add_action( 'edit_attachment', array( $this->settings, 'meta_box_save' ) );
91
		add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_init' ), 20 );
92
		add_action( 'sharing_admin_update',   array( $this->settings, 'admin_settings_callback' ), 20 );
93
	}
94
95
	/**
96
	 * Set the social_notifications_like option to `on` when the Likes module is activated.
97
	 *
98
	 * @since 3.7.0
99
	 *
100
	 * @return null
101
	 */
102
	function set_social_notifications_like() {
103
		update_option( 'social_notifications_like', 'on' );
104
	}
105
106
	/**
107
	 * Delete the social_notifications_like option that was set to `on` on module activation.
108
	 *
109
	 * @since 3.7.0
110
	 *
111
	 * @return null
112
	 */
113
	function delete_social_notifications_like() {
114
		delete_option( 'social_notifications_like' );
115
	}
116
117
	/**
118
	 * Redirects to the likes section of the sharing page.
119
	 */
120
	function configuration_redirect() {
121
		wp_safe_redirect( admin_url( 'options-general.php?page=sharing#likes' ) );
122
		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...
123
	}
124
125
	/**
126
	 * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable
127
	 */
128
	function load_jp_css() {
129
		// Do we really need `admin_styles`? With the new admin UI, it's breaking some bits.
130
		// Jetpack::init()->admin_styles();
131
	}
132
133
	/**
134
	 * Load scripts and styles for front end.
135
	 * @return null
136
	 */
137
	function load_styles_register_scripts() {
138
		if ( $this->in_jetpack ) {
139
			wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION );
140
			$this->register_scripts();
141
		}
142
	}
143
144
	/**
145
	 * Stub for is_likes_visible, since some themes were calling it directly from this class
146
	 *
147
	 * @deprecated 5.4
148
	 * @return bool
149
	 */
150
	function is_likes_visible() {
151
		_deprecated_function( __METHOD__, 'jetpack-5.4', 'Jetpack_Likes_Settings()->is_likes_visible' );
152
153
		$settings = new Jetpack_Likes_Settings();
154
		return $settings->is_likes_visible();
155
	}
156
157
	/**
158
	 * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box
159
	 * @param  string $html row heading for the sharedaddy "which page" setting
160
	 * @return string       html with the jetpack-targetable class and likes id. tbody gets closed after the like settings
161
	 */
162
	function configuration_target_area( $html = '' ) {
163
		$html = "<tbody id='likes' class='jetpack-targetable'>" . $html;
164
		return $html;
165
	}
166
167
	/**
168
	 * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog)
169
	 */
170
	function sharing_meta_box_content( $post ) {
171
		$post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
172
		$disabled = get_post_meta( $post_id, 'sharing_disabled', true ); ?>
173
		<p>
174
			<label for="wpl_enable_post_sharing">
175
				<input type="checkbox" name="wpl_enable_post_sharing" id="wpl_enable_post_sharing" value="1" <?php checked( !$disabled ); ?>>
176
				<?php _e( 'Show sharing buttons.', 'jetpack' ); ?>
177
			</label>
178
			<input type="hidden" name="wpl_sharing_status_hidden" value="1" />
179
		</p> <?php
180
	}
181
182
	/**
183
	  * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page)
184
	  */
185
186 View Code Duplication
	function admin_discussion_likes_settings_init() {
187
		// Add a temporary section, until we can move the setting out of there and with the rest of the email notification settings
188
		add_settings_section( 'likes-notifications', __( 'Likes Notifications', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_section' ), 'discussion' );
189
		add_settings_field( 'social-notifications', __( 'Email me whenever', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_field' ), 'discussion', 'likes-notifications' );
190
		// Register the setting
191
		register_setting( 'discussion', 'social_notifications_like', array( $this, 'admin_discussion_likes_settings_validate' ) );
192
	}
193
194
	function admin_discussion_likes_settings_section() {
195
		// Atypical usage here.  We emit jquery to move likes notification checkbox to be with the rest of the email notification settings
196
?>
197
	<script type="text/javascript">
198
	jQuery( function( $ )  {
199
		var table = $( '#social_notifications_like' ).parents( 'table:first' ),
200
			header = table.prevAll( 'h2:first' ),
201
			newParent = $( '#moderation_notify' ).parent( 'label' ).parent();
202
203
		if ( !table.length || !header.length || !newParent.length ) {
204
			return;
205
		}
206
207
		newParent.append( '<br/>' ).append( table.end().parent( 'label' ).siblings().andSelf() );
208
		header.remove();
209
		table.remove();
210
	} );
211
	</script>
212
<?php
213
	}
214
215
	function admin_likes_get_option( $option ) {
216
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
217
			$option_setting = get_blog_option( get_current_blog_id(), $option, 'on' );
218
		} else {
219
			$option_setting = get_option( $option, 'on' );
220
		}
221
222
		return intval( 'on' == $option_setting );
223
	}
224
225
	function admin_discussion_likes_settings_field() {
226
		$like = $this->admin_likes_get_option( 'social_notifications_like' );
227
?>
228
		<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>
229
<?php
230
	}
231
232
	function admin_discussion_likes_settings_validate( $input ) {
233
		// If it's not set (was unchecked during form submission) or was set to off (during option update), return 'off'.
234
		if ( !$input || 'off' == $input )
235
			return 'off';
236
237
		// Otherwise, return 'on'.
238
		return 'on';
239
	}
240
241
	function admin_init() {
242
		add_filter( 'manage_posts_columns', array( $this, 'add_like_count_column' ) );
243
		add_filter( 'manage_pages_columns', array( $this, 'add_like_count_column' ) );
244
		add_action( 'manage_posts_custom_column', array( $this, 'likes_edit_column' ), 10, 2 );
245
		add_action( 'manage_pages_custom_column', array( $this, 'likes_edit_column' ), 10, 2 );
246
		add_action( 'admin_print_styles-edit.php', array( $this, 'load_admin_css' ) );
247
		add_action( "admin_print_scripts-edit.php", array( $this, 'enqueue_admin_scripts' ) );
248
	}
249
250
	function action_init() {
251
		if ( is_admin() ) {
252
			return;
253
		}
254
255
		if ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ||
256
			 ( defined( 'APP_REQUEST' ) && APP_REQUEST ) ||
257
			 ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
258
			 ( defined( 'COOKIE_AUTH_REQUEST' ) && COOKIE_AUTH_REQUEST ) ||
259
			 ( defined( 'JABBER_SERVER' ) && JABBER_SERVER ) ) {
260
			return;
261
		}
262
263
		if ( $this->in_jetpack ) {
264
			add_filter( 'the_content', array( &$this, 'post_likes' ), 30, 1 );
265
			add_filter( 'the_excerpt', array( &$this, 'post_likes' ), 30, 1 );
266
267
		} else {
268
			add_filter( 'post_flair', array( &$this, 'post_likes' ), 30, 1 );
269
			add_filter( 'post_flair_block_css', array( $this, 'post_flair_service_enabled_like' ) );
270
271
			wp_enqueue_script( 'postmessage', '/wp-content/js/postmessage.js', array( 'jquery' ), JETPACK__VERSION, false );
272
			wp_enqueue_script( 'jetpack_resize', '/wp-content/js/jquery/jquery.jetpack-resize.js', array( 'jquery' ), JETPACK__VERSION, false );
273
			wp_enqueue_script( 'jetpack_likes_queuehandler', plugins_url( 'queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize' ), JETPACK__VERSION, true );
274
			wp_enqueue_style( 'jetpack_likes', plugins_url( 'jetpack-likes.css', __FILE__ ), array(), JETPACK__VERSION );
275
		}
276
	}
277
278
	/**
279
	* Register scripts
280
	*/
281
	function register_scripts() {
282
		wp_register_script(
283
			'postmessage',
284
			Jetpack::get_file_url_for_environment( '_inc/build/postmessage.min.js', '_inc/postmessage.js' ),
285
			array( 'jquery' ),
286
			JETPACK__VERSION,
287
			false
288
		);
289
		wp_register_script(
290
			'jetpack_resize',
291
			Jetpack::get_file_url_for_environment(
292
				'_inc/build/jquery.jetpack-resize.min.js',
293
				'_inc/jquery.jetpack-resize.js'
294
			),
295
			array( 'jquery' ),
296
			JETPACK__VERSION,
297
			false
298
		);
299
		wp_register_script(
300
			'jetpack_likes_queuehandler',
301
			Jetpack::get_file_url_for_environment(
302
				'_inc/build/likes/queuehandler.min.js',
303
				'modules/likes/queuehandler.js'
304
			),
305
			array( 'jquery', 'postmessage', 'jetpack_resize' ),
306
			JETPACK__VERSION,
307
			true
308
		);
309
	}
310
311
	/**
312
	* Load the CSS needed for the wp-admin area.
313
	*/
314
	function load_admin_css() {
315
	?>
316
		<style type="text/css">
317
			.vers img { display: none; }
318
			.metabox-prefs .vers img { display: inline; }
319
			.fixed .column-likes { width: 5.5em; padding: 8px 0; text-align: left; }
320
			.fixed .column-stats { width: 5em; }
321
			.fixed .column-likes .post-com-count {
322
				-webkit-box-sizing: border-box;
323
				-moz-box-sizing: border-box;
324
				box-sizing: border-box;
325
				display: inline-block;
326
				padding: 0 8px;
327
				height: 2em;
328
				margin-top: 5px;
329
				-webkit-border-radius: 5px;
330
				border-radius: 5px;
331
				background-color: #72777C;
332
				color: #FFF;
333
				font-size: 11px;
334
				line-height: 21px;
335
			}
336
			.fixed .column-likes .post-com-count::after { border: none !important; }
337
			.fixed .column-likes .post-com-count:hover { background-color: #0073AA; }
338
			.fixed .column-likes .vers:before {
339
				font: normal 20px/1 dashicons;
340
				content: '\f155';
341
				speak: none;
342
				-webkit-font-smoothing: antialiased;
343
				-moz-osx-font-smoothing: grayscale;
344
			}
345
			@media screen and (max-width: 782px) {
346
				.fixed .column-likes {
347
					display: none;
348
				}
349
			}
350
		</style>
351
		<?php
352
	}
353
354
	/**
355
	* Load the JS required for loading the like counts.
356
	*/
357
	function enqueue_admin_scripts() {
358
		if ( empty( $_GET['post_type'] ) || 'post' == $_GET['post_type'] || 'page' == $_GET['post_type'] ) {
359
			if ( $this->in_jetpack ) {
360
				wp_enqueue_script(
361
					'likes-post-count',
362
					Jetpack::get_file_url_for_environment(
363
						'_inc/build/likes/post-count.min.js',
364
						'modules/likes/post-count.js'
365
					),
366
					array( 'jquery' ),
367
					JETPACK__VERSION
368
				);
369
				wp_enqueue_script(
370
					'likes-post-count-jetpack',
371
					Jetpack::get_file_url_for_environment(
372
						'_inc/build/likes/post-count-jetpack.min.js',
373
						'modules/likes/post-count-jetpack.js'
374
					),
375
					array( 'likes-post-count' ),
376
					JETPACK__VERSION
377
				);
378
			} else {
379
				wp_enqueue_script( 'jquery.wpcom-proxy-request', "/wp-content/js/jquery/jquery.wpcom-proxy-request.js", array('jquery'), NULL, true );
380
				wp_enqueue_script( 'likes-post-count', plugins_url( 'likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION );
381
				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 );
382
			}
383
		}
384
	}
385
386
	/**
387
	* Add "Likes" column data to the post edit table in wp-admin.
388
	*
389
	* @param string $column_name
390
	* @param int $post_id
391
	*/
392
	function likes_edit_column( $column_name, $post_id ) {
393
		if ( 'likes' == $column_name ) {
394
395
			if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
396
				$blog_id = get_current_blog_id();
397
			} else {
398
				$blog_id = Jetpack_Options::get_option( 'id' );
399
			}
400
401
			$permalink = get_permalink( get_the_ID() ); ?>
402
			<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; ?>">
403
				<span class="comment-count">0</span>
404
			</a>
405
			<?php
406
		}
407
	}
408
409
	/**
410
	* Add a "Likes" column header to the post edit table in wp-admin.
411
	*
412
	* @param array $columns
413
	* @return array
414
	*/
415
	function add_like_count_column( $columns ) {
416
		$date = $columns['date'];
417
		unset( $columns['date'] );
418
419
		$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>';
420
		$columns['date'] = $date;
421
422
		return $columns;
423
	}
424
425
	function post_likes( $content ) {
426
		$post_id = get_the_ID();
427
428
		if ( ! is_numeric( $post_id ) || ! $this->settings->is_likes_visible() )
429
			return $content;
430
431 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
432
			$blog_id = get_current_blog_id();
433
			$bloginfo = get_blog_details( (int) $blog_id );
434
			$domain = $bloginfo->domain;
435
		} else {
436
			$blog_id = Jetpack_Options::get_option( 'id' );
437
			$url = home_url();
438
			$url_parts = parse_url( $url );
439
			$domain = $url_parts['host'];
440
		}
441
		// make sure to include the scripts before the iframe otherwise weird things happen
442
		add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
443
444
		/**
445
		* if the same post appears more then once on a page the page goes crazy
446
		* we need a slightly more unique id / name for the widget wrapper.
447
		*/
448
		$uniqid = uniqid();
449
450
		$src = sprintf( 'https://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 );
451
		$name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid );
452
		$wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid );
453
		$headline = sprintf(
454
			/** This filter is already documented in modules/sharedaddy/sharing-service.php */
455
			apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', esc_html__( 'Like this:', 'jetpack' ), 'likes' ),
456
			esc_html__( 'Like this:', 'jetpack' )
457
		);
458
459
		$html  = "<div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper' data-src='$src' data-name='$name'>";
460
		$html .= $headline;
461
		$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>';
462
		$html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>";
463
		$html .= '</div>';
464
465
		// Let's make sure that the script is enqueued
466
		wp_enqueue_script( 'jetpack_likes_queuehandler' );
467
468
		return $content . $html;
469
	}
470
471
	function post_flair_service_enabled_like( $classes ) {
472
		$classes[] = 'sd-like-enabled';
473
		return $classes;
474
	}
475
476
	function is_admin_bar_button_visible() {
477
		global $wp_admin_bar;
478
479
		if ( ! is_object( $wp_admin_bar ) )
480
			return false;
481
482
		if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) )
483
			return false;
484
485
		if ( ! $this->settings->is_likes_visible() )
486
			return false;
487
488
		if ( ! $this->settings->is_post_likeable() )
489
			return false;
490
491
		/**
492
		 * Filters whether the Like button is enabled in the admin bar.
493
		 *
494
		 * @module likes
495
		 *
496
		 * @since 2.2.0
497
		 *
498
		 * @param bool true Should the Like button be visible in the Admin bar. Default to true.
499
		 */
500
		return (bool) apply_filters( 'jetpack_admin_bar_likes_enabled', true );
501
	}
502
503
	function admin_bar_likes() {
504
		global $wp_admin_bar;
505
506
		$post_id = get_the_ID();
507
508
		if ( ! is_numeric( $post_id ) || ! $this->is_admin_bar_button_visible() ) {
509
			return;
510
		}
511
512
		$protocol = 'http';
513
		if ( is_ssl() )
514
			$protocol = 'https';
515
516 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
517
			$blog_id = get_current_blog_id();
518
			$bloginfo = get_blog_details( (int) $blog_id );
519
			$domain = $bloginfo->domain;
520
		} else {
521
			$blog_id = Jetpack_Options::get_option( 'id' );
522
			$url = home_url();
523
			$url_parts = parse_url( $url );
524
			$domain = $url_parts['host'];
525
		}
526
		// make sure to include the scripts before the iframe otherwise weird things happen
527
		add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
528
529
		$src = sprintf( 'https://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 );
530
531
		$html = "<iframe class='admin-bar-likes-widget jetpack-likes-widget' scrolling='no' frameBorder='0' name='admin-bar-likes-widget' src='$src'></iframe>";
532
533
		$node = array(
534
				'id'   => 'admin-bar-likes-widget',
535
				'meta' => array(
536
							'html' => $html
537
				)
538
		);
539
540
		$wp_admin_bar->add_node( $node );
541
	}
542
}
543
544
Jetpack_Likes::init();
545