Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
Automattic\Jetpack\Extensions\Tock\register_block
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Extensions\Tock\render_block
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Tock Block.
4 *
5 * @since 12.3
6 *
7 * @package automattic/jetpack
8 */
9
10namespace Automattic\Jetpack\Extensions\Tock;
11
12use Automattic\Jetpack\Blocks;
13use Jetpack_Gutenberg;
14
15if ( ! defined( 'ABSPATH' ) ) {
16    exit( 0 );
17}
18
19/**
20 * Registers the block for use in Gutenberg
21 * This is done via an action so that we can disable
22 * registration if we need to.
23 */
24function register_block() {
25    Blocks::jetpack_register_block(
26        __DIR__,
27        array( 'render_callback' => __NAMESPACE__ . '\render_block' )
28    );
29}
30add_action( 'init', __NAMESPACE__ . '\register_block' );
31
32/**
33 * Render the widget and associated JS
34 *
35 * @param array $attr    The block attributes.
36 */
37function render_block( $attr ) {
38    $content = '<div id="Tock_widget_container" data-tock-display-mode="Button" data-tock-color-mode="Blue" data-tock-locale="en-us" data-tock-timezone="America/New_York"></div>';
39    if ( empty( $attr['url'] ) ) {
40        if ( ! current_user_can( 'edit_posts' ) ) {
41            return;
42        }
43
44        return Jetpack_Gutenberg::notice(
45            __( 'The block will not be shown to your site visitors until a Tock business name is set.', 'jetpack' ),
46            'warning',
47            Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr )
48        );
49    }
50
51    wp_enqueue_script( 'tock-widget', 'https://www.exploretock.com/tock.js', array(), JETPACK__VERSION, true );
52
53    // Add CSS to hide direct link.
54    Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
55
56    wp_add_inline_script(
57        'tock-widget',
58        "!function(t,o){if(!t.tock){var e=t.tock=function(){e.callMethod?
59          e.callMethod.apply(e,arguments):e.queue.push(arguments)};t._tock||(t._tock=e),
60          e.push=e,e.loaded=!0,e.version='1.0',e.queue=[];}}(window,document);
61            tock('init', " . wp_json_encode( $attr['url'], JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ');',
62        'before'
63    );
64    return sprintf(
65        '<div class="%1$s">%2$s</div>',
66        esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ),
67        $content
68    );
69}