PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.1
Jetpack – WP Security, Backup, Speed, & Growth v6.1
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / slideshow.php

slideshow.php in Jetpack – WP Security, Backup, Speed, & Growth 6.1, at modules/shortcodes/slideshow.php

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