PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 12.5.2
Jetpack – WP Security, Backup, Speed, & Growth v12.5.2
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 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7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3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / tiled-gallery / tiled-gallery.php
jetpack / modules / tiled-gallery Last commit date
math 3 years ago tiled-gallery 3 years ago tiled-gallery.php 3 years ago
tiled-gallery.php
380 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 use Automattic\Jetpack\Assets;
4 use Automattic\Jetpack\Image_CDN\Image_CDN;
5 use Automattic\Jetpack\Status;
6
7 // Include the class file containing methods for rounding constrained array elements.
8 // Here the constrained array element is the dimension of a row, group or an image in the tiled gallery.
9 require_once __DIR__ . '/math/class-constrained-array-rounding.php';
10
11 // Layouts
12 require_once __DIR__ . '/tiled-gallery/tiled-gallery-rectangular.php';
13 require_once __DIR__ . '/tiled-gallery/tiled-gallery-square.php';
14 require_once __DIR__ . '/tiled-gallery/tiled-gallery-circle.php';
15
16 /**
17 * Jetpack tiled gallery class.
18 */
19 class Jetpack_Tiled_Gallery {
20 /**
21 * Shortcode attributes.
22 *
23 * @var array
24 */
25 public $atts;
26
27 /**
28 * Text direction (right or left).
29 *
30 * @var string
31 */
32 public $float;
33
34 /**
35 * Supported gallery design types.
36 *
37 * @var array
38 */
39 private static $talaveras = array( 'rectangular', 'square', 'circle', 'rectangle', 'columns' );
40
41 /**
42 * Class constructor.
43 */
44 public function __construct() {
45 add_action( 'admin_init', array( $this, 'settings_api_init' ) );
46 add_filter( 'jetpack_gallery_types', array( $this, 'jetpack_gallery_types' ), 9 );
47 add_filter( 'jetpack_default_gallery_type', array( $this, 'jetpack_default_gallery_type' ) );
48 }
49
50 /**
51 * Check whether tiling is enabled.
52 *
53 * @return bool
54 */
55 public function tiles_enabled() {
56 return '' !== Jetpack_Options::get_option_and_ensure_autoload( 'tiled_galleries', '' );
57 }
58
59 /**
60 * Set attributes.
61 *
62 * @param array $atts - the attributes.
63 */
64 public function set_atts( $atts ) {
65 global $post;
66
67 $this->atts = shortcode_atts(
68 array(
69 'order' => 'ASC',
70 'orderby' => 'menu_order ID',
71 'id' => isset( $post->ID ) ? $post->ID : 0,
72 'include' => '',
73 'exclude' => '',
74 'type' => '',
75 'grayscale' => false,
76 'link' => '',
77 'columns' => 3,
78 ),
79 $atts,
80 'gallery'
81 );
82
83 $this->atts['id'] = (int) $this->atts['id'];
84 $this->float = is_rtl() ? 'right' : 'left';
85
86 // Default to rectangular is tiled galleries are checked
87 if ( $this->tiles_enabled() && ( ! $this->atts['type'] || 'default' === $this->atts['type'] ) ) {
88 /** This filter is already documented in class-jetpack-gallery-settings.php */
89 $this->atts['type'] = apply_filters( 'jetpack_default_gallery_type', 'rectangular' );
90 }
91
92 if ( ! $this->atts['orderby'] ) {
93 $this->atts['orderby'] = sanitize_sql_orderby( $this->atts['orderby'] );
94 if ( ! $this->atts['orderby'] ) {
95 $this->atts['orderby'] = 'menu_order ID';
96 }
97 }
98
99 if ( 'rand' === strtolower( $this->atts['order'] ) ) {
100 $this->atts['orderby'] = 'rand';
101 }
102
103 // We shouldn't have more than 20 columns.
104 if ( ! is_numeric( $this->atts['columns'] ) || 20 < $this->atts['columns'] ) {
105 $this->atts['columns'] = 3;
106 }
107 }
108
109 /**
110 * Get the media attachments.
111 *
112 * @return WP_Post[]
113 */
114 public function get_attachments() {
115 $atts = $this->atts;
116
117 if ( ! empty( $atts['include'] ) ) {
118 $include = preg_replace( '/[^0-9,]+/', '', $atts['include'] );
119 $_attachments = get_posts(
120 array(
121 'include' => $include,
122 'post_status' => 'inherit',
123 'post_type' => 'attachment',
124 'post_mime_type' => 'image',
125 'order' => $atts['order'],
126 'orderby' => $atts['orderby'],
127 'suppress_filters' => false,
128 )
129 );
130
131 $attachments = array();
132 foreach ( $_attachments as $key => $val ) {
133 $attachments[ $val->ID ] = $_attachments[ $key ];
134 }
135 } elseif ( 0 === $atts['id'] ) {
136 /*
137 * Should NEVER Happen but infinite_scroll_load_other_plugins_scripts means it does
138 * Querying with post_parent == 0 can generate stupidly memcache sets
139 * on sites with 10000's of unattached attachments as get_children puts every post in the cache.
140 * TODO Fix this properly.
141 */
142 $attachments = array();
143 } elseif ( ! empty( $atts['exclude'] ) ) {
144 $exclude = preg_replace( '/[^0-9,]+/', '', $atts['exclude'] );
145 $attachments = get_children(
146 array(
147 'post_parent' => $atts['id'],
148 'exclude' => $exclude,
149 'post_status' => 'inherit',
150 'post_type' => 'attachment',
151 'post_mime_type' => 'image',
152 'order' => $atts['order'],
153 'orderby' => $atts['orderby'],
154 'suppress_filters' => false,
155 )
156 );
157 } else {
158 $attachments = get_children(
159 array(
160 'post_parent' => $atts['id'],
161 'post_status' => 'inherit',
162 'post_type' => 'attachment',
163 'post_mime_type' => 'image',
164 'order' => $atts['order'],
165 'orderby' => $atts['orderby'],
166 'suppress_filters' => false,
167 )
168 );
169 }
170 return $attachments;
171 }
172
173 /**
174 * Enqueue the default scripts and styles.
175 */
176 public static function default_scripts_and_styles() {
177 wp_enqueue_script(
178 'tiled-gallery',
179 Assets::get_file_url_for_environment(
180 '_inc/build/tiled-gallery/tiled-gallery/tiled-gallery.min.js',
181 'modules/tiled-gallery/tiled-gallery/tiled-gallery.js'
182 ),
183 array(),
184 JETPACK__VERSION,
185 false
186 );
187 wp_enqueue_style( 'tiled-gallery', plugins_url( 'tiled-gallery/tiled-gallery.css', __FILE__ ), array(), '2012-09-21' );
188 wp_style_add_data( 'tiled-gallery', 'rtl', 'replace' );
189 }
190
191 /**
192 * The gallery shortcode.
193 *
194 * @param mixed $val - the value.
195 * @param array $atts - the attributes.
196 *
197 * @return string
198 */
199 public function gallery_shortcode( $val, $atts ) {
200 if ( ! empty( $val ) ) { // something else is overriding post_gallery, like a custom VIP shortcode
201 return $val;
202 }
203
204 $this->set_atts( $atts );
205
206 $attachments = $this->get_attachments();
207 if ( empty( $attachments ) ) {
208 return '';
209 }
210
211 if ( is_feed() || defined( 'IS_HTML_EMAIL' ) ) {
212 return '';
213 }
214
215 /**
216 * Filters the permissible Tiled Gallery types.
217 *
218 * @module tiled-gallery
219 *
220 * @since 3.7.0
221 *
222 * @param array Array of allowed types. Default: 'rectangular', 'square', 'circle', 'rectangle', 'columns'.
223 */
224 $talaveras = apply_filters( 'jetpack_tiled_gallery_types', self::$talaveras );
225
226 if ( in_array( $this->atts['type'], $talaveras, true ) ) {
227 // Enqueue styles and scripts
228 self::default_scripts_and_styles();
229
230 // Generate gallery HTML
231 $gallery_class = 'Jetpack_Tiled_Gallery_Layout_' . ucfirst( $this->atts['type'] );
232 $gallery = new $gallery_class( $attachments, $this->atts['link'], $this->atts['grayscale'], (int) $this->atts['columns'] );
233 $gallery_html = $gallery->HTML();
234
235 if ( $gallery_html && class_exists( 'Jetpack' ) && class_exists( Image_CDN::class ) ) {
236 // Tiled Galleries in Jetpack require that Photon be active.
237 // If it's not active, run it just on the gallery output.
238 if ( ! Image_CDN::is_enabled() && ! ( new Status() )->is_offline_mode() ) {
239 $gallery_html = Image_CDN::filter_the_content( $gallery_html );
240 }
241 }
242
243 return trim( preg_replace( '/\s+/', ' ', $gallery_html ) ); // remove any new lines from the output so that the reader parses it better
244 }
245
246 return '';
247 }
248
249 /**
250 * See if gallery is already defined.
251 *
252 * @return bool
253 */
254 public static function gallery_already_redefined() {
255 global $shortcode_tags;
256 $redefined = false;
257 if ( ! isset( $shortcode_tags['gallery'] ) || $shortcode_tags['gallery'] !== 'gallery_shortcode' ) {
258 $redefined = true;
259 }
260 /**
261 * Filter the output of the check for another plugin or theme affecting WordPress galleries.
262 *
263 * This will let folks that replace core’s shortcode confirm feature parity with it, so Jetpack's Tiled Galleries can still work.
264 *
265 * @module tiled-gallery
266 *
267 * @since 3.1.0
268 *
269 * @param bool $redefined Does another plugin or theme already redefines the default WordPress gallery?
270 */
271 return apply_filters( 'jetpack_tiled_gallery_shortcode_redefined', $redefined );
272 }
273
274 /**
275 * Initialize the tiled gallery.
276 */
277 public static function init() {
278 if ( self::gallery_already_redefined() ) {
279 return;
280 }
281
282 $gallery = new Jetpack_Tiled_Gallery();
283 add_filter( 'post_gallery', array( $gallery, 'gallery_shortcode' ), 1001, 2 );
284 }
285
286 /**
287 * Get the width of the gallery.
288 *
289 * @return int
290 */
291 public static function get_content_width() {
292 $tiled_gallery_content_width = Jetpack::get_content_width();
293
294 if ( ! $tiled_gallery_content_width ) {
295 $tiled_gallery_content_width = 500;
296 }
297
298 /**
299 * Filter overwriting the default content width.
300 *
301 * @module tiled-gallery
302 *
303 * @since 2.1.0
304 *
305 * @param string $tiled_gallery_content_width Default Tiled Gallery content width.
306 */
307 return apply_filters( 'tiled_gallery_content_width', $tiled_gallery_content_width );
308 }
309
310 /**
311 * Media UI integration
312 *
313 * @param array $types - the type of gallery.
314 *
315 * @return array
316 */
317 public function jetpack_gallery_types( $types ) {
318 if ( get_option( 'tiled_galleries' ) && isset( $types['default'] ) ) {
319 // Tiled is set as the default, meaning that type='default'
320 // will still display the mosaic.
321 $types['thumbnails'] = $types['default'];
322 unset( $types['default'] );
323 }
324
325 $types['rectangular'] = __( 'Tiled Mosaic', 'jetpack' );
326 $types['square'] = __( 'Square Tiles', 'jetpack' );
327 $types['circle'] = __( 'Circles', 'jetpack' );
328 $types['columns'] = __( 'Tiled Columns', 'jetpack' );
329
330 return $types;
331 }
332
333 /**
334 * Get the default gallery type.
335 *
336 * @return string
337 */
338 public function jetpack_default_gallery_type() {
339 return ( get_option( 'tiled_galleries' ) ? 'rectangular' : 'default' );
340 }
341
342 /**
343 * Get the talaveras.
344 *
345 * @return array
346 */
347 public static function get_talaveras() {
348 return self::$talaveras;
349 }
350
351 /**
352 * Add a checkbox field to the Carousel section in Settings > Media
353 * for setting tiled galleries as the default.
354 */
355 public function settings_api_init() {
356 global $wp_settings_sections;
357
358 // Add the setting field [tiled_galleries] and place it in Settings > Media
359 if ( isset( $wp_settings_sections['media']['carousel_section'] ) ) {
360 $section = 'carousel_section';
361 } else {
362 $section = 'default';
363 }
364
365 add_settings_field( 'tiled_galleries', __( 'Tiled Galleries', 'jetpack' ), array( $this, 'setting_html' ), 'media', $section );
366 register_setting( 'media', 'tiled_galleries', 'esc_attr' );
367 }
368
369 /**
370 * Render the settings HTML.
371 */
372 public function setting_html() {
373 echo '<label><input name="tiled_galleries" type="checkbox" value="1" ' .
374 checked( 1, '' !== get_option( 'tiled_galleries' ), false ) . ' /> ' .
375 esc_html__( 'Display all your gallery pictures in a cool mosaic.', 'jetpack' ) . '</br></label>';
376 }
377 }
378
379 add_action( 'init', array( 'Jetpack_Tiled_Gallery', 'init' ) );
380