PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 14.2.2
Jetpack – WP Security, Backup, Speed, & Growth v14.2.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 2 years ago tiled-gallery 1 year ago tiled-gallery.php 2 years ago
tiled-gallery.php
383 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 array(
186 'in_footer' => true,
187 'strategy' => 'defer',
188 )
189 );
190 wp_enqueue_style( 'tiled-gallery', plugins_url( 'tiled-gallery/tiled-gallery.css', __FILE__ ), array(), '2023-08-21' );
191 wp_style_add_data( 'tiled-gallery', 'rtl', 'replace' );
192 }
193
194 /**
195 * The gallery shortcode.
196 *
197 * @param mixed $val - the value.
198 * @param array $atts - the attributes.
199 *
200 * @return string
201 */
202 public function gallery_shortcode( $val, $atts ) {
203 if ( ! empty( $val ) ) { // something else is overriding post_gallery, like a custom VIP shortcode
204 return $val;
205 }
206
207 $this->set_atts( $atts );
208
209 $attachments = $this->get_attachments();
210 if ( empty( $attachments ) ) {
211 return '';
212 }
213
214 if ( is_feed() || defined( 'IS_HTML_EMAIL' ) ) {
215 return '';
216 }
217
218 /**
219 * Filters the permissible Tiled Gallery types.
220 *
221 * @module tiled-gallery
222 *
223 * @since 3.7.0
224 *
225 * @param array Array of allowed types. Default: 'rectangular', 'square', 'circle', 'rectangle', 'columns'.
226 */
227 $talaveras = apply_filters( 'jetpack_tiled_gallery_types', self::$talaveras );
228
229 if ( in_array( $this->atts['type'], $talaveras, true ) ) {
230 // Enqueue styles and scripts
231 self::default_scripts_and_styles();
232
233 // Generate gallery HTML
234 $gallery_class = 'Jetpack_Tiled_Gallery_Layout_' . ucfirst( $this->atts['type'] );
235 $gallery = new $gallery_class( $attachments, $this->atts['link'], $this->atts['grayscale'], (int) $this->atts['columns'] );
236 $gallery_html = $gallery->HTML();
237
238 if ( $gallery_html && class_exists( 'Jetpack' ) && class_exists( Image_CDN::class ) ) {
239 // Tiled Galleries in Jetpack require that Photon be active.
240 // If it's not active, run it just on the gallery output.
241 if ( ! Image_CDN::is_enabled() && ! ( new Status() )->is_offline_mode() ) {
242 $gallery_html = Image_CDN::filter_the_content( $gallery_html );
243 }
244 }
245
246 return trim( preg_replace( '/\s+/', ' ', $gallery_html ) ); // remove any new lines from the output so that the reader parses it better
247 }
248
249 return '';
250 }
251
252 /**
253 * See if gallery is already defined.
254 *
255 * @return bool
256 */
257 public static function gallery_already_redefined() {
258 global $shortcode_tags;
259 $redefined = false;
260 if ( ! isset( $shortcode_tags['gallery'] ) || $shortcode_tags['gallery'] !== 'gallery_shortcode' ) {
261 $redefined = true;
262 }
263 /**
264 * Filter the output of the check for another plugin or theme affecting WordPress galleries.
265 *
266 * This will let folks that replace core’s shortcode confirm feature parity with it, so Jetpack's Tiled Galleries can still work.
267 *
268 * @module tiled-gallery
269 *
270 * @since 3.1.0
271 *
272 * @param bool $redefined Does another plugin or theme already redefines the default WordPress gallery?
273 */
274 return apply_filters( 'jetpack_tiled_gallery_shortcode_redefined', $redefined );
275 }
276
277 /**
278 * Initialize the tiled gallery.
279 */
280 public static function init() {
281 if ( self::gallery_already_redefined() ) {
282 return;
283 }
284
285 $gallery = new Jetpack_Tiled_Gallery();
286 add_filter( 'post_gallery', array( $gallery, 'gallery_shortcode' ), 1001, 2 );
287 }
288
289 /**
290 * Get the width of the gallery.
291 *
292 * @return int
293 */
294 public static function get_content_width() {
295 $tiled_gallery_content_width = Jetpack::get_content_width();
296
297 if ( ! $tiled_gallery_content_width ) {
298 $tiled_gallery_content_width = 500;
299 }
300
301 /**
302 * Filter overwriting the default content width.
303 *
304 * @module tiled-gallery
305 *
306 * @since 2.1.0
307 *
308 * @param string $tiled_gallery_content_width Default Tiled Gallery content width.
309 */
310 return apply_filters( 'tiled_gallery_content_width', $tiled_gallery_content_width );
311 }
312
313 /**
314 * Media UI integration
315 *
316 * @param array $types - the type of gallery.
317 *
318 * @return array
319 */
320 public function jetpack_gallery_types( $types ) {
321 if ( get_option( 'tiled_galleries' ) && isset( $types['default'] ) ) {
322 // Tiled is set as the default, meaning that type='default'
323 // will still display the mosaic.
324 $types['thumbnails'] = $types['default'];
325 unset( $types['default'] );
326 }
327
328 $types['rectangular'] = __( 'Tiled Mosaic', 'jetpack' );
329 $types['square'] = __( 'Square Tiles', 'jetpack' );
330 $types['circle'] = __( 'Circles', 'jetpack' );
331 $types['columns'] = __( 'Tiled Columns', 'jetpack' );
332
333 return $types;
334 }
335
336 /**
337 * Get the default gallery type.
338 *
339 * @return string
340 */
341 public function jetpack_default_gallery_type() {
342 return ( get_option( 'tiled_galleries' ) ? 'rectangular' : 'default' );
343 }
344
345 /**
346 * Get the talaveras.
347 *
348 * @return array
349 */
350 public static function get_talaveras() {
351 return self::$talaveras;
352 }
353
354 /**
355 * Add a checkbox field to the Carousel section in Settings > Media
356 * for setting tiled galleries as the default.
357 */
358 public function settings_api_init() {
359 global $wp_settings_sections;
360
361 // Add the setting field [tiled_galleries] and place it in Settings > Media
362 if ( isset( $wp_settings_sections['media']['carousel_section'] ) ) {
363 $section = 'carousel_section';
364 } else {
365 $section = 'default';
366 }
367
368 add_settings_field( 'tiled_galleries', __( 'Tiled Galleries', 'jetpack' ), array( $this, 'setting_html' ), 'media', $section );
369 register_setting( 'media', 'tiled_galleries', 'esc_attr' );
370 }
371
372 /**
373 * Render the settings HTML.
374 */
375 public function setting_html() {
376 echo '<label><input name="tiled_galleries" type="checkbox" value="1" ' .
377 checked( 1, '' !== get_option( 'tiled_galleries' ), false ) . ' /> ' .
378 esc_html__( 'Display all your gallery pictures in a cool mosaic.', 'jetpack' ) . '</br></label>';
379 }
380 }
381
382 add_action( 'init', array( 'Jetpack_Tiled_Gallery', 'init' ) );
383