| @@ -9,10 +9,13 @@ | ||
| 9 | 9 | |
| 10 | 10 | namespace Automattic\Jetpack\Extensions\Tock; |
| 11 | 11 | |
| 12 | 12 | use Automattic\Jetpack\Blocks; |
| 13 | -use Jetpack_Gutenberg; | |
| 14 | 13 | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | + | |
| 15 | 18 | /** |
| 16 | 19 | * Registers the block for use in Gutenberg |
| 17 | 20 | * This is done via an action so that we can disable |
| 18 | 21 | * registration if we need to. |
| @@ -27,39 +30,13 @@ | ||
| 27 | 30 | |
| 28 | 31 | /** |
| 29 | 32 | * Render the widget and associated JS |
| 30 | 33 | * |
| 34 | + * The render implementation lives in render.php and is only loaded when the | |
| 35 | + * block is actually rendered, keeping it out of the eager front-end path. | |
| 36 | + * | |
| 31 | 37 | * @param array $attr The block attributes. |
| 32 | 38 | */ |
| 33 | 39 | function render_block( $attr ) { |
| 34 | - $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>'; | |
| 35 | - if ( empty( $attr['url'] ) ) { | |
| 36 | - if ( ! current_user_can( 'edit_posts' ) ) { | |
| 37 | - return; | |
| 38 | - } | |
| 39 | - | |
| 40 | - return Jetpack_Gutenberg::notice( | |
| 41 | - __( 'The block will not be shown to your site visitors until a Tock business name is set.', 'jetpack' ), | |
| 42 | - 'warning', | |
| 43 | - Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) | |
| 44 | - ); | |
| 45 | - } | |
| 46 | - | |
| 47 | - wp_enqueue_script( 'tock-widget', 'https://www.exploretock.com/tock.js', array(), JETPACK__VERSION, true ); | |
| 48 | - | |
| 49 | - // Add CSS to hide direct link. | |
| 50 | - Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); | |
| 51 | - | |
| 52 | - wp_add_inline_script( | |
| 53 | - 'tock-widget', | |
| 54 | - "!function(t,o){if(!t.tock){var e=t.tock=function(){e.callMethod? | |
| 55 | - e.callMethod.apply(e,arguments):e.queue.push(arguments)};t._tock||(t._tock=e), | |
| 56 | - e.push=e,e.loaded=!0,e.version='1.0',e.queue=[];}}(window,document); | |
| 57 | - tock('init', '" . esc_js( $attr['url'] ) . "');", | |
| 58 | - 'before' | |
| 59 | - ); | |
| 60 | - return sprintf( | |
| 61 | - '<div class="%1$s">%2$s</div>', | |
| 62 | - esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), | |
| 63 | - $content | |
| 64 | - ); | |
| 40 | + require_once __DIR__ . '/render.php'; | |
| 41 | + return render_block_implementation( $attr ); | |
| 65 | 42 | } |