PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 12.4.2
Jetpack – WP Security, Backup, Speed, & Growth v12.4.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 / tiled-gallery-rectangular.php
jetpack / modules / tiled-gallery / tiled-gallery Last commit date
rtl 10 years ago templates 3 years ago tiled-gallery-circle.php 4 years ago tiled-gallery-item.php 3 years ago tiled-gallery-layout.php 3 years ago tiled-gallery-rectangular.php 3 years ago tiled-gallery-rtl.css 3 years ago tiled-gallery-shape.php 3 years ago tiled-gallery-square.php 3 years ago tiled-gallery.css 5 years ago tiled-gallery.js 3 years ago
tiled-gallery-rectangular.php
473 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 require_once __DIR__ . '/tiled-gallery-layout.php';
3 require_once __DIR__ . '/tiled-gallery-shape.php';
4 require_once __DIR__ . '/tiled-gallery-item.php';
5
6 // phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
7 /**
8 * Tiled gallery rectangular layout.
9 */
10 class Jetpack_Tiled_Gallery_Layout_Rectangular extends Jetpack_Tiled_Gallery_Layout {
11
12 /**
13 * The layout type.
14 *
15 * @var string
16 */
17 protected $type = 'rectangular';
18
19 /**
20 * The HTML function.
21 *
22 * @param array $context - the context array, unused.
23 * @return string HTML
24 */
25 public function HTML( $context = array() ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
26 $grouper = new Jetpack_Tiled_Gallery_Grouper( $this->attachments );
27 Jetpack_Tiled_Gallery_Shape::reset_last_shape();
28
29 return parent::HTML( array( 'rows' => $grouper->grouped_images ) );
30 }
31 }
32
33 /**
34 * Tiled gallery layout columns class.
35 */
36 class Jetpack_Tiled_Gallery_Layout_Columns extends Jetpack_Tiled_Gallery_Layout {
37
38 /**
39 * The layout type.
40 *
41 * @var string
42 */
43 protected $type = 'rectangular'; // It doesn't need separate template for now
44
45 /**
46 * The HTML function.
47 *
48 * @param array $context - the context array, unused.
49 * @return string HTML
50 */
51 public function HTML( $context = array() ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
52 $grouper = new Jetpack_Tiled_Gallery_Grouper( $this->attachments, array( 'Three_Columns', 'Two' ) );
53
54 return parent::HTML( array( 'rows' => $grouper->grouped_images ) );
55 }
56 }
57
58 /**
59 * Gallery layout rectangle alis.
60 */
61 class Jetpack_Tiled_Gallery_Layout_Rectangle extends Jetpack_Tiled_Gallery_Layout_Rectangular {}
62
63 /**
64 * Image grouping and HTML generation logic class.
65 */
66 class Jetpack_Tiled_Gallery_Grouper {
67
68 /**
69 * The margin.
70 *
71 * @var int
72 */
73 public $margin = 4;
74
75 /**
76 * Shapes array.
77 * This list is ordered. If you put a shape that's likely to occur on top, it will happen all the time.
78 *
79 * @var array
80 */
81 public $shapes = array(
82 'Reverse_Symmetric_Row',
83 'Long_Symmetric_Row',
84 'Symmetric_Row',
85 'One_Three',
86 'Three_One',
87 'One_Two',
88 'Five',
89 'Four',
90 'Three',
91 'Two_One',
92 'Panoramic',
93 );
94
95 /**
96 * Shape of the last row in gallery.
97 *
98 * @var string
99 */
100 public $last_shape = '';
101
102 /**
103 * All images to be displayed.
104 *
105 * @var array
106 */
107 public $images = array();
108
109 /**
110 * Array of tiled gallery rows (groups of images).
111 *
112 * @var array
113 */
114 public $grouped_images = array();
115
116 /**
117 * Constructor function.
118 *
119 * @param object $attachments - the attachments.
120 * @param array $shapes - the shapes.
121 */
122 public function __construct( $attachments, $shapes = array() ) {
123 $content_width = Jetpack_Tiled_Gallery::get_content_width();
124
125 $this->overwrite_shapes( $shapes );
126 $this->last_shape = '';
127 $this->images = $this->get_images_with_sizes( $attachments );
128 $this->grouped_images = $this->get_grouped_images();
129 $this->apply_content_width( $content_width );
130 }
131
132 /**
133 * Overwrite the shapes.
134 *
135 * @param array $shapes - the shapes.
136 */
137 public function overwrite_shapes( $shapes ) {
138 if ( ! empty( $shapes ) ) {
139 $this->shapes = $shapes;
140 }
141 }
142
143 /**
144 * Get the current row size.
145 *
146 * @return array
147 */
148 public function get_current_row_size() {
149 $images_left = is_countable( $this->images ) ? count( $this->images ) : 0;
150 if ( $images_left < 3 ) {
151 return array_fill( 0, $images_left, 1 );
152 }
153
154 foreach ( $this->shapes as $shape_name ) {
155 $class_name = "Jetpack_Tiled_Gallery_$shape_name";
156 $shape = new $class_name( $this->images );
157 if ( $shape->is_possible() ) {
158 Jetpack_Tiled_Gallery_Shape::set_last_shape( $class_name );
159 return $shape->shape;
160 }
161 }
162
163 Jetpack_Tiled_Gallery_Shape::set_last_shape( 'Two' );
164 return array( 1, 1 );
165 }
166
167 /**
168 * Get images with sizes.
169 *
170 * @param object $attachments - the attachments.
171 *
172 * @return array
173 */
174 public function get_images_with_sizes( $attachments ) {
175 $images_with_sizes = array();
176
177 foreach ( $attachments as $image ) {
178 $meta = wp_get_attachment_metadata( $image->ID );
179 $image->width_orig = ( isset( $meta['width'] ) && $meta['width'] > 0 ) ? $meta['width'] : 1;
180 $image->height_orig = ( isset( $meta['height'] ) && $meta['height'] > 0 ) ? $meta['height'] : 1;
181 $image->ratio = $image->width_orig / $image->height_orig;
182 $image->ratio = $image->ratio ? $image->ratio : 1;
183 $images_with_sizes[] = $image;
184 }
185
186 return $images_with_sizes;
187 }
188
189 /**
190 * Get the current row size.
191 *
192 * @return array
193 */
194 public function read_row() {
195 $vector = $this->get_current_row_size();
196
197 $row = array();
198 foreach ( $vector as $group_size ) {
199 $row[] = new Jetpack_Tiled_Gallery_Group( array_splice( $this->images, 0, $group_size ) );
200 }
201
202 return $row;
203 }
204
205 /**
206 * Get grouped images.
207 *
208 * @return array
209 */
210 public function get_grouped_images() {
211 $grouped_images = array();
212
213 while ( ! empty( $this->images ) ) {
214 $grouped_images[] = new Jetpack_Tiled_Gallery_Row( $this->read_row() );
215 }
216
217 return $grouped_images;
218 }
219
220 /**
221 * Apply content width.
222 *
223 * @param int $width - the width.
224 *
225 * @todo split in functions
226 * @todo do not stretch images
227 */
228 public function apply_content_width( $width ) {
229 foreach ( $this->grouped_images as $row ) {
230 $row->width = $width;
231 $group_count = is_countable( $row->groups ) ? count( $row->groups ) : 0;
232 $row->raw_height = 1 / $row->ratio * ( $width - $this->margin * ( $group_count - $row->weighted_ratio ) );
233 $row->height = round( $row->raw_height );
234
235 $this->calculate_group_sizes( $row );
236 }
237 }
238
239 /**
240 * Calculate group sizes.
241 *
242 * @param object $row - the row.
243 */
244 public function calculate_group_sizes( $row ) {
245 // Storing the calculated group heights in an array for rounding them later while preserving their sum
246 // This fixes the rounding error that can lead to a few ugly pixels sticking out in the gallery
247 $group_widths_array = array();
248 foreach ( $row->groups as $group ) {
249 $group->height = $row->height;
250 $image_count = is_countable( $group->images ) ? count( $group->images ) : 0;
251 // Storing the raw calculations in a separate property to prevent rounding errors from cascading down and for diagnostics
252 $group->raw_width = ( $row->raw_height - $this->margin * $image_count ) * $group->ratio + $this->margin;
253 $group_widths_array[] = $group->raw_width;
254 }
255 $rounded_group_widths_array = Jetpack_Constrained_Array_Rounding::get_rounded_constrained_array( $group_widths_array, $row->width );
256
257 foreach ( $row->groups as $group ) {
258 $group->width = array_shift( $rounded_group_widths_array );
259 $this->calculate_image_sizes( $group );
260 }
261 }
262
263 /**
264 * Calculate image sizes.
265 *
266 * @param object $group - the group of images.
267 */
268 public function calculate_image_sizes( $group ) {
269 // Storing the calculated image heights in an array for rounding them later while preserving their sum
270 // This fixes the rounding error that can lead to a few ugly pixels sticking out in the gallery
271 $image_heights_array = array();
272 foreach ( $group->images as $image ) {
273 $image->width = $group->width - $this->margin;
274 // Storing the raw calculations in a separate property for diagnostics
275 $image->raw_height = ( $group->raw_width - $this->margin ) / $image->ratio;
276 $image_heights_array[] = $image->raw_height;
277 }
278
279 $image_height_sum = $group->height - count( $image_heights_array ) * $this->margin;
280 $rounded_image_heights_array = Jetpack_Constrained_Array_Rounding::get_rounded_constrained_array( $image_heights_array, $image_height_sum );
281
282 foreach ( $group->images as $image ) {
283 $image->height = array_shift( $rounded_image_heights_array );
284 }
285 }
286 }
287
288 /**
289 * Jetpack tiled row class.
290 */
291 class Jetpack_Tiled_Gallery_Row {
292 /**
293 * Groups of images
294 *
295 * @var object[]
296 */
297 public $groups;
298
299 /**
300 * Image ratio.
301 *
302 * @var float
303 */
304 public $ratio;
305
306 /**
307 * Weighted ratio based on all the images.
308 *
309 * @var float
310 */
311 public $weighted_ratio;
312
313 /**
314 * Rounded value of the raw width.
315 *
316 * @var int
317 */
318 public $width;
319
320 /**
321 * Rounded value of the raw height.
322 *
323 * @var int
324 */
325 public $height;
326
327 /**
328 * Raw width of the row.
329 *
330 * @var int
331 */
332 public $raw_width;
333
334 /**
335 * Raw height of the row.
336 *
337 * @var int
338 */
339 public $raw_height;
340
341 /**
342 * Constructor class.
343 *
344 * @param object[] $groups - the group of images.
345 */
346 public function __construct( $groups ) {
347 $this->groups = $groups;
348 $this->ratio = $this->get_ratio();
349 $this->weighted_ratio = $this->get_weighted_ratio();
350 }
351
352 /**
353 * Get the ratio.
354 *
355 * @return float
356 */
357 public function get_ratio() {
358 $ratio = 0;
359 foreach ( $this->groups as $group ) {
360 $ratio += $group->ratio;
361 }
362 return $ratio > 0 ? $ratio : 1;
363 }
364
365 /**
366 * Get weighted ratio.
367 *
368 * @return float
369 */
370 public function get_weighted_ratio() {
371 $weighted_ratio = 0;
372 foreach ( $this->groups as $group ) {
373 $image_count = is_countable( $group->images ) ? count( $group->images ) : 0;
374 $weighted_ratio += $group->ratio * $image_count;
375 }
376 return $weighted_ratio > 0 ? $weighted_ratio : 1;
377 }
378 }
379
380 /**
381 * Tiled gallery group class.
382 */
383 class Jetpack_Tiled_Gallery_Group {
384 /**
385 * Images to be displayed in group.
386 *
387 * @var object[]
388 */
389 public $images;
390
391 /**
392 * Image ratio.
393 *
394 * @var float
395 */
396 public $ratio;
397
398 /**
399 * Rounded value of the raw width.
400 *
401 * @var int
402 */
403 public $width;
404
405 /**
406 * Rounded value of the raw height.
407 *
408 * @var int
409 */
410 public $height;
411
412 /**
413 * Raw width of the group.
414 *
415 * @var int
416 */
417 public $raw_width;
418
419 /**
420 * Raw height of the group.
421 *
422 * @var int
423 */
424 public $raw_height;
425
426 /**
427 * Constructor class.
428 *
429 * @param object[] $images - the images.
430 */
431 public function __construct( $images ) {
432 $this->images = $images;
433 $this->ratio = $this->get_ratio();
434 }
435
436 /**
437 * Get the ratio.
438 *
439 * @return float
440 */
441 public function get_ratio() {
442 $ratio = 0;
443 foreach ( $this->images as $image ) {
444 if ( $image->ratio ) {
445 $ratio += 1 / $image->ratio;
446 }
447 }
448 if ( ! $ratio ) {
449 return 1;
450 }
451
452 return 1 / $ratio;
453 }
454
455 /**
456 * The items.
457 *
458 * @param string $needs_attachment_link - the attachment link.
459 * @param bool $grayscale - if the image is in grayscale.
460 *
461 * @return array
462 */
463 public function items( $needs_attachment_link, $grayscale ) {
464 $items = array();
465 foreach ( $this->images as $image ) {
466 $items[] = new Jetpack_Tiled_Gallery_Rectangular_Item( $image, $needs_attachment_link, $grayscale );
467 }
468
469 return $items;
470 }
471 }
472
473