Completed
Push — update/remove-disconnect-link ( 4b6a2c )
by
unknown
73:18 queued 63:47
created

modules/widgets/gallery.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
Plugin Name: Gallery
5
Description: Gallery widget
6
Author: Automattic, Inc.
7
Version: 1.0
8
Author URI: http://automattic.com
9
*/
10
11
class Jetpack_Gallery_Widget extends WP_Widget {
12
	const THUMB_SIZE 		= 45;
13
	const DEFAULT_WIDTH 	= 265;
14
15
	protected $_instance_width ;
16
17
	public function __construct() {
18
		$widget_ops 	= array(
0 ignored issues
show
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

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