| @@ -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,117 +49,7 @@ | ||
| 41 | 49 | * |
| 42 | 50 | * @return string |
| 43 | 51 | */ |
| 44 | 52 | function render_block( $attr, $content, $block ) { |
| 45 | - // Do not render the Like block in other context than front-end (i.e. feed, emails, API, etc.). | |
| 46 | - if ( ! jetpack_is_frontend() ) { | |
| 47 | - return; | |
| 48 | - } | |
| 49 | - | |
| 50 | - /* | |
| 51 | - * Enqueue necessary scripts and styles. | |
| 52 | - */ | |
| 53 | - Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); | |
| 54 | - | |
| 55 | - $html = ''; | |
| 56 | - | |
| 57 | - $uniqid = uniqid(); | |
| 58 | - $post_id = $block->context['postId'] ?? null; | |
| 59 | - $title = esc_html__( 'Like or Reblog', 'jetpack' ); | |
| 60 | - | |
| 61 | - if ( ! $post_id ) { | |
| 62 | - return; | |
| 63 | - } | |
| 64 | - | |
| 65 | - /** | |
| 66 | - * Enable an alternate Likes layout. | |
| 67 | - * | |
| 68 | - * @since 12.9 | |
| 69 | - * | |
| 70 | - * @module likes | |
| 71 | - * | |
| 72 | - * @param bool $new_layout Enable the new Likes layout. False by default. | |
| 73 | - */ | |
| 74 | - $new_layout = apply_filters( 'likes_new_layout', true ) ? '&n=1' : ''; | |
| 75 | - | |
| 76 | - add_action( 'wp_footer', __NAMESPACE__ . '\render_iframe', 25 ); | |
| 77 | - | |
| 78 | - if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | |
| 79 | - $style_url = content_url( 'mu-plugins/likes/jetpack-likes.css' ); | |
| 80 | - $script_url = content_url( 'mu-plugins/likes/queuehandler.js' ); | |
| 81 | - } else { | |
| 82 | - require_once JETPACK__PLUGIN_DIR . 'modules/likes.php'; | |
| 83 | - $style_url = plugins_url( 'modules/likes/style.css', dirname( __DIR__, 2 ) ); | |
| 84 | - $script_url = Assets::get_file_url_for_environment( | |
| 85 | - '_inc/build/likes/queuehandler.min.js', | |
| 86 | - 'modules/likes/queuehandler.js' | |
| 87 | - ); | |
| 88 | - } | |
| 89 | - wp_enqueue_script( 'jetpack_likes_queuehandler', $script_url, array(), JETPACK__VERSION, true ); | |
| 90 | - wp_enqueue_style( 'jetpack_likes', $style_url, array(), JETPACK__VERSION ); | |
| 91 | - | |
| 92 | - $show_reblog_button = $attr['showReblogButton'] ?? false; | |
| 93 | - if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | |
| 94 | - $blog_id = get_current_blog_id(); | |
| 95 | - $bloginfo = get_blog_details( (int) $blog_id ); | |
| 96 | - $domain = $bloginfo->domain; | |
| 97 | - $reblog_param = $show_reblog_button ? '&reblog=1' : ''; | |
| 98 | - $src = sprintf( '//widgets.wp.com/likes/index.html?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%7$s', rawurlencode( JETPACK__VERSION ), $blog_id, $post_id, $domain, $uniqid, $new_layout, $reblog_param ); | |
| 99 | - | |
| 100 | - // provide the mapped domain when needed | |
| 101 | - if ( isset( $_SERVER['HTTP_HOST'] ) && strpos( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ), '.wordpress.com' ) === false ) { | |
| 102 | - $sanitized_host = filter_var( wp_unslash( $_SERVER['HTTP_HOST'] ), FILTER_SANITIZE_URL ); | |
| 103 | - $src .= '&domain=' . rawurlencode( $sanitized_host ); | |
| 104 | - } | |
| 105 | - } else { | |
| 106 | - $blog_id = \Jetpack_Options::get_option( 'id' ); | |
| 107 | - $url = home_url(); | |
| 108 | - $url_parts = wp_parse_url( $url ); | |
| 109 | - $domain = $url_parts['host']; | |
| 110 | - $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', rawurlencode( JETPACK__VERSION ), $blog_id, $post_id, $domain, $uniqid, $new_layout ); | |
| 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 | - . "<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>' | |
| 118 | - . "<span class='sd-text-color'></span><a class='sd-link-color'></a>" | |
| 119 | - . '</div>'; | |
| 120 | - return sprintf( | |
| 121 | - '<div class="%1$s">%2$s</div>', | |
| 122 | - esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), | |
| 123 | - $html | |
| 124 | - ); | |
| 125 | -} | |
| 126 | - | |
| 127 | -/** | |
| 128 | - * Helper function to determine whether the Like module has been disabled | |
| 129 | - */ | |
| 130 | -function is_legacy_likes_disabled() { | |
| 131 | - $settings = new \Jetpack_Likes_Settings(); | |
| 132 | - | |
| 133 | - $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; | |
| 134 | - $is_likes_module_inactive = ! \Jetpack::is_module_active( 'likes' ); | |
| 135 | - $is_disabled_on_wpcom = $is_wpcom && get_option( 'disabled_likes' ) && get_option( 'disabled_reblogs' ); | |
| 136 | - $is_disabled_on_non_wpcom = ! $is_wpcom && get_option( 'disabled_likes' ); | |
| 137 | - return $is_likes_module_inactive || $is_disabled_on_wpcom || $is_disabled_on_non_wpcom || ! $settings->is_likes_module_enabled(); | |
| 138 | -} | |
| 139 | - | |
| 140 | -/** | |
| 141 | - * Renders the iframe and enqueues the necessary scripts. | |
| 142 | - */ | |
| 143 | -function render_iframe() { | |
| 144 | - static $main_iframe_added = false; | |
| 145 | - | |
| 146 | - if ( ! $main_iframe_added ) { | |
| 147 | - if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | |
| 148 | - // @phan-suppress-next-line PhanUndeclaredStaticMethod -- Can't do a stub for this one since Jetpack has its own class with the same name. | |
| 149 | - \Jetpack_Likes::likes_master(); | |
| 150 | - } else { | |
| 151 | - require_once JETPACK__PLUGIN_DIR . 'modules/likes.php'; | |
| 152 | - jetpack_likes_master_iframe(); | |
| 153 | - } | |
| 154 | - | |
| 155 | - $main_iframe_added = true; | |
| 156 | - } | |
| 53 | + require_once __DIR__ . '/render.php'; | |
| 54 | + return render_block_implementation( $attr, $content, $block ); | |
| 157 | 55 | } |