/wp-content/plugins/jetpack/extensions/blocks/slideshow/slideshow.php

https://github.com/livinglab/openlab · PHP · 206 lines · 132 code · 14 blank · 60 comment · 2 complexity · 32dc885af91e9b46c234f453b9f2cda4 MD5 · raw file

  1. <?php
  2. /**
  3. * Slideshow Block.
  4. *
  5. * @since 7.1.0
  6. *
  7. * @package automattic/jetpack
  8. */
  9. namespace Automattic\Jetpack\Extensions\Slideshow;
  10. use Automattic\Jetpack\Blocks;
  11. use Jetpack_Gutenberg;
  12. const FEATURE_NAME = 'slideshow';
  13. const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
  14. /**
  15. * Registers the block for use in Gutenberg
  16. * This is done via an action so that we can disable
  17. * registration if we need to.
  18. */
  19. function register_block() {
  20. Blocks::jetpack_register_block(
  21. BLOCK_NAME,
  22. array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
  23. );
  24. }
  25. add_action( 'init', __NAMESPACE__ . '\register_block' );
  26. /**
  27. * Slideshow block registration/dependency declaration.
  28. *
  29. * @param array $attr Array containing the slideshow block attributes.
  30. * @param string $content String containing the slideshow block content.
  31. *
  32. * @return string
  33. */
  34. function load_assets( $attr, $content ) {
  35. Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
  36. if ( Blocks::is_amp_request() ) {
  37. return render_amp( $attr );
  38. }
  39. return $content;
  40. }
  41. /**
  42. * Render slideshow block for AMP
  43. *
  44. * @param array $attr Array containing the slideshow block attributes.
  45. *
  46. * @return string
  47. */
  48. function render_amp( $attr ) {
  49. if ( empty( $attr['ids'] ) ) {
  50. return '';
  51. }
  52. static $wp_block_jetpack_slideshow_id = 0;
  53. $wp_block_jetpack_slideshow_id++;
  54. $ids = $attr['ids'];
  55. $autoplay = empty( $attr['autoplay'] ) ? false : true;
  56. $extras = array(
  57. 'wp-amp-block',
  58. $autoplay ? 'wp-block-jetpack-slideshow__autoplay' : null,
  59. $autoplay ? 'wp-block-jetpack-slideshow__autoplay-playing' : null,
  60. );
  61. $classes = Blocks::classes( FEATURE_NAME, $attr, $extras );
  62. return sprintf(
  63. '<div class="%1$s" id="wp-block-jetpack-slideshow__%2$d"><div class="wp-block-jetpack-slideshow_container swiper-container">%3$s%4$s%5$s</div></div>',
  64. esc_attr( $classes ),
  65. absint( $wp_block_jetpack_slideshow_id ),
  66. amp_carousel( $attr, $wp_block_jetpack_slideshow_id ),
  67. $autoplay ? autoplay_ui( $wp_block_jetpack_slideshow_id ) : '',
  68. bullets( $ids, $wp_block_jetpack_slideshow_id )
  69. );
  70. }
  71. /**
  72. * Generate amp-carousel markup
  73. *
  74. * @param array $attr Array of block attributes.
  75. * @param int $block_ordinal The ordinal number of the block, used in unique ID.
  76. *
  77. * @return string amp-carousel markup.
  78. */
  79. function amp_carousel( $attr, $block_ordinal ) {
  80. $ids = empty( $attr['ids'] ) ? array() : $attr['ids'];
  81. $first_image = wp_get_attachment_metadata( $ids[0] );
  82. $delay = empty( $attr['delay'] ) ? 3 : absint( $attr['delay'] );
  83. $autoplay = empty( $attr['autoplay'] ) ? false : $attr['autoplay'];
  84. $width = empty( $first_image['width'] ) ? 800 : $first_image['width'];
  85. $height = empty( $first_image['height'] ) ? 600 : $first_image['height'];
  86. return sprintf(
  87. '<amp-carousel width="%1$d" height="%2$d" layout="responsive" type="slides" data-next-button-aria-label="%3$s" data-prev-button-aria-label="%4$s" controls loop %5$s id="wp-block-jetpack-slideshow__amp-carousel__%6$s" on="slideChange:wp-block-jetpack-slideshow__amp-pagination__%6$s.toggle(index=event.index, value=true)">%7$s</amp-carousel>',
  88. esc_attr( $width ),
  89. esc_attr( $height ),
  90. esc_attr__( 'Next Slide', 'jetpack' ),
  91. esc_attr__( 'Previous Slide', 'jetpack' ),
  92. $autoplay ? 'autoplay delay=' . esc_attr( $delay * 1000 ) : '',
  93. absint( $block_ordinal ),
  94. implode( '', slides( $ids, $width, $height ) )
  95. );
  96. }
  97. /**
  98. * Generate array of slides markup
  99. *
  100. * @param array $ids Array of image ids.
  101. * @param int $width Width of the container.
  102. * @param int $height Height of the container.
  103. *
  104. * @return array Array of slides markup.
  105. */
  106. function slides( $ids = array(), $width = 400, $height = 300 ) {
  107. return array_map(
  108. function ( $id ) use ( $width, $height ) {
  109. $caption = wp_get_attachment_caption( $id );
  110. $figcaption = $caption ? sprintf(
  111. '<figcaption class="wp-block-jetpack-slideshow_caption gallery-caption">%s</figcaption>',
  112. wp_kses_post( $caption )
  113. ) : '';
  114. $image = wp_get_attachment_image(
  115. $id,
  116. array( $width, $height ),
  117. false,
  118. array(
  119. 'class' => 'wp-block-jetpack-slideshow_image',
  120. 'object-fit' => 'contain',
  121. )
  122. );
  123. return sprintf(
  124. '<div class="wp-block-jetpack-slideshow_slide"><figure>%s%s</figure></div>',
  125. $image,
  126. $figcaption
  127. );
  128. },
  129. $ids
  130. );
  131. }
  132. /**
  133. * Generate array of bullets markup
  134. *
  135. * @param array $ids Array of image ids.
  136. * @param int $block_ordinal The ordinal number of the block, used in unique ID.
  137. *
  138. * @return array Array of bullets markup.
  139. */
  140. function bullets( $ids = array(), $block_ordinal = 0 ) {
  141. $buttons = array_map(
  142. function ( $index ) {
  143. $aria_label = sprintf(
  144. /* translators: %d: Slide number. */
  145. __( 'Go to slide %d', 'jetpack' ),
  146. absint( $index + 1 )
  147. );
  148. return sprintf(
  149. '<button option="%d" class="swiper-pagination-bullet" tabindex="0" role="button" aria-label="%s" %s></button>',
  150. absint( $index ),
  151. esc_attr( $aria_label ),
  152. 0 === $index ? 'selected' : ''
  153. );
  154. },
  155. array_keys( $ids )
  156. );
  157. return sprintf(
  158. '<amp-selector id="wp-block-jetpack-slideshow__amp-pagination__%1$d" class="wp-block-jetpack-slideshow_pagination swiper-pagination swiper-pagination-bullets amp-pagination" on="select:wp-block-jetpack-slideshow__amp-carousel__%1$d.goToSlide(index=event.targetOption)" layout="container">%2$s</amp-selector>',
  159. absint( $block_ordinal ),
  160. implode( '', $buttons )
  161. );
  162. }
  163. /**
  164. * Generate autoplay play/pause UI.
  165. *
  166. * @param int $block_ordinal The ordinal number of the block, used in unique ID.
  167. *
  168. * @return string Autoplay UI markup.
  169. */
  170. function autoplay_ui( $block_ordinal = 0 ) {
  171. $block_id = sprintf(
  172. 'wp-block-jetpack-slideshow__%d',
  173. absint( $block_ordinal )
  174. );
  175. $amp_carousel_id = sprintf(
  176. 'wp-block-jetpack-slideshow__amp-carousel__%d',
  177. absint( $block_ordinal )
  178. );
  179. $autoplay_pause = sprintf(
  180. '<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>',
  181. esc_attr__( 'Pause Slideshow', 'jetpack' ),
  182. esc_attr( $amp_carousel_id ),
  183. esc_attr( $block_id )
  184. );
  185. $autoplay_play = sprintf(
  186. '<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>',
  187. esc_attr__( 'Play Slideshow', 'jetpack' ),
  188. esc_attr( $amp_carousel_id ),
  189. esc_attr( $block_id )
  190. );
  191. return $autoplay_pause . $autoplay_play;
  192. }