2023-12-07 09:44:11 +01:00
< ? php
/**
* Like Block .
*
* @ since 12.9
*
* @ package automattic / jetpack
*/
namespace Automattic\Jetpack\Extensions\Like ;
2024-02-16 11:03:01 +01:00
use Automattic\Jetpack\Assets ;
2023-12-07 09:44:11 +01:00
use Automattic\Jetpack\Blocks ;
2025-07-27 19:58:08 +02:00
use Automattic\Jetpack\Status\Request ;
2023-12-07 09:44:11 +01:00
use Jetpack_Gutenberg ;
2025-12-12 13:13:07 +01:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ( 0 );
}
2023-12-07 09:44:11 +01:00
/**
* Registers the block for use in Gutenberg
* This is done via an action so that we can disable
* registration if we need to .
*/
function register_block () {
2025-07-27 19:58:08 +02:00
$is_wpcom = defined ( 'IS_WPCOM' ) && IS_WPCOM ;
$is_connected = \Jetpack :: is_connection_ready ();
2024-02-16 11:03:01 +01:00
2025-07-27 19:58:08 +02:00
if ( $is_wpcom || $is_connected ) {
Blocks :: jetpack_register_block (
__DIR__ ,
array (
'api_version' => 3 ,
'render_callback' => __NAMESPACE__ . '\render_block' ,
'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' ),
)
);
}
2023-12-07 09:44:11 +01:00
}
add_action ( 'init' , __NAMESPACE__ . '\register_block' );
/**
* Like block render function .
*
2024-02-16 11:03:01 +01:00
* @ param array $attr Array containing the Like block attributes .
* @ param string $content String containing the Like block content .
* @ param object $block Object containing the Like block data .
2023-12-07 09:44:11 +01:00
*
* @ return string
*/
2024-02-16 11:03:01 +01:00
function render_block ( $attr , $content , $block ) {
// Do not render the Like block in other context than front-end (i.e. feed, emails, API, etc.).
2025-07-27 19:58:08 +02:00
if ( ! Request :: is_frontend () ) {
2024-02-16 11:03:01 +01:00
return ;
}
2023-12-07 09:44:11 +01:00
/*
* Enqueue necessary scripts and styles .
*/
Jetpack_Gutenberg :: load_assets_as_required ( __DIR__ );
2024-02-16 11:03:01 +01:00
$html = '' ;
$uniqid = uniqid ();
2025-02-28 08:42:11 +01:00
$post_id = $block -> context [ 'postId' ] ? ? null ;
2024-02-16 11:03:01 +01:00
$title = esc_html__ ( 'Like or Reblog' , 'jetpack' );
if ( ! $post_id ) {
return ;
}
2025-05-07 06:48:41 +02:00
// make sure we have `jetpack_likes_master_iframe` defined
require_once JETPACK__PLUGIN_DIR . 'modules/likes/jetpack-likes-master-iframe.php' ;
if ( ! has_action ( 'wp_footer' , 'jetpack_likes_master_iframe' ) ) {
add_action ( 'wp_footer' , 'jetpack_likes_master_iframe' , 21 );
}
2024-02-16 11:03:01 +01:00
if ( defined ( 'IS_WPCOM' ) && IS_WPCOM ) {
$style_url = content_url ( 'mu-plugins/likes/jetpack-likes.css' );
$script_url = content_url ( 'mu-plugins/likes/queuehandler.js' );
} else {
$style_url = plugins_url ( 'modules/likes/style.css' , dirname ( __DIR__ , 2 ) );
$script_url = Assets :: get_file_url_for_environment (
'_inc/build/likes/queuehandler.min.js' ,
'modules/likes/queuehandler.js'
);
}
2025-12-12 13:13:07 +01:00
wp_enqueue_script (
'jetpack_likes_queuehandler' ,
$script_url ,
array (),
JETPACK__VERSION ,
array (
'strategy' => 'defer' ,
'in_footer' => true ,
)
);
2024-02-16 11:03:01 +01:00
wp_enqueue_style ( 'jetpack_likes' , $style_url , array (), JETPACK__VERSION );
$show_reblog_button = $attr [ 'showReblogButton' ] ? ? false ;
2025-02-28 08:42:11 +01:00
$show_avatars = $attr [ 'showAvatars' ] ? ? true ;
2024-02-16 11:03:01 +01:00
if ( defined ( 'IS_WPCOM' ) && IS_WPCOM ) {
2025-02-28 08:42:11 +01:00
$blog_id = get_current_blog_id ();
$bloginfo = get_blog_details ( ( int ) $blog_id );
$domain = $bloginfo -> domain ;
$reblog_param = $show_reblog_button ? '&reblog=1' : '' ;
$show_avatars_param = $show_avatars ? '' : '&slim=1' ;
2025-04-25 12:30:07 +02:00
$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 );
2024-02-16 11:03:01 +01:00
// provide the mapped domain when needed
if ( isset ( $_SERVER [ 'HTTP_HOST' ] ) && strpos ( sanitize_text_field ( wp_unslash ( $_SERVER [ 'HTTP_HOST' ] ) ), '.wordpress.com' ) === false ) {
$sanitized_host = filter_var ( wp_unslash ( $_SERVER [ 'HTTP_HOST' ] ), FILTER_SANITIZE_URL );
$src .= '&domain=' . rawurlencode ( $sanitized_host );
}
} else {
2025-02-28 08:42:11 +01:00
$blog_id = \Jetpack_Options :: get_option ( 'id' );
$url = home_url ();
$url_parts = wp_parse_url ( $url );
$domain = $url_parts [ 'host' ];
$show_avatars_param = $show_avatars ? '' : '&slim=1' ;
2025-04-25 12:30:07 +02:00
$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 );
2024-02-16 11:03:01 +01:00
}
$name = sprintf ( 'like-post-frame-%1$d-%2$d-%3$s' , $blog_id , $post_id , $uniqid );
$wrapper = sprintf ( 'like-post-wrapper-%1$d-%2$d-%3$s' , $blog_id , $post_id , $uniqid );
2023-12-07 09:44:11 +01:00
2024-02-16 11:03:01 +01:00
$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 ) . " '> "
2025-04-25 12:30:07 +02:00
. " <div class='likes-widget-placeholder post-likes-widget-placeholder' style='height: 55px;'><span class='loading'> " . esc_html__ ( 'Loading…' , 'jetpack' ) . '</span></div>'
2024-02-16 11:03:01 +01:00
. " <span class='sd-text-color'></span><a class='sd-link-color'></a> "
. '</div>' ;
2023-12-07 09:44:11 +01:00
return sprintf (
'<div class="%1$s">%2$s</div>' ,
esc_attr ( Blocks :: classes ( Blocks :: get_block_feature ( __DIR__ ), $attr ) ),
2024-02-16 11:03:01 +01:00
$html
2023-12-07 09:44:11 +01:00
);
}