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