PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.5.1
Jetpack – WP Security, Backup, Speed, & Growth v7.5.1
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 7.5.1, at modules/likes.php

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