render.php
131 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Like block render implementation. |
| 4 | * |
| 5 | * Loaded lazily from like.php only when the block is rendered, to keep the |
| 6 | * render body out of the eager front-end PHP/opcache footprint. |
| 7 | * |
| 8 | * @package automattic/jetpack |
| 9 | */ |
| 10 | |
| 11 | namespace Automattic\Jetpack\Extensions\Like; |
| 12 | |
| 13 | use Automattic\Jetpack\Assets; |
| 14 | use Automattic\Jetpack\Blocks; |
| 15 | use Automattic\Jetpack\Status\Request; |
| 16 | use Jetpack_Gutenberg; |
| 17 | |
| 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 19 | exit( 0 ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Like block render implementation. |
| 24 | * |
| 25 | * @param array $attr Array containing the Like block attributes. |
| 26 | * @param string $content String containing the Like block content. |
| 27 | * @param object $block Object containing the Like block data. |
| 28 | * |
| 29 | * @return string |
| 30 | */ |
| 31 | function render_block_implementation( $attr, $content, $block ) { |
| 32 | // Do not render the Like block in other context than front-end (i.e. feed, emails, API, etc.). |
| 33 | if ( ! Request::is_frontend() ) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Enqueue necessary scripts and styles. |
| 39 | */ |
| 40 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 41 | |
| 42 | $html = ''; |
| 43 | |
| 44 | $uniqid = uniqid(); |
| 45 | $post_id = $block->context['postId'] ?? null; |
| 46 | $title = esc_html__( 'Like or Reblog', 'jetpack' ); |
| 47 | |
| 48 | if ( ! $post_id ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | // make sure we have `jetpack_likes_master_iframe` defined |
| 53 | require_once JETPACK__PLUGIN_DIR . 'modules/likes/jetpack-likes-master-iframe.php'; |
| 54 | |
| 55 | if ( ! has_action( 'wp_footer', 'jetpack_likes_master_iframe' ) ) { |
| 56 | add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 ); |
| 57 | } |
| 58 | |
| 59 | $style_path = null; |
| 60 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 61 | $style_url = content_url( 'mu-plugins/likes/jetpack-likes.css' ); |
| 62 | if ( defined( 'WP_CONTENT_DIR' ) && WP_CONTENT_DIR ) { |
| 63 | $style_path = WP_CONTENT_DIR . '/mu-plugins/likes/jetpack-likes.css'; |
| 64 | } |
| 65 | $script_url = content_url( 'mu-plugins/likes/queuehandler.js' ); |
| 66 | } else { |
| 67 | $style_url = Assets::get_file_url_for_environment( |
| 68 | '_inc/build/likes/style.min.css', |
| 69 | 'modules/likes/style.css' |
| 70 | ); |
| 71 | /** This filter is documented in projects/plugins/jetpack/load-jetpack.php */ |
| 72 | $style_path = JETPACK__PLUGIN_DIR . ( apply_filters( 'jetpack_should_use_minified_assets', true ) ? '_inc/build/likes/style.min.css' : 'modules/likes/style.css' ); |
| 73 | $script_url = Assets::get_file_url_for_environment( |
| 74 | '_inc/build/likes/queuehandler.min.js', |
| 75 | 'modules/likes/queuehandler.js' |
| 76 | ); |
| 77 | } |
| 78 | wp_enqueue_script( |
| 79 | 'jetpack_likes_queuehandler', |
| 80 | $script_url, |
| 81 | array(), |
| 82 | JETPACK__VERSION, |
| 83 | array( |
| 84 | 'strategy' => 'defer', |
| 85 | 'in_footer' => true, |
| 86 | ) |
| 87 | ); |
| 88 | wp_enqueue_style( 'jetpack_likes', $style_url, array(), JETPACK__VERSION ); |
| 89 | |
| 90 | if ( $style_path ) { |
| 91 | wp_style_add_data( 'jetpack_likes', 'path', $style_path ); |
| 92 | } |
| 93 | |
| 94 | $show_reblog_button = $attr['showReblogButton'] ?? false; |
| 95 | $show_avatars = $attr['showAvatars'] ?? true; |
| 96 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 97 | $blog_id = get_current_blog_id(); |
| 98 | $bloginfo = get_blog_details( (int) $blog_id ); |
| 99 | $domain = $bloginfo->domain; |
| 100 | $reblog_param = $show_reblog_button ? '&reblog=1' : ''; |
| 101 | $show_avatars_param = $show_avatars ? '' : '&slim=1'; |
| 102 | $src = sprintf( 'https://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%7$s&block=1', rawurlencode( JETPACK__VERSION ), $blog_id, $post_id, $domain, $uniqid, $reblog_param, $show_avatars_param ); |
| 103 | |
| 104 | // provide the mapped domain when needed |
| 105 | if ( isset( $_SERVER['HTTP_HOST'] ) && strpos( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ), '.wordpress.com' ) === false ) { |
| 106 | $sanitized_host = filter_var( wp_unslash( $_SERVER['HTTP_HOST'] ), FILTER_SANITIZE_URL ); |
| 107 | $src .= '&domain=' . rawurlencode( $sanitized_host ); |
| 108 | } |
| 109 | } else { |
| 110 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
| 111 | $url = home_url(); |
| 112 | $url_parts = wp_parse_url( $url ); |
| 113 | $domain = $url_parts['host']; |
| 114 | $show_avatars_param = $show_avatars ? '' : '&slim=1'; |
| 115 | $src = sprintf( 'https://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', rawurlencode( JETPACK__VERSION ), $blog_id, $post_id, $domain, $uniqid, $show_avatars_param ); |
| 116 | } |
| 117 | |
| 118 | $name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); |
| 119 | $wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); |
| 120 | |
| 121 | $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 ) . "'>" |
| 122 | . "<div class='likes-widget-placeholder post-likes-widget-placeholder' style='height: 55px;'><span class='loading'>" . esc_html__( 'Loading…', 'jetpack' ) . '</span></div>' |
| 123 | . "<span class='sd-text-color'></span><a class='sd-link-color'></a>" |
| 124 | . '</div>'; |
| 125 | return sprintf( |
| 126 | '<div class="%1$s">%2$s</div>', |
| 127 | esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), |
| 128 | $html |
| 129 | ); |
| 130 | } |
| 131 |