PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.1
Jetpack – WP Security, Backup, Speed, & Growth v12.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 / widgets / gallery.php

gallery.php in Jetpack – WP Security, Backup, Speed, & Growth 12.1, at modules/widgets/gallery.php

521 lines 16.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore eWordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Module Name: Gallery widget
4 *
5 * @package automattic/jetpack
6 */
7
8 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
9
10 use Automattic\Jetpack\Assets;
11
12 /**
13 * Jetpack_Gallery_Widget main class.
14 */
15 class Jetpack_Gallery_Widget extends WP_Widget {
16 const THUMB_SIZE = 45;
17 const DEFAULT_WIDTH = 265;
18
19 /**
20 * The width of the gallery widget.
21 * May be customized by the 'gallery_widget_content_width' filter.
22 *
23 * @var int
24 */
25 protected $instance_width;
26
27 /**
28 * Jetpack_Gallery_Widget constructor.
29 */
30 public function __construct() {
31 $widget_ops = array(
32 'classname' => 'widget-gallery',
33 'description' => __( 'Display a photo gallery or slideshow', 'jetpack' ),
34 'customize_selective_refresh' => true,
35 );
36
37 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
38
39 parent::__construct(
40 'gallery',
41 /** This filter is documented in modules/widgets/facebook-likebox.php */
42 apply_filters( 'jetpack_widget_name', __( 'Gallery', 'jetpack' ) ),
43 $widget_ops
44 );
45
46 if ( is_customize_preview() ) {
47 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
48
49 if ( class_exists( 'Jetpack_Tiled_Gallery' ) ) {
50 add_action( 'wp_enqueue_scripts', array( 'Jetpack_Tiled_Gallery', 'default_scripts_and_styles' ) );
51 }
52
53 if ( class_exists( 'Jetpack_Slideshow_Shortcode' ) ) {
54 $slideshow = new Jetpack_Slideshow_Shortcode();
55 add_action( 'wp_enqueue_scripts', array( $slideshow, 'enqueue_scripts' ) );
56 }
57
58 if ( class_exists( 'Jetpack_Carousel' ) ) {
59 $carousel = new Jetpack_Carousel();
60 add_action( 'wp_enqueue_scripts', array( $carousel, 'enqueue_assets' ) );
61 }
62 }
63 }
64
65 /**
66 * Display the widget.
67 *
68 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
69 * @param array $instance The settings for the particular instance of the widget.
70 */
71 public function widget( $args, $instance ) {
72 $instance = wp_parse_args( (array) $instance, $this->defaults() );
73
74 $this->enqueue_frontend_scripts();
75
76 $before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : '';
77 $before_title = isset( $args['before_title'] ) ? $args['before_title'] : '';
78 $after_title = isset( $args['after_title'] ) ? $args['after_title'] : '';
79 $after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : '';
80
81 $instance['attachments'] = $this->get_attachments( $instance );
82
83 $classes = array();
84
85 $classes[] = 'widget-gallery-' . $instance['type'];
86
87 /*
88 * Due to a bug in the carousel plugin,
89 * carousels will be triggered for all tiled galleries that exist on a page with other tiled galleries,
90 * regardless of whether or not the widget was set to Carousel mode.
91 * The onClick selector is simply too broad, since it was not written with widgets in mind.
92 * This special class prevents that behavior, via an override handler in gallery.js.
93 */
94 if ( 'carousel' !== $instance['link'] && 'slideshow' !== $instance['type'] ) {
95 $classes[] = 'no-carousel';
96 } else {
97 $classes[] = 'carousel';
98 }
99
100 $classes = implode( ' ', $classes );
101
102 if ( 'carousel' === $instance['link'] ) {
103 require_once plugin_dir_path( realpath( __DIR__ . '/../carousel/jetpack-carousel.php' ) ) . 'jetpack-carousel.php';
104
105 if ( class_exists( 'Jetpack_Carousel' ) ) {
106 // Create new carousel so we can use the enqueue_assets() method. Not ideal, but there is a decent amount
107 // of logic in that method that shouldn't be duplicated.
108 $carousel = new Jetpack_Carousel();
109
110 // First parameter is $output, which comes from filters, and causes bypass of the asset enqueuing. Passing null is correct.
111 $carousel->enqueue_assets( null );
112 }
113 }
114
115 echo $before_widget . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
116
117 /** This filter is documented in core/src/wp-includes/default-widgets.php */
118 $title = apply_filters( 'widget_title', $instance['title'] );
119
120 if ( $title ) {
121 echo $before_title . $title . $after_title . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
122 }
123
124 echo '<div class="' . esc_attr( $classes ) . '">' . "\n";
125
126 $method = $instance['type'] . '_widget';
127
128 /**
129 * Allow the width of a gallery to be altered by themes or other code.
130 *
131 * @module widgets
132 *
133 * @since 2.5.0
134 *
135 * @param int self::DEFAULT_WIDTH Default gallery width. Default is 265.
136 * @param string $args Display arguments including before_title, after_title, before_widget, and after_widget.
137 * @param array $instance The settings for the particular instance of the widget.
138 */
139 $this->instance_width = apply_filters( 'gallery_widget_content_width', self::DEFAULT_WIDTH, $args, $instance );
140
141 // Register a filter to modify the tiled_gallery_content_width, so Jetpack_Tiled_Gallery
142 // can appropriately size the tiles.
143 add_filter( 'tiled_gallery_content_width', array( $this, 'tiled_gallery_content_width' ) );
144
145 if ( method_exists( $this, $method ) ) {
146 echo $this->$method( $args, $instance ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
147 }
148
149 // Remove the stored $instance_width, as it is no longer needed.
150 $this->instance_width = null;
151
152 // Remove the filter, so any Jetpack_Tiled_Gallery in a post is not affected.
153 remove_filter( 'tiled_gallery_content_width', array( $this, 'tiled_gallery_content_width' ) );
154
155 echo "\n" . '</div>'; // .widget-gallery-$type
156
157 echo "\n" . $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
158
159 /** This action is documented in modules/widgets/gravatar-profile.php */
160 do_action( 'jetpack_stats_extra', 'widget_view', 'gallery' );
161 }
162
163 /**
164 * Fetch the images attached to the gallery Widget
165 *
166 * @param array $instance The Widget instance for which you'd like attachments.
167 * @return array Array of attachment objects for the Widget in $instance
168 */
169 public function get_attachments( $instance ) {
170 $ids = explode( ',', $instance['ids'] );
171
172 if ( isset( $instance['random'] ) && 'on' === $instance['random'] ) {
173 shuffle( $ids );
174 }
175
176 $attachments_query = new WP_Query(
177 array(
178 'post__in' => $ids,
179 'post_status' => 'inherit',
180 'post_type' => 'attachment',
181 'post_mime_type' => 'image',
182 'posts_per_page' => -1,
183 'orderby' => 'post__in',
184 )
185 );
186
187 $attachments = $attachments_query->get_posts();
188
189 wp_reset_postdata();
190
191 return $attachments;
192 }
193
194 /**
195 * Generate HTML for a rectangular, tiled Widget
196 *
197 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
198 * @param array $instance The Widget instance to generate HTML for.
199 * @return string String of HTML representing a rectangular gallery
200 */
201 public function rectangular_widget( $args, $instance ) {
202 if ( ! class_exists( 'Jetpack_Tiled_Gallery' )
203 && ! class_exists( 'Jetpack_Tiled_Gallery_Layout_Rectangular' ) ) {
204 return;
205 }
206
207 Jetpack_Tiled_Gallery::default_scripts_and_styles();
208
209 $layout = new Jetpack_Tiled_Gallery_Layout_Rectangular( $instance['attachments'], $instance['link'], false, 3 );
210 return $layout->HTML();
211 }
212
213 /**
214 * Generate HTML for a square (grid style) Widget
215 *
216 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
217 * @param array $instance The Widget instance to generate HTML for.
218 * @return string String of HTML representing a square gallery
219 */
220 public function square_widget( $args, $instance ) {
221 if ( ! class_exists( 'Jetpack_Tiled_Gallery' )
222 && ! class_exists( 'Jetpack_Tiled_Gallery_Layout_Square' ) ) {
223 return;
224 }
225
226 Jetpack_Tiled_Gallery::default_scripts_and_styles();
227
228 $layout = new Jetpack_Tiled_Gallery_Layout_Square( $instance['attachments'], $instance['link'], false, 3 );
229 return $layout->HTML();
230 }
231
232 /**
233 * Generate HTML for a circular (grid style) Widget
234 *
235 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
236 * @param array $instance The Widget instance to generate HTML for.
237 * @return string String of HTML representing a circular gallery
238 */
239 public function circle_widget( $args, $instance ) {
240 if ( ! class_exists( 'Jetpack_Tiled_Gallery' )
241 && ! class_exists( 'Jetpack_Tiled_Gallery_Layout_Circle' ) ) {
242 return;
243 }
244
245 Jetpack_Tiled_Gallery::default_scripts_and_styles();
246
247 $layout = new Jetpack_Tiled_Gallery_Layout_Circle( $instance['attachments'], $instance['link'], false, 3 );
248 return $layout->HTML();
249 }
250
251 /**
252 * Generate HTML for a slideshow Widget
253 *
254 * @todo Is slideshow_widget() still used?
255 *
256 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
257 * @param array $instance The Widget instance to generate HTML for.
258 * @return string String of HTML representing a slideshow gallery
259 */
260 public function slideshow_widget( $args, $instance ) {
261 global $content_width;
262
263 require_once plugin_dir_path( realpath( __DIR__ . '/../shortcodes/slideshow.php' ) ) . 'slideshow.php';
264
265 if ( ! class_exists( 'Jetpack_Slideshow_Shortcode' ) ) {
266 return;
267 }
268
269 if ( count( $instance['attachments'] ) < 1 ) {
270 return;
271 }
272
273 $slideshow = new Jetpack_Slideshow_Shortcode();
274
275 $slideshow->enqueue_scripts();
276
277 $gallery_instance = 'widget-' . $args['widget_id'];
278
279 $gallery = array();
280
281 foreach ( $instance['attachments'] as $attachment ) {
282 $attachment_image_src = wp_get_attachment_image_src( $attachment->ID, 'full' );
283 $attachment_image_src = jetpack_photon_url( $attachment_image_src[0], array( 'w' => $this->instance_width ) ); /** [url, width, height] */
284
285 $caption = wptexturize( wp_strip_all_tags( $attachment->post_excerpt ) );
286
287 $gallery[] = (object) array(
288 'src' => (string) esc_url_raw( $attachment_image_src ),
289 'id' => (string) $attachment->ID,
290 'caption' => (string) $caption,
291 );
292 }
293
294 $max_width = (int) get_option( 'large_size_w' );
295 $max_height = 175;
296
297 if ( (int) $content_width > 0 ) {
298 $max_width = min( (int) $content_width, $max_width );
299 }
300
301 $color = Jetpack_Options::get_option( 'slideshow_background_color', 'black' );
302 $autostart = isset( $attr['autostart'] ) ? $attr['autostart'] : true; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable -- Todo: should read off the $instance? Also not sure if slideshow_widget() is used still.
303
304 $js_attr = array(
305 'gallery' => $gallery,
306 'selector' => $gallery_instance,
307 'width' => $max_width,
308 'height' => $max_height,
309 'trans' => 'fade',
310 'color' => $color,
311 'autostart' => $autostart,
312 );
313
314 $html = $slideshow->slideshow_js( $js_attr );
315
316 return $html;
317 }
318
319 /**
320 * Used to adjust the content width of Jetpack_Tiled_Gallery's in sidebars
321 *
322 * $this->instance_width is filtered in widget() and this filter is added then removed in widget()
323 *
324 * @return int The filtered width
325 */
326 public function tiled_gallery_content_width() {
327 return $this->instance_width;
328 }
329
330 /**
331 * Outputs the widget settings form.
332 *
333 * @param array $instance Current settings.
334 */
335 public function form( $instance ) {
336 $defaults = $this->defaults();
337 $allowed_values = $this->allowed_values(); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Used in included form template.
338
339 $instance = wp_parse_args( (array) $instance, $defaults );
340
341 include __DIR__ . '/gallery/templates/form.php';
342 }
343
344 /**
345 * Save the widget options.
346 *
347 * @param array $new_instance The new instance options.
348 * @param array $old_instance The old instance options.
349 * @return array The saved options.
350 */
351 public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
352 $instance = $this->sanitize( $new_instance );
353
354 return $instance;
355 }
356
357 /**
358 * Sanitize the $instance's values to the set of allowed values. If a value is not acceptable,
359 * it is set to its default.
360 *
361 * Helps keep things nice and secure by listing only allowed values.
362 *
363 * @param array $instance The Widget instance to sanitize values for.
364 * @return array $instance The Widget instance with values sanitized
365 */
366 public function sanitize( $instance ) {
367 $allowed_values = $this->allowed_values();
368 $defaults = $this->defaults();
369
370 foreach ( $instance as $key => $value ) {
371 if ( ! is_scalar( $value ) ) {
372 // $instance may hold an Array value type for the Jetpack widget visibility feature.
373 continue;
374 }
375
376 $value = trim( $value );
377
378 if ( isset( $allowed_values[ $key ] ) && $allowed_values[ $key ] && ! array_key_exists( $value, $allowed_values[ $key ] ) ) {
379 $instance[ $key ] = $defaults[ $key ];
380 } else {
381 $instance[ $key ] = sanitize_text_field( $value );
382 }
383 }
384
385 return $instance;
386 }
387
388 /**
389 * Return a multi-dimensional array of allowed values (and their labels) for all widget form
390 * elements
391 *
392 * To allow all values on an input, omit it from the returned array
393 *
394 * @return array Array of allowed values for each option
395 */
396 public function allowed_values() {
397 $max_columns = 5;
398
399 // Create an associative array of allowed column values. This just automates the generation of
400 // column <option>s, from 1 to $max_columns.
401 $allowed_columns = array_combine( range( 1, $max_columns ), range( 1, $max_columns ) );
402
403 return array(
404 'type' => array(
405 'rectangular' => __( 'Tiles', 'jetpack' ),
406 'square' => __( 'Square Tiles', 'jetpack' ),
407 'circle' => __( 'Circles', 'jetpack' ),
408 'slideshow' => __( 'Slideshow', 'jetpack' ),
409 ),
410 'columns' => $allowed_columns,
411 'link' => array(
412 'carousel' => __( 'Carousel', 'jetpack' ),
413 'post' => __( 'Attachment Page', 'jetpack' ),
414 'file' => __( 'Media File', 'jetpack' ),
415 ),
416 );
417 }
418
419 /**
420 * Return an associative array of default values
421 *
422 * These values are used in new widgets as well as when sanitizing input. If a given value is not allowed,
423 * as defined in allowed_values(), that input is set to the default value defined here.
424 *
425 * @return array Array of default values for the Widget's options
426 */
427 public function defaults() {
428 return array(
429 'title' => '',
430 'type' => 'rectangular',
431 'ids' => '',
432 'columns' => 3,
433 'link' => 'carousel',
434 );
435 }
436
437 /**
438 * Enqueue frontend scripts.
439 */
440 public function enqueue_frontend_scripts() {
441 wp_register_script(
442 'gallery-widget',
443 Assets::get_file_url_for_environment(
444 '_inc/build/widgets/gallery/js/gallery.min.js',
445 'modules/widgets/gallery/js/gallery.js'
446 ),
447 array( 'jquery' ),
448 JETPACK__VERSION,
449 false
450 );
451
452 wp_enqueue_script( 'gallery-widget' );
453 }
454
455 /**
456 * Enqueue admin scripts and styles.
457 */
458 public function enqueue_admin_scripts() {
459 global $pagenow;
460
461 if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) {
462 wp_enqueue_media();
463
464 wp_enqueue_script(
465 'gallery-widget-admin',
466 Assets::get_file_url_for_environment(
467 '_inc/build/widgets/gallery/js/admin.min.js',
468 'modules/widgets/gallery/js/admin.js'
469 ),
470 array(
471 'jquery',
472 'media-models',
473 'media-views',
474 ),
475 '20150501',
476 false
477 );
478
479 $js_settings = array(
480 'thumbSize' => self::THUMB_SIZE,
481 );
482
483 wp_localize_script( 'gallery-widget-admin', '_wpGalleryWidgetAdminSettings', $js_settings );
484 wp_enqueue_style(
485 'gallery-widget-admin',
486 plugins_url( '/gallery/css/admin.css', __FILE__ ),
487 array(),
488 JETPACK__VERSION
489 );
490 wp_style_add_data( 'gallery-widget-admin', 'rtl', 'replace' );
491 }
492 }
493 }
494
495 add_action( 'widgets_init', 'jetpack_gallery_widget_init' );
496
497 /**
498 * Jetpack Gallery widget init; the widget is conditionally registered.
499 */
500 function jetpack_gallery_widget_init() {
501 /**
502 * Allow the Gallery Widget to be enabled even when Core supports the Media Gallery Widget
503 *
504 * @module widgets
505 *
506 * @since 5.5.0
507 *
508 * @param bool false Whether to force-enable the gallery widget
509 */
510 if (
511 ! apply_filters( 'jetpack_force_enable_gallery_widget', false )
512 && class_exists( 'WP_Widget_Media_Gallery' )
513 && Jetpack_Options::get_option( 'gallery_widget_migration' )
514 ) {
515 return;
516 }
517 if ( ! method_exists( 'Jetpack', 'is_module_active' ) || Jetpack::is_module_active( 'tiled-gallery' ) ) {
518 register_widget( 'Jetpack_Gallery_Widget' );
519 }
520 }
521