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
|
|
View Code Duplication |
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
|
|
|
) |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
if ( count( $attachments ) < 1 ) { |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$gallery_instance = sprintf( 'gallery-%d-%d', $attr['id'], ++$this->instance_count ); |
160
|
|
|
|
161
|
|
|
$gallery = array(); |
162
|
|
|
foreach ( $attachments as $attachment ) { |
163
|
|
|
$attachment_image_src = wp_get_attachment_image_src( $attachment->ID, $attr['size'] ); |
164
|
|
|
$attachment_image_src = $attachment_image_src[0]; // [url, width, height] |
165
|
|
|
$attachment_image_title = get_the_title( $attachment->ID ); |
166
|
|
|
$attachment_image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ); |
167
|
|
|
/** |
168
|
|
|
* Filters the Slideshow slide caption. |
169
|
|
|
* |
170
|
|
|
* @module shortcodes |
171
|
|
|
* |
172
|
|
|
* @since 2.3.0 |
173
|
|
|
* |
174
|
|
|
* @param string wptexturize( strip_tags( $attachment->post_excerpt ) ) Post excerpt. |
175
|
|
|
* @param string $attachment ->ID Attachment ID. |
176
|
|
|
*/ |
177
|
|
|
$caption = apply_filters( 'jetpack_slideshow_slide_caption', wptexturize( strip_tags( $attachment->post_excerpt ) ), $attachment->ID ); |
178
|
|
|
|
179
|
|
|
$gallery[] = (object) array( |
180
|
|
|
'src' => (string) esc_url_raw( $attachment_image_src ), |
181
|
|
|
'id' => (string) $attachment->ID, |
182
|
|
|
'title' => (string) esc_attr( $attachment_image_title ), |
183
|
|
|
'alt' => (string) esc_attr( $attachment_image_alt ), |
184
|
|
|
'caption' => (string) $caption, |
185
|
|
|
'itemprop' => 'image', |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$color = Jetpack_Options::get_option( 'slideshow_background_color', 'black' ); |
190
|
|
|
|
191
|
|
|
$js_attr = array( |
192
|
|
|
'gallery' => $gallery, |
193
|
|
|
'selector' => $gallery_instance, |
194
|
|
|
'trans' => $attr['trans'] ? $attr['trans'] : 'fade', |
195
|
|
|
'autostart' => $attr['autostart'] ? $attr['autostart'] : 'true', |
196
|
|
|
'color' => $color, |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
// Show a link to the gallery in feeds. |
200
|
|
|
if ( is_feed() ) { |
201
|
|
|
return sprintf( |
202
|
|
|
'<a href="%s">%s</a>', |
203
|
|
|
esc_url( get_permalink( $post_id ) . '#' . $gallery_instance . '-slideshow' ), |
204
|
|
|
esc_html__( 'Click to view slideshow.', 'jetpack' ) |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $this->slideshow_js( $js_attr ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Render the slideshow js |
213
|
|
|
* |
214
|
|
|
* Returns the necessary markup and js to fire a slideshow. |
215
|
|
|
* |
216
|
|
|
* @param $attr array Attributes for the slideshow. |
217
|
|
|
* |
218
|
|
|
* @uses $this->enqueue_scripts() |
219
|
|
|
* |
220
|
|
|
* @return string HTML output. |
221
|
|
|
*/ |
222
|
|
|
function slideshow_js( $attr ) { |
223
|
|
|
// Enqueue scripts |
224
|
|
|
$this->enqueue_scripts(); |
225
|
|
|
|
226
|
|
|
$output = ''; |
227
|
|
|
|
228
|
|
|
if ( defined( 'JSON_HEX_AMP' ) ) { |
229
|
|
|
// This is nice to have, but not strictly necessary since we use _wp_specialchars() below |
230
|
|
|
$gallery = json_encode( $attr['gallery'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); |
231
|
|
|
} else { |
232
|
|
|
$gallery = json_encode( $attr['gallery'] ); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$output .= '<p class="jetpack-slideshow-noscript robots-nocontent">' . esc_html__( 'This slideshow requires JavaScript.', 'jetpack' ) . '</p>'; |
236
|
|
|
$output .= sprintf( |
237
|
|
|
'<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>', |
238
|
|
|
esc_attr( $attr['selector'] . '-slideshow' ), |
239
|
|
|
esc_attr( $attr['color'] ), |
240
|
|
|
esc_attr( $attr['trans'] ), |
241
|
|
|
esc_attr( $attr['autostart'] ), |
242
|
|
|
/* |
243
|
|
|
* The input to json_encode() above can contain '"'. |
244
|
|
|
* |
245
|
|
|
* For calls to json_encode() lacking the JSON_HEX_AMP option, |
246
|
|
|
* that '"' is left unaltered. Running '"' through esc_attr() |
247
|
|
|
* also leaves it unaltered since esc_attr() does not double-encode. |
248
|
|
|
* |
249
|
|
|
* This means we end up with an attribute like |
250
|
|
|
* `data-gallery="{"foo":"""}"`, |
251
|
|
|
* which is interpreted by the browser as `{"foo":"""}`, |
252
|
|
|
* which cannot be JSON decoded. |
253
|
|
|
* |
254
|
|
|
* The preferred workaround is to include the JSON_HEX_AMP (and friends) |
255
|
|
|
* options, but these are not available until 5.3.0. |
256
|
|
|
* Alternatively, we can use _wp_specialchars( , , , true ) instead of |
257
|
|
|
* esc_attr(), which will double-encode. |
258
|
|
|
* |
259
|
|
|
* Since we can't rely on JSON_HEX_AMP, we do both. |
260
|
|
|
*/ |
261
|
|
|
_wp_specialchars( wp_check_invalid_utf8( $gallery ), ENT_QUOTES, false, true ) |
262
|
|
|
); |
263
|
|
|
|
264
|
|
|
return $output; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Actually enqueues the scripts and styles. |
269
|
|
|
*/ |
270
|
|
|
function enqueue_scripts() { |
271
|
|
|
|
272
|
|
|
wp_enqueue_script( 'jquery-cycle', plugins_url( '/js/jquery.cycle.min.js', __FILE__ ), array( 'jquery' ), '20161231', true ); |
273
|
|
|
wp_enqueue_script( 'jetpack-slideshow', plugins_url( '/js/slideshow-shortcode.js', __FILE__ ), array( 'jquery-cycle' ), '20121214.1', true ); |
274
|
|
|
if ( is_rtl() ) { |
275
|
|
|
wp_enqueue_style( 'jetpack-slideshow', plugins_url( '/css/rtl/slideshow-shortcode-rtl.css', __FILE__ ) ); |
276
|
|
|
} else { |
277
|
|
|
wp_enqueue_style( 'jetpack-slideshow', plugins_url( '/css/slideshow-shortcode.css', __FILE__ ) ); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
wp_localize_script( |
281
|
|
|
'jetpack-slideshow', |
282
|
|
|
'jetpackSlideshowSettings', |
283
|
|
|
/** |
284
|
|
|
* Filters the slideshow JavaScript spinner. |
285
|
|
|
* |
286
|
|
|
* @module shortcodes |
287
|
|
|
* |
288
|
|
|
* @since 2.1.0 |
289
|
|
|
* |
290
|
|
|
* @param array $args |
291
|
|
|
* - string - spinner - URL of the spinner image. |
292
|
|
|
*/ |
293
|
|
|
apply_filters( |
294
|
|
|
'jetpack_js_slideshow_settings', array( |
295
|
|
|
'spinner' => plugins_url( '/img/slideshow-loader.gif', __FILE__ ), |
296
|
|
|
) |
297
|
|
|
) |
298
|
|
|
); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public static function init() { |
302
|
|
|
new Jetpack_Slideshow_Shortcode; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
Jetpack_Slideshow_Shortcode::init(); |
307
|
|
|
|