| @@ -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,25 +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( 'publicize', $active, true ) && ! in_array( 'sharedaddy', $active, true ) ) { | |
| 87 | - // we have a sharing page but not the global options area. | |
| 88 | - add_action( 'pre_admin_screen_sharing', array( $this->settings, 'sharing_block' ), 20 ); | |
| 89 | - add_action( 'pre_admin_screen_sharing', array( $this->settings, 'updated_message' ), -10 ); | |
| 90 | - } | |
| 91 | - | |
| 92 | - if ( ! in_array( 'sharedaddy', $active, true ) ) { | |
| 93 | - add_action( 'admin_init', array( $this->settings, 'process_update_requests_if_sharedaddy_not_loaded' ) ); | |
| 94 | - add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_showbuttonon_init' ), 19 ); | |
| 95 | - add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_showbuttonon_callback' ), 19 ); | |
| 108 | + if ( ! $sharedaddy_active ) { | |
| 96 | 109 | add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) ); |
| 97 | 110 | } else { |
| 98 | 111 | add_filter( 'sharing_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) ); |
| 99 | 112 | add_action( 'start_sharing_meta_box_content', array( $this->settings, 'meta_box_content' ) ); |
| @@ -104,10 +117,8 @@ | ||
| 104 | 117 | add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) ); |
| 105 | 118 | |
| 106 | 119 | add_action( 'save_post', array( $this->settings, 'meta_box_save' ) ); |
| 107 | 120 | add_action( 'edit_attachment', array( $this->settings, 'meta_box_save' ) ); |
| 108 | - add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_init' ), 20 ); | |
| 109 | - add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_callback' ), 20 ); | |
| 110 | 121 | } |
| 111 | 122 | |
| 112 | 123 | /** |
| 113 | 124 | * Set the social_notifications_like option to `on` when the Likes module is activated. |
| @@ -138,21 +149,51 @@ | ||
| 138 | 149 | } |
| 139 | 150 | |
| 140 | 151 | /** |
| 141 | 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 | |
| 142 | 157 | */ |
| 143 | 158 | public function load_jp_css() { |
| 144 | - /** | |
| 145 | - * Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. | |
| 146 | - * Jetpack::init()->admin_styles(); | |
| 147 | - */ | |
| 159 | + _deprecated_function( __METHOD__, 'jetpack-16.3' ); | |
| 148 | 160 | } |
| 149 | 161 | |
| 150 | 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 | + /** | |
| 151 | 176 | * Load scripts and styles for front end. |
| 152 | 177 | */ |
| 153 | 178 | public function load_styles_register_scripts() { |
| 154 | - 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 ); | |
| 155 | 196 | wp_register_script( |
| 156 | 197 | 'jetpack_likes_queuehandler', |
| 157 | 198 | Assets::get_file_url_for_environment( |
| 158 | 199 | '_inc/build/likes/queuehandler.min.js', |
| @@ -164,21 +205,10 @@ | ||
| 164 | 205 | ); |
| 165 | 206 | } |
| 166 | 207 | |
| 167 | 208 | /** |
| 168 | - * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box | |
| 169 | - * | |
| 170 | - * @param string $html row heading for the sharedaddy "which page" setting. | |
| 171 | - * @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. | |
| 172 | 210 | */ |
| 173 | - public function configuration_target_area( $html = '' ) { | |
| 174 | - $html = "<tbody id='likes' class='jetpack-targetable'>" . $html; | |
| 175 | - return $html; | |
| 176 | - } | |
| 177 | - | |
| 178 | - /** | |
| 179 | - * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page) | |
| 180 | - */ | |
| 181 | 211 | public function admin_discussion_likes_settings_init() { |
| 182 | 212 | // Add a temporary section, until we can move the setting out of there and with the rest of the email notification settings. |
| 183 | 213 | add_settings_section( 'likes-notifications', __( 'Likes Notifications', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_section' ), 'discussion' ); |
| 184 | 214 | add_settings_field( 'social-notifications', __( 'Email me whenever', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_field' ), 'discussion', 'likes-notifications' ); |
| @@ -287,16 +317,18 @@ | ||
| 287 | 317 | ?> |
| 288 | 318 | <style type="text/css"> |
| 289 | 319 | .vers img { display: none; } |
| 290 | 320 | .metabox-prefs .vers img { display: inline; } |
| 291 | - .fixed .column-likes { width: 5.5em; padding: 8px 0; text-align: left; } | |
| 292 | - .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; } | |
| 293 | 323 | .fixed .column-likes .post-com-count { |
| 294 | 324 | -webkit-box-sizing: border-box; |
| 295 | 325 | -moz-box-sizing: border-box; |
| 296 | 326 | box-sizing: border-box; |
| 297 | 327 | display: inline-block; |
| 298 | - padding: 0 8px; | |
| 328 | + padding: 0 4px; | |
| 329 | + min-width: 2em; | |
| 330 | + text-align: center; | |
| 299 | 331 | height: 2em; |
| 300 | 332 | margin-top: 5px; |
| 301 | 333 | -webkit-border-radius: 5px; |
| 302 | 334 | border-radius: 5px; |
| @@ -306,9 +338,9 @@ | ||
| 306 | 338 | line-height: 21px; |
| 307 | 339 | } |
| 308 | 340 | .fixed .column-likes .post-com-count::after { border: none !important; } |
| 309 | 341 | .fixed .column-likes .post-com-count:hover { background-color: #2271b1; } |
| 310 | - .fixed .column-likes .vers:before { | |
| 342 | + .fixed .column-likes .vers::before { | |
| 311 | 343 | font: normal 20px/1 dashicons; |
| 312 | 344 | content: '\f155'; |
| 313 | 345 | speak: none; |
| 314 | 346 | -webkit-font-smoothing: antialiased; |
| @@ -335,9 +367,9 @@ | ||
| 335 | 367 | 'modules/likes/post-count.js' |
| 336 | 368 | ), |
| 337 | 369 | array( 'jquery' ), |
| 338 | 370 | JETPACK__VERSION, |
| 339 | - $in_footer = false | |
| 371 | + false | |
| 340 | 372 | ); |
| 341 | 373 | wp_enqueue_script( |
| 342 | 374 | 'likes-post-count-jetpack', |
| 343 | 375 | Assets::get_file_url_for_environment( |
| @@ -345,9 +377,9 @@ | ||
| 345 | 377 | 'modules/likes/post-count-jetpack.js' |
| 346 | 378 | ), |
| 347 | 379 | array( 'jquery', 'likes-post-count' ), |
| 348 | 380 | JETPACK__VERSION, |
| 349 | - $in_footer = false | |
| 381 | + false | |
| 350 | 382 | ); |
| 351 | 383 | } |
| 352 | 384 | } |
| 353 | 385 | |
| @@ -417,28 +449,19 @@ | ||
| 417 | 449 | $url = home_url(); |
| 418 | 450 | $url_parts = wp_parse_url( $url ); |
| 419 | 451 | $domain = $url_parts['host']; |
| 420 | 452 | |
| 421 | - // Make sure to include the scripts before the iframe otherwise weird things happen. | |
| 422 | - 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 | + } | |
| 423 | 457 | |
| 424 | 458 | /** |
| 425 | 459 | * If the same post appears more then once on a page the page goes crazy |
| 426 | 460 | * we need a slightly more unique id / name for the widget wrapper. |
| 427 | 461 | */ |
| 428 | - $uniqid = uniqid(); | |
| 429 | - /** | |
| 430 | - * Enable an alternate Likes layout. | |
| 431 | - * | |
| 432 | - * @since 12.9 | |
| 433 | - * | |
| 434 | - * @module likes | |
| 435 | - * | |
| 436 | - * @param bool $new_layout Enable the new Likes layout. False by default. | |
| 437 | - */ | |
| 438 | - $new_layout = apply_filters( 'likes_new_layout', true ) ? '&n=1' : ''; | |
| 439 | - | |
| 440 | - $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%6$s', rawurlencode( JETPACK__VERSION ), $blog_id, $post_id, $domain, $uniqid, $new_layout ); | |
| 462 | + $uniqid = uniqid(); | |
| 463 | + $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 ); | |
| 441 | 464 | $name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); |
| 442 | 465 | $wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); |
| 443 | 466 | $headline = sprintf( |
| 444 | 467 | /** This filter is already documented in modules/sharedaddy/sharing-service.php */ |
| @@ -447,11 +470,15 @@ | ||
| 447 | 470 | ); |
| 448 | 471 | |
| 449 | 472 | $title = esc_html__( 'Like or Reblog', 'jetpack' ); |
| 450 | 473 | |
| 474 | + /** This filter is documented in modules/likes/jetpack-likes-master-iframe.php */ | |
| 475 | + $src = apply_filters( 'jetpack_likes_iframe_src', $src ); | |
| 476 | + | |
| 451 | 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'>"; |
| 452 | 478 | $html .= $headline; |
| 453 | - $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>'; | |
| 454 | 481 | $html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>"; |
| 455 | 482 | $html .= '</div>'; |
| 456 | 483 | |
| 457 | 484 | // Let's make sure that the script is enqueued. |