| @@ -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,27 +95,33 @@ | ||
| 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 | 105 | add_action( 'admin_print_scripts-settings_page_sharing', array( $this, 'load_jp_css' ) ); |
| 82 | 106 | add_filter( 'sharing_show_buttons_on_row_start', array( $this, 'configuration_target_area' ) ); |
| 83 | 107 | |
| 84 | - $active = Jetpack::get_active_modules(); | |
| 108 | + $publicize_active = Jetpack::is_module_active( 'publicize' ); | |
| 109 | + $sharedaddy_active = Jetpack::is_module_active( 'sharedaddy' ); | |
| 85 | 110 | |
| 86 | - if ( ! in_array( 'sharedaddy', $active, true ) && ! in_array( 'publicize', $active, true ) ) { | |
| 87 | - // we don't have a sharing page yet. | |
| 111 | + if ( $this->settings->needs_own_sharing_menu( $sharedaddy_active ) ) { | |
| 112 | + /* | |
| 113 | + * Sharedaddy is what registers Settings > Sharing, and it is off, so we | |
| 114 | + * register that screen ourselves; our settings are displayed on it. | |
| 115 | + */ | |
| 88 | 116 | add_action( 'admin_menu', array( $this->settings, 'sharing_menu' ) ); |
| 89 | - } | |
| 90 | - | |
| 91 | - if ( in_array( 'publicize', $active, true ) && ! in_array( 'sharedaddy', $active, true ) ) { | |
| 117 | + } elseif ( $publicize_active && ! $sharedaddy_active ) { | |
| 92 | 118 | // we have a sharing page but not the global options area. |
| 93 | 119 | add_action( 'pre_admin_screen_sharing', array( $this->settings, 'sharing_block' ), 20 ); |
| 94 | 120 | add_action( 'pre_admin_screen_sharing', array( $this->settings, 'updated_message' ), -10 ); |
| 95 | 121 | } |
| 96 | 122 | |
| 97 | - if ( ! in_array( 'sharedaddy', $active, true ) ) { | |
| 123 | + if ( ! $sharedaddy_active ) { | |
| 98 | 124 | add_action( 'admin_init', array( $this->settings, 'process_update_requests_if_sharedaddy_not_loaded' ) ); |
| 99 | 125 | add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_showbuttonon_init' ), 19 ); |
| 100 | 126 | add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_showbuttonon_callback' ), 19 ); |
| 101 | 127 | add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) ); |
| @@ -105,10 +131,8 @@ | ||
| 105 | 131 | } |
| 106 | 132 | |
| 107 | 133 | add_action( 'admin_init', array( $this, 'admin_discussion_likes_settings_init' ) ); // Likes notifications. |
| 108 | 134 | |
| 109 | - add_action( 'admin_bar_menu', array( $this, 'admin_bar_likes' ), 60 ); | |
| 110 | - | |
| 111 | 135 | add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) ); |
| 112 | 136 | |
| 113 | 137 | add_action( 'save_post', array( $this->settings, 'meta_box_save' ) ); |
| 114 | 138 | add_action( 'edit_attachment', array( $this->settings, 'meta_box_save' ) ); |
| @@ -157,9 +181,25 @@ | ||
| 157 | 181 | /** |
| 158 | 182 | * Load scripts and styles for front end. |
| 159 | 183 | */ |
| 160 | 184 | public function load_styles_register_scripts() { |
| 161 | - wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION ); | |
| 185 | + // Likes are only rendered on pages that display post content. | |
| 186 | + if ( ! is_singular() && ! is_home() && ! is_front_page() && ! is_archive() && ! is_search() ) { | |
| 187 | + return; | |
| 188 | + } | |
| 189 | + | |
| 190 | + $style_url = Assets::get_file_url_for_environment( | |
| 191 | + '_inc/build/likes/style.min.css', | |
| 192 | + 'modules/likes/style.css' | |
| 193 | + ); | |
| 194 | + wp_enqueue_style( 'jetpack_likes', $style_url, array(), JETPACK__VERSION ); | |
| 195 | + $style_path = plugin_dir_path( JETPACK__PLUGIN_FILE ) . ( | |
| 196 | + /** This filter is documented in projects/plugins/jetpack/load-jetpack.php */ | |
| 197 | + apply_filters( 'jetpack_should_use_minified_assets', true ) | |
| 198 | + ? '_inc/build/likes/style.min.css' | |
| 199 | + : 'modules/likes/style.css' | |
| 200 | + ); | |
| 201 | + wp_style_add_data( 'jetpack_likes', 'path', $style_path ); | |
| 162 | 202 | wp_register_script( |
| 163 | 203 | 'jetpack_likes_queuehandler', |
| 164 | 204 | Assets::get_file_url_for_environment( |
| 165 | 205 | '_inc/build/likes/queuehandler.min.js', |
| @@ -294,16 +334,18 @@ | ||
| 294 | 334 | ?> |
| 295 | 335 | <style type="text/css"> |
| 296 | 336 | .vers img { display: none; } |
| 297 | 337 | .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; } | |
| 338 | + .fixed .column-likes { width: 2.5em; padding: 4px 0; text-align: left; } | |
| 339 | + .fixed .column-stats { width: 5em; white-space: nowrap; } | |
| 300 | 340 | .fixed .column-likes .post-com-count { |
| 301 | 341 | -webkit-box-sizing: border-box; |
| 302 | 342 | -moz-box-sizing: border-box; |
| 303 | 343 | box-sizing: border-box; |
| 304 | 344 | display: inline-block; |
| 305 | - padding: 0 8px; | |
| 345 | + padding: 0 4px; | |
| 346 | + min-width: 2em; | |
| 347 | + text-align: center; | |
| 306 | 348 | height: 2em; |
| 307 | 349 | margin-top: 5px; |
| 308 | 350 | -webkit-border-radius: 5px; |
| 309 | 351 | border-radius: 5px; |
| @@ -313,9 +355,9 @@ | ||
| 313 | 355 | line-height: 21px; |
| 314 | 356 | } |
| 315 | 357 | .fixed .column-likes .post-com-count::after { border: none !important; } |
| 316 | 358 | .fixed .column-likes .post-com-count:hover { background-color: #2271b1; } |
| 317 | - .fixed .column-likes .vers:before { | |
| 359 | + .fixed .column-likes .vers::before { | |
| 318 | 360 | font: normal 20px/1 dashicons; |
| 319 | 361 | content: '\f155'; |
| 320 | 362 | speak: none; |
| 321 | 363 | -webkit-font-smoothing: antialiased; |
| @@ -342,9 +384,9 @@ | ||
| 342 | 384 | 'modules/likes/post-count.js' |
| 343 | 385 | ), |
| 344 | 386 | array( 'jquery' ), |
| 345 | 387 | JETPACK__VERSION, |
| 346 | - $in_footer = false | |
| 388 | + false | |
| 347 | 389 | ); |
| 348 | 390 | wp_enqueue_script( |
| 349 | 391 | 'likes-post-count-jetpack', |
| 350 | 392 | Assets::get_file_url_for_environment( |
| @@ -352,9 +394,9 @@ | ||
| 352 | 394 | 'modules/likes/post-count-jetpack.js' |
| 353 | 395 | ), |
| 354 | 396 | array( 'jquery', 'likes-post-count' ), |
| 355 | 397 | JETPACK__VERSION, |
| 356 | - $in_footer = false | |
| 398 | + false | |
| 357 | 399 | ); |
| 358 | 400 | } |
| 359 | 401 | } |
| 360 | 402 | |
| @@ -424,18 +466,19 @@ | ||
| 424 | 466 | $url = home_url(); |
| 425 | 467 | $url_parts = wp_parse_url( $url ); |
| 426 | 468 | $domain = $url_parts['host']; |
| 427 | 469 | |
| 428 | - // Make sure to include the scripts before the iframe otherwise weird things happen. | |
| 429 | - add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 ); | |
| 470 | + // Make sure to include the `queuehandler` scripts before the iframe otherwise the script won't find the iframe. | |
| 471 | + if ( ! has_action( 'wp_footer', 'jetpack_likes_master_iframe' ) ) { | |
| 472 | + add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 ); | |
| 473 | + } | |
| 430 | 474 | |
| 431 | 475 | /** |
| 432 | 476 | * If the same post appears more then once on a page the page goes crazy |
| 433 | 477 | * we need a slightly more unique id / name for the widget wrapper. |
| 434 | 478 | */ |
| 435 | - $uniqid = uniqid(); | |
| 436 | - | |
| 437 | - $src = sprintf( 'https://widgets.wp.com/likes/#blog_id=%1$d&post_id=%2$d&origin=%3$s&obj_id=%1$d-%2$d-%4$s', $blog_id, $post_id, $domain, $uniqid ); | |
| 479 | + $uniqid = uniqid(); | |
| 480 | + $src = sprintf( 'https://widgets.wp.com/likes/?ver=%1$s#blog_id=%2$d&post_id=%3$d&origin=%4$s&obj_id=%2$d-%3$d-%5$s', rawurlencode( JETPACK__VERSION ), $blog_id, $post_id, $domain, $uniqid ); | |
| 438 | 481 | $name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); |
| 439 | 482 | $wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); |
| 440 | 483 | $headline = sprintf( |
| 441 | 484 | /** This filter is already documented in modules/sharedaddy/sharing-service.php */ |
| @@ -444,11 +487,15 @@ | ||
| 444 | 487 | ); |
| 445 | 488 | |
| 446 | 489 | $title = esc_html__( 'Like or Reblog', 'jetpack' ); |
| 447 | 490 | |
| 491 | + /** This filter is documented in modules/likes/jetpack-likes-master-iframe.php */ | |
| 492 | + $src = apply_filters( 'jetpack_likes_iframe_src', $src ); | |
| 493 | + | |
| 448 | 494 | $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 | 495 | $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>'; | |
| 496 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-spinner.php'; | |
| 497 | + $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 | 498 | $html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>"; |
| 452 | 499 | $html .= '</div>'; |
| 453 | 500 | |
| 454 | 501 | // Let's make sure that the script is enqueued. |
| @@ -455,76 +502,8 @@ | ||
| 455 | 502 | wp_enqueue_script( 'jetpack_likes_queuehandler' ); |
| 456 | 503 | |
| 457 | 504 | return $content . $html; |
| 458 | 505 | } |
| 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&post_id=%3$d&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 | 506 | } |
| 528 | 507 | |
| 529 | 508 | /** |
| 530 | 509 | * Callback to get the value for the jetpack_likes_enabled field. |
| @@ -534,10 +513,16 @@ | ||
| 534 | 513 | * When it is set to 0, we disable likes on the post, regardless of the global setting. |
| 535 | 514 | * When it is set to 1, we enable likes on the post, regardless of the global setting. |
| 536 | 515 | * |
| 537 | 516 | * @param array $post - post data we're checking. |
| 517 | + * | |
| 518 | + * @return bool | |
| 538 | 519 | */ |
| 539 | 520 | function jetpack_post_likes_get_value( array $post ) { |
| 521 | + if ( ! isset( $post['id'] ) ) { | |
| 522 | + return false; | |
| 523 | + } | |
| 524 | + | |
| 540 | 525 | $post_likes_switched = get_post_meta( $post['id'], 'switch_like_status', true ); |
| 541 | 526 | |
| 542 | 527 | /** This filter is documented in modules/jetpack-likes-settings.php */ |
| 543 | 528 | $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) ); |