Completed
Push — fix/17278-label-image-compare ( d0ad97...37164e )
by
unknown
09:48
created

image-compare.php ➔ block_localization_assets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
	if ( Blocks::is_amp_request() ) {
42
		$content = preg_replace(
43
			'#<div class="juxtapose".+?</div>#s',
44
			render_amp( $attr ),
45
			$content
46
		);
47
	}
48
49
	return $content;
50
}
51
52
/**
53
 * Localisation for block
54
 *
55
 * @return void
56
 */
57
function block_localization_assets() {
58
	?>
59
	<script>
60
		var imageCompareHandle = '<?php esc_attr_e( 'Slide to compare images', 'jetpack' ); ?>';
61
	</script>
62
	<?php
63
}
64
add_action( 'wp_head', __NAMESPACE__ . '\block_localization_assets' );
65
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\block_localization_assets' );
66
67
/**
68
 * Render image compare block for AMP
69
 *
70
 * @param array $attr Array containing the image-compare block attributes.
71
 *
72
 * @return string Markup for amp-image-slider.
73
 */
74
function render_amp( $attr ) {
75
	$img_before = $attr['imageBefore'];
76
	$img_after  = $attr['imageAfter'];
77
78
	$width  = ! empty( $img_before['width'] ) ? absint( $img_before['width'] ) : 0;
79
	$height = ! empty( $img_before['height'] ) ? absint( $img_before['height'] ) : 0;
80
81
	// As fallback, give 1:1 aspect ratio.
82
	if ( ! $width || ! $height ) {
83
		$width  = 1;
84
		$height = 1;
85
	}
86
87
	return sprintf(
88
		'<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>',
89
		esc_attr( $width ),
90
		esc_attr( $height ),
91
		absint( $img_before['id'] ),
92
		esc_url( $img_before['url'] ),
93
		esc_attr( $img_before['alt'] ),
94
		absint( $img_after['id'] ),
95
		esc_url( $img_after['url'] ),
96
		esc_attr( $img_after['alt'] )
97
	);
98
}
99