Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Tiled_Gallery_Layout_Square | |
0.00% |
0 / 42 |
|
0.00% |
0 / 2 |
132 | |
0.00% |
0 / 1 |
| compute_items | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
110 | |||
| HTML | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit( 0 ); |
| 5 | } |
| 6 | |
| 7 | require_once __DIR__ . '/tiled-gallery-layout.php'; |
| 8 | require_once __DIR__ . '/tiled-gallery-item.php'; |
| 9 | |
| 10 | /** |
| 11 | * Jetpack tiled gallery square layout class. |
| 12 | */ |
| 13 | class Jetpack_Tiled_Gallery_Layout_Square extends Jetpack_Tiled_Gallery_Layout { |
| 14 | |
| 15 | /** |
| 16 | * Layout type. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $type = 'square'; |
| 21 | |
| 22 | /** |
| 23 | * Compute the items. |
| 24 | */ |
| 25 | private function compute_items() { |
| 26 | $content_width = Jetpack_Tiled_Gallery::get_content_width(); |
| 27 | $images_per_row = ( $this->columns > 1 ? $this->columns : 1 ); |
| 28 | $margin = 2; |
| 29 | |
| 30 | $margin_space = ( $images_per_row * $margin ) * 2; |
| 31 | $size = floor( ( $content_width - $margin_space ) / $images_per_row ); |
| 32 | $remainder_size = $size; |
| 33 | $img_size = $remainder_size; |
| 34 | $attachment_count = is_countable( $this->attachments ) ? count( $this->attachments ) : 0; |
| 35 | $remainder = $attachment_count % $images_per_row; |
| 36 | if ( $remainder > 0 ) { |
| 37 | $remainder_space = ( $remainder * $margin ) * 2; |
| 38 | $remainder_size = floor( ( $content_width - $remainder_space ) / $remainder ); |
| 39 | } |
| 40 | |
| 41 | $c = 1; |
| 42 | $items_in_row = 0; |
| 43 | $rows = array(); |
| 44 | $row = new stdClass(); |
| 45 | $row->images = array(); |
| 46 | foreach ( $this->attachments as $image ) { |
| 47 | if ( $remainder > 0 && $c <= $remainder ) { |
| 48 | $img_size = $remainder_size; |
| 49 | } else { |
| 50 | $img_size = $size; |
| 51 | } |
| 52 | |
| 53 | $image->width = $img_size; |
| 54 | $image->height = $image->width; |
| 55 | |
| 56 | $item = new Jetpack_Tiled_Gallery_Square_Item( $image, $this->needs_attachment_link, $this->grayscale ); |
| 57 | |
| 58 | $row->images[] = $item; |
| 59 | ++$c; |
| 60 | ++$items_in_row; |
| 61 | |
| 62 | if ( $images_per_row === $items_in_row || $remainder + 1 === $c ) { |
| 63 | $rows[] = $row; |
| 64 | $items_in_row = 0; |
| 65 | |
| 66 | $row->height = $img_size + $margin * 2; |
| 67 | $row->width = $content_width; |
| 68 | $row->group_size = $img_size + 2 * $margin; |
| 69 | |
| 70 | $row = new stdClass(); |
| 71 | $row->images = array(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if ( ! empty( $row->images ) ) { |
| 76 | $row->height = $img_size + $margin * 2; |
| 77 | $row->width = $content_width; |
| 78 | $row->group_size = $img_size + 2 * $margin; |
| 79 | |
| 80 | $rows[] = $row; |
| 81 | } |
| 82 | |
| 83 | return $rows; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * The html. |
| 88 | * |
| 89 | * @param array $context - the context, unused. |
| 90 | * @return string HTML |
| 91 | */ |
| 92 | public function HTML( $context = array() ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 93 | return parent::HTML( array( 'rows' => $this->compute_items() ) ); |
| 94 | } |
| 95 | } |