PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/blog/wp-content/plugins/jetpack/modules/shortcodes/slideshow.php

https://gitlab.com/relacilia/cakra
PHP | 306 lines | 179 code | 44 blank | 83 comment | 27 complexity | adc407597ca62ae155d17e40c1042ccb MD5 | raw file
  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. $needs_scripts = false;
  10. // Only if the slideshow shortcode has not already been defined.
  11. if ( ! array_key_exists( 'slideshow', $shortcode_tags ) ) {
  12. add_shortcode( 'slideshow', array( $this, 'shortcode_callback' ) );
  13. $needs_scripts = true;
  14. }
  15. // Only if the gallery shortcode has not been redefined.
  16. if ( isset( $shortcode_tags['gallery'] ) && $shortcode_tags['gallery'] == 'gallery_shortcode' ) {
  17. add_filter( 'post_gallery', array( $this, 'post_gallery' ), 1002, 2 );
  18. add_filter( 'jetpack_gallery_types', array( $this, 'add_gallery_type' ), 10 );
  19. $needs_scripts = true;
  20. }
  21. if ( $needs_scripts )
  22. add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_scripts' ), 1 );
  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. *
  33. * @param $value An empty string if nothing has modified the gallery output, the output html otherwise
  34. * @param $attr The shortcode attributes array
  35. *
  36. * @return string The (un)modified $value
  37. */
  38. function post_gallery( $value, $attr ) {
  39. // Bail if somebody else has done something
  40. if ( ! empty( $value ) )
  41. return $value;
  42. // If [gallery type="slideshow"] have it behave just like [slideshow]
  43. if ( ! empty( $attr['type'] ) && 'slideshow' == $attr['type'] )
  44. return $this->shortcode_callback( $attr );
  45. return $value;
  46. }
  47. /**
  48. * Add the Slideshow type to gallery settings
  49. *
  50. * @param $types An array of types where the key is the value, and the value is the caption.
  51. * @see Jetpack_Tiled_Gallery::media_ui_print_templates
  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 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. $attr = shortcode_atts( array(
  95. 'trans' => 'fade',
  96. 'order' => 'ASC',
  97. 'orderby' => 'menu_order ID',
  98. 'id' => $post->ID,
  99. 'include' => '',
  100. 'exclude' => '',
  101. 'autostart' => true,
  102. 'size' => '',
  103. ), $attr, 'slideshow' );
  104. if ( 'rand' == strtolower( $attr['order'] ) ) {
  105. $attr['orderby'] = 'none';
  106. }
  107. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  108. if ( ! $attr['orderby'] ) {
  109. $attr['orderby'] = 'menu_order ID';
  110. }
  111. if ( ! $attr['size'] ) {
  112. $attr['size'] = 'full';
  113. }
  114. // Don't restrict to the current post if include
  115. $post_parent = ( empty( $attr['include'] ) ) ? intval( $attr['id'] ) : null;
  116. $attachments = get_posts( array(
  117. 'post_status' => 'inherit',
  118. 'post_type' => 'attachment',
  119. 'post_mime_type' => 'image',
  120. 'posts_per_page' => -1,
  121. 'post_parent' => $post_parent,
  122. 'order' => $attr['order'],
  123. 'orderby' => $attr['orderby'],
  124. 'include' => $attr['include'],
  125. 'exclude' => $attr['exclude'],
  126. ) );
  127. if ( count( $attachments ) < 1 ) {
  128. return;
  129. }
  130. $gallery_instance = sprintf( "gallery-%d-%d", $attr['id'], ++$this->instance_count );
  131. $gallery = array();
  132. foreach ( $attachments as $attachment ) {
  133. $attachment_image_src = wp_get_attachment_image_src( $attachment->ID, $attr['size'] );
  134. $attachment_image_src = $attachment_image_src[0]; // [url, width, height]
  135. $attachment_image_title = get_the_title( $attachment->ID );
  136. $attachment_image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true );
  137. /**
  138. * Filters the Slideshow slide caption.
  139. *
  140. * @module shortcodes
  141. *
  142. * @since 2.3.0
  143. *
  144. * @param string wptexturize( strip_tags( $attachment->post_excerpt ) ) Post excerpt.
  145. * @param string $attachment->ID Attachment ID.
  146. */
  147. $caption = apply_filters( 'jetpack_slideshow_slide_caption', wptexturize( strip_tags( $attachment->post_excerpt ) ), $attachment->ID );
  148. $gallery[] = (object) array(
  149. 'src' => (string) esc_url_raw( $attachment_image_src ),
  150. 'id' => (string) $attachment->ID,
  151. 'title' => (string) esc_attr( $attachment_image_title ),
  152. 'alt' => (string) esc_attr( $attachment_image_alt ),
  153. 'caption' => (string) $caption,
  154. );
  155. }
  156. $color = Jetpack_Options::get_option( 'slideshow_background_color', 'black' );
  157. $js_attr = array(
  158. 'gallery' => $gallery,
  159. 'selector' => $gallery_instance,
  160. 'trans' => $attr['trans'] ? $attr['trans'] : 'fade',
  161. 'autostart' => $attr['autostart'] ? $attr['autostart'] : 'true',
  162. 'color' => $color,
  163. );
  164. // Show a link to the gallery in feeds.
  165. if ( is_feed() )
  166. return sprintf( '<a href="%s">%s</a>',
  167. esc_url( get_permalink( $post->ID ) . '#' . $gallery_instance . '-slideshow' ),
  168. esc_html__( 'Click to view slideshow.', 'jetpack' )
  169. );
  170. return $this->slideshow_js( $js_attr );
  171. }
  172. /**
  173. * Render the slideshow js
  174. *
  175. * Returns the necessary markup and js to fire a slideshow.
  176. *
  177. * @uses $this->enqueue_scripts()
  178. */
  179. function slideshow_js( $attr ) {
  180. // Enqueue scripts
  181. $this->enqueue_scripts();
  182. $output = '';
  183. if ( defined( 'JSON_HEX_AMP' ) ) {
  184. // This is nice to have, but not strictly necessary since we use _wp_specialchars() below
  185. $gallery = json_encode( $attr['gallery'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT );
  186. } else {
  187. $gallery = json_encode( $attr['gallery'] );
  188. }
  189. $output .= '<p class="jetpack-slideshow-noscript robots-nocontent">' . esc_html__( 'This slideshow requires JavaScript.', 'jetpack' ) . '</p>';
  190. $output .= sprintf( '<div id="%s" class="slideshow-window jetpack-slideshow slideshow-%s" data-trans="%s" data-autostart="%s" data-gallery="%s"></div>',
  191. esc_attr( $attr['selector'] . '-slideshow' ),
  192. esc_attr( $attr['color'] ),
  193. esc_attr( $attr['trans'] ),
  194. esc_attr( $attr['autostart'] ),
  195. /*
  196. * The input to json_encode() above can contain '&quot;'.
  197. *
  198. * For calls to json_encode() lacking the JSON_HEX_AMP option,
  199. * that '&quot;' is left unaltered. Running '&quot;' through esc_attr()
  200. * also leaves it unaltered since esc_attr() does not double-encode.
  201. *
  202. * This means we end up with an attribute like
  203. * `data-gallery="{&quot;foo&quot;:&quot;&quot;&quot;}"`,
  204. * which is interpreted by the browser as `{"foo":"""}`,
  205. * which cannot be JSON decoded.
  206. *
  207. * The preferred workaround is to include the JSON_HEX_AMP (and friends)
  208. * options, but these are not available until 5.3.0.
  209. * Alternatively, we can use _wp_specialchars( , , , true ) instead of
  210. * esc_attr(), which will double-encode.
  211. *
  212. * Since we can't rely on JSON_HEX_AMP, we do both.
  213. */
  214. _wp_specialchars( wp_check_invalid_utf8( $gallery ), ENT_QUOTES, false, true )
  215. );
  216. return $output;
  217. }
  218. /**
  219. * Infinite Scroll needs the scripts to be present at all times
  220. */
  221. function maybe_enqueue_scripts() {
  222. if ( is_home() && current_theme_supports( 'infinite-scroll' ) )
  223. $this->enqueue_scripts();
  224. }
  225. /**
  226. * Actually enqueues the scripts and styles.
  227. */
  228. function enqueue_scripts() {
  229. static $enqueued = false;
  230. if ( $enqueued )
  231. return;
  232. wp_enqueue_script( 'jquery-cycle', plugins_url( '/js/jquery.cycle.js', __FILE__ ) , array( 'jquery' ), '2.9999.8', true );
  233. wp_enqueue_script( 'jetpack-slideshow', plugins_url( '/js/slideshow-shortcode.js', __FILE__ ), array( 'jquery-cycle' ), '20121214.1', true );
  234. if( is_rtl() ) {
  235. wp_enqueue_style( 'jetpack-slideshow', plugins_url( '/css/rtl/slideshow-shortcode-rtl.css', __FILE__ ) );
  236. } else {
  237. wp_enqueue_style( 'jetpack-slideshow', plugins_url( '/css/slideshow-shortcode.css', __FILE__ ) );
  238. }
  239. wp_localize_script(
  240. 'jetpack-slideshow',
  241. 'jetpackSlideshowSettings',
  242. /**
  243. * Filters the slideshow Javascript spinner.
  244. *
  245. * @module shortcodes
  246. *
  247. * @since 2.1.0
  248. *
  249. * @param array $args
  250. * - string - spinner - URL of the spinner image.
  251. */
  252. apply_filters( 'jetpack_js_slideshow_settings', array(
  253. 'spinner' => plugins_url( '/img/slideshow-loader.gif', __FILE__ ),
  254. ) )
  255. );
  256. $enqueued = true;
  257. }
  258. public static function init() {
  259. $gallery = new Jetpack_Slideshow_Shortcode;
  260. }
  261. }
  262. add_action( 'init', array( 'Jetpack_Slideshow_Shortcode', 'init' ) );