| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit( 0 ); |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Tiled gallery layout class. |
| 9 |
*/ |
| 10 |
abstract class Jetpack_Tiled_Gallery_Layout { |
| 11 |
/** |
| 12 |
* Template allow list. |
| 13 |
* |
| 14 |
* @var array |
| 15 |
*/ |
| 16 |
private static $templates = array( 'carousel-container', 'circle-layout', 'rectangular-layout', 'square-layout' ); |
| 17 |
|
| 18 |
/** |
| 19 |
* Partial list. |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
private static $partials = array( 'carousel-image-args', 'item' ); |
| 24 |
|
| 25 |
/** |
| 26 |
* Type of gallery - defined in parent class. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $type; |
| 31 |
|
| 32 |
/** |
| 33 |
* The attachments. |
| 34 |
* |
| 35 |
* @var object |
| 36 |
*/ |
| 37 |
public $attachments; |
| 38 |
|
| 39 |
/** |
| 40 |
* The attachment link. |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
public $link; |
| 45 |
|
| 46 |
/** |
| 47 |
* If the image is in grayscale. |
| 48 |
* |
| 49 |
* @var bool |
| 50 |
*/ |
| 51 |
public $grayscale; |
| 52 |
|
| 53 |
/** |
| 54 |
* How many columns. |
| 55 |
* |
| 56 |
* @var int |
| 57 |
*/ |
| 58 |
public $columns; |
| 59 |
|
| 60 |
/** |
| 61 |
* Attachment link |
| 62 |
* |
| 63 |
* @var bool |
| 64 |
*/ |
| 65 |
public $needs_attachment_link; |
| 66 |
|
| 67 |
/** |
| 68 |
* Constructor function. |
| 69 |
* |
| 70 |
* @param object $attachments - the attachmed image. |
| 71 |
* @param string $link - the attachment link. |
| 72 |
* @param bool $grayscale - if the image is in grayscale. |
| 73 |
* @param int $columns - how many columns. |
| 74 |
*/ |
| 75 |
public function __construct( $attachments, $link, $grayscale, $columns ) { |
| 76 |
$this->attachments = $attachments; |
| 77 |
$this->link = $link; |
| 78 |
$this->needs_attachment_link = $link !== 'file'; |
| 79 |
$this->grayscale = $grayscale; |
| 80 |
$this->columns = $columns; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Render carousel container template. |
| 85 |
* |
| 86 |
* @param array $context - the context. |
| 87 |
* @return string HTML |
| 88 |
*/ |
| 89 |
public function HTML( $context = array() ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
| 90 |
// Render the carousel container template, which will take the |
| 91 |
// appropriate strategy to fill it |
| 92 |
ob_start(); |
| 93 |
$this->template( |
| 94 |
'carousel-container', |
| 95 |
array_merge( |
| 96 |
$context, |
| 97 |
array( |
| 98 |
'attachments' => $this->attachments, |
| 99 |
'link' => $this->link, |
| 100 |
'needs_attachment_link' => $this->needs_attachment_link, |
| 101 |
'grayscale' => $this->grayscale, |
| 102 |
) |
| 103 |
) |
| 104 |
); |
| 105 |
$html = ob_get_clean(); |
| 106 |
|
| 107 |
return $html; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Handle tiled gallery template path. |
| 112 |
* |
| 113 |
* @html-template-var array $context |
| 114 |
* |
| 115 |
* @param string $name Template name. |
| 116 |
* @param array $context Context array passed to the template. |
| 117 |
*/ |
| 118 |
private function template( $name, $context = array() ) { |
| 119 |
if ( ! in_array( $name, self::$templates, true ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Filters the Tiled Gallery template path |
| 125 |
* |
| 126 |
* @module tiled-gallery |
| 127 |
* @since 4.4.0 |
| 128 |
* |
| 129 |
* @param string $path Template path. |
| 130 |
* @param string $path Template name. |
| 131 |
* @param array $context Context array passed to the template. |
| 132 |
*/ |
| 133 |
require apply_filters( 'jetpack_tiled_gallery_template', __DIR__ . "/templates/$name.php", $name, $context ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Handle tiled gallery partial path. |
| 138 |
* |
| 139 |
* @html-template-var array $context |
| 140 |
* |
| 141 |
* @param string $name - the name. |
| 142 |
* @param array $context Context array passed to the partial. |
| 143 |
*/ |
| 144 |
private function partial( $name, $context = array() ) { |
| 145 |
if ( ! in_array( $name, self::$partials, true ) ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Filters the Tiled Gallery partial path |
| 151 |
* |
| 152 |
* @module tiled-gallery |
| 153 |
* @since 4.4.0 |
| 154 |
* |
| 155 |
* @param string $path Partial path. |
| 156 |
* @param string $path Partial name. |
| 157 |
* @param array $context Context array passed to the partial. |
| 158 |
*/ |
| 159 |
require apply_filters( 'jetpack_tiled_gallery_partial', __DIR__ . "/templates/partials/$name.php", $name, $context ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get extra container data. |
| 164 |
*/ |
| 165 |
protected function get_container_extra_data() { |
| 166 |
global $post; |
| 167 |
|
| 168 |
$blog_id = (int) get_current_blog_id(); |
| 169 |
|
| 170 |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 171 |
$likes_blog_id = $blog_id; |
| 172 |
} else { |
| 173 |
$likes_blog_id = Jetpack_Options::get_option( 'id' ); |
| 174 |
} |
| 175 |
|
| 176 |
if ( class_exists( 'Jetpack_Carousel' ) || in_array( 'carousel', Jetpack::get_active_modules(), true ) || 'carousel' === $this->link ) { |
| 177 |
$extra_data = array( |
| 178 |
'blog_id' => $blog_id, |
| 179 |
'permalink' => get_permalink( $post->ID ?? 0 ), |
| 180 |
'likes_blog_id' => $likes_blog_id, |
| 181 |
); |
| 182 |
} else { |
| 183 |
$extra_data = null; |
| 184 |
} |
| 185 |
|
| 186 |
return $extra_data; |
| 187 |
} |
| 188 |
} |
| 189 |
|