Sin descripción

slideshow.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. use Automattic\Jetpack\Assets;
  3. use Automattic\Jetpack\Extensions\Slideshow;
  4. /**
  5. * Slideshow shortcode.
  6. * Adds a new "slideshow" gallery type when adding a gallery using the classic editor.
  7. *
  8. * @package automattic/jetpack
  9. */
  10. /**
  11. * Slideshow shortcode usage: [gallery type="slideshow"] or the older [slideshow]
  12. */
  13. class Jetpack_Slideshow_Shortcode {
  14. /**
  15. * Number of slideshows on a page.
  16. *
  17. * @var int
  18. */
  19. public $instance_count = 0;
  20. /**
  21. * Constructor
  22. */
  23. public function __construct() {
  24. global $shortcode_tags;
  25. // Only if the slideshow shortcode has not already been defined.
  26. if ( ! array_key_exists( 'slideshow', $shortcode_tags ) ) {
  27. add_shortcode( 'slideshow', array( $this, 'shortcode_callback' ) );
  28. }
  29. // Only if the gallery shortcode has not been redefined.
  30. if ( isset( $shortcode_tags['gallery'] ) && 'gallery_shortcode' === $shortcode_tags['gallery'] ) {
  31. add_filter( 'post_gallery', array( $this, 'post_gallery' ), 1002, 2 );
  32. add_filter( 'jetpack_gallery_types', array( $this, 'add_gallery_type' ), 10 );
  33. }
  34. }
  35. /**
  36. * Responds to the [gallery] shortcode, but not an actual shortcode callback.
  37. *
  38. * @param string $value An empty string if nothing has modified the gallery output, the output html otherwise.
  39. * @param array $attr The shortcode attributes array.
  40. *
  41. * @return string The (un)modified $value
  42. */
  43. public function post_gallery( $value, $attr ) {
  44. // Bail if somebody else has done something.
  45. if ( ! empty( $value ) ) {
  46. return $value;
  47. }
  48. // If [gallery type="slideshow"] have it behave just like [slideshow].
  49. if ( ! empty( $attr['type'] ) && 'slideshow' === $attr['type'] ) {
  50. return $this->shortcode_callback( $attr );
  51. }
  52. return $value;
  53. }
  54. /**
  55. * Add the Slideshow type to gallery settings
  56. *
  57. * @see Jetpack_Tiled_Gallery::media_ui_print_templates
  58. *
  59. * @param array $types An array of types where the key is the value, and the value is the caption.
  60. *
  61. * @return array
  62. */
  63. public function add_gallery_type( $types = array() ) {
  64. $types['slideshow'] = esc_html__( 'Slideshow', 'jetpack' );
  65. return $types;
  66. }
  67. /**
  68. * Display shortcode.
  69. *
  70. * @param array $attr Shortcode attributes.
  71. */
  72. public function shortcode_callback( $attr ) {
  73. $post_id = get_the_ID();
  74. $attr = shortcode_atts(
  75. array(
  76. 'trans' => 'fade',
  77. 'order' => 'ASC',
  78. 'orderby' => 'menu_order ID',
  79. 'id' => $post_id,
  80. 'include' => '',
  81. 'exclude' => '',
  82. 'autostart' => true,
  83. 'size' => '',
  84. ),
  85. $attr,
  86. 'slideshow'
  87. );
  88. if ( 'rand' === strtolower( $attr['order'] ) ) {
  89. $attr['orderby'] = 'none';
  90. }
  91. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  92. if ( ! $attr['orderby'] ) {
  93. $attr['orderby'] = 'menu_order ID';
  94. }
  95. if ( ! $attr['size'] ) {
  96. $attr['size'] = 'full';
  97. }
  98. // Don't restrict to the current post if include.
  99. $post_parent = ( empty( $attr['include'] ) ) ? (int) $attr['id'] : null;
  100. $attachments = get_posts(
  101. array(
  102. 'post_status' => 'inherit',
  103. 'post_type' => 'attachment',
  104. 'post_mime_type' => 'image',
  105. 'posts_per_page' => - 1,
  106. 'post_parent' => $post_parent,
  107. 'order' => $attr['order'],
  108. 'orderby' => $attr['orderby'],
  109. 'include' => $attr['include'],
  110. 'exclude' => $attr['exclude'],
  111. 'suppress_filters' => false,
  112. )
  113. );
  114. if ( count( $attachments ) < 1 ) {
  115. return false;
  116. }
  117. $gallery_instance = sprintf( 'gallery-%d-%d', $attr['id'], ++$this->instance_count );
  118. $gallery = array();
  119. foreach ( $attachments as $attachment ) {
  120. $attachment_image_src = wp_get_attachment_image_src( $attachment->ID, $attr['size'] );
  121. $attachment_image_src = $attachment_image_src[0]; // [url, width, height].
  122. $attachment_image_title = get_the_title( $attachment->ID );
  123. $attachment_image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true );
  124. /**
  125. * Filters the Slideshow slide caption.
  126. *
  127. * @module shortcodes
  128. *
  129. * @since 2.3.0
  130. *
  131. * @param string wptexturize( strip_tags( $attachment->post_excerpt ) ) Post excerpt.
  132. * @param string $attachment ->ID Attachment ID.
  133. */
  134. $caption = apply_filters( 'jetpack_slideshow_slide_caption', wptexturize( wp_strip_all_tags( $attachment->post_excerpt ) ), $attachment->ID );
  135. $gallery[] = (object) array(
  136. 'src' => (string) esc_url_raw( $attachment_image_src ),
  137. 'id' => (string) $attachment->ID,
  138. 'title' => (string) esc_attr( $attachment_image_title ),
  139. 'alt' => (string) esc_attr( $attachment_image_alt ),
  140. 'caption' => (string) $caption,
  141. 'itemprop' => 'image',
  142. );
  143. }
  144. $color = Jetpack_Options::get_option( 'slideshow_background_color', 'black' );
  145. $autostart = $attr['autostart'] ? $attr['autostart'] : 'true';
  146. $js_attr = array(
  147. 'gallery' => $gallery,
  148. 'selector' => $gallery_instance,
  149. 'trans' => $attr['trans'] ? $attr['trans'] : 'fade',
  150. 'autostart' => $autostart,
  151. 'color' => $color,
  152. );
  153. // Show a link to the gallery in feeds.
  154. if ( is_feed() ) {
  155. return sprintf(
  156. '<a href="%s">%s</a>',
  157. esc_url( get_permalink( $post_id ) . '#' . $gallery_instance . '-slideshow' ),
  158. esc_html__( 'Click to view slideshow.', 'jetpack' )
  159. );
  160. }
  161. if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
  162. // Load the styles and use the rendering method from the Slideshow block.
  163. Jetpack_Gutenberg::load_styles_as_required( 'slideshow' );
  164. $amp_args = array(
  165. 'ids' => wp_list_pluck( $gallery, 'id' ),
  166. );
  167. if ( 'true' == $autostart ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- attribute can be stored as boolean or string.
  168. $amp_args['autoplay'] = true;
  169. }
  170. return Slideshow\render_amp( $amp_args );
  171. }
  172. return $this->slideshow_js( $js_attr );
  173. }
  174. /**
  175. * Render the slideshow js
  176. *
  177. * Returns the necessary markup and js to fire a slideshow.
  178. *
  179. * @param array $attr Attributes for the slideshow.
  180. *
  181. * @uses $this->enqueue_scripts()
  182. *
  183. * @return string HTML output.
  184. */
  185. public function slideshow_js( $attr ) {
  186. // Enqueue scripts.
  187. $this->enqueue_scripts();
  188. $output = '';
  189. if ( defined( 'JSON_HEX_AMP' ) ) {
  190. // This is nice to have, but not strictly necessary since we use _wp_specialchars() below.
  191. $gallery = wp_json_encode( $attr['gallery'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); // phpcs:ignore PHPCompatibility
  192. } else {
  193. $gallery = wp_json_encode( $attr['gallery'] );
  194. }
  195. $output .= '<p class="jetpack-slideshow-noscript robots-nocontent">' . esc_html__( 'This slideshow requires JavaScript.', 'jetpack' ) . '</p>';
  196. /*
  197. * The input to json_encode() above can contain '&quot;'.
  198. *
  199. * For calls to json_encode() lacking the JSON_HEX_AMP option,
  200. * that '&quot;' is left unaltered. Running '&quot;' through esc_attr()
  201. * also leaves it unaltered since esc_attr() does not double-encode.
  202. *
  203. * This means we end up with an attribute like
  204. * `data-gallery="{&quot;foo&quot;:&quot;&quot;&quot;}"`,
  205. * which is interpreted by the browser as `{"foo":"""}`,
  206. * which cannot be JSON decoded.
  207. *
  208. * The preferred workaround is to include the JSON_HEX_AMP (and friends)
  209. * options, but these are not available until 5.3.0.
  210. * Alternatively, we can use _wp_specialchars( , , , true ) instead of
  211. * esc_attr(), which will double-encode.
  212. *
  213. * Since we can't rely on JSON_HEX_AMP, we do both.
  214. *
  215. * @todo Update when minimum is PHP 5.3+
  216. */
  217. $gallery_attributes = _wp_specialchars( wp_check_invalid_utf8( $gallery ), ENT_QUOTES, false, true );
  218. $output .= sprintf(
  219. '<div id="%s" class="slideshow-window jetpack-slideshow slideshow-%s" data-trans="%s" data-autostart="%s" data-gallery="%s" itemscope itemtype="https://schema.org/ImageGallery"></div>',
  220. esc_attr( $attr['selector'] . '-slideshow' ),
  221. esc_attr( $attr['color'] ),
  222. esc_attr( $attr['trans'] ),
  223. esc_attr( $attr['autostart'] ),
  224. $gallery_attributes
  225. );
  226. return $output;
  227. }
  228. /**
  229. * Actually enqueues the scripts and styles.
  230. */
  231. public function enqueue_scripts() {
  232. wp_enqueue_script( 'jquery-cycle', plugins_url( '/js/jquery.cycle.min.js', __FILE__ ), array( 'jquery' ), '20161231', true );
  233. wp_enqueue_script(
  234. 'jetpack-slideshow',
  235. Assets::get_file_url_for_environment( '_inc/build/shortcodes/js/slideshow-shortcode.min.js', 'modules/shortcodes/js/slideshow-shortcode.js' ),
  236. array( 'jquery-cycle' ),
  237. '20160119.1',
  238. true
  239. );
  240. wp_enqueue_style(
  241. 'jetpack-slideshow',
  242. plugins_url( '/css/slideshow-shortcode.css', __FILE__ ),
  243. array(),
  244. JETPACK__VERSION
  245. );
  246. wp_style_add_data( 'jetpack-slideshow', 'rtl', 'replace' );
  247. wp_localize_script(
  248. 'jetpack-slideshow',
  249. 'jetpackSlideshowSettings',
  250. /**
  251. * Filters the slideshow JavaScript spinner.
  252. *
  253. * @module shortcodes
  254. *
  255. * @since 2.1.0
  256. * @since 4.7.0 Added the `speed` option to the array of options.
  257. *
  258. * @param array $args
  259. * - string - spinner - URL of the spinner image.
  260. * - string - speed - Speed of the slideshow. Defaults to 4000.
  261. * - string - label_prev - Aria label for slideshow's previous button
  262. * - string - label_stop - Aria label for slideshow's pause button
  263. * - string - label_next - Aria label for slideshow's next button
  264. */
  265. apply_filters(
  266. 'jetpack_js_slideshow_settings',
  267. array(
  268. 'spinner' => plugins_url( '/img/slideshow-loader.gif', __FILE__ ),
  269. 'speed' => '4000',
  270. 'label_prev' => __( 'Previous Slide', 'jetpack' ),
  271. 'label_stop' => __( 'Pause Slideshow', 'jetpack' ),
  272. 'label_next' => __( 'Next Slide', 'jetpack' ),
  273. )
  274. )
  275. );
  276. }
  277. /**
  278. * Instantiate shortcode.
  279. */
  280. public static function init() {
  281. new Jetpack_Slideshow_Shortcode();
  282. }
  283. }
  284. Jetpack_Slideshow_Shortcode::init();