Completed
Push — add/amp-slideshow ( 6e225a...63815f )
by
unknown
06:09
created

slideshow.php ➔ jetpack_slideshow_block_slides()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 3
dl 0
loc 26
rs 9.504
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
	$delay    = empty( $attr['delay'] ) ? 3 : intval( $attr['delay'] );
47
	$align    = isset( $attr['align'] ) ? $attr['align'] : 'center';
48
	$classes  = array(
49
		'wp-block-jetpack-slideshow',
50
		'wp-amp-block',
51
		'align' . $align,
52
		$autoplay ? 'wp-block-jetpack-slideshow__autoplay' : null,
53
		$autoplay ? 'wp-block-jetpack-slideshow__autoplay-playing' : null,
54
	);
55
56
	$first_image = wp_get_attachment_metadata( $ids[0] );
57
58
	$amp_carousel = sprintf(
59
		'<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>',
60
		esc_attr( $first_image['width'] ),
61
		esc_attr( $first_image['height'] ),
62
		esc_attr__( 'Next Slide', 'jetpack' ),
63
		esc_attr__( 'Previous Slide', 'jetpack' ),
64
		$autoplay ? 'autoplay delay=' . esc_attr( $delay * 1000 ) : '',
65
		esc_attr( $wp_block_jetpack_slideshow_id ),
66
		implode( '', jetpack_slideshow_block_slides( $ids, $first_image['width'], $first_image['height'] ) )
67
	);
68
69
	return sprintf(
70
		'<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>',
71
		esc_attr( implode( $classes, ' ' ) ),
72
		esc_attr( $wp_block_jetpack_slideshow_id ),
73
		$amp_carousel,
74
		$autoplay ? jetpack_slideshow_block_autoplay_ui( $wp_block_jetpack_slideshow_id ) : '',
75
		implode( '', jetpack_slideshow_block_bullets( $ids, $wp_block_jetpack_slideshow_id ) )
76
	);
77
}
78
79
/**
80
 * Generate array of slides markup
81
 *
82
 * @param array $ids Array of image ids.
83
 * @param int   $width Width of the container.
84
 * @param int   $height Height of the container.
85
 *
86
 * @return array Array of slides markup.
87
 */
88
function jetpack_slideshow_block_slides( $ids = [], $width = 400, $height = 300 ) {
89
	return array_map(
90
		function( $id ) use ( $width, $height ) {
91
			$caption    = wp_get_attachment_caption( $id );
92
			$figcaption = $caption ? sprintf(
93
				'<figcaption class="wp-block-jetpack-slideshow_caption gallery-caption">%s</figcaption>',
94
				wp_kses_post( $caption )
95
			) : '';
96
			$image      = wp_get_attachment_image(
97
				$id,
98
				[ $width, $height ],
99
				false,
100
				[
101
					'class'      => 'wp-block-jetpack-slideshow_image',
102
					'object-fit' => 'contain',
103
				]
104
			);
105
			return sprintf(
106
				'<div class="wp-block-jetpack-slideshow_slide"><figure>%s%s</figure></div>',
107
				$image,
108
				$figcaption
109
			);
110
		},
111
		$ids
112
	);
113
}
114
115
/**
116
 * Generate array of bullets markup
117
 *
118
 * @param array $ids Array of image ids.
119
 * @param int   $block_ordinal The ordinal number of the block, used in unique ID.
120
 *
121
 * @return array Array of bullets markup.
122
 */
123
function jetpack_slideshow_block_bullets( $ids = [], $block_ordinal = 0 ) {
124
	$amp_carousel_id = sprintf(
125
		'wp-block-jetpack-slideshow__amp_carousel_%s',
126
		intval( $block_ordinal )
127
	);
128
	return array_map(
129
		function( $index ) use ( $amp_carousel_id ) {
130
			return sprintf(
131
				'<button class="swiper-pagination-bullet" tabindex="0" role="button" aria-label="Go to slide %s" on="tap:%s.goToSlide(index=%s)"></button>',
132
				( $index + 1 ),
133
				esc_attr( $amp_carousel_id ),
134
				$index
135
			);
136
		},
137
		array_keys( $ids )
138
	);
139
}
140
141
/**
142
 * Generate autoplay play/pause UI.
143
 *
144
 * @param int $block_ordinal The ordinal number of the block, used in unique ID.
145
 *
146
 * @return string Autoplay UI markup.
147
 */
148
function jetpack_slideshow_block_autoplay_ui( $block_ordinal = 0 ) {
149
	$block_id        = sprintf(
150
		'wp-block-jetpack-slideshow__%s',
151
		intval( $block_ordinal )
152
	);
153
	$amp_carousel_id = sprintf(
154
		'wp-block-jetpack-slideshow__amp_carousel_%s',
155
		intval( $block_ordinal )
156
	);
157
	$autoplay_pause  = sprintf(
158
		'<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>',
159
		esc_attr__( 'Pause Slideshow', 'jetpack' ),
160
		esc_attr( $amp_carousel_id ),
161
		esc_attr( $block_id )
162
	);
163
	$autoplay_play   = sprintf(
164
		'<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>',
165
		esc_attr__( 'Play Slideshow', 'jetpack' ),
166
		esc_attr( $amp_carousel_id ),
167
		esc_attr( $block_id )
168
	);
169
	return $autoplay_pause . $autoplay_play;
170
}
171