PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.0.3
Jetpack – WP Security, Backup, Speed, & Growth v12.0.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 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 All 508 releases
jetpack / modules / tiled-gallery / tiled-gallery / tiled-gallery-shape.php

tiled-gallery-shape.php in Jetpack – WP Security, Backup, Speed, & Growth 12.0.3, at modules/tiled-gallery/tiled-gallery/tiled-gallery-shape.php

444 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 // phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
3 /**
4 * Jetpack tiled gallery shape class.
5 */
6 class Jetpack_Tiled_Gallery_Shape {
7
8 /**
9 * Shapes used.
10 *
11 * @var array
12 */
13 public static $shapes_used = array();
14
15 /**
16 * Constructor class.
17 *
18 * @param object $images - the images.
19 */
20 public function __construct( $images ) {
21 $this->images = $images;
22 $this->images_left = count( $images );
23 }
24
25 /**
26 * Return the sum of ratio images.
27 *
28 * @param int $number_of_images - the number of images.
29 *
30 * @return int
31 */
32 public function sum_ratios( $number_of_images = 3 ) {
33 return array_sum( array_slice( wp_list_pluck( $this->images, 'ratio' ), 0, $number_of_images ) );
34 }
35
36 /**
37 * Check that the next images are symmetric
38 *
39 * @return bool
40 */
41 public function next_images_are_symmetric() {
42 return $this->images_left > 2 && $this->images[0]->ratio === $this->images[2]->ratio;
43 }
44
45 /**
46 * Is not as previous.
47 *
48 * @param int $n - the previous image.
49 *
50 * @return bool
51 */
52 public function is_not_as_previous( $n = 1 ) {
53 return ! in_array( get_class( $this ), array_slice( self::$shapes_used, -$n ), true );
54 }
55
56 /**
57 * Check if the theme is wide.
58 *
59 * @return bool
60 */
61 public function is_wide_theme() {
62 return Jetpack::get_content_width() > 1000;
63 }
64
65 /**
66 * Check if the image is landscape.
67 *
68 * @param object $image - the image.
69 *
70 * @return bool
71 */
72 public function image_is_landscape( $image ) {
73 return $image->ratio >= 1 && $image->ratio < 2;
74 }
75
76 /**
77 * Check if the image is portrait.
78 *
79 * @param object $image - the image.
80 *
81 * @return bool
82 */
83 public function image_is_portrait( $image ) {
84 return $image->ratio < 1;
85 }
86
87 /**
88 * Check if the image is panoramic.
89 *
90 * @param object $image - the image.
91 *
92 * @return bool
93 */
94 public function image_is_panoramic( $image ) {
95 return $image->ratio >= 2;
96 }
97
98 /**
99 * Set the last shape.
100 *
101 * @param string $last_shape - the last shape.
102 */
103 public static function set_last_shape( $last_shape ) {
104 self::$shapes_used[] = $last_shape;
105 }
106
107 /**
108 * Reset the last shape.
109 */
110 public static function reset_last_shape() {
111 self::$shapes_used = array();
112 }
113 }
114
115 /**
116 * Jetpack tiled gallery three class.
117 */
118 class Jetpack_Tiled_Gallery_Three extends Jetpack_Tiled_Gallery_Shape {
119
120 /**
121 * The shape.
122 *
123 * @var array
124 */
125 public $shape = array( 1, 1, 1 );
126
127 /**
128 * Checks if there's enough images.
129 *
130 * @return array
131 */
132 public function is_possible() {
133 $ratio = $this->sum_ratios( 3 );
134 $has_enough_images = $this->images_left >= 3 && ! in_array( $this->images_left, array( 4, 6 ), true );
135 return $has_enough_images && $this->is_not_as_previous( 3 ) &&
136 ( ( $ratio < 2.5 ) || ( $ratio < 5 && $this->next_images_are_symmetric() ) || $this->is_wide_theme() );
137 }
138 }
139
140 /**
141 * Jetpack tiled gallery four class.
142 */
143 class Jetpack_Tiled_Gallery_Four extends Jetpack_Tiled_Gallery_Shape {
144 /**
145 * The shape.
146 *
147 * @var array
148 */
149 public $shape = array( 1, 1, 1, 1 );
150
151 /**
152 * Check if it's possible.
153 *
154 * @return bool
155 */
156 public function is_possible() {
157 return $this->is_not_as_previous() &&
158 (
159 ( $this->sum_ratios( 4 ) < 3.5 && $this->images_left > 5 ) ||
160 ( $this->sum_ratios( 4 ) < 7 && $this->images_left === 4 )
161 );
162 }
163 }
164
165 /**
166 * Jetpack tiled gallery five class.
167 */
168 class Jetpack_Tiled_Gallery_Five extends Jetpack_Tiled_Gallery_Shape {
169 /**
170 * The shape.
171 *
172 * @var array
173 */
174 public $shape = array( 1, 1, 1, 1, 1 );
175
176 /**
177 * Check if it's possible.
178 *
179 * @return bool
180 */
181 public function is_possible() {
182 return $this->is_wide_theme() && $this->is_not_as_previous() && $this->sum_ratios( 5 ) < 5 &&
183 ( $this->images_left === 5 || ( $this->images_left !== 10 && $this->images_left > 6 ) );
184 }
185 }
186
187 /**
188 * Jetpack tiled gallery two one class.
189 */
190 class Jetpack_Tiled_Gallery_Two_One extends Jetpack_Tiled_Gallery_Shape {
191 /**
192 * The shape.
193 *
194 * @var array
195 */
196 public $shape = array( 2, 1 );
197
198 /**
199 * Check if it's possible.
200 *
201 * @return bool
202 */
203 public function is_possible() {
204 return $this->is_not_as_previous( 3 ) && $this->images_left >= 2 &&
205 $this->images[2]->ratio < 1.6 && $this->images[0]->ratio >= 0.9 && $this->images[0]->ratio < 2.0 && $this->images[1]->ratio >= 0.9 && $this->images[1]->ratio < 2.0;
206 }
207 }
208
209 /**
210 * Jetpack tiled gallery one two class.
211 */
212 class Jetpack_Tiled_Gallery_One_Two extends Jetpack_Tiled_Gallery_Shape {
213 /**
214 * The shape.
215 *
216 * @var array
217 */
218 public $shape = array( 1, 2 );
219
220 /**
221 * Check if it's possible.
222 *
223 * @return bool
224 */
225 public function is_possible() {
226 return $this->is_not_as_previous( 3 ) && $this->images_left >= 2 &&
227 $this->images[0]->ratio < 1.6 && $this->images[1]->ratio >= 0.9 && $this->images[1]->ratio < 2.0 && $this->images[2]->ratio >= 0.9 && $this->images[2]->ratio < 2.0;
228 }
229 }
230
231 /**
232 * Jetpack tiled gallery one three class.
233 */
234 class Jetpack_Tiled_Gallery_One_Three extends Jetpack_Tiled_Gallery_Shape {
235 /**
236 * The shape.
237 *
238 * @var array
239 */
240 public $shape = array( 1, 3 );
241
242 /**
243 * Check if it's possible.
244 *
245 * @return bool
246 */
247 public function is_possible() {
248 return $this->is_not_as_previous( 3 ) && $this->images_left > 3 &&
249 $this->image_is_portrait( $this->images[0] ) &&
250 $this->image_is_landscape( $this->images[1] ) &&
251 $this->image_is_landscape( $this->images[2] ) &&
252 $this->image_is_landscape( $this->images[3] );
253 }
254 }
255
256 /**
257 * Jetpack tiled gallery three one class.
258 */
259 class Jetpack_Tiled_Gallery_Three_One extends Jetpack_Tiled_Gallery_Shape {
260 /**
261 * The shape.
262 *
263 * @var array
264 */
265 public $shape = array( 3, 1 );
266
267 /**
268 * Check if it's possible.
269 *
270 * @return bool
271 */
272 public function is_possible() {
273 return $this->is_not_as_previous( 3 ) && $this->images_left > 3 &&
274 $this->image_is_portrait( $this->images[3] ) &&
275 $this->image_is_landscape( $this->images[0] ) &&
276 $this->image_is_landscape( $this->images[1] ) &&
277 $this->image_is_landscape( $this->images[2] );
278 }
279 }
280
281 /**
282 * Jetpack tiled gallery panoramic class.
283 */
284 class Jetpack_Tiled_Gallery_Panoramic extends Jetpack_Tiled_Gallery_Shape {
285 /**
286 * The shape.
287 *
288 * @var array
289 */
290 public $shape = array( 1 );
291
292 /**
293 * Check if it's possible.
294 *
295 * @return bool
296 */
297 public function is_possible() {
298 return $this->image_is_panoramic( $this->images[0] );
299 }
300 }
301
302 /**
303 * Jetpack tiled gallery symmetric class.
304 */
305 class Jetpack_Tiled_Gallery_Symmetric_Row extends Jetpack_Tiled_Gallery_Shape {
306 /**
307 * The shape.
308 *
309 * @var array
310 */
311 public $shape = array( 1, 2, 1 );
312
313 /**
314 * Check if it's possible.
315 *
316 * @return bool
317 */
318 public function is_possible() {
319 return $this->is_not_as_previous( 5 ) &&
320 $this->images_left > 3 &&
321 $this->images_left !== 5 &&
322 $this->image_is_portrait( $this->images[0] ) &&
323 $this->image_is_landscape( $this->images[1] ) &&
324 $this->image_is_landscape( $this->images[2] ) &&
325 $this->image_is_portrait( $this->images[3] );
326 }
327 }
328
329 /**
330 * Jetpack tiled gallery reverse symmetric row class.
331 */
332 class Jetpack_Tiled_Gallery_Reverse_Symmetric_Row extends Jetpack_Tiled_Gallery_Shape {
333 /**
334 * The shape.
335 *
336 * @var array
337 */
338 public $shape = array( 2, 1, 2 );
339
340 /**
341 * Check if it's possible.
342 *
343 * @return bool
344 */
345 public function is_possible() {
346 return $this->is_not_as_previous( 5 ) && $this->images_left > 15 &&
347 $this->image_is_landscape( $this->images[0] ) &&
348 $this->image_is_landscape( $this->images[1] ) &&
349 $this->image_is_portrait( $this->images[2] ) &&
350 $this->image_is_landscape( $this->images[3] ) &&
351 $this->image_is_landscape( $this->images[4] );
352 }
353 }
354
355 /**
356 * Jetpack tiled gallery long symmetric row class.
357 */
358 class Jetpack_Tiled_Gallery_Long_Symmetric_Row extends Jetpack_Tiled_Gallery_Shape {
359 /**
360 * The shape.
361 *
362 * @var array
363 */
364 public $shape = array( 3, 1, 3 );
365
366 /**
367 * Check if it's possible.
368 *
369 * @return bool
370 */
371 public function is_possible() {
372 return $this->is_not_as_previous( 5 ) && $this->images_left > 15 &&
373 $this->image_is_landscape( $this->images[0] ) &&
374 $this->image_is_landscape( $this->images[1] ) &&
375 $this->image_is_landscape( $this->images[2] ) &&
376 $this->image_is_portrait( $this->images[3] ) &&
377 $this->image_is_landscape( $this->images[4] ) &&
378 $this->image_is_landscape( $this->images[5] ) &&
379 $this->image_is_landscape( $this->images[6] );
380 }
381 }
382
383 /**
384 * Jetpack tiled gallery three columns class.
385 */
386 class Jetpack_Tiled_Gallery_Three_Columns extends Jetpack_Tiled_Gallery_Shape {
387 /**
388 * The shape.
389 *
390 * @var array
391 */
392 public $shape = array();
393
394 /**
395 * Constructor class.
396 *
397 * @param object $images - the images.
398 */
399 public function __construct( $images ) {
400 parent::__construct( $images );
401
402 $total_ratio = $this->sum_ratios( $this->images_left );
403 $approximate_column_ratio = $total_ratio / 3;
404 $column_one_images = 0;
405 $column_two_images = 0;
406 $column_three_images = 0;
407 $sum = 0;
408
409 foreach ( $this->images as $image ) {
410 if ( $sum <= $approximate_column_ratio ) {
411 ++$column_one_images;
412 }
413
414 if ( $sum > $approximate_column_ratio && $sum <= 2 * $approximate_column_ratio ) {
415 ++$column_two_images;
416 }
417 $sum += $image->ratio;
418 }
419
420 $column_three_images = $this->images_left - $column_two_images - $column_one_images;
421
422 if ( $column_one_images ) {
423 $this->shape[] = $column_one_images;
424 }
425
426 if ( $column_two_images ) {
427 $this->shape[] = $column_two_images;
428 }
429
430 if ( $column_three_images ) {
431 $this->shape[] = $column_three_images;
432 }
433 }
434
435 /**
436 * Check if it's possible.
437 *
438 * @return bool
439 */
440 public function is_possible() {
441 return ! empty( $this->shape );
442 }
443 }
444