Jetpack_Slideshow_Shortcode

Slideshow shortcode usage: or the older [slideshow].

Defined (1)

The class is defined in the following location(s).

/modules/shortcodes/slideshow.php  
  1. class Jetpack_Slideshow_Shortcode { 
  2. public $instance_count = 0; 
  3.  
  4. function __construct() { 
  5. global $shortcode_tags; 
  6.  
  7. $needs_scripts = false; 
  8.  
  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. $needs_scripts = true; 
  13.  
  14. // Only if the gallery shortcode has not been redefined. 
  15. if ( isset( $shortcode_tags['gallery'] ) && $shortcode_tags['gallery'] == 'gallery_shortcode' ) { 
  16. add_filter( 'post_gallery', array( $this, 'post_gallery' ), 1002, 2 ); 
  17. add_filter( 'jetpack_gallery_types', array( $this, 'add_gallery_type' ), 10 ); 
  18. $needs_scripts = true; 
  19.  
  20. if ( $needs_scripts ) 
  21. add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_scripts' ), 1 ); 
  22.  
  23. /** 
  24. * For the moment, comment out the setting for v2.8. 
  25. * The remainder should work as it always has. 
  26. * See: https://github.com/Automattic/jetpack/pull/85/files 
  27. */ 
  28. // add_action( 'admin_init', array( $this, 'register_settings' ), 5 ); 
  29.  
  30. /** 
  31. * Responds to the [gallery] shortcode, but not an actual shortcode callback. 
  32. * @param $value An empty string if nothing has modified the gallery output, the output html otherwise 
  33. * @param $attr The shortcode attributes array 
  34. * @return string The (un)modified $value 
  35. */ 
  36. function post_gallery( $value, $attr ) { 
  37. // Bail if somebody else has done something 
  38. if ( ! empty( $value ) ) 
  39. return $value; 
  40.  
  41. // If [gallery type="slideshow"] have it behave just like [slideshow] 
  42. if ( ! empty( $attr['type'] ) && 'slideshow' == $attr['type'] ) 
  43. return $this->shortcode_callback( $attr ); 
  44.  
  45. return $value; 
  46.  
  47. /** 
  48. * Add the Slideshow type to gallery settings 
  49. * @param $types An array of types where the key is the value, and the value is the caption. 
  50. * @see Jetpack_Tiled_Gallery::media_ui_print_templates 
  51. */ 
  52. function add_gallery_type( $types = array() ) { 
  53. $types['slideshow'] = esc_html__( 'Slideshow', 'jetpack' ); 
  54. return $types; 
  55.  
  56. function register_settings() { 
  57. add_settings_section( 'slideshow_section', __( 'Image Gallery Slideshow', 'jetpack' ), '__return_empty_string', 'media' ); 
  58.  
  59. add_settings_field( 'jetpack_slideshow_background_color', __( 'Background color', 'jetpack' ), array( $this, 'slideshow_background_color_callback' ), 'media', 'slideshow_section' ); 
  60.  
  61. register_setting( 'media', 'jetpack_slideshow_background_color', array( $this, 'slideshow_background_color_sanitize' ) ); 
  62.  
  63. function slideshow_background_color_callback() { 
  64. $options = array( 
  65. 'black' => __( 'Black', 'jetpack' ),  
  66. 'white' => __( 'White', 'jetpack' ),  
  67. ); 
  68. $this->settings_select( 'jetpack_slideshow_background_color', $options ); 
  69.  
  70. function settings_select( $name, $values, $extra_text = '' ) { 
  71. if ( empty( $name ) || empty( $values ) || ! is_array( $values ) ) { 
  72. return; 
  73. $option = get_option( $name ); 
  74. ?> 
  75. <fieldset> 
  76. <select name="<?php echo esc_attr( $name ); ?>" id="<?php 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, $content = null ) { 
  93. global $post; 
  94.  
  95. $attr = shortcode_atts( 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.  
  119. $attachments = get_posts( 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. ) ); 
  130.  
  131. if ( count( $attachments ) < 1 ) { 
  132. return; 
  133.  
  134. $gallery_instance = sprintf( "gallery-%d-%d", $attr['id'], ++$this->instance_count ); 
  135.  
  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. * @module shortcodes 
  145. * @since 2.3.0 
  146. * @param string wptexturize( strip_tags( $attachment->post_excerpt ) ) Post excerpt. 
  147. * @param string $attachment->ID Attachment ID. 
  148. */ 
  149. $caption = apply_filters( 'jetpack_slideshow_slide_caption', wptexturize( strip_tags( $attachment->post_excerpt ) ), $attachment->ID ); 
  150.  
  151. $gallery[] = (object) array( 
  152. 'src' => (string) esc_url_raw( $attachment_image_src ),  
  153. 'id' => (string) $attachment->ID,  
  154. 'title' => (string) esc_attr( $attachment_image_title ),  
  155. 'alt' => (string) esc_attr( $attachment_image_alt ),  
  156. 'caption' => (string) $caption,  
  157. ); 
  158.  
  159. $color = Jetpack_Options::get_option( 'slideshow_background_color', 'black' ); 
  160.  
  161. $js_attr = array( 
  162. 'gallery' => $gallery,  
  163. 'selector' => $gallery_instance,  
  164. 'trans' => $attr['trans'] ? $attr['trans'] : 'fade',  
  165. 'autostart' => $attr['autostart'] ? $attr['autostart'] : 'true',  
  166. 'color' => $color,  
  167. ); 
  168.  
  169. // Show a link to the gallery in feeds. 
  170. if ( is_feed() ) 
  171. return sprintf( '<a href="%s">%s</a>',  
  172. esc_url( get_permalink( $post->ID ) . '#' . $gallery_instance . '-slideshow' ),  
  173. esc_html__( 'Click to view slideshow.', 'jetpack' ) 
  174. ); 
  175.  
  176. return $this->slideshow_js( $js_attr ); 
  177.  
  178. /** 
  179. * Render the slideshow js 
  180. * Returns the necessary markup and js to fire a slideshow. 
  181. * @uses $this->enqueue_scripts() 
  182. */ 
  183. function slideshow_js( $attr ) { 
  184. // Enqueue scripts 
  185. $this->enqueue_scripts(); 
  186.  
  187. $output = ''; 
  188.  
  189. if ( defined( 'JSON_HEX_AMP' ) ) { 
  190. // This is nice to have, but not strictly necessary since we use _wp_specialchars() below 
  191. $gallery = json_encode( $attr['gallery'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); 
  192. } else { 
  193. $gallery = json_encode( $attr['gallery'] ); 
  194.  
  195. $output .= '<p class="jetpack-slideshow-noscript robots-nocontent">' . esc_html__( 'This slideshow requires JavaScript.', 'jetpack' ) . '</p>'; 
  196. $output .= sprintf( '<div id="%s" class="slideshow-window jetpack-slideshow slideshow-%s" data-trans="%s" data-autostart="%s" data-gallery="%s"></div>',  
  197. esc_attr( $attr['selector'] . '-slideshow' ),  
  198. esc_attr( $attr['color'] ),  
  199. esc_attr( $attr['trans'] ),  
  200. esc_attr( $attr['autostart'] ),  
  201. /** 
  202. * The input to json_encode() above can contain '"'. 
  203. * For calls to json_encode() lacking the JSON_HEX_AMP option,  
  204. * that '"' is left unaltered. Running '"' through esc_attr() 
  205. * also leaves it unaltered since esc_attr() does not double-encode. 
  206. * This means we end up with an attribute like 
  207. * `data-gallery="{"foo":"""}"`,  
  208. * which is interpreted by the browser as `{"foo":"""}`,  
  209. * which cannot be JSON decoded. 
  210. * The preferred workaround is to include the JSON_HEX_AMP (and friends) 
  211. * options, but these are not available until 5.3.0. 
  212. * Alternatively, we can use _wp_specialchars( , , , true ) instead of 
  213. * esc_attr(), which will double-encode. 
  214. * Since we can't rely on JSON_HEX_AMP, we do both. 
  215. */ 
  216. _wp_specialchars( wp_check_invalid_utf8( $gallery ), ENT_QUOTES, false, true ) 
  217. ); 
  218.  
  219. return $output; 
  220.  
  221. /** 
  222. * Infinite Scroll needs the scripts to be present at all times 
  223. */ 
  224. function maybe_enqueue_scripts() { 
  225. if ( is_home() && current_theme_supports( 'infinite-scroll' ) ) 
  226. $this->enqueue_scripts(); 
  227.  
  228. /** 
  229. * Actually enqueues the scripts and styles. 
  230. */ 
  231. function enqueue_scripts() { 
  232. static $enqueued = false; 
  233.  
  234. if ( $enqueued ) 
  235. return; 
  236.  
  237. wp_enqueue_script( 'jquery-cycle', plugins_url( '/js/jquery.cycle.js', __FILE__ ) , array( 'jquery' ), '2.9999.8', true ); 
  238. wp_enqueue_script( 'jetpack-slideshow', plugins_url( '/js/slideshow-shortcode.js', __FILE__ ), array( 'jquery-cycle' ), '20121214.1', true ); 
  239. if( is_rtl() ) { 
  240. wp_enqueue_style( 'jetpack-slideshow', plugins_url( '/css/rtl/slideshow-shortcode-rtl.css', __FILE__ ) ); 
  241. } else { 
  242. wp_enqueue_style( 'jetpack-slideshow', plugins_url( '/css/slideshow-shortcode.css', __FILE__ ) ); 
  243.  
  244. wp_localize_script( 
  245. 'jetpack-slideshow',  
  246. 'jetpackSlideshowSettings',  
  247. /** 
  248. * Filters the slideshow Javascript spinner. 
  249. * @module shortcodes 
  250. * @since 2.1.0 
  251. * @param array $args 
  252. * - string - spinner - URL of the spinner image. 
  253. */ 
  254. apply_filters( 'jetpack_js_slideshow_settings', array( 
  255. 'spinner' => plugins_url( '/img/slideshow-loader.gif', __FILE__ ),  
  256. ) ) 
  257. ); 
  258.  
  259. $enqueued = true; 
  260.  
  261. public static function init() { 
  262. $gallery = new Jetpack_Slideshow_Shortcode;