image-compare.php
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Image Compare Block.
*
* @since 8.6
*
* @package automattic/jetpack
*/
namespace Automattic\Jetpack\Extensions\ImageCompare;
use Automattic\Jetpack\Blocks;
use Jetpack_Gutenberg;
const FEATURE_NAME = 'image-compare';
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
/**
* Registers the block for use in Gutenberg
* This is done via an action so that we can disable
* registration if we need to.
*/
function register_block() {
Blocks::jetpack_register_block(
BLOCK_NAME,
array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
);
}
add_action( 'init', __NAMESPACE__ . '\register_block' );
/**
* Image Compare block registration/dependency declaration.
*
* @param array $attr Array containing the image-compare block attributes.
* @param string $content String containing the image-compare block content.
*
* @return string
*/
function load_assets( $attr, $content ) {
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
wp_localize_script(
'jetpack-block-' . sanitize_title_with_dashes( FEATURE_NAME ),
'imageCompareHandle',
array(
'msg' => __( 'Slide to compare images', 'jetpack' ),
)
);
if ( Blocks::is_amp_request() ) {
$content = preg_replace(
'#<div class="juxtapose".+?</div>#s',
render_amp( $attr ),
$content
);
}
return $content;
}
/**
* Render image compare block for AMP
*
* @param array $attr Array containing the image-compare block attributes.
*
* @return string Markup for amp-image-slider.
*/
function render_amp( $attr ) {
$img_before = $attr['imageBefore'];
$img_after = $attr['imageAfter'];
$width = ! empty( $img_before['width'] ) ? absint( $img_before['width'] ) : 0;
$height = ! empty( $img_before['height'] ) ? absint( $img_before['height'] ) : 0;
// As fallback, give 1:1 aspect ratio.
if ( ! $width || ! $height ) {
$width = 1;
$height = 1;
}
return sprintf(
'<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>',
esc_attr( $width ),
esc_attr( $height ),
absint( $img_before['id'] ),
esc_url( $img_before['url'] ),
esc_attr( $img_before['alt'] ),
absint( $img_after['id'] ),
esc_url( $img_after['url'] ),
esc_attr( $img_after['alt'] )
);
}