| @@ -1,0 +1,57 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Tock block render implementation. | |
| 4 | + * | |
| 5 | + * Loaded lazily from tock.php only when the block is rendered, to keep | |
| 6 | + * the render body out of the eager front-end PHP/opcache footprint. | |
| 7 | + * | |
| 8 | + * @package automattic/jetpack | |
| 9 | + */ | |
| 10 | + | |
| 11 | +namespace Automattic\Jetpack\Extensions\Tock; | |
| 12 | + | |
| 13 | +use Automattic\Jetpack\Blocks; | |
| 14 | +use Jetpack_Gutenberg; | |
| 15 | + | |
| 16 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 17 | + exit( 0 ); | |
| 18 | +} | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * Render the widget and associated JS | |
| 22 | + * | |
| 23 | + * @param array $attr The block attributes. | |
| 24 | + */ | |
| 25 | +function render_block_implementation( $attr ) { | |
| 26 | + $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>'; | |
| 27 | + if ( empty( $attr['url'] ) ) { | |
| 28 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 29 | + return; | |
| 30 | + } | |
| 31 | + | |
| 32 | + return Jetpack_Gutenberg::notice( | |
| 33 | + __( 'The block will not be shown to your site visitors until a Tock business name is set.', 'jetpack' ), | |
| 34 | + 'warning', | |
| 35 | + Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) | |
| 36 | + ); | |
| 37 | + } | |
| 38 | + | |
| 39 | + wp_enqueue_script( 'tock-widget', 'https://www.exploretock.com/tock.js', array(), JETPACK__VERSION, true ); | |
| 40 | + | |
| 41 | + // Add CSS to hide direct link. | |
| 42 | + Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); | |
| 43 | + | |
| 44 | + wp_add_inline_script( | |
| 45 | + 'tock-widget', | |
| 46 | + "!function(t,o){if(!t.tock){var e=t.tock=function(){e.callMethod? | |
| 47 | + e.callMethod.apply(e,arguments):e.queue.push(arguments)};t._tock||(t._tock=e), | |
| 48 | + e.push=e,e.loaded=!0,e.version='1.0',e.queue=[];}}(window,document); | |
| 49 | + tock('init', " . wp_json_encode( $attr['url'], JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ');', | |
| 50 | + 'before' | |
| 51 | + ); | |
| 52 | + return sprintf( | |
| 53 | + '<div class="%1$s">%2$s</div>', | |
| 54 | + esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), | |
| 55 | + $content | |
| 56 | + ); | |
| 57 | +} | |