Completed
Push — add/amp-slideshow ( 63815f...3e8b99 )
by
unknown
06:04
created

slideshow.php ➔ jetpack_slideshow_block_amp_carousel()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 2
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
1
<?php
2
/**
3
 * Slideshow Block.
4
 *
5
 * @since 7.1.0
6
 *
7
 * @package Jetpack
8
 */
9
10
jetpack_register_block(
11
	'jetpack/slideshow',
12
	array(
13
		'render_callback' => 'jetpack_slideshow_block_load_assets',
14
	)
15
);
16
17
/**
18
 * Slideshow block registration/dependency declaration.
19
 *
20
 * @param array  $attr    Array containing the slideshow block attributes.
21
 * @param string $content String containing the slideshow block content.
22
 *
23
 * @return string
24
 */
25
function jetpack_slideshow_block_load_assets( $attr, $content ) {
26
	Jetpack_Gutenberg::load_assets_as_required( 'slideshow' );
27
	if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
28
		return jetpack_slideshow_block_render_amp( $attr );
29
	}
30
	return $content;
31
}
32
33
/**
34
 * Render slideshow block for AMP
35
 *
36
 * @param array $attr Array containing the slideshow block attributes.
37
 *
38
 * @return string
39
 */
40
function jetpack_slideshow_block_render_amp( $attr ) {
41
	static $wp_block_jetpack_slideshow_id = 0;
42
	$wp_block_jetpack_slideshow_id++;
43
44
	$ids      = empty( $attr['ids'] ) ? array() : $attr['ids'];
45
	$autoplay = empty( $attr['autoplay'] ) ? false : $attr['autoplay'];
46
	$classes  = array(
47
		'wp-block-jetpack-slideshow',
48
		'wp-amp-block',
49
		'align' . isset( $attr['align'] ) ? $attr['align'] : 'center',
50
		$autoplay ? 'wp-block-jetpack-slideshow__autoplay' : null,
51
		$autoplay ? 'wp-block-jetpack-slideshow__autoplay-playing' : null,
52
	);
53
54
	return sprintf(
55
		'<div class="%s" id="wp-block-jetpack-slideshow__%s"><div class="wp-block-jetpack-slideshow_container swiper-container">%s%s<div class="wp-block-jetpack-slideshow_pagination swiper-pagination swiper-pagination-bullets amp-pagination">%s</div></div></div>',
56
		esc_attr( implode( $classes, ' ' ) ),
57
		esc_attr( $wp_block_jetpack_slideshow_id ),
58
		jetpack_slideshow_block_amp_carousel( $attr, $wp_block_jetpack_slideshow_id ),
59
		$autoplay ? jetpack_slideshow_block_autoplay_ui( $wp_block_jetpack_slideshow_id ) : '',
60
		implode( '', jetpack_slideshow_block_bullets( $ids, $wp_block_jetpack_slideshow_id ) )
61
	);
62
}
63
64
/**
65
 * Generate amp-carousel markup
66
 *
67
 * @param array $attr Array of block attributes.
68
 * @param int   $block_ordinal The ordinal number of the block, used in unique ID.
69
 *
70
 * @return string amp-carousel markup.
71
 */
72
function jetpack_slideshow_block_amp_carousel( $attr, $block_ordinal ) {
73
	$ids         = empty( $attr['ids'] ) ? array() : $attr['ids'];
74
	$first_image = wp_get_attachment_metadata( $ids[0] );
75
	$delay       = empty( $attr['delay'] ) ? 3 : intval( $attr['delay'] );
76
	$autoplay    = empty( $attr['autoplay'] ) ? false : $attr['autoplay'];
77
	return sprintf(
78
		'<amp-carousel width="%s" height="%s" layout="responsive" type="slides" data-next-button-aria-label="%s" data-prev-button-aria-label="%s" controls loop %s id="wp-block-jetpack-slideshow__amp_carousel_%s">%s</amp-carousel>',
79
		esc_attr( $first_image['width'] ),
80
		esc_attr( $first_image['height'] ),
81
		esc_attr__( 'Next Slide', 'jetpack' ),
82
		esc_attr__( 'Previous Slide', 'jetpack' ),
83
		$autoplay ? 'autoplay delay=' . esc_attr( $delay * 1000 ) : '',
84
		esc_attr( $block_ordinal ),
85
		implode( '', jetpack_slideshow_block_slides( $ids, $first_image['width'], $first_image['height'] ) )
86
	);
87
}
88
89
/**
90
 * Generate array of slides markup
91
 *
92
 * @param array $ids Array of image ids.
93
 * @param int   $width Width of the container.
94
 * @param int   $height Height of the container.
95
 *
96
 * @return array Array of slides markup.
97
 */
98
function jetpack_slideshow_block_slides( $ids = [], $width = 400, $height = 300 ) {
99
	return array_map(
100
		function( $id ) use ( $width, $height ) {
101
			$caption    = wp_get_attachment_caption( $id );
102
			$figcaption = $caption ? sprintf(
103
				'<figcaption class="wp-block-jetpack-slideshow_caption gallery-caption">%s</figcaption>',
104
				wp_kses_post( $caption )
105
			) : '';
106
			$image      = wp_get_attachment_image(
107
				$id,
108
				[ $width, $height ],
109
				false,
110
				[
111
					'class'      => 'wp-block-jetpack-slideshow_image',
112
					'object-fit' => 'contain',
113
				]
114
			);
115
			return sprintf(
116
				'<div class="wp-block-jetpack-slideshow_slide"><figure>%s%s</figure></div>',
117
				$image,
118
				$figcaption
119
			);
120
		},
121
		$ids
122
	);
123
}
124
125
/**
126
 * Generate array of bullets markup
127
 *
128
 * @param array $ids Array of image ids.
129
 * @param int   $block_ordinal The ordinal number of the block, used in unique ID.
130
 *
131
 * @return array Array of bullets markup.
132
 */
133
function jetpack_slideshow_block_bullets( $ids = [], $block_ordinal = 0 ) {
134
	$amp_carousel_id = sprintf(
135
		'wp-block-jetpack-slideshow__amp_carousel_%s',
136
		intval( $block_ordinal )
137
	);
138
	return array_map(
139
		function( $index ) use ( $amp_carousel_id ) {
140
			return sprintf(
141
				'<button class="swiper-pagination-bullet" tabindex="0" role="button" aria-label="Go to slide %s" on="tap:%s.goToSlide(index=%s)"></button>',
142
				( $index + 1 ),
143
				esc_attr( $amp_carousel_id ),
144
				$index
145
			);
146
		},
147
		array_keys( $ids )
148
	);
149
}
150
151
/**
152
 * Generate autoplay play/pause UI.
153
 *
154
 * @param int $block_ordinal The ordinal number of the block, used in unique ID.
155
 *
156
 * @return string Autoplay UI markup.
157
 */
158
function jetpack_slideshow_block_autoplay_ui( $block_ordinal = 0 ) {
159
	$block_id        = sprintf(
160
		'wp-block-jetpack-slideshow__%s',
161
		intval( $block_ordinal )
162
	);
163
	$amp_carousel_id = sprintf(
164
		'wp-block-jetpack-slideshow__amp_carousel_%s',
165
		intval( $block_ordinal )
166
	);
167
	$autoplay_pause  = sprintf(
168
		'<a aria-label="%s" class="wp-block-jetpack-slideshow_button-pause" role="button" on="tap:%s.toggleAutoplay(toggleOn=false),%s.toggleClass(class=wp-block-jetpack-slideshow__autoplay-playing,force=false)"></a>',
169
		esc_attr__( 'Pause Slideshow', 'jetpack' ),
170
		esc_attr( $amp_carousel_id ),
171
		esc_attr( $block_id )
172
	);
173
	$autoplay_play   = sprintf(
174
		'<a aria-label="%s" class="wp-block-jetpack-slideshow_button-play" role="button" on="tap:%s.toggleAutoplay(toggleOn=true),%s.toggleClass(class=wp-block-jetpack-slideshow__autoplay-playing,force=true)"></a>',
175
		esc_attr__( 'Play Slideshow', 'jetpack' ),
176
		esc_attr( $amp_carousel_id ),
177
		esc_attr( $block_id )
178
	);
179
	return $autoplay_pause . $autoplay_play;
180
}
181