PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.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 All 506 releases
← All changes | modules/likes.php +99 -140 12.2.3 → 16.3-a.7 View file →
@@ -1,8 +1,8 @@
1 1 <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 2 /**
3 3 * Module Name: Likes
4 - * Module Description: Give visitors an easy way to show they appreciate your content.
4 + * Module Description: Let readers like your posts to show appreciation and encourage interaction.
5 5 * First Introduced: 2.2
6 6 * Sort Order: 23
7 7 * Requires Connection: Yes
8 8 * Auto Activate: No
@@ -20,18 +20,39 @@
20 20
21 21 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
22 22
23 23 use Automattic\Jetpack\Assets;
24 +use Automattic\Jetpack\Status\Host;
24 25
25 -Assets::add_resource_hint(
26 - array(
27 - '//widgets.wp.com',
28 - '//s0.wp.com',
29 - '//0.gravatar.com',
30 - '//1.gravatar.com',
31 - '//2.gravatar.com',
32 - ),
33 - 'dns-prefetch'
26 +if ( ! defined( 'ABSPATH' ) ) {
27 + exit( 0 );
28 +}
29 +
30 +add_filter(
31 + 'wp_resource_hints',
32 + function ( $hints, $relation_type ) {
33 + if ( 'dns-prefetch' !== $relation_type ) {
34 + return $hints;
35 + }
36 +
37 + // Only hint on pages where Likes can render.
38 + if ( ! is_singular() && ! is_home() && ! is_front_page() && ! is_archive() && ! is_search() ) {
39 + return $hints;
40 + }
41 +
42 + return array_merge(
43 + $hints,
44 + array(
45 + '//widgets.wp.com',
46 + '//s0.wp.com',
47 + '//0.gravatar.com',
48 + '//1.gravatar.com',
49 + '//2.gravatar.com',
50 + )
51 + );
52 + },
53 + 10,
54 + 2
34 55 );
35 56
36 57 require_once __DIR__ . '/likes/jetpack-likes-master-iframe.php';
37 58 require_once __DIR__ . '/likes/jetpack-likes-settings.php';
@@ -39,9 +60,8 @@
39 60 /**
40 61 * Jetpack Like Class
41 62 */
42 63 class Jetpack_Likes {
43 -
44 64 /**
45 65 * Jetpack_Likes_Settings object
46 66 *
47 67 * @var Jetpack_Likes_Settings
@@ -75,30 +95,18 @@
75 95
76 96 add_action( 'jetpack_activate_module_likes', array( $this, 'set_social_notifications_like' ) );
77 97 add_action( 'jetpack_deactivate_module_likes', array( $this, 'delete_social_notifications_like' ) );
78 98
79 - Jetpack::enable_module_configurable( __FILE__ );
99 + // The `enable_module_configurable` method doesn't exist in the WP.com loader implementation.
100 + if ( ! ( new Host() )->is_wpcom_simple() ) {
101 + Jetpack::enable_module_configurable( __FILE__ );
102 + }
103 +
80 104 add_filter( 'jetpack_module_configuration_url_likes', array( $this, 'jetpack_likes_configuration_url' ) );
81 - add_action( 'admin_print_scripts-settings_page_sharing', array( $this, 'load_jp_css' ) );
82 - add_filter( 'sharing_show_buttons_on_row_start', array( $this, 'configuration_target_area' ) );
83 105
84 - $active = Jetpack::get_active_modules();
106 + $sharedaddy_active = Jetpack::is_module_active( 'sharedaddy' );
85 107
86 - if ( ! in_array( 'sharedaddy', $active, true ) && ! in_array( 'publicize', $active, true ) ) {
87 - // we don't have a sharing page yet.
88 - add_action( 'admin_menu', array( $this->settings, 'sharing_menu' ) );
89 - }
90 -
91 - if ( in_array( 'publicize', $active, true ) && ! in_array( 'sharedaddy', $active, true ) ) {
92 - // we have a sharing page but not the global options area.
93 - add_action( 'pre_admin_screen_sharing', array( $this->settings, 'sharing_block' ), 20 );
94 - add_action( 'pre_admin_screen_sharing', array( $this->settings, 'updated_message' ), -10 );
95 - }
96 -
97 - if ( ! in_array( 'sharedaddy', $active, true ) ) {
98 - add_action( 'admin_init', array( $this->settings, 'process_update_requests_if_sharedaddy_not_loaded' ) );
99 - add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_showbuttonon_init' ), 19 );
100 - add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_showbuttonon_callback' ), 19 );
108 + if ( ! $sharedaddy_active ) {
101 109 add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) );
102 110 } else {
103 111 add_filter( 'sharing_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) );
104 112 add_action( 'start_sharing_meta_box_content', array( $this->settings, 'meta_box_content' ) );
@@ -105,16 +113,12 @@
105 113 }
106 114
107 115 add_action( 'admin_init', array( $this, 'admin_discussion_likes_settings_init' ) ); // Likes notifications.
108 116
109 - add_action( 'admin_bar_menu', array( $this, 'admin_bar_likes' ), 60 );
110 -
111 117 add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) );
112 118
113 119 add_action( 'save_post', array( $this->settings, 'meta_box_save' ) );
114 120 add_action( 'edit_attachment', array( $this->settings, 'meta_box_save' ) );
115 - add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_init' ), 20 );
116 - add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_callback' ), 20 );
117 121 }
118 122
119 123 /**
120 124 * Set the social_notifications_like option to `on` when the Likes module is activated.
@@ -145,21 +149,51 @@
145 149 }
146 150
147 151 /**
148 152 * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable
153 + *
154 + * @deprecated 16.3 Settings > Sharing is a plain WordPress settings screen now.
155 + *
156 + * @return void
149 157 */
150 158 public function load_jp_css() {
151 - /**
152 - * Do we really need `admin_styles`? With the new admin UI, it's breaking some bits.
153 - * Jetpack::init()->admin_styles();
154 - */
159 + _deprecated_function( __METHOD__, 'jetpack-16.3' );
155 160 }
156 161
157 162 /**
163 + * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box
164 + *
165 + * @deprecated 16.3 The Likes settings have a section of their own on Settings > Sharing.
166 + *
167 + * @param string $html row heading for the sharedaddy "which page" setting.
168 + * @return string The unchanged $html.
169 + */
170 + public function configuration_target_area( $html = '' ) {
171 + _deprecated_function( __METHOD__, 'jetpack-16.3' );
172 + return $html;
173 + }
174 +
175 + /**
158 176 * Load scripts and styles for front end.
159 177 */
160 178 public function load_styles_register_scripts() {
161 - wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION );
179 + // Likes are only rendered on pages that display post content.
180 + if ( ! is_singular() && ! is_home() && ! is_front_page() && ! is_archive() && ! is_search() ) {
181 + return;
182 + }
183 +
184 + $style_url = Assets::get_file_url_for_environment(
185 + '_inc/build/likes/style.min.css',
186 + 'modules/likes/style.css'
187 + );
188 + wp_enqueue_style( 'jetpack_likes', $style_url, array(), JETPACK__VERSION );
189 + $style_path = plugin_dir_path( JETPACK__PLUGIN_FILE ) . (
190 + /** This filter is documented in projects/plugins/jetpack/load-jetpack.php */
191 + apply_filters( 'jetpack_should_use_minified_assets', true )
192 + ? '_inc/build/likes/style.min.css'
193 + : 'modules/likes/style.css'
194 + );
195 + wp_style_add_data( 'jetpack_likes', 'path', $style_path );
162 196 wp_register_script(
163 197 'jetpack_likes_queuehandler',
164 198 Assets::get_file_url_for_environment(
165 199 '_inc/build/likes/queuehandler.min.js',
@@ -171,21 +205,10 @@
171 205 );
172 206 }
173 207
174 208 /**
175 - * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box
176 - *
177 - * @param string $html row heading for the sharedaddy "which page" setting.
178 - * @return string $html with the jetpack-targetable class and likes id. tbody gets closed after the like settings
209 + * Options to be added to the discussion page. The Sharing settings live in Sharing_Likes\Settings\Likes_Section.
179 210 */
180 - public function configuration_target_area( $html = '' ) {
181 - $html = "<tbody id='likes' class='jetpack-targetable'>" . $html;
182 - return $html;
183 - }
184 -
185 - /**
186 - * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page)
187 - */
188 211 public function admin_discussion_likes_settings_init() {
189 212 // Add a temporary section, until we can move the setting out of there and with the rest of the email notification settings.
190 213 add_settings_section( 'likes-notifications', __( 'Likes Notifications', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_section' ), 'discussion' );
191 214 add_settings_field( 'social-notifications', __( 'Email me whenever', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_field' ), 'discussion', 'likes-notifications' );
@@ -294,16 +317,18 @@
294 317 ?>
295 318 <style type="text/css">
296 319 .vers img { display: none; }
297 320 .metabox-prefs .vers img { display: inline; }
298 - .fixed .column-likes { width: 5.5em; padding: 8px 0; text-align: left; }
299 - .fixed .column-stats { width: 5em; }
321 + .fixed .column-likes { width: 2.5em; padding: 4px 0; text-align: left; }
322 + .fixed .column-stats { width: 5em; white-space: nowrap; }
300 323 .fixed .column-likes .post-com-count {
301 324 -webkit-box-sizing: border-box;
302 325 -moz-box-sizing: border-box;
303 326 box-sizing: border-box;
304 327 display: inline-block;
305 - padding: 0 8px;
328 + padding: 0 4px;
329 + min-width: 2em;
330 + text-align: center;
306 331 height: 2em;
307 332 margin-top: 5px;
308 333 -webkit-border-radius: 5px;
309 334 border-radius: 5px;
@@ -313,9 +338,9 @@
313 338 line-height: 21px;
314 339 }
315 340 .fixed .column-likes .post-com-count::after { border: none !important; }
316 341 .fixed .column-likes .post-com-count:hover { background-color: #2271b1; }
317 - .fixed .column-likes .vers:before {
342 + .fixed .column-likes .vers::before {
318 343 font: normal 20px/1 dashicons;
319 344 content: '\f155';
320 345 speak: none;
321 346 -webkit-font-smoothing: antialiased;
@@ -342,9 +367,9 @@
342 367 'modules/likes/post-count.js'
343 368 ),
344 369 array( 'jquery' ),
345 370 JETPACK__VERSION,
346 - $in_footer = false
371 + false
347 372 );
348 373 wp_enqueue_script(
349 374 'likes-post-count-jetpack',
350 375 Assets::get_file_url_for_environment(
@@ -352,9 +377,9 @@
352 377 'modules/likes/post-count-jetpack.js'
353 378 ),
354 379 array( 'jquery', 'likes-post-count' ),
355 380 JETPACK__VERSION,
356 - $in_footer = false
381 + false
357 382 );
358 383 }
359 384 }
360 385
@@ -424,18 +449,19 @@
424 449 $url = home_url();
425 450 $url_parts = wp_parse_url( $url );
426 451 $domain = $url_parts['host'];
427 452
428 - // Make sure to include the scripts before the iframe otherwise weird things happen.
429 - add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
453 + // Make sure to include the `queuehandler` scripts before the iframe otherwise the script won't find the iframe.
454 + if ( ! has_action( 'wp_footer', 'jetpack_likes_master_iframe' ) ) {
455 + add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
456 + }
430 457
431 458 /**
432 459 * If the same post appears more then once on a page the page goes crazy
433 460 * we need a slightly more unique id / name for the widget wrapper.
434 461 */
435 - $uniqid = uniqid();
436 -
437 - $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 );
462 + $uniqid = uniqid();
463 + $src = sprintf( 'https://widgets.wp.com/likes/?ver=%1$s#blog_id=%2$d&amp;post_id=%3$d&amp;origin=%4$s&amp;obj_id=%2$d-%3$d-%5$s', rawurlencode( JETPACK__VERSION ), $blog_id, $post_id, $domain, $uniqid );
438 464 $name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid );
439 465 $wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid );
440 466 $headline = sprintf(
441 467 /** This filter is already documented in modules/sharedaddy/sharing-service.php */
@@ -444,11 +470,15 @@
444 470 );
445 471
446 472 $title = esc_html__( 'Like or Reblog', 'jetpack' );
447 473
474 + /** This filter is documented in modules/likes/jetpack-likes-master-iframe.php */
475 + $src = apply_filters( 'jetpack_likes_iframe_src', $src );
476 +
448 477 $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'>";
449 478 $html .= $headline;
450 - $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>';
479 + require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-spinner.php';
480 + $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">' . Jetpack_Spinner::render( 18 ) . '<span class="screen-reader-text">' . esc_html__( 'Loading…', 'jetpack' ) . '</span></span></div>';
451 481 $html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>";
452 482 $html .= '</div>';
453 483
454 484 // Let's make sure that the script is enqueued.
@@ -455,76 +485,8 @@
455 485 wp_enqueue_script( 'jetpack_likes_queuehandler' );
456 486
457 487 return $content . $html;
458 488 }
459 -
460 - /** Checks if admin bar is visible.*/
461 - public function is_admin_bar_button_visible() {
462 - global $wp_admin_bar;
463 -
464 - if ( ! is_object( $wp_admin_bar ) ) {
465 - return false;
466 - }
467 -
468 - if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) ) {
469 - return false;
470 - }
471 -
472 - if ( ! $this->settings->is_likes_visible() ) {
473 - return false;
474 - }
475 -
476 - if ( ! $this->settings->is_post_likeable() ) {
477 - return false;
478 - }
479 -
480 - /**
481 - * Filters whether the Like button is enabled in the admin bar.
482 - *
483 - * @module likes
484 - *
485 - * @since 2.2.0
486 - *
487 - * @param bool true Should the Like button be visible in the Admin bar. Default to true.
488 - */
489 - return (bool) apply_filters( 'jetpack_admin_bar_likes_enabled', true );
490 - }
491 -
492 - /** Adds like section in admin bar. */
493 - public function admin_bar_likes() {
494 - global $wp_admin_bar;
495 -
496 - $post_id = get_the_ID();
497 -
498 - if ( ! is_numeric( $post_id ) || ! $this->is_admin_bar_button_visible() ) {
499 - return;
500 - }
501 -
502 - $protocol = 'http';
503 - if ( is_ssl() ) {
504 - $protocol = 'https';
505 - }
506 - $blog_id = Jetpack_Options::get_option( 'id' );
507 - $url = home_url();
508 - $url_parts = wp_parse_url( $url );
509 - $domain = $url_parts['host'];
510 -
511 - // Make sure to include the scripts before the iframe otherwise weird things happen.
512 - add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
513 -
514 - $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 );
515 -
516 - $html = "<iframe class='admin-bar-likes-widget jetpack-likes-widget' scrolling='no' frameBorder='0' name='admin-bar-likes-widget' src='$src'></iframe>";
517 -
518 - $node = array(
519 - 'id' => 'admin-bar-likes-widget',
520 - 'meta' => array(
521 - 'html' => $html,
522 - ),
523 - );
524 -
525 - $wp_admin_bar->add_node( $node );
526 - }
527 489 }
528 490
529 491 /**
530 492 * Callback to get the value for the jetpack_likes_enabled field.
@@ -534,10 +496,16 @@
534 496 * When it is set to 0, we disable likes on the post, regardless of the global setting.
535 497 * When it is set to 1, we enable likes on the post, regardless of the global setting.
536 498 *
537 499 * @param array $post - post data we're checking.
500 + *
501 + * @return bool
538 502 */
539 503 function jetpack_post_likes_get_value( array $post ) {
504 + if ( ! isset( $post['id'] ) ) {
505 + return false;
506 + }
507 +
540 508 $post_likes_switched = get_post_meta( $post['id'], 'switch_like_status', true );
541 509
542 510 /** This filter is documented in modules/jetpack-likes-settings.php */
543 511 $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
@@ -616,15 +584,6 @@
616 584
617 585 // Some CPTs (e.g. Jetpack portfolios and testimonials) get registered with
618 586 // restapi_theme_init because they depend on theme support, so let's also hook to that.
619 587 add_action( 'restapi_theme_init', 'jetpack_post_likes_register_rest_field', 20 );
620 -
621 -/**
622 - * Set the Likes and Sharing Gutenberg extension availability.
623 - */
624 -function jetpack_post_likes_set_extension_availability() {
625 - Jetpack_Gutenberg::set_extension_available( 'likes' );
626 -}
627 -
628 -add_action( 'jetpack_register_gutenberg_extensions', 'jetpack_post_likes_set_extension_availability' );
629 588
630 589 Jetpack_Likes::init();