-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathRenderer.php
More file actions
872 lines (775 loc) · 22.1 KB
/
Renderer.php
File metadata and controls
872 lines (775 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
<?php
/**
* Stories Renderer Base class.
*
* @link https://github.com/googleforcreators/web-stories-wp
*
* @copyright 2020 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare(strict_types = 1);
namespace Google\Web_Stories\Renderer\Stories;
use Google\Web_Stories\AMP_Story_Player_Assets;
use Google\Web_Stories\Assets;
use Google\Web_Stories\Context;
use Google\Web_Stories\Interfaces\Renderer as RenderingInterface;
use Google\Web_Stories\Model\Story;
use Google\Web_Stories\Services;
use Google\Web_Stories\Story_Post_Type;
use Google\Web_Stories\Story_Query;
use Iterator;
use WP_Post;
/**
* Renderer class.
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*
* @phpstan-import-type StoryAttributes from \Google\Web_Stories\Story_Query
*
* @implements Iterator<int, Story>
*/
abstract class Renderer implements RenderingInterface, Iterator {
/**
* Web Stories stylesheet handle.
*/
public const STYLE_HANDLE = 'web-stories-list-styles';
/**
* Web Stories stylesheet handle.
*/
public const LIGHTBOX_SCRIPT_HANDLE = 'web-stories-lightbox';
/**
* Number of instances invoked. Kept it static to keep track.
*/
protected static int $instances = 0;
/**
* Assets instance.
*
* @var Assets Assets instance.
*/
protected Assets $assets;
/**
* Context instance.
*
* @var Context Context instance.
*/
protected Context $context;
/**
* Object ID for the Renderer class.
* To enable support for multiple carousels and lightboxes
* on the same page, we needed to identify each Renderer instance.
*
* This variable is used to add appropriate class to the Web Stories
* wrapper.
*/
protected int $instance_id = 0;
/**
* Story_Query instance.
*
* @var Story_Query Story_Query instance.
*/
protected Story_Query $query;
/**
* Story attributes
*
* @var array<string, string|int|bool> An array of story attributes.
* @phpstan-var StoryAttributes
*/
protected array $attributes = [];
/**
* Story posts.
*
* @var Story[] An array of story posts.
*/
protected array $stories = [];
/**
* Holds required html for the lightbox.
*
* @var string A string of lightbox markup.
*/
protected string $lightbox_html = '';
/**
* Pointer to iterate over stories.
*/
private int $position = 0;
/**
* Height for displaying story.
*/
protected int $height = 308;
/**
* Width for displaying story.
*/
protected int $width = 185;
/**
* Whether content overlay is enabled for story.
*/
protected bool $content_overlay;
/**
* Constructor
*
* @since 1.5.0
*
* @param Story_Query $query Story_Query instance.
*/
public function __construct( Story_Query $query ) {
$this->query = $query;
$this->attributes = $this->query->get_story_attributes();
$this->content_overlay = $this->attributes['show_title'] || $this->attributes['show_date'] || $this->attributes['show_author'] || $this->attributes['show_excerpt'];
// TODO, find a way to inject this a cleaner way.
$injector = Services::get_injector();
/**
* Assets instance.
*
* @var Assets $assets Assets instance.
*/
$assets = $injector->make( Assets::class );
/**
* Context instance.
*
* @var Context $context Context instance.
*/
$context = $injector->make( Context::class );
$this->assets = $assets;
$this->context = $context;
}
/**
* Output markup for amp stories.
*
* @since 1.5.0
*
* @param array<string,mixed> $args Array of rendering arguments.
* @return string
*/
public function render( array $args = [] ): string {
++self::$instances;
$this->instance_id = self::$instances;
foreach ( $args as $key => $val ) {
if ( property_exists( $this, $key ) ) {
$this->{$key} = $val;
}
}
return '';
}
/**
* Retrieve current story.
*
* @since 1.5.0
*
* @return Story|null
*/
public function current(): ?Story {
return $this->stories[ $this->position ] ?? null;
}
/**
* Retrieve next story.
*
* @since 1.5.0
*
* @return void
*/
public function next(): void {
++$this->position;
}
/**
* Retrieve the key for current node in list.
*
* @since 1.5.0
*
* @return bool|float|int|string|void|null
*/
#[\ReturnTypeWillChange]
public function key() {
return $this->position;
}
/**
* Check if current position is valid.
*
* @since 1.5.0
*
* @return bool
*/
public function valid(): bool {
return isset( $this->stories[ $this->position ] );
}
/**
* Reset pointer to start of the list.
*
* @since 1.5.0
*
* @return void
*/
public function rewind(): void {
$this->position = 0;
}
/**
* Perform initial setup for object.
*
* @since 1.5.0
*
* @return void
*/
public function init(): void {
$this->stories = array_filter( array_map( [ $this, 'prepare_stories' ], $this->query->get_stories() ) );
add_action( 'wp_footer', [ $this, 'render_stories_lightbox' ] );
add_action( 'amp_post_template_footer', [ $this, 'render_stories_lightbox' ] );
}
/**
* Initializes renderer functionality.
*
* @since 1.5.0
*
* @return void
*/
public function load_assets(): void {
if ( wp_style_is( self::STYLE_HANDLE, 'registered' ) ) {
return;
}
// Web Stories styles for AMP and non-AMP pages.
$this->assets->register_style_asset( self::STYLE_HANDLE );
// Web Stories lightbox script.
$this->assets->register_script_asset( self::LIGHTBOX_SCRIPT_HANDLE, [ AMP_Story_Player_Assets::SCRIPT_HANDLE ] );
if ( \defined( 'AMPFORWP_VERSION' ) ) {
add_action( 'amp_post_template_css', [ $this, 'add_amp_post_template_css' ] );
}
}
/**
* Prints required inline CSS when using the AMP for WP plugin.
*
* @since 1.13.0
*
* @return void
*/
public function add_amp_post_template_css(): void {
$path = $this->assets->get_base_path( sprintf( 'assets/css/%s%s.css', self::STYLE_HANDLE, is_rtl() ? '-rtl' : '' ) );
if ( is_readable( $path ) ) {
$css = file_get_contents( $path ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown
echo $css; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
/**
* Returns story item data.
*
* @SuppressWarnings(PHPMD.NPathComplexity)
*
* @since 1.5.0
*
* @param object $post Array of post objects.
* @return Story|null Story object or null if given post
*/
public function prepare_stories( $post ): ?Story {
if ( ! $post instanceof WP_Post ) {
return null;
}
$story_data = [];
// TODO: get from field state instead.
if ( ! $this->is_view_type( 'circles' ) ) {
if ( isset( $this->attributes['show_author'] ) && true === $this->attributes['show_author'] ) {
$author_id = absint( get_post_field( 'post_author', $post->ID ) );
$story_data['author'] = get_the_author_meta( 'display_name', $author_id );
}
if ( isset( $this->attributes['show_date'] ) && true === $this->attributes['show_date'] ) {
/* translators: Date format, see https://www.php.net/manual/en/datetime.format.php */
$story_data['date'] = get_the_date( __( 'M j, Y', 'web-stories' ), $post->ID );
}
}
$story_data['classes'] = $this->get_single_story_classes();
$story = new Story( $story_data );
$story->load_from_post( $post );
return $story;
}
/**
* Render story markup.
*
* @since 1.5.0
*
* @return void
*/
public function render_single_story_content(): void {
/**
* Story object.
*
* @var Story $story
*/
$story = $this->current();
global $wp_version;
$single_story_classes = $this->get_single_story_classes();
$lightbox_state = 'lightbox' . $story->get_id() . $this->instance_id;
// No need to load these styles on admin as editor styles are being loaded by the block.
if ( ! is_admin() || ( \defined( 'IFRAME_REQUEST' ) && IFRAME_REQUEST ) ) {
// Web Stories Styles for AMP and non-AMP pages.
$this->assets->enqueue_style_asset( self::STYLE_HANDLE );
}
if ( $this->context->is_amp() ) {
?>
<div
class="<?php echo esc_attr( $single_story_classes ); ?>"
on="<?php echo esc_attr( sprintf( 'tap:AMP.setState({%1$s: ! %1$s})', $lightbox_state ) ); ?>"
tabindex="0"
role="button"
>
<?php $this->render_story_with_poster(); ?>
</div>
<?php
} else {
$this->assets->enqueue_style( AMP_Story_Player_Assets::SCRIPT_HANDLE );
$this->assets->enqueue_script( AMP_Story_Player_Assets::SCRIPT_HANDLE );
if ( version_compare( $wp_version, '6.5', '<' ) ) {
$this->assets->enqueue_script_asset( self::LIGHTBOX_SCRIPT_HANDLE );
}
?>
<div
class="<?php echo esc_attr( $single_story_classes ); ?>"
data-wp-interactive="web-stories-block"
<?php
if ( version_compare( $wp_version, '6.5', '>=' ) ) {
echo esc_attr(
wp_interactivity_data_wp_context(
[
'instanceId' => $this->instance_id,
]
)
);
}
?>
data-wp-on--click="actions.open"
data-wp-on-window--popstate="actions.onPopstate"
>
<?php $this->render_story_with_poster(); ?>
</div>
<?php
}
}
/**
* Renders the lightbox markup for non-amp pages.
*
* @since 1.5.0
*
* @return void
*/
public function render_stories_with_lightbox(): void {
$data = [
'controls' => [
[
'name' => 'close',
'position' => 'start',
],
[
'name' => 'skip-next',
],
],
'behavior' => [
'autoplay' => false,
],
];
?>
<div class="web-stories-list__lightbox">
<amp-story-player
width="3.6"
height="6"
layout="responsive"
data-wp-interactive="web-stories-block"
data-wp-on--amp-story-player-close="actions.close"
data-wp-on--navigation="actions.navigation"
>
<script type="application/json">
<?php echo wp_json_encode( $data ); ?>
</script>
<?php echo wp_kses_post( $this->lightbox_html ); ?>
</amp-story-player>
</div>
<?php
}
/**
* Renders the lightbox markup for non-amp pages.
*
* @since 1.5.0
*
* @return void
*/
public function render_stories_with_lightbox_amp(): void {
// Have to ignore this as the escaping functions are stripping off 'amp-bind' custom attribute '[class]'.
echo $this->lightbox_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Generated with properly escaped data.
}
/**
* Renders stories lightbox on 'wp_footer'.
*
* @return void
*/
public function render_stories_lightbox(): void {
// Return if we don't have anything to render.
if ( empty( $this->lightbox_html ) ) {
return;
}
?>
<div class="web-stories-list__lightbox-wrapper <?php echo esc_attr( 'ws-lightbox-' . $this->instance_id ); ?>">
<?php
if ( $this->context->is_amp() ) {
$this->render_stories_with_lightbox_amp();
} else {
$this->render_stories_with_lightbox();
}
?>
</div>
<?php
}
/**
* Verifies the current view type.
*
* @since 1.5.0
*
* @param string $view_type View type to check.
* @return bool Whether or not current view type matches the one passed.
*/
protected function is_view_type( $view_type ): bool {
return ( ! empty( $this->attributes['view_type'] ) && $view_type === $this->attributes['view_type'] );
}
/**
* Get view type for stories.
*
* @since 1.5.0
*
* @return string
*/
protected function get_view_type(): string {
return ! empty( $this->attributes['view_type'] ) ? $this->attributes['view_type'] : 'circles';
}
/**
* Renders stories archive link if the 'show_archive_link' attribute is set to true.
*
* @since 1.5.0
*
* @return void
*/
protected function maybe_render_archive_link(): void {
if (
empty( $this->attributes['show_archive_link'] ) ||
true !== $this->attributes['show_archive_link'] ||
empty( $this->attributes['archive_link_label'] )
) {
return;
}
$web_stories_archive = get_post_type_archive_link( Story_Post_Type::POST_TYPE_SLUG );
if ( empty( $web_stories_archive ) ) {
return;
}
?>
<div class="web-stories-list__archive-link">
<a href="<?php echo esc_url( $web_stories_archive ); ?>">
<?php echo esc_html( $this->attributes['archive_link_label'] ); ?>
</a>
</div>
<?php
}
/**
* Gets the classes for renderer container.
*
* @since 1.5.0
*
* @return string
*/
protected function get_view_classes(): string {
$view_classes = [];
$view_classes[] = ! empty( $this->attributes['view_type'] ) ? sprintf( 'is-view-type-%1$s', $this->attributes['view_type'] ) : 'is-view-type-circles';
if ( $this->is_view_type( 'grid' ) && ! empty( $this->attributes['number_of_columns'] ) ) {
$view_classes[] = sprintf( 'columns-%1$d', $this->attributes['number_of_columns'] );
}
if ( ! $this->is_view_type( 'circles' ) && ! empty( $this->attributes['sharp_corners'] ) ) {
$view_classes[] = 'is-style-squared';
} else {
$view_classes[] = 'is-style-default';
}
if ( $this->is_view_type( 'circles' ) && ! empty( $this->attributes['show_title'] ) ) {
$view_classes[] = 'has-title';
}
if ( $this->is_view_type( 'circles' ) || $this->is_view_type( 'carousel' ) ) {
$view_classes[] = 'is-carousel';
}
return implode( ' ', $view_classes );
}
/**
* Gets the classes for renderer container.
*
* @since 1.5.0
*
* @return string
*/
protected function get_container_classes(): string {
$container_classes = [];
$container_classes[] = 'web-stories-list';
$container_classes[] = ! empty( $this->attributes['align'] ) ? sprintf( 'align%1$s', $this->attributes['align'] ) : 'alignnone';
$container_classes[] = ! empty( $this->attributes['class'] ) ? $this->attributes['class'] : '';
if ( ! empty( $this->attributes['show_archive_link'] ) ) {
$container_classes[] = 'has-archive-link';
}
$container_classes = array_filter( $container_classes );
$view_type_classes = $this->get_view_classes();
return sprintf( '%1$s %2$s', implode( ' ', $container_classes ), $view_type_classes );
}
/**
* Gets the single story container classes.
*
* @since 1.5.0
*
* @return string
*/
protected function get_single_story_classes(): string {
$single_story_classes = [];
$single_story_classes[] = 'web-stories-list__story';
if ( $this->context->is_amp() ) {
$single_story_classes[] = 'web-stories-list__story--amp';
}
if ( ! empty( $this->attributes['image_alignment'] ) && ( 'right' === $this->attributes['image_alignment'] ) ) {
$single_story_classes[] = 'image-align-right';
}
$classes = implode( ' ', $single_story_classes );
/**
* Filters the web stories renderer single story classes.
*
* @since 1.5.0
*
* @param string $classes Single story classes.
*/
return apply_filters( 'web_stories_renderer_single_story_classes', $classes );
}
/**
* Gets the single story container styles.
*
* @since 1.5.0
*
* @return string Style string.
*/
protected function get_container_styles(): string {
$story_styles = ! empty( $this->attributes['circle_size'] ) && $this->is_view_type( 'circles' ) ?
sprintf( '--ws-circle-size:%1$dpx', $this->attributes['circle_size'] ) :
'';
$story_styles .= $this->is_view_type( 'carousel' ) ? sprintf( '--ws-story-max-width:%1$dpx', $this->width ) : '';
/**
* Filters the web stories renderer single story classes.
*
* @since 1.5.0
*
* @param string $story_styles Single story classes.
*/
return apply_filters( 'web_stories_renderer_container_styles', $story_styles );
}
/**
* Renders a story with story's poster image.
*
* @since 1.5.0
*
* @return void
*/
protected function render_story_with_poster(): void {
/**
* Story object.
*
* @var Story $story
*/
$story = $this->current();
$poster_url = $story->get_poster_portrait();
$poster_srcset = $story->get_poster_srcset();
$poster_sizes = $story->get_poster_sizes();
if ( ! $poster_url ) {
?>
<div class="web-stories-list__story-poster">
<div class="web-stories-list__story-poster-placeholder">
<a href="<?php echo esc_url( $story->get_url() ); ?>" <?php $this->render_link_attributes(); ?>>
<?php echo esc_html( $story->get_title() ); ?>
</a>
</div>
</div>
<?php
} else {
?>
<div class="web-stories-list__story-poster">
<a href="<?php echo esc_url( $story->get_url() ); ?>" <?php $this->render_link_attributes(); ?>>
<img
src="<?php echo esc_url( $poster_url ); ?>"
alt="<?php echo esc_attr( $story->get_title() ); ?>"
width="<?php echo absint( $this->width ); ?>"
height="<?php echo absint( $this->height ); ?>"
<?php if ( ! empty( $poster_srcset ) ) { ?>
srcset="<?php echo esc_attr( $poster_srcset ); ?>"
<?php } ?>
<?php if ( ! empty( $poster_sizes ) ) { ?>
sizes="<?php echo esc_attr( $poster_sizes ); ?>"
<?php } ?>
loading="lazy"
decoding="async"
>
</a>
</div>
<?php
}
$this->get_content_overlay();
if ( ! $this->context->is_amp() ) {
$this->generate_lightbox_html( $story );
} else {
$this->generate_amp_lightbox_html_amp( $story );
}
}
/**
* Render additional link attributes.
*
* Allows customization of html attributes in the web stories widget anchor tag loop
* Converts array into escaped inline html attributes.
*
* @since 1.17.0
*
* @return void
*/
protected function render_link_attributes(): void {
/**
* The current story.
*
* @var Story $story
*/
$story = $this->current();
/**
* Filters the link attributes added to a story's <a> tag.
*
* @since 1.17.0
*
* @param array $attributes Key value array of attribute name to attribute value.
* @param Story $story The current story instance.
* @param int $position The current story's position within the list.
* @param string $view_type The current view type.
*/
$attributes = apply_filters( 'web_stories_renderer_link_attributes', [], $story, $this->position, $this->get_view_type() );
$attrs = [];
if ( ! empty( $attributes ) ) {
foreach ( $attributes as $attribute => $value ) {
$attrs[] = wp_kses_one_attr( $attribute . '="' . esc_attr( $value ) . '"', 'a' );
}
}
$attrs = array_filter( $attrs ); // Filter out empty values rejected by KSES.
//phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
echo implode( ' ', $attrs );
}
/**
* Renders the content overlay markup.
*
* @since 1.5.0
*
* @return void
*/
protected function get_content_overlay(): void {
/**
* Story object.
*
* @var Story $story
*/
$story = $this->current();
if ( empty( $this->content_overlay ) ) {
return;
}
?>
<div class="web-stories-list__story-content-overlay">
<?php if ( ! empty( $this->attributes['show_title'] ) ) { ?>
<div class="story-content-overlay__title">
<?php echo esc_html( $story->get_title() ); ?>
</div>
<?php } ?>
<?php if ( ! empty( $this->attributes['show_excerpt'] ) ) { ?>
<div class="story-content-overlay__excerpt">
<?php echo esc_html( $story->get_excerpt() ); ?>
</div>
<?php } ?>
<?php if ( ! empty( $story->get_author() ) ) { ?>
<div class="story-content-overlay__author">
<?php
/* translators: byline. %s: author name. */
echo esc_html( sprintf( __( 'By %s', 'web-stories' ), $story->get_author() ) );
?>
</div>
<?php } ?>
<?php if ( ! empty( $story->get_date() ) ) { ?>
<time class="story-content-overlay__date">
<?php
/* translators: %s: publish date. */
echo esc_html( sprintf( __( 'On %s', 'web-stories' ), $story->get_date() ) );
?>
</time>
<?php } ?>
</div>
<?php
}
/**
* Generate HTML for the non-AMP page request.
*
* @since 1.5.0
*
* @param Story $story Current Story.
* @return void
*/
protected function generate_lightbox_html( $story ): void {
// Start collecting markup for the lightbox stories. This way we don't have to re-run the loop.
ob_start();
// Collect story links to fill-in non-AMP lightbox 'amp-story-player'.
?>
<a href="<?php echo esc_url( $story->get_url() ); ?>" <?php $this->render_link_attributes(); ?>><?php echo esc_html( $story->get_title() ); ?></a>
<?php
$this->lightbox_html .= ob_get_clean();
}
/**
* Markup for the lightbox used on AMP pages.
*
* @since 1.5.0
*
* @param Story $story Current Story.
* @return void
*/
protected function generate_amp_lightbox_html_amp( $story ): void {
// Start collecting markup for the lightbox stories. This way we don't have to re-run the loop.
ob_start();
$lightbox_state = 'lightbox' . $story->get_id() . $this->instance_id;
$lightbox_id = 'lightbox-' . $story->get_id() . $this->instance_id;
?>
<amp-lightbox
id="<?php echo esc_attr( $lightbox_id ); ?>"
[open]="<?php echo esc_attr( $lightbox_state ); ?>"
layout="nodisplay"
on="lightboxClose:AMP.setState({<?php echo esc_attr( $lightbox_state ); ?>: false})"
role="button"
tabindex="0"
>
<div class="web-stories-list__lightbox show">
<button type="button"
class="story-lightbox__close-button"
on="tap:<?php echo esc_attr( $lightbox_id ); ?>.close"
aria-label="<?php esc_attr_e( 'Close', 'web-stories' ); ?>"
>
<span class="story-lightbox__close-button--stick"></span>
<span class="story-lightbox__close-button--stick"></span>
</button>
<amp-story-player
width="3.6"
height="6"
layout="responsive"
data-wp-interactive="web-stories-block"
data-wp-on--amp-story-player-close="actions.close"
data-wp-on--navigation="actions.navigation"
>
<a href="<?php echo esc_url( $story->get_url() ); ?>" <?php $this->render_link_attributes(); ?>><?php echo esc_html( $story->get_title() ); ?></a>
</amp-story-player>
</div>
</amp-lightbox>
<?php
$this->lightbox_html .= ob_get_clean();
}
}