PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 10.5.3
Jetpack – WP Security, Backup, Speed, & Growth v10.5.3
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 10.5.3, at modules/widgets/gallery.php

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