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 |