| @@ -8,12 +8,14 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | namespace Automattic\Jetpack\Extensions\Like; |
| 11 | 11 | |
| 12 | -use Automattic\Jetpack\Assets; | |
| 13 | 12 | use Automattic\Jetpack\Blocks; |
| 14 | -use Jetpack_Gutenberg; | |
| 15 | 13 | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | + | |
| 16 | 18 | /** |
| 17 | 19 | * Registers the block for use in Gutenberg |
| 18 | 20 | * This is done via an action so that we can disable |
| 19 | 21 | * registration if we need to. |
| @@ -18,18 +20,21 @@ | ||
| 18 | 20 | * This is done via an action so that we can disable |
| 19 | 21 | * registration if we need to. |
| 20 | 22 | */ |
| 21 | 23 | function register_block() { |
| 22 | - $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; | |
| 24 | + $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; | |
| 25 | + $is_connected = \Jetpack::is_connection_ready(); | |
| 23 | 26 | |
| 24 | - Blocks::jetpack_register_block( | |
| 25 | - __DIR__, | |
| 26 | - array( | |
| 27 | - 'api_version' => 3, | |
| 28 | - 'render_callback' => __NAMESPACE__ . '\render_block', | |
| 29 | - 'description' => $is_wpcom ? __( 'Give your readers the ability to show appreciation for your posts and easily share them with others.', 'jetpack' ) : __( 'Give your readers the ability to show appreciation for your posts.', 'jetpack' ), | |
| 30 | - ) | |
| 31 | - ); | |
| 27 | + if ( $is_wpcom || $is_connected ) { | |
| 28 | + Blocks::jetpack_register_block( | |
| 29 | + __DIR__, | |
| 30 | + array( | |
| 31 | + 'api_version' => 3, | |
| 32 | + 'render_callback' => __NAMESPACE__ . '\render_block', | |
| 33 | + 'description' => $is_wpcom ? __( 'Give your readers the ability to show appreciation for your posts and easily share them with others.', 'jetpack' ) : __( 'Give your readers the ability to show appreciation for your posts.', 'jetpack' ), | |
| 34 | + ) | |
| 35 | + ); | |
| 36 | + } | |
| 32 | 37 | } |
| 33 | 38 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 34 | 39 | |
| 35 | 40 | /** |
| @@ -34,8 +39,11 @@ | ||
| 34 | 39 | |
| 35 | 40 | /** |
| 36 | 41 | * Like block render function. |
| 37 | 42 | * |
| 43 | + * The render implementation lives in render.php and is only loaded when the | |
| 44 | + * block is actually rendered, keeping it out of the eager front-end path. | |
| 45 | + * | |
| 38 | 46 | * @param array $attr Array containing the Like block attributes. |
| 39 | 47 | * @param string $content String containing the Like block content. |
| 40 | 48 | * @param object $block Object containing the Like block data. |
| 41 | 49 | * |
| @@ -41,131 +49,7 @@ | ||
| 41 | 49 | * |
| 42 | 50 | * @return string |
| 43 | 51 | */ |
| 44 | 52 | function render_block( $attr, $content, $block ) { |
| 45 | - /* | |
| 46 | - * Enqueue necessary scripts and styles. | |
| 47 | - */ | |
| 48 | - Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); | |
| 49 | - | |
| 50 | - $html = ''; | |
| 51 | - | |
| 52 | - $uniqid = uniqid(); | |
| 53 | - $post_id = $block->context['postId']; | |
| 54 | - $title = esc_html__( 'Like or Reblog', 'jetpack' ); | |
| 55 | - | |
| 56 | - if ( ! $post_id ) { | |
| 57 | - return; | |
| 58 | - } | |
| 59 | - | |
| 60 | - /** | |
| 61 | - * Enable an alternate Likes layout. | |
| 62 | - * | |
| 63 | - * @since 12.9 | |
| 64 | - * | |
| 65 | - * @module likes | |
| 66 | - * | |
| 67 | - * @param bool $new_layout Enable the new Likes layout. False by default. | |
| 68 | - */ | |
| 69 | - $new_layout = apply_filters( 'likes_new_layout', true ) ? '&n=1' : ''; | |
| 70 | - | |
| 71 | - static $main_iframe_added = false; | |
| 72 | - | |
| 73 | - if ( ! $main_iframe_added && is_legacy_likes_disabled() ) { | |
| 74 | - if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | |
| 75 | - add_action( 'wp_footer', array( 'Jetpack_Likes', 'likes_master' ), 21 ); | |
| 76 | - } else { | |
| 77 | - require_once JETPACK__PLUGIN_DIR . 'modules/likes.php'; | |
| 78 | - add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 ); | |
| 79 | - } | |
| 80 | - wp_enqueue_script( 'jetpack_likes_queuehandler' ); | |
| 81 | - wp_enqueue_style( 'jetpack_likes' ); | |
| 82 | - $main_iframe_added = true; | |
| 83 | - } | |
| 84 | - | |
| 85 | - $show_reblog_button = $attr['showReblogButton'] ?? false; | |
| 86 | - if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | |
| 87 | - $blog_id = get_current_blog_id(); | |
| 88 | - $bloginfo = get_blog_details( (int) $blog_id ); | |
| 89 | - $domain = $bloginfo->domain; | |
| 90 | - $version = '20231201'; | |
| 91 | - $reblog_param = $show_reblog_button ? '&reblog=1' : ''; | |
| 92 | - $src = sprintf( '//widgets.wp.com/likes/index.html?ver=%1$d#blog_id=%2$d&post_id=%3$d&origin=%4$s&obj_id=%2$d-%3$d-%5$s%6$s&block=1%7$s', $version, $blog_id, $post_id, $domain, $uniqid, $new_layout, $reblog_param ); | |
| 93 | - $headline = ''; | |
| 94 | - | |
| 95 | - // provide the mapped domain when needed | |
| 96 | - if ( isset( $_SERVER['HTTP_HOST'] ) && strpos( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ), '.wordpress.com' ) === false ) { | |
| 97 | - $sanitized_host = filter_var( wp_unslash( $_SERVER['HTTP_HOST'] ), FILTER_SANITIZE_URL ); | |
| 98 | - $src .= '&domain=' . rawurlencode( $sanitized_host ); | |
| 99 | - } | |
| 100 | - } else { | |
| 101 | - $blog_id = \Jetpack_Options::get_option( 'id' ); | |
| 102 | - $url = home_url(); | |
| 103 | - $url_parts = wp_parse_url( $url ); | |
| 104 | - $domain = $url_parts['host']; | |
| 105 | - $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&block=1', JETPACK__VERSION, $blog_id, $post_id, $domain, $uniqid, $new_layout ); | |
| 106 | - $headline = sprintf( | |
| 107 | - /** This filter is already documented in modules/sharedaddy/sharing-service.php */ | |
| 108 | - apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', esc_html__( 'Like this:', 'jetpack' ), 'likes' ), | |
| 109 | - esc_html__( 'Like this:', 'jetpack' ) | |
| 110 | - ); | |
| 111 | - } | |
| 112 | - | |
| 113 | - $name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); | |
| 114 | - $wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); | |
| 115 | - | |
| 116 | - $html = "<div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='" . esc_attr( $wrapper ) . "' data-src='" . esc_attr( $src ) . "' data-name='" . esc_attr( $name ) . "' data-title='" . esc_attr( $title ) . "'>" | |
| 117 | - . $headline | |
| 118 | - . "<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>' | |
| 119 | - . "<span class='sd-text-color'></span><a class='sd-link-color'></a>" | |
| 120 | - . '</div>'; | |
| 121 | - return sprintf( | |
| 122 | - '<div class="%1$s">%2$s</div>', | |
| 123 | - esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), | |
| 124 | - $html | |
| 125 | - ); | |
| 53 | + require_once __DIR__ . '/render.php'; | |
| 54 | + return render_block_implementation( $attr, $content, $block ); | |
| 126 | 55 | } |
| 127 | - | |
| 128 | -/** | |
| 129 | - * Helper function to determine whether the Like module has been disabled | |
| 130 | - */ | |
| 131 | -function is_legacy_likes_disabled() { | |
| 132 | - $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; | |
| 133 | - $is_likes_module_inactive = ! \Jetpack::is_module_active( 'likes' ); | |
| 134 | - $is_disabled_on_wpcom = $is_wpcom && get_option( 'disabled_likes' ) && get_option( 'disabled_reblogs' ); | |
| 135 | - $is_disabled_on_non_wpcom = ! $is_wpcom && get_option( 'disabled_likes' ); | |
| 136 | - | |
| 137 | - return $is_likes_module_inactive || $is_disabled_on_wpcom || $is_disabled_on_non_wpcom; | |
| 138 | -} | |
| 139 | - | |
| 140 | -/** | |
| 141 | - * Registers and enqueues script and style for Jetpack Likes. | |
| 142 | - * | |
| 143 | - * This function conditionally registers and enqueues the Jetpack Likes | |
| 144 | - * CSS and JavaScript files based on the environment. It handles both | |
| 145 | - * WordPress.com and self-hosted WordPress environments. The function | |
| 146 | - * relies on `IS_WPCOM` to determine the environment and uses different | |
| 147 | - * paths for script and style files accordingly. | |
| 148 | - * | |
| 149 | - * @return void Early return if legacy likes are not disabled. | |
| 150 | - */ | |
| 151 | -function register_script_and_style() { | |
| 152 | - if ( ! is_legacy_likes_disabled() ) { | |
| 153 | - return; | |
| 154 | - } | |
| 155 | - | |
| 156 | - if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | |
| 157 | - $style_url = content_url( 'mu-plugins/likes/jetpack-likes.css' ); | |
| 158 | - $script_url = content_url( 'mu-plugins/likes/queuehandler.js' ); | |
| 159 | - } else { | |
| 160 | - $style_url = plugins_url( 'modules/likes/style.css', dirname( __DIR__, 2 ) ); | |
| 161 | - $script_url = Assets::get_file_url_for_environment( | |
| 162 | - '_inc/build/likes/queuehandler.min.js', | |
| 163 | - 'modules/likes/queuehandler.js' | |
| 164 | - ); | |
| 165 | - } | |
| 166 | - | |
| 167 | - wp_register_style( 'jetpack_likes', $style_url, array(), JETPACK__VERSION ); | |
| 168 | - wp_register_script( 'jetpack_likes_queuehandler', $script_url, array(), JETPACK__VERSION, true ); | |
| 169 | -} | |
| 170 | - | |
| 171 | -add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\register_script_and_style' ); | |