| 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 |
/* |
| 53 |
* Never render the Like block on password-protected posts, even for viewers |
| 54 |
* who can read the post (owners, admins, or after unlocking). We gate on the |
| 55 |
* post having a password rather than post_password_required(), which is |
| 56 |
* request-scoped and would let the Like button leak once a viewer unlocked |
| 57 |
* the post. |
| 58 |
*/ |
| 59 |
$post = get_post( $post_id ); |
| 60 |
if ( $post instanceof \WP_Post && ! empty( $post->post_password ) ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
// make sure we have `jetpack_likes_master_iframe` defined |
| 65 |
require_once JETPACK__PLUGIN_DIR . 'modules/likes/jetpack-likes-master-iframe.php'; |
| 66 |
|
| 67 |
if ( ! has_action( 'wp_footer', 'jetpack_likes_master_iframe' ) ) { |
| 68 |
add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 ); |
| 69 |
} |
| 70 |
|
| 71 |
$style_path = null; |
| 72 |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 73 |
$style_url = content_url( 'mu-plugins/likes/jetpack-likes.css' ); |
| 74 |
if ( defined( 'WP_CONTENT_DIR' ) && WP_CONTENT_DIR ) { |
| 75 |
$style_path = WP_CONTENT_DIR . '/mu-plugins/likes/jetpack-likes.css'; |
| 76 |
} |
| 77 |
$script_url = content_url( 'mu-plugins/likes/queuehandler.js' ); |
| 78 |
} else { |
| 79 |
$style_url = Assets::get_file_url_for_environment( |
| 80 |
'_inc/build/likes/style.min.css', |
| 81 |
'modules/likes/style.css' |
| 82 |
); |
| 83 |
/** This filter is documented in projects/plugins/jetpack/load-jetpack.php */ |
| 84 |
$style_path = JETPACK__PLUGIN_DIR . ( apply_filters( 'jetpack_should_use_minified_assets', true ) ? '_inc/build/likes/style.min.css' : 'modules/likes/style.css' ); |
| 85 |
$script_url = Assets::get_file_url_for_environment( |
| 86 |
'_inc/build/likes/queuehandler.min.js', |
| 87 |
'modules/likes/queuehandler.js' |
| 88 |
); |
| 89 |
} |
| 90 |
wp_enqueue_script( |
| 91 |
'jetpack_likes_queuehandler', |
| 92 |
$script_url, |
| 93 |
array(), |
| 94 |
JETPACK__VERSION, |
| 95 |
array( |
| 96 |
'strategy' => 'defer', |
| 97 |
'in_footer' => true, |
| 98 |
) |
| 99 |
); |
| 100 |
wp_enqueue_style( 'jetpack_likes', $style_url, array(), JETPACK__VERSION ); |
| 101 |
|
| 102 |
if ( $style_path ) { |
| 103 |
wp_style_add_data( 'jetpack_likes', 'path', $style_path ); |
| 104 |
} |
| 105 |
|
| 106 |
$show_reblog_button = $attr['showReblogButton'] ?? false; |
| 107 |
$show_avatars = $attr['showAvatars'] ?? true; |
| 108 |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 109 |
$blog_id = get_current_blog_id(); |
| 110 |
$bloginfo = get_blog_details( (int) $blog_id ); |
| 111 |
$domain = $bloginfo->domain; |
| 112 |
$reblog_param = $show_reblog_button ? '&reblog=1' : ''; |
| 113 |
$show_avatars_param = $show_avatars ? '' : '&slim=1'; |
| 114 |
$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 ); |
| 115 |
|
| 116 |
// provide the mapped domain when needed |
| 117 |
if ( isset( $_SERVER['HTTP_HOST'] ) && strpos( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ), '.wordpress.com' ) === false ) { |
| 118 |
$sanitized_host = filter_var( wp_unslash( $_SERVER['HTTP_HOST'] ), FILTER_SANITIZE_URL ); |
| 119 |
$src .= '&domain=' . rawurlencode( $sanitized_host ); |
| 120 |
} |
| 121 |
} else { |
| 122 |
$blog_id = \Jetpack_Options::get_option( 'id' ); |
| 123 |
$url = home_url(); |
| 124 |
$url_parts = wp_parse_url( $url ); |
| 125 |
$domain = $url_parts['host']; |
| 126 |
$show_avatars_param = $show_avatars ? '' : '&slim=1'; |
| 127 |
$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 ); |
| 128 |
} |
| 129 |
|
| 130 |
$name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); |
| 131 |
$wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); |
| 132 |
|
| 133 |
$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 ) . "'>" |
| 134 |
. "<div class='likes-widget-placeholder post-likes-widget-placeholder' style='height: 55px;'><span class='loading'>" . esc_html__( 'Loading…', 'jetpack' ) . '</span></div>' |
| 135 |
. "<span class='sd-text-color'></span><a class='sd-link-color'></a>" |
| 136 |
. '</div>'; |
| 137 |
return sprintf( |
| 138 |
'<div class="%1$s">%2$s</div>', |
| 139 |
esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), |
| 140 |
$html |
| 141 |
); |
| 142 |
} |
| 143 |
|