| @@ -9,12 +9,12 @@ | ||
| 9 | 9 | |
| 10 | 10 | namespace Automattic\Jetpack\Extensions\Gif; |
| 11 | 11 | |
| 12 | 12 | use Automattic\Jetpack\Blocks; |
| 13 | -use Jetpack_Gutenberg; | |
| 14 | 13 | |
| 15 | -const FEATURE_NAME = 'gif'; | |
| 16 | -const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Registers the block for use in Gutenberg |
| 20 | 20 | * This is done via an action so that we can disable |
| @@ -21,9 +21,9 @@ | ||
| 21 | 21 | * registration if we need to. |
| 22 | 22 | */ |
| 23 | 23 | function register_block() { |
| 24 | 24 | Blocks::jetpack_register_block( |
| 25 | - BLOCK_NAME, | |
| 25 | + __DIR__, | |
| 26 | 26 | array( 'render_callback' => __NAMESPACE__ . '\render_block' ) |
| 27 | 27 | ); |
| 28 | 28 | } |
| 29 | 29 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| @@ -30,52 +30,15 @@ | ||
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | 32 | * Gif block registration/dependency declaration. |
| 33 | 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 | + * | |
| 34 | 37 | * @param array $attr - Array containing the gif block attributes. |
| 35 | 38 | * |
| 36 | 39 | * @return string |
| 37 | 40 | */ |
| 38 | 41 | function render_block( $attr ) { |
| 39 | - $padding_top = isset( $attr['paddingTop'] ) ? $attr['paddingTop'] : 0; | |
| 40 | - $style = 'padding-top:' . $padding_top; | |
| 41 | - $giphy_url = isset( $attr['giphyUrl'] ) | |
| 42 | - ? Jetpack_Gutenberg::validate_block_embed_url( $attr['giphyUrl'], array( 'giphy.com' ) ) | |
| 43 | - : null; | |
| 44 | - $search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : ''; | |
| 45 | - $caption = isset( $attr['caption'] ) ? $attr['caption'] : null; | |
| 46 | - | |
| 47 | - if ( ! $giphy_url ) { | |
| 48 | - return null; | |
| 49 | - } | |
| 50 | - | |
| 51 | - $classes = Blocks::classes( FEATURE_NAME, $attr ); | |
| 52 | - | |
| 53 | - $placeholder = sprintf( '<a href="%s">%s</a>', esc_url( $giphy_url ), esc_attr( $search_text ) ); | |
| 54 | - | |
| 55 | - ob_start(); | |
| 56 | - ?> | |
| 57 | - <div class="<?php echo esc_attr( $classes ); ?>"> | |
| 58 | - <figure> | |
| 59 | - <?php if ( Blocks::is_amp_request() ) : ?> | |
| 60 | - <amp-iframe src="<?php echo esc_url( $giphy_url ); ?>" width="100" height="<?php echo absint( $padding_top ); ?>" sandbox="allow-scripts allow-same-origin" layout="responsive"> | |
| 61 | - <div placeholder> | |
| 62 | - <?php echo wp_kses_post( $placeholder ); ?> | |
| 63 | - </div> | |
| 64 | - </amp-iframe> | |
| 65 | - <?php else : ?> | |
| 66 | - <div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>"> | |
| 67 | - <iframe src="<?php echo esc_url( $giphy_url ); ?>" title="<?php echo esc_attr( $search_text ); ?>"></iframe> | |
| 68 | - </div> | |
| 69 | - <?php endif; ?> | |
| 70 | - <?php if ( $caption ) : ?> | |
| 71 | - <figcaption class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption> | |
| 72 | - <?php endif; ?> | |
| 73 | - </figure> | |
| 74 | - </div> | |
| 75 | - <?php | |
| 76 | - $html = ob_get_clean(); | |
| 77 | - | |
| 78 | - Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); | |
| 79 | - | |
| 80 | - return $html; | |
| 42 | + require_once __DIR__ . '/render.php'; | |
| 43 | + return render_implementation( $attr ); | |
| 81 | 44 | } |