Completed
Push — update/editor-blocks-icon-colo... ( 093ab2...3cfb5e )
by
unknown
08:47
created

Jetpack_Likes::load_admin_css()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

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

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
471
			esc_html__( 'Like this:', 'jetpack' )
472
		);
473
474
		$html  = "<div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper' data-src='$src' data-name='$name'>";
475
		$html .= $headline;
476
		$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>';
477
		$html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>";
478
		$html .= '</div>';
479
480
		// Let's make sure that the script is enqueued
481
		wp_enqueue_script( 'jetpack_likes_queuehandler' );
482
483
		return $content . $html;
484
	}
485
486
	function post_flair_service_enabled_like( $classes ) {
487
		$classes[] = 'sd-like-enabled';
488
		return $classes;
489
	}
490
491
	function is_admin_bar_button_visible() {
492
		global $wp_admin_bar;
493
494
		if ( ! is_object( $wp_admin_bar ) )
495
			return false;
496
497
		if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) )
498
			return false;
499
500
		if ( ! $this->settings->is_likes_visible() )
501
			return false;
502
503
		if ( ! $this->settings->is_post_likeable() )
504
			return false;
505
506
		/**
507
		 * Filters whether the Like button is enabled in the admin bar.
508
		 *
509
		 * @module likes
510
		 *
511
		 * @since 2.2.0
512
		 *
513
		 * @param bool true Should the Like button be visible in the Admin bar. Default to true.
514
		 */
515
		return (bool) apply_filters( 'jetpack_admin_bar_likes_enabled', true );
516
	}
517
518
	function admin_bar_likes() {
519
		global $wp_admin_bar;
520
521
		$post_id = get_the_ID();
522
523
		if ( ! is_numeric( $post_id ) || ! $this->is_admin_bar_button_visible() ) {
524
			return;
525
		}
526
527
		$protocol = 'http';
528
		if ( is_ssl() )
529
			$protocol = 'https';
530
531 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
532
			$blog_id = get_current_blog_id();
533
			$bloginfo = get_blog_details( (int) $blog_id );
534
			$domain = $bloginfo->domain;
535
		} else {
536
			$blog_id = Jetpack_Options::get_option( 'id' );
537
			$url = home_url();
538
			$url_parts = wp_parse_url( $url );
539
			$domain = $url_parts['host'];
540
		}
541
		// make sure to include the scripts before the iframe otherwise weird things happen
542
		add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
543
544
		$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 );
545
546
		$html = "<iframe class='admin-bar-likes-widget jetpack-likes-widget' scrolling='no' frameBorder='0' name='admin-bar-likes-widget' src='$src'></iframe>";
547
548
		$node = array(
549
				'id'   => 'admin-bar-likes-widget',
550
				'meta' => array(
551
							'html' => $html
552
				)
553
		);
554
555
		$wp_admin_bar->add_node( $node );
556
	}
557
}
558
559
/**
560
 * Callback to get the value for the jetpack_likes_enabled field.
561
 *
562
 * Warning: this behavior is somewhat complicated!
563
 * When the switch_like_status post_meta is unset, we follow the global setting in Sharing.
564
 * When it is set to 0, we disable likes on the post, regardless of the global setting.
565
 * When it is set to 1, we enable likes on the post, regardless of the global setting.
566
 */
567
function jetpack_post_likes_get_value( array $post ) {
568
	$post_likes_switched = get_post_meta( $post['id'], 'switch_like_status', true );
569
570
	/** This filter is documented in modules/jetpack-likes-settings.php */
571
	$sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
572
573
	// an empty string: post meta was not set, so go with the global setting
574
	if ( "" === $post_likes_switched ) {
575
		return $sitewide_likes_enabled;
576
	}
577
578
	// user overrode the global setting to disable likes
579
	elseif ( "0" === $post_likes_switched ) {
580
		return false;
581
	}
582
583
	// user overrode the global setting to enable likes
584
	elseif ( "1" === $post_likes_switched ) {
585
		return true;
586
	}
587
588
	// no default fallback, let's stay explicit
589
}
590
591
/**
592
 * Callback to set switch_like_status post_meta when jetpack_likes_enabled is updated.
593
 *
594
 * Warning: this behavior is somewhat complicated!
595
 * When the switch_like_status post_meta is unset, we follow the global setting in Sharing.
596
 * When it is set to 0, we disable likes on the post, regardless of the global setting.
597
 * When it is set to 1, we enable likes on the post, regardless of the global setting.
598
 */
599
function jetpack_post_likes_update_value( $enable_post_likes, $post_object ) {
600
	/** This filter is documented in modules/jetpack-likes-settings.php */
601
	$sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
602
603
	$should_switch_status = $enable_post_likes !== $sitewide_likes_enabled;
604
605
	if ( $should_switch_status ) {
606
		// set the meta to 0 if the user wants to disable likes, 1 if user wants to enable
607
		$switch_like_status = ( $enable_post_likes ? 1 : 0 );
608
		return update_post_meta( $post_object->ID, 'switch_like_status', $switch_like_status );
609
	} else {
610
		// unset the meta otherwise
611
		return delete_post_meta( $post_object->ID, 'switch_like_status' );
612
	}
613
}
614
615
/**
616
 * Add Likes post_meta to the REST API Post response.
617
 *
618
 * @action rest_api_init
619
 * @uses register_rest_field
620
 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
621
 */
622 View Code Duplication
function jetpack_post_likes_register_rest_field() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
623
	$post_types = get_post_types( array( 'public' => true ) );
624
	foreach ( $post_types as $post_type ) {
625
		register_rest_field(
626
			$post_type,
627
			'jetpack_likes_enabled',
628
			array(
629
				'get_callback'    => 'jetpack_post_likes_get_value',
630
				'update_callback' => 'jetpack_post_likes_update_value',
631
				'schema'          => array(
632
					'description' => __( 'Are Likes enabled?', 'jetpack' ),
633
					'type'        => 'boolean',
634
				),
635
			)
636
		);
637
638
		/**
639
		 * Ensures all public internal post-types support `likes`
640
		 * This feature support flag is used by the REST API and Gutenberg.
641
		 */
642
		add_post_type_support( $post_type, 'jetpack-post-likes' );
643
	}
644
}
645
646
// Add Likes post_meta to the REST API Post response.
647
add_action( 'rest_api_init', 'jetpack_post_likes_register_rest_field' );
648
649
// Some CPTs (e.g. Jetpack portfolios and testimonials) get registered with
650
// restapi_theme_init because they depend on theme support, so let's also hook to that
651
add_action( 'restapi_theme_init', 'jetpack_post_likes_register_rest_field', 20 );
652
653
/**
654
 * Set the Likes and Sharing Gutenberg extension availability
655
 */
656
function jetpack_post_likes_set_extension_availability() {
657
	Jetpack_Gutenberg::set_extension_available( 'likes' );
658
}
659
660
add_action( 'jetpack_register_gutenberg_extensions', 'jetpack_post_likes_set_extension_availability' );
661
662
Jetpack_Likes::init();
663