PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 12.0.3
Jetpack – WP Security, Backup, Speed, & Growth v12.0.3
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 4 years ago
tiled-gallery.php
365 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 use Automattic\Jetpack\Assets;
4 use Automattic\Jetpack\Status;
5
6 // Include the class file containing methods for rounding constrained array elements.
7 // Here the constrained array element is the dimension of a row, group or an image in the tiled gallery.
8 require_once __DIR__ . '/math/class-constrained-array-rounding.php';
9
10 // Layouts
11 require_once __DIR__ . '/tiled-gallery/tiled-gallery-rectangular.php';
12 require_once __DIR__ . '/tiled-gallery/tiled-gallery-square.php';
13 require_once __DIR__ . '/tiled-gallery/tiled-gallery-circle.php';
14
15 /**
16 * Jetpack tiled gallery class.
17 */
18 class Jetpack_Tiled_Gallery {
19 /**
20 * Supported gallery design types.
21 *
22 * @var array
23 */
24 private static $talaveras = array( 'rectangular', 'square', 'circle', 'rectangle', 'columns' );
25
26 /**
27 * Class constructor.
28 */
29 public function __construct() {
30 add_action( 'admin_init', array( $this, 'settings_api_init' ) );
31 add_filter( 'jetpack_gallery_types', array( $this, 'jetpack_gallery_types' ), 9 );
32 add_filter( 'jetpack_default_gallery_type', array( $this, 'jetpack_default_gallery_type' ) );
33 }
34
35 /**
36 * Check whether tiling is enabled.
37 *
38 * @return bool
39 */
40 public function tiles_enabled() {
41 return '' !== Jetpack_Options::get_option_and_ensure_autoload( 'tiled_galleries', '' );
42 }
43
44 /**
45 * Set attributes.
46 *
47 * @param array $atts - the attributes.
48 */
49 public function set_atts( $atts ) {
50 global $post;
51
52 $this->atts = shortcode_atts(
53 array(
54 'order' => 'ASC',
55 'orderby' => 'menu_order ID',
56 'id' => isset( $post->ID ) ? $post->ID : 0,
57 'include' => '',
58 'exclude' => '',
59 'type' => '',
60 'grayscale' => false,
61 'link' => '',
62 'columns' => 3,
63 ),
64 $atts,
65 'gallery'
66 );
67
68 $this->atts['id'] = (int) $this->atts['id'];
69 $this->float = is_rtl() ? 'right' : 'left';
70
71 // Default to rectangular is tiled galleries are checked
72 if ( $this->tiles_enabled() && ( ! $this->atts['type'] || 'default' === $this->atts['type'] ) ) {
73 /** This filter is already documented in class-jetpack-gallery-settings.php */
74 $this->atts['type'] = apply_filters( 'jetpack_default_gallery_type', 'rectangular' );
75 }
76
77 if ( ! $this->atts['orderby'] ) {
78 $this->atts['orderby'] = sanitize_sql_orderby( $this->atts['orderby'] );
79 if ( ! $this->atts['orderby'] ) {
80 $this->atts['orderby'] = 'menu_order ID';
81 }
82 }
83
84 if ( 'rand' === strtolower( $this->atts['order'] ) ) {
85 $this->atts['orderby'] = 'rand';
86 }
87
88 // We shouldn't have more than 20 columns.
89 if ( ! is_numeric( $this->atts['columns'] ) || 20 < $this->atts['columns'] ) {
90 $this->atts['columns'] = 3;
91 }
92 }
93
94 /**
95 * Get the media attachments.
96 *
97 * @return WP_Post[]
98 */
99 public function get_attachments() {
100 $atts = $this->atts;
101
102 if ( ! empty( $atts['include'] ) ) {
103 $include = preg_replace( '/[^0-9,]+/', '', $atts['include'] );
104 $_attachments = get_posts(
105 array(
106 'include' => $include,
107 'post_status' => 'inherit',
108 'post_type' => 'attachment',
109 'post_mime_type' => 'image',
110 'order' => $atts['order'],
111 'orderby' => $atts['orderby'],
112 'suppress_filters' => false,
113 )
114 );
115
116 $attachments = array();
117 foreach ( $_attachments as $key => $val ) {
118 $attachments[ $val->ID ] = $_attachments[ $key ];
119 }
120 } elseif ( 0 === $atts['id'] ) {
121 /*
122 * Should NEVER Happen but infinite_scroll_load_other_plugins_scripts means it does
123 * Querying with post_parent == 0 can generate stupidly memcache sets
124 * on sites with 10000's of unattached attachments as get_children puts every post in the cache.
125 * TODO Fix this properly.
126 */
127 $attachments = array();
128 } elseif ( ! empty( $atts['exclude'] ) ) {
129 $exclude = preg_replace( '/[^0-9,]+/', '', $atts['exclude'] );
130 $attachments = get_children(
131 array(
132 'post_parent' => $atts['id'],
133 'exclude' => $exclude,
134 'post_status' => 'inherit',
135 'post_type' => 'attachment',
136 'post_mime_type' => 'image',
137 'order' => $atts['order'],
138 'orderby' => $atts['orderby'],
139 'suppress_filters' => false,
140 )
141 );
142 } else {
143 $attachments = get_children(
144 array(
145 'post_parent' => $atts['id'],
146 'post_status' => 'inherit',
147 'post_type' => 'attachment',
148 'post_mime_type' => 'image',
149 'order' => $atts['order'],
150 'orderby' => $atts['orderby'],
151 'suppress_filters' => false,
152 )
153 );
154 }
155 return $attachments;
156 }
157
158 /**
159 * Enqueue the default scripts and styles.
160 */
161 public static function default_scripts_and_styles() {
162 wp_enqueue_script(
163 'tiled-gallery',
164 Assets::get_file_url_for_environment(
165 '_inc/build/tiled-gallery/tiled-gallery/tiled-gallery.min.js',
166 'modules/tiled-gallery/tiled-gallery/tiled-gallery.js'
167 ),
168 array(),
169 JETPACK__VERSION,
170 false
171 );
172 wp_enqueue_style( 'tiled-gallery', plugins_url( 'tiled-gallery/tiled-gallery.css', __FILE__ ), array(), '2012-09-21' );
173 wp_style_add_data( 'tiled-gallery', 'rtl', 'replace' );
174 }
175
176 /**
177 * The gallery shortcode.
178 *
179 * @param mixed $val - the value.
180 * @param array $atts - the attributes.
181 *
182 * @return string
183 */
184 public function gallery_shortcode( $val, $atts ) {
185 if ( ! empty( $val ) ) { // something else is overriding post_gallery, like a custom VIP shortcode
186 return $val;
187 }
188
189 $this->set_atts( $atts );
190
191 $attachments = $this->get_attachments();
192 if ( empty( $attachments ) ) {
193 return '';
194 }
195
196 if ( is_feed() || defined( 'IS_HTML_EMAIL' ) ) {
197 return '';
198 }
199
200 /**
201 * Filters the permissible Tiled Gallery types.
202 *
203 * @module tiled-gallery
204 *
205 * @since 3.7.0
206 *
207 * @param array Array of allowed types. Default: 'rectangular', 'square', 'circle', 'rectangle', 'columns'.
208 */
209 $talaveras = apply_filters( 'jetpack_tiled_gallery_types', self::$talaveras );
210
211 if ( in_array( $this->atts['type'], $talaveras, true ) ) {
212 // Enqueue styles and scripts
213 self::default_scripts_and_styles();
214
215 // Generate gallery HTML
216 $gallery_class = 'Jetpack_Tiled_Gallery_Layout_' . ucfirst( $this->atts['type'] );
217 $gallery = new $gallery_class( $attachments, $this->atts['link'], $this->atts['grayscale'], (int) $this->atts['columns'] );
218 $gallery_html = $gallery->HTML();
219
220 if ( $gallery_html && class_exists( 'Jetpack' ) && class_exists( 'Jetpack_Photon' ) ) {
221 // Tiled Galleries in Jetpack require that Photon be active.
222 // If it's not active, run it just on the gallery output.
223 if ( ! in_array( 'photon', Jetpack::get_active_modules(), true ) && ! ( new Status() )->is_offline_mode() ) {
224 $gallery_html = Jetpack_Photon::filter_the_content( $gallery_html );
225 }
226 }
227
228 return trim( preg_replace( '/\s+/', ' ', $gallery_html ) ); // remove any new lines from the output so that the reader parses it better
229 }
230
231 return '';
232 }
233
234 /**
235 * See if gallery is already defined.
236 *
237 * @return bool
238 */
239 public static function gallery_already_redefined() {
240 global $shortcode_tags;
241 $redefined = false;
242 if ( ! isset( $shortcode_tags['gallery'] ) || $shortcode_tags['gallery'] !== 'gallery_shortcode' ) {
243 $redefined = true;
244 }
245 /**
246 * Filter the output of the check for another plugin or theme affecting WordPress galleries.
247 *
248 * This will let folks that replace core’s shortcode confirm feature parity with it, so Jetpack's Tiled Galleries can still work.
249 *
250 * @module tiled-gallery
251 *
252 * @since 3.1.0
253 *
254 * @param bool $redefined Does another plugin or theme already redefines the default WordPress gallery?
255 */
256 return apply_filters( 'jetpack_tiled_gallery_shortcode_redefined', $redefined );
257 }
258
259 /**
260 * Initialize the tiled gallery.
261 */
262 public static function init() {
263 if ( self::gallery_already_redefined() ) {
264 return;
265 }
266
267 $gallery = new Jetpack_Tiled_Gallery();
268 add_filter( 'post_gallery', array( $gallery, 'gallery_shortcode' ), 1001, 2 );
269 }
270
271 /**
272 * Get the width of the gallery.
273 *
274 * @return int
275 */
276 public static function get_content_width() {
277 $tiled_gallery_content_width = Jetpack::get_content_width();
278
279 if ( ! $tiled_gallery_content_width ) {
280 $tiled_gallery_content_width = 500;
281 }
282
283 /**
284 * Filter overwriting the default content width.
285 *
286 * @module tiled-gallery
287 *
288 * @since 2.1.0
289 *
290 * @param string $tiled_gallery_content_width Default Tiled Gallery content width.
291 */
292 return apply_filters( 'tiled_gallery_content_width', $tiled_gallery_content_width );
293 }
294
295 /**
296 * Media UI integration
297 *
298 * @param array $types - the type of gallery.
299 *
300 * @return array
301 */
302 public function jetpack_gallery_types( $types ) {
303 if ( get_option( 'tiled_galleries' ) && isset( $types['default'] ) ) {
304 // Tiled is set as the default, meaning that type='default'
305 // will still display the mosaic.
306 $types['thumbnails'] = $types['default'];
307 unset( $types['default'] );
308 }
309
310 $types['rectangular'] = __( 'Tiled Mosaic', 'jetpack' );
311 $types['square'] = __( 'Square Tiles', 'jetpack' );
312 $types['circle'] = __( 'Circles', 'jetpack' );
313 $types['columns'] = __( 'Tiled Columns', 'jetpack' );
314
315 return $types;
316 }
317
318 /**
319 * Get the default gallery type.
320 *
321 * @return string
322 */
323 public function jetpack_default_gallery_type() {
324 return ( get_option( 'tiled_galleries' ) ? 'rectangular' : 'default' );
325 }
326
327 /**
328 * Get the talaveras.
329 *
330 * @return array
331 */
332 public static function get_talaveras() {
333 return self::$talaveras;
334 }
335
336 /**
337 * Add a checkbox field to the Carousel section in Settings > Media
338 * for setting tiled galleries as the default.
339 */
340 public function settings_api_init() {
341 global $wp_settings_sections;
342
343 // Add the setting field [tiled_galleries] and place it in Settings > Media
344 if ( isset( $wp_settings_sections['media']['carousel_section'] ) ) {
345 $section = 'carousel_section';
346 } else {
347 $section = 'default';
348 }
349
350 add_settings_field( 'tiled_galleries', __( 'Tiled Galleries', 'jetpack' ), array( $this, 'setting_html' ), 'media', $section );
351 register_setting( 'media', 'tiled_galleries', 'esc_attr' );
352 }
353
354 /**
355 * Render the settings HTML.
356 */
357 public function setting_html() {
358 echo '<label><input name="tiled_galleries" type="checkbox" value="1" ' .
359 checked( 1, '' !== get_option( 'tiled_galleries' ), false ) . ' /> ' .
360 esc_html__( 'Display all your gallery pictures in a cool mosaic.', 'jetpack' ) . '</br></label>';
361 }
362 }
363
364 add_action( 'init', array( 'Jetpack_Tiled_Gallery', 'init' ) );
365