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