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