PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.5.2
Jetpack – WP Security, Backup, Speed, & Growth v11.5.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / likes.php

likes.php in Jetpack – WP Security, Backup, Speed, & Growth 11.5.2, at modules/likes.php

669 lines 23.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
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 * @package automattic/jetpack
14 */
15
16 use Automattic\Jetpack\Assets;
17
18 Assets::add_resource_hint(
19 array(
20 '//widgets.wp.com',
21 '//s0.wp.com',
22 '//0.gravatar.com',
23 '//1.gravatar.com',
24 '//2.gravatar.com',
25 ),
26 'dns-prefetch'
27 );
28
29 require_once __DIR__ . '/likes/jetpack-likes-master-iframe.php';
30 require_once __DIR__ . '/likes/jetpack-likes-settings.php';
31
32 /**
33 * Jetpack Like Class
34 */
35 class Jetpack_Likes {
36
37 /**
38 * Initialize class
39 */
40 public static function init() {
41 static $instance = null;
42
43 if ( ! $instance ) {
44 $instance = new Jetpack_Likes();
45 }
46
47 return $instance;
48 }
49
50 /**
51 * Constructs Likes class
52 */
53 public function __construct() {
54 $this->in_jetpack = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? false : true;
55 $this->settings = new Jetpack_Likes_Settings();
56
57 // We need to run on wp hook rather than init because we check is_amp_endpoint()
58 // when bootstrapping hooks.
59 add_action( 'wp', array( $this, 'action_init' ), 99 );
60
61 add_action( 'admin_init', array( $this, 'admin_init' ) );
62
63 if ( $this->in_jetpack ) {
64 add_action( 'jetpack_activate_module_likes', array( $this, 'set_social_notifications_like' ) );
65 add_action( 'jetpack_deactivate_module_likes', array( $this, 'delete_social_notifications_like' ) );
66
67 Jetpack::enable_module_configurable( __FILE__ );
68 add_filter( 'jetpack_module_configuration_url_likes', array( $this, 'jetpack_likes_configuration_url' ) );
69 add_action( 'admin_print_scripts-settings_page_sharing', array( $this, 'load_jp_css' ) );
70 add_filter( 'sharing_show_buttons_on_row_start', array( $this, 'configuration_target_area' ) );
71
72 $active = Jetpack::get_active_modules();
73
74 if ( ! in_array( 'sharedaddy', $active, true ) && ! in_array( 'publicize', $active, true ) ) {
75 // we don't have a sharing page yet.
76 add_action( 'admin_menu', array( $this->settings, 'sharing_menu' ) );
77 }
78
79 if ( in_array( 'publicize', $active, true ) && ! in_array( 'sharedaddy', $active, true ) ) {
80 // we have a sharing page but not the global options area.
81 add_action( 'pre_admin_screen_sharing', array( $this->settings, 'sharing_block' ), 20 );
82 add_action( 'pre_admin_screen_sharing', array( $this->settings, 'updated_message' ), -10 );
83 }
84
85 if ( ! in_array( 'sharedaddy', $active, true ) ) {
86 add_action( 'admin_init', array( $this->settings, 'process_update_requests_if_sharedaddy_not_loaded' ) );
87 add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_showbuttonon_init' ), 19 );
88 add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_showbuttonon_callback' ), 19 );
89 add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) );
90 } else {
91 add_filter( 'sharing_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) );
92 add_action( 'start_sharing_meta_box_content', array( $this->settings, 'meta_box_content' ) );
93 }
94 } else { // wpcom.
95 add_action( 'wpmu_new_blog', array( $this, 'enable_comment_likes' ), 10, 1 );
96 add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) );
97 add_action( 'end_likes_meta_box_content', array( $this->settings, 'sharing_meta_box_content' ) );
98 add_filter( 'likes_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) );
99 }
100
101 add_action( 'admin_init', array( $this, 'admin_discussion_likes_settings_init' ) ); // Likes notifications.
102
103 add_action( 'admin_bar_menu', array( $this, 'admin_bar_likes' ), 60 );
104
105 add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) );
106
107 add_action( 'save_post', array( $this->settings, 'meta_box_save' ) );
108 add_action( 'edit_attachment', array( $this->settings, 'meta_box_save' ) );
109 add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_init' ), 20 );
110 add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_callback' ), 20 );
111 }
112
113 /**
114 * Set the social_notifications_like option to `on` when the Likes module is activated.
115 *
116 * @since 3.7.0
117 */
118 public function set_social_notifications_like() {
119 update_option( 'social_notifications_like', 'on' );
120 }
121
122 /**
123 * Delete the social_notifications_like option that was set to `on` on module activation.
124 *
125 * @since 3.7.0
126 */
127 public function delete_social_notifications_like() {
128 delete_option( 'social_notifications_like' );
129 }
130
131 /**
132 * Overrides default configuration url
133 *
134 * @uses admin_url
135 * @return string module settings URL
136 */
137 public function jetpack_likes_configuration_url() {
138 return admin_url( 'options-general.php?page=sharing#likes' );
139 }
140
141 /**
142 * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable
143 */
144 public function load_jp_css() {
145 /**
146 * Do we really need `admin_styles`? With the new admin UI, it's breaking some bits.
147 * Jetpack::init()->admin_styles();
148 */
149 }
150
151 /**
152 * Load scripts and styles for front end.
153 */
154 public function load_styles_register_scripts() {
155 if ( $this->in_jetpack ) {
156 wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION );
157 $this->register_scripts();
158 }
159 }
160
161 /**
162 * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box
163 *
164 * @param string $html row heading for the sharedaddy "which page" setting.
165 * @return string $html with the jetpack-targetable class and likes id. tbody gets closed after the like settings
166 */
167 public function configuration_target_area( $html = '' ) {
168 $html = "<tbody id='likes' class='jetpack-targetable'>" . $html;
169 return $html;
170 }
171
172 /**
173 * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page)
174 */
175 public function admin_discussion_likes_settings_init() {
176 // Add a temporary section, until we can move the setting out of there and with the rest of the email notification settings.
177 add_settings_section( 'likes-notifications', __( 'Likes Notifications', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_section' ), 'discussion' );
178 add_settings_field( 'social-notifications', __( 'Email me whenever', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_field' ), 'discussion', 'likes-notifications' );
179 // Register the setting.
180 register_setting( 'discussion', 'social_notifications_like', array( $this, 'admin_discussion_likes_settings_validate' ) );
181 }
182
183 /** Add email notification options to WordPress discussion settings */
184 public function admin_discussion_likes_settings_section() {
185 // Atypical usage here. We emit jquery to move likes notification checkbox to be with the rest of the email notification settings.
186 ?>
187 <script type="text/javascript">
188 jQuery( function( $ ) {
189 var table = $( '#social_notifications_like' ).parents( 'table:first' ),
190 header = table.prevAll( 'h2:first' ),
191 newParent = $( '#moderation_notify' ).parent( 'label' ).parent();
192
193 if ( !table.length || !header.length || !newParent.length ) {
194 return;
195 }
196
197 newParent.append( '<br/>' ).append( table.end().parent( 'label' ).siblings().andSelf() );
198 header.remove();
199 table.remove();
200 } );
201 </script>
202 <?php
203 }
204
205 /** Check if email notifications for likes is on or off.
206 *
207 * @param string $option - which option we're checking (social_notifications_like).
208 */
209 public function admin_likes_get_option( $option ) {
210 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
211 $option_setting = get_blog_option( get_current_blog_id(), $option, 'on' );
212 } else {
213 $option_setting = get_option( $option, 'on' );
214 }
215
216 return (int) ( 'on' === $option_setting );
217 }
218
219 /** Display email notification for likes setting in WordPress' discussion settings. */
220 public function admin_discussion_likes_settings_field() {
221 $like = $this->admin_likes_get_option( 'social_notifications_like' );
222 ?>
223 <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>
224 <?php
225 }
226
227 /**
228 * Validate email notification settings.
229 *
230 * @param string $input - determines if checbox is on or off.
231 */
232 public 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 /** Initialize admin settings */
242 public function admin_init() {
243 add_filter( 'manage_posts_columns', array( $this, 'add_like_count_column' ) );
244 add_filter( 'manage_pages_columns', array( $this, 'add_like_count_column' ) );
245 add_action( 'manage_posts_custom_column', array( $this, 'likes_edit_column' ), 10, 2 );
246 add_action( 'manage_pages_custom_column', array( $this, 'likes_edit_column' ), 10, 2 );
247 add_action( 'admin_print_styles-edit.php', array( $this, 'load_admin_css' ) );
248 add_action( 'admin_print_scripts-edit.php', array( $this, 'enqueue_admin_scripts' ) );
249 }
250
251 /** Initialize action */
252 public function action_init() {
253 /*
254 * Only check if the module is enabled here because
255 * we are not currently in The Loop and do not yet have access to check
256 * the switch_like_status post meta flag for the post to be loaded.
257 */
258 if ( is_admin() || ! $this->settings->is_likes_module_enabled() ) {
259 return;
260 }
261
262 if ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ||
263 ( defined( 'APP_REQUEST' ) && APP_REQUEST ) ||
264 ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
265 ( defined( 'COOKIE_AUTH_REQUEST' ) && COOKIE_AUTH_REQUEST ) ||
266 ( defined( 'JABBER_SERVER' ) && JABBER_SERVER ) ) {
267 return;
268 }
269
270 if (
271 class_exists( 'Jetpack_AMP_Support' )
272 && Jetpack_AMP_Support::is_amp_request()
273 ) {
274 return;
275 }
276
277 if ( $this->in_jetpack ) {
278 add_filter( 'the_content', array( $this, 'post_likes' ), 30, 1 );
279 add_filter( 'the_excerpt', array( $this, 'post_likes' ), 30, 1 );
280
281 } else {
282 add_filter( 'post_flair', array( $this, 'post_likes' ), 30, 1 );
283 add_filter( 'post_flair_block_css', array( $this, 'post_flair_service_enabled_like' ) );
284
285 wp_enqueue_script( 'jetpack_likes_queuehandler', plugins_url( 'queuehandler.js', __FILE__ ), array(), JETPACK__VERSION, true );
286 wp_enqueue_style( 'jetpack_likes', plugins_url( 'jetpack-likes.css', __FILE__ ), array(), JETPACK__VERSION );
287 }
288 }
289
290 /**
291 * Register scripts.
292 */
293 public function register_scripts() {
294 wp_register_script(
295 'jetpack_likes_queuehandler',
296 Assets::get_file_url_for_environment(
297 '_inc/build/likes/queuehandler.min.js',
298 'modules/likes/queuehandler.js'
299 ),
300 array(),
301 JETPACK__VERSION,
302 true
303 );
304 }
305
306 /**
307 * Load the CSS needed for the wp-admin area.
308 */
309 public function load_admin_css() {
310 ?>
311 <style type="text/css">
312 .vers img { display: none; }
313 .metabox-prefs .vers img { display: inline; }
314 .fixed .column-likes { width: 5.5em; padding: 8px 0; text-align: left; }
315 .fixed .column-stats { width: 5em; }
316 .fixed .column-likes .post-com-count {
317 -webkit-box-sizing: border-box;
318 -moz-box-sizing: border-box;
319 box-sizing: border-box;
320 display: inline-block;
321 padding: 0 8px;
322 height: 2em;
323 margin-top: 5px;
324 -webkit-border-radius: 5px;
325 border-radius: 5px;
326 background-color: #787c82;
327 color: #FFF;
328 font-size: 11px;
329 line-height: 21px;
330 }
331 .fixed .column-likes .post-com-count::after { border: none !important; }
332 .fixed .column-likes .post-com-count:hover { background-color: #2271b1; }
333 .fixed .column-likes .vers:before {
334 font: normal 20px/1 dashicons;
335 content: '\f155';
336 speak: none;
337 -webkit-font-smoothing: antialiased;
338 -moz-osx-font-smoothing: grayscale;
339 }
340 @media screen and (max-width: 782px) {
341 .fixed .column-likes {
342 display: none;
343 }
344 }
345 </style>
346 <?php
347 }
348
349 /**
350 * Load the JS required for loading the like counts.
351 */
352 public function enqueue_admin_scripts() {
353 if ( empty( $_GET['post_type'] ) || 'post' === $_GET['post_type'] || 'page' === $_GET['post_type'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
354 if ( $this->in_jetpack ) {
355 wp_enqueue_script(
356 'likes-post-count',
357 Assets::get_file_url_for_environment(
358 '_inc/build/likes/post-count.min.js',
359 'modules/likes/post-count.js'
360 ),
361 array( 'jquery' ),
362 JETPACK__VERSION,
363 $in_footer = false
364 );
365 wp_enqueue_script(
366 'likes-post-count-jetpack',
367 Assets::get_file_url_for_environment(
368 '_inc/build/likes/post-count-jetpack.min.js',
369 'modules/likes/post-count-jetpack.js'
370 ),
371 array( 'likes-post-count' ),
372 JETPACK__VERSION,
373 $in_footer = false
374 );
375 } else {
376 wp_enqueue_script( 'jquery.wpcom-proxy-request', '/wp-content/js/jquery/jquery.wpcom-proxy-request.js', array( 'jquery' ), JETPACK__VERSION, true );
377 wp_enqueue_script( 'likes-post-count', plugins_url( 'likes/post-count.js', __DIR__ ), array( 'jquery' ), JETPACK__VERSION, $in_footer = false );
378 wp_enqueue_script( 'likes-post-count-wpcom', plugins_url( 'likes/post-count-wpcom.js', __DIR__ ), array( 'likes-post-count', 'jquery.wpcom-proxy-request' ), JETPACK__VERSION, $in_footer = false );
379 }
380 }
381 }
382
383 /**
384 * Add "Likes" column data to the post edit table in wp-admin.
385 *
386 * @param string $column_name - name of the column.
387 * @param int $post_id - the post id.
388 */
389 public function likes_edit_column( $column_name, $post_id ) {
390 if ( 'likes' === $column_name ) {
391
392 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
393 $blog_id = get_current_blog_id();
394 } else {
395 $blog_id = Jetpack_Options::get_option( 'id' );
396 }
397
398 $permalink = get_permalink( get_the_ID() );
399 ?>
400 <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; ?>">
401 <span class="comment-count">0</span>
402 </a>
403 <?php
404 }
405 }
406
407 /**
408 * Add a "Likes" column header to the post edit table in wp-admin.
409 *
410 * @param array $columns - array of columns in wp-admin.
411 */
412 public function add_like_count_column( $columns ) {
413 $date = $columns['date'];
414 unset( $columns['date'] );
415
416 $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>';
417 $columns['date'] = $date;
418
419 return $columns;
420 }
421
422 /**
423 * Append like button to content.
424 *
425 * @param string $content - content of the page.
426 */
427 public function post_likes( $content ) {
428 global $wp_current_filter;
429 $post_id = get_the_ID();
430
431 if ( ! is_numeric( $post_id ) || ! $this->settings->is_likes_visible() ) {
432 return $content;
433 }
434
435 // Ensure we don't display like button on post excerpts that are hooked inside the post content
436 if ( in_array( 'the_excerpt', (array) $wp_current_filter, true ) &&
437 in_array( 'the_content', (array) $wp_current_filter, true ) ) {
438 return $content;
439 }
440
441 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
442 $blog_id = get_current_blog_id();
443 $bloginfo = get_blog_details( (int) $blog_id );
444 $domain = $bloginfo->domain;
445 } else {
446 $blog_id = Jetpack_Options::get_option( 'id' );
447 $url = home_url();
448 $url_parts = wp_parse_url( $url );
449 $domain = $url_parts['host'];
450 }
451 // Make sure to include the scripts before the iframe otherwise weird things happen.
452 add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
453
454 /**
455 * If the same post appears more then once on a page the page goes crazy
456 * we need a slightly more unique id / name for the widget wrapper.
457 */
458 $uniqid = uniqid();
459
460 $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 );
461 $name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid );
462 $wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid );
463 $headline = sprintf(
464 /** This filter is already documented in modules/sharedaddy/sharing-service.php */
465 apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', esc_html__( 'Like this:', 'jetpack' ), 'likes' ),
466 esc_html__( 'Like this:', 'jetpack' )
467 );
468
469 $title = esc_html__( 'Like or Reblog', 'jetpack' );
470
471 $html = "<div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper' data-src='$src' data-name='$name' data-title='$title'>";
472 $html .= $headline;
473 $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>';
474 $html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>";
475 $html .= '</div>';
476
477 // Let's make sure that the script is enqueued.
478 wp_enqueue_script( 'jetpack_likes_queuehandler' );
479
480 return $content . $html;
481 }
482
483 /**
484 * Adds sd-like-enabled CSS class
485 *
486 * @param array $classes CSS class for post flair.
487 */
488 public function post_flair_service_enabled_like( $classes ) {
489 $classes[] = 'sd-like-enabled';
490 return $classes;
491 }
492
493 /** Checks if admin bar is visible.*/
494 public function is_admin_bar_button_visible() {
495 global $wp_admin_bar;
496
497 if ( ! is_object( $wp_admin_bar ) ) {
498 return false;
499 }
500
501 if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) ) {
502 return false;
503 }
504
505 if ( ! $this->settings->is_likes_visible() ) {
506 return false;
507 }
508
509 if ( ! $this->settings->is_post_likeable() ) {
510 return false;
511 }
512
513 /**
514 * Filters whether the Like button is enabled in the admin bar.
515 *
516 * @module likes
517 *
518 * @since 2.2.0
519 *
520 * @param bool true Should the Like button be visible in the Admin bar. Default to true.
521 */
522 return (bool) apply_filters( 'jetpack_admin_bar_likes_enabled', true );
523 }
524
525 /** Adds like section in admin bar. */
526 public function admin_bar_likes() {
527 global $wp_admin_bar;
528
529 $post_id = get_the_ID();
530
531 if ( ! is_numeric( $post_id ) || ! $this->is_admin_bar_button_visible() ) {
532 return;
533 }
534
535 $protocol = 'http';
536 if ( is_ssl() ) {
537 $protocol = 'https';
538 }
539 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
540 $blog_id = get_current_blog_id();
541 $bloginfo = get_blog_details( (int) $blog_id );
542 $domain = $bloginfo->domain;
543 } else {
544 $blog_id = Jetpack_Options::get_option( 'id' );
545 $url = home_url();
546 $url_parts = wp_parse_url( $url );
547 $domain = $url_parts['host'];
548 }
549 // Make sure to include the scripts before the iframe otherwise weird things happen.
550 add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
551
552 $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 );
553
554 $html = "<iframe class='admin-bar-likes-widget jetpack-likes-widget' scrolling='no' frameBorder='0' name='admin-bar-likes-widget' src='$src'></iframe>";
555
556 $node = array(
557 'id' => 'admin-bar-likes-widget',
558 'meta' => array(
559 'html' => $html,
560 ),
561 );
562
563 $wp_admin_bar->add_node( $node );
564 }
565 }
566
567 /**
568 * Callback to get the value for the jetpack_likes_enabled field.
569 *
570 * Warning: this behavior is somewhat complicated!
571 * When the switch_like_status post_meta is unset, we follow the global setting in Sharing.
572 * When it is set to 0, we disable likes on the post, regardless of the global setting.
573 * When it is set to 1, we enable likes on the post, regardless of the global setting.
574 *
575 * @param array $post - post data we're checking.
576 */
577 function jetpack_post_likes_get_value( array $post ) {
578 $post_likes_switched = get_post_meta( $post['id'], 'switch_like_status', true );
579
580 /** This filter is documented in modules/jetpack-likes-settings.php */
581 $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
582
583 // An empty string: post meta was not set, so go with the global setting.
584 if ( '' === $post_likes_switched ) {
585 return $sitewide_likes_enabled;
586 } elseif ( '0' === $post_likes_switched ) { // User overrode the global setting to disable likes.
587 return false;
588 } elseif ( '1' === $post_likes_switched ) { // User overrode the global setting to enable likes.
589 return true;
590 }
591 // No default fallback, let's stay explicit.
592 }
593
594 /**
595 * Callback to set switch_like_status post_meta when jetpack_likes_enabled is updated.
596 *
597 * Warning: this behavior is somewhat complicated!
598 * When the switch_like_status post_meta is unset, we follow the global setting in Sharing.
599 * When it is set to 0, we disable likes on the post, regardless of the global setting.
600 * When it is set to 1, we enable likes on the post, regardless of the global setting.
601 *
602 * @param bool $enable_post_likes - checks if post likes are enabled.
603 * @param object $post_object - object containing post data.
604 */
605 function jetpack_post_likes_update_value( $enable_post_likes, $post_object ) {
606 /** This filter is documented in modules/jetpack-likes-settings.php */
607 $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
608
609 $should_switch_status = $enable_post_likes !== $sitewide_likes_enabled;
610
611 if ( $should_switch_status ) {
612 // Set the meta to 0 if the user wants to disable likes, 1 if user wants to enable.
613 $switch_like_status = ( $enable_post_likes ? 1 : 0 );
614 return update_post_meta( $post_object->ID, 'switch_like_status', $switch_like_status );
615 } else {
616 // Unset the meta otherwise.
617 return delete_post_meta( $post_object->ID, 'switch_like_status' );
618 }
619 }
620
621 /**
622 * Add Likes post_meta to the REST API Post response.
623 *
624 * @action rest_api_init
625 * @uses register_rest_field
626 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
627 */
628 function jetpack_post_likes_register_rest_field() {
629 $post_types = get_post_types( array( 'public' => true ) );
630 foreach ( $post_types as $post_type ) {
631 register_rest_field(
632 $post_type,
633 'jetpack_likes_enabled',
634 array(
635 'get_callback' => 'jetpack_post_likes_get_value',
636 'update_callback' => 'jetpack_post_likes_update_value',
637 'schema' => array(
638 'description' => __( 'Are Likes enabled?', 'jetpack' ),
639 'type' => 'boolean',
640 ),
641 )
642 );
643
644 /**
645 * Ensures all public internal post-types support `likes`
646 * This feature support flag is used by the REST API and Gutenberg.
647 */
648 add_post_type_support( $post_type, 'jetpack-post-likes' );
649 }
650 }
651
652 // Add Likes post_meta to the REST API Post response.
653 add_action( 'rest_api_init', 'jetpack_post_likes_register_rest_field' );
654
655 // Some CPTs (e.g. Jetpack portfolios and testimonials) get registered with
656 // restapi_theme_init because they depend on theme support, so let's also hook to that.
657 add_action( 'restapi_theme_init', 'jetpack_post_likes_register_rest_field', 20 );
658
659 /**
660 * Set the Likes and Sharing Gutenberg extension availability.
661 */
662 function jetpack_post_likes_set_extension_availability() {
663 Jetpack_Gutenberg::set_extension_available( 'likes' );
664 }
665
666 add_action( 'jetpack_register_gutenberg_extensions', 'jetpack_post_likes_set_extension_availability' );
667
668 Jetpack_Likes::init();
669