Completed
Push — update/simplify-jetpack-build-... ( f269d3...3a2ec1 )
by Bernhard
09:10 queued 58s
created

Jetpack_Slideshow_Shortcode::settings_select()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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