slideshow.php 9.8 KB

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