← All changes
|
extensions/blocks/image-compare/image-compare.php
+9
-51
13.7.2
→
16.3-a.1
View file →
| @@ -9,10 +9,13 @@ | ||
| 9 | 9 | |
| 10 | 10 | namespace Automattic\Jetpack\Extensions\ImageCompare; |
| 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,8 +30,11 @@ | ||
| 27 | 30 | |
| 28 | 31 | /** |
| 29 | 32 | * Image Compare block registration/dependency declaration. |
| 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 Array containing the image-compare block attributes. |
| 32 | 38 | * @param string $content String containing the image-compare block content. |
| 33 | 39 | * |
| 34 | 40 | * @return string |
| @@ -33,55 +39,7 @@ | ||
| 33 | 39 | * |
| 34 | 40 | * @return string |
| 35 | 41 | */ |
| 36 | 42 | function load_assets( $attr, $content ) { |
| 37 | - Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); | |
| 38 | - wp_localize_script( | |
| 39 | - 'jetpack-block-' . sanitize_title_with_dashes( Blocks::get_block_feature( __DIR__ ) ), | |
| 40 | - 'imageCompareHandle', | |
| 41 | - array( | |
| 42 | - 'msg' => __( 'Slide to compare images', 'jetpack' ), | |
| 43 | - ) | |
| 44 | - ); | |
| 45 | - if ( Blocks::is_amp_request() ) { | |
| 46 | - $content = preg_replace( | |
| 47 | - '#<div class="juxtapose".+?</div>#s', | |
| 48 | - render_amp( $attr ), | |
| 49 | - $content | |
| 50 | - ); | |
| 51 | - } | |
| 52 | - | |
| 53 | - return $content; | |
| 54 | -} | |
| 55 | - | |
| 56 | -/** | |
| 57 | - * Render image compare block for AMP | |
| 58 | - * | |
| 59 | - * @param array $attr Array containing the image-compare block attributes. | |
| 60 | - * | |
| 61 | - * @return string Markup for amp-image-slider. | |
| 62 | - */ | |
| 63 | -function render_amp( $attr ) { | |
| 64 | - $img_before = $attr['imageBefore']; | |
| 65 | - $img_after = $attr['imageAfter']; | |
| 66 | - | |
| 67 | - $width = ! empty( $img_before['width'] ) ? absint( $img_before['width'] ) : 0; | |
| 68 | - $height = ! empty( $img_before['height'] ) ? absint( $img_before['height'] ) : 0; | |
| 69 | - | |
| 70 | - // As fallback, give 1:1 aspect ratio. | |
| 71 | - if ( ! $width || ! $height ) { | |
| 72 | - $width = 1; | |
| 73 | - $height = 1; | |
| 74 | - } | |
| 75 | - | |
| 76 | - return sprintf( | |
| 77 | - '<amp-image-slider layout="responsive" width="%1$s" height="%2$s"> <amp-img id="%3$d" src="%4$s" alt="%5$s" layout="fill"></amp-img> <amp-img id="%6$d" src="%7$s" alt="%8$s" layout="fill"></amp-img></amp-image-slider>', | |
| 78 | - esc_attr( $width ), | |
| 79 | - esc_attr( $height ), | |
| 80 | - absint( $img_before['id'] ), | |
| 81 | - esc_url( $img_before['url'] ), | |
| 82 | - esc_attr( $img_before['alt'] ), | |
| 83 | - absint( $img_after['id'] ), | |
| 84 | - esc_url( $img_after['url'] ), | |
| 85 | - esc_attr( $img_after['alt'] ) | |
| 86 | - ); | |
| 43 | + require_once __DIR__ . '/render.php'; | |
| 44 | + return render_implementation( $attr, $content ); | |
| 87 | 45 | } |