PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.4.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.4.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Models / GalleryItemVideoAttachment.php
GalleryItemVideoAttachment.php
354 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace SureCart\Models;
3
4 use SureCart\Models\GalleryItem as ModelsGalleryItem;
5 use SureCart\Support\Contracts\GalleryItem;
6
7 /**
8 * Gallery item model for video attachments
9 */
10 class GalleryItemVideoAttachment extends ModelsGalleryItem implements GalleryItem {
11 /**
12 * The featured image (post thumbnail) of the product.
13 *
14 * @var \WP_Post|null
15 */
16 protected $featured_image = null;
17
18 /**
19 * Create a new gallery item.
20 *
21 * @param int|array|\WP_Post $item The item.
22 * @param \WP_Post|null $featured_image The featured image (post thumbnail) of the product.
23 *
24 * @return void
25 */
26 public function __construct( $item, $featured_image = null ) {
27 if ( is_array( $item ) && isset( $item['id'] ) ) {
28 $item = $item['id'];
29 }
30
31 $this->item = get_post( $item );
32 $this->featured_image = $featured_image;
33 }
34
35 /**
36 * Get the video poster image ID.
37 *
38 * @return int The Poster image ID.
39 */
40 public function posterId() {
41 return $this->getMetadata( 'thumbnail_image' )['id'] ?? $this->featured_image->ID ?? null;
42 }
43
44 /**
45 * Get the thumbnail attribute markup.
46 *
47 * @param string $size The size of the image.
48 * @param array $attr The attributes for the tag.
49 * @param array $metadata Additional metadata.
50 *
51 * @return string
52 */
53 public function html( $size = 'full', $attr = [], $metadata = [] ): string {
54 // If the item is not set, return null.
55 if ( empty( $this->posterId() ) ) {
56 return '';
57 }
58
59 // Handle image attachments.
60 $image = wp_get_attachment_image( $this->posterId(), $size, false, $attr );
61
62 // add any styles.
63 $tags = new \WP_HTML_Tag_Processor( $image );
64
65 // get the image tag.
66 $has_image = $tags->next_tag( 'img' );
67
68 // add inline styles.
69 if ( ! empty( $attr['style'] ) ) {
70 if ( $has_image && ! empty( $attr['style'] ) ) {
71 $tags->set_attribute( 'style', $attr['style'] );
72 }
73 }
74
75 if ( $this->with_lightbox ) {
76 $this->with_lightbox = false; // reset to false.
77
78 // get the image data.
79 $full_data = $this->attributes( 'full' );
80
81 // set the lightbox state.
82 wp_interactivity_state(
83 'surecart/lightbox',
84 array(
85 'metadata' => array(
86 // metadata keyed by unique image id.
87 $this->id => wp_parse_args(
88 $metadata,
89 array(
90 'uploadedSrc' => $full_data->src,
91 'imgClassNames' => $full_data->class,
92 'targetWidth' => $full_data->width,
93 'targetHeight' => $full_data->height,
94 'scaleAttr' => false, // false or 'contain'.
95 'alt' => $full_data->alt,
96 // translators: %s is the image title.
97 'screenReaderText' => sprintf( __( 'Viewing image: %s.', 'surecart' ), $this->post_title ),
98 'galleryId' => get_the_ID(),
99 ),
100 ),
101 ),
102 )
103 );
104
105 $tags->set_attribute( 'data-wp-on--load', 'callbacks.setImageRef' );
106 $tags->set_attribute( 'data-wp-init', 'callbacks.setImageRef' );
107 $tags->set_attribute( 'data-wp-on--click', 'actions.showLightbox' );
108 $tags->set_attribute( 'data-wp-class--sc-hide', 'state.isContentHidden' );
109 $tags->set_attribute( 'data-wp-class--sc-show', 'state.isContentVisible' );
110 $tags->add_class( 'has-image-lightbox' );
111
112 // add the lightbox trigger button.
113 return $tags->get_updated_html() .
114 '<button
115 class="lightbox-trigger"
116 type="button"
117 aria-haspopup="dialog"
118 aria-label="' . esc_attr__( 'Expand image', 'surecart' ) . '"
119 data-wp-init="callbacks.initTriggerButton"
120 data-wp-on--click="actions.showLightbox"
121 data-wp-style--right="state.imageButtonRight"
122 data-wp-style--top="state.imageButtonTop"
123 >
124 ' . \SureCart::svg()->get(
125 'maximize',
126 [
127 'width' => 16,
128 'height' => 16,
129 'aria-hidden' => 'true',
130 ]
131 ) . '
132 </button>';
133 }
134
135 // return updated html.
136 return $tags->get_updated_html();
137 }
138
139 /**
140 * Get the poster image attributes.
141 *
142 * @param string $size The size of the video.
143 * @param array $attr The attributes for the tag.
144 *
145 * @return object|null
146 */
147 public function attributes( $size = 'full', $attr = [] ): object {
148 // If the poster ID is not set, return null.
149 if ( empty( $this->posterId() ) ) {
150 return (object) [
151 'src' => apply_filters( 'surecart/product-video-poster/fallback_src', trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'images/placeholder.jpg' ),
152 'alt' => ! empty( get_the_title() ) ? get_the_title() : __( 'Product Video', 'surecart' ),
153 'title' => ! empty( get_the_title() ) ? get_the_title() : __( 'Product Video', 'surecart' ),
154 ];
155 }
156
157 $image = wp_get_attachment_image_src( $this->posterId(), $size, $attr['icon'] ?? false, $attr );
158
159 if ( ! $image ) {
160 return (object) [];
161 }
162
163 list( $src, $width, $height ) = $image;
164
165 $attachment = get_post( $this->posterId() );
166 $size_class = $size;
167
168 if ( is_array( $size_class ) ) {
169 $size_class = implode( 'x', $size_class );
170 }
171
172 $default_attr = array(
173 'src' => $src,
174 'class' => "attachment-$size_class size-$size_class",
175 'alt' => trim( wp_strip_all_tags( get_post_meta( $this->posterId(), '_wp_attachment_image_alt', true ) ) ),
176 'width' => $width,
177 'height' => $height,
178 );
179
180 /**
181 * Filters the context in which wp_get_attachment_image() is used.
182 *
183 * @since 6.3.0
184 *
185 * @param string $context The context. Default 'wp_get_attachment_image'.
186 */
187 $context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
188 $attr = wp_parse_args( $attr, $default_attr );
189
190 $loading_attr = $attr;
191 $loading_attr['width'] = $width;
192 $loading_attr['height'] = $height;
193 $loading_optimization_attr = wp_get_loading_optimization_attributes(
194 'img',
195 $loading_attr,
196 $context
197 );
198
199 // Add loading optimization attributes if not available.
200 $attr = array_merge( $attr, $loading_optimization_attr );
201
202 // Omit the `decoding` attribute if the value is invalid according to the spec.
203 if ( empty( $attr['decoding'] ) || ! in_array( $attr['decoding'], array( 'async', 'sync', 'auto' ), true ) ) {
204 unset( $attr['decoding'] );
205 }
206
207 /*
208 * If the default value of `lazy` for the `loading` attribute is overridden
209 * to omit the attribute for this image, ensure it is not included.
210 */
211 if ( isset( $attr['loading'] ) && ! $attr['loading'] ) {
212 unset( $attr['loading'] );
213 }
214
215 // If the `fetchpriority` attribute is overridden and set to false or an empty string.
216 if ( isset( $attr['fetchpriority'] ) && ! $attr['fetchpriority'] ) {
217 unset( $attr['fetchpriority'] );
218 }
219
220 // Generate 'srcset' and 'sizes' if not already present.
221 if ( empty( $attr['srcset'] ) ) {
222 $image_meta = wp_get_attachment_metadata( $this->posterId() );
223
224 if ( is_array( $image_meta ) ) {
225 $size_array = array( absint( $width ), absint( $height ) );
226 $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $this->posterId() );
227 $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $this->posterId() );
228
229 if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
230 $attr['srcset'] = $srcset;
231
232 if ( empty( $attr['sizes'] ) ) {
233 $attr['sizes'] = $sizes;
234 }
235 }
236 }
237 }
238
239 /**
240 * Filters the list of attachment image attributes.
241 *
242 * @since 2.8.0
243 *
244 * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
245 * See wp_get_attachment_image().
246 * @param WP_Post $attachment Image attachment post.
247 * @param string|int[] $size Requested image size. Can be any registered image size name, or
248 * an array of width and height values in pixels (in that order).
249 */
250 $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
251
252 return (object) $attr;
253 }
254
255 /**
256 * Get the media attribute markup.
257 *
258 * @param string $size The size of the video.
259 * @param array $attr The attributes for the tag.
260 * @param array $metadata Additional metadata for the lightbox.
261 *
262 * @return string
263 */
264 public function video_html( $size = 'full', $attr = [], $metadata = [] ): string {
265 // If the item is not set, return null.
266 if ( ! isset( $this->item->ID ) ) {
267 return '';
268 }
269
270 // If the item is not set, return null.
271 $poster = $this->attributes( $size, $attr );
272
273 if ( empty( $poster ) ) {
274 return '';
275 }
276
277 $metadata = $this->getMetadata();
278 return \SureCart::view( 'media/video' )->with(
279 [
280 'poster' => $poster->src,
281 'src' => wp_get_attachment_url( $this->item->ID ),
282 'alt' => $this->item->alt_text ?? $this->item->post_title ?? '',
283 'title' => $this->item->post_title ?? '',
284 'style' => ! empty( $metadata['aspect_ratio'] ) ? 'aspect-ratio: ' . esc_attr( $metadata['aspect_ratio'] ) . ';' : '',
285 'controls' => $metadata['controls'] ?? true,
286 'autoplay' => $metadata['autoplay'] ?? false,
287 'loop' => $metadata['loop'] ?? false,
288 'muted' => $metadata['muted'] ?? false,
289 ]
290 )->toString();
291 }
292
293 /**
294 * Get the video thumbnail attribute markup.
295 *
296 * @param string $size The size of the video.
297 * @param array $attr The attributes for the tag.
298 * @param array $metadata Additional metadata for the lightbox.
299 *
300 * @return string
301 */
302 public function video_thumbnail_html( $size = 'full', $attr = [], $metadata = [] ): string {
303 return \SureCart::view( 'media/video-thumbnail' )
304 ->with( (array) $this->attributes( $size, $attr, $metadata ) )
305 ->toString();
306 }
307
308 /**
309 * Get the video attributes.
310 *
311 * @param string $size The size of the video.
312 * @param array $attr The attributes for the tag.
313 *
314 * @return object
315 */
316 public function video_attributes( $size = 'full', $attr = [] ): object {
317 // If the item is not set, return null.
318 if ( ! isset( $this->item->ID ) ) {
319 return (object) [];
320 }
321
322 $image_attributes = $this->attributes( $size, $attr );
323
324 $video = wp_get_attachment_url( $this->item->ID );
325 if ( ! $video ) {
326 return (object) [];
327 }
328
329 $size_class = $size;
330
331 if ( is_array( $size_class ) ) {
332 $size_class = implode( 'x', $size_class );
333 }
334
335 $attr = (object) wp_parse_args(
336 $attr,
337 array(
338 'src' => $video,
339 'poster' => $image_attributes->src ?? '',
340 'class' => "attachment-$size_class size-$size_class video-attachment",
341 'alt' => trim( wp_strip_all_tags( get_post_meta( $this->item->ID, '_wp_attachment_image_alt', true ) ) ),
342 'mime_type' => get_post_mime_type( $this->item->ID ),
343 )
344 );
345
346 return apply_filters(
347 'surecart_gallery_item_video_attributes',
348 $attr,
349 $this->item->ID,
350 $size,
351 );
352 }
353 }
354