Completed
Push — add/partial-reconnect ( ffa9f4...dfe562 )
by
unknown
07:36
created

story.php ➔ render_block()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 43
rs 9.232
c 0
b 0
f 0
1
<?php
2
/**
3
 * Story Block.
4
 *
5
 * @since 8.6.1
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Automattic\Jetpack\Extensions\Story;
11
12
use Jetpack_Gutenberg;
13
14
const FEATURE_NAME = 'story';
15
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
16
17
/**
18
 * Registers the block for use in Gutenberg
19
 * This is done via an action so that we can disable
20
 * registration if we need to.
21
 */
22
function register_block() {
23
	jetpack_register_block(
24
		BLOCK_NAME,
25
		array( 'render_callback' => __NAMESPACE__ . '\render_block' )
26
	);
27
}
28
add_action( 'init', __NAMESPACE__ . '\register_block' );
29
30
/**
31
 * Render an image inside a slide
32
 *
33
 * @param array $media  Image information.
34
 *
35
 * @return string
36
 */
37
function render_image( $media ) {
38
	if ( empty( $media['id'] ) || empty( $media['url'] ) ) {
39
		return __( 'Error retrieving media', 'jetpack' );
40
	}
41
	return sprintf(
42
		'<img
43
			alt="%1$s"
44
			class="wp-block-jetpack-story_image wp-story-image wp-image-%2$s"
45
			data-id="%2$s"
46
			src="%3$s">',
47
		esc_attr( $media['alt'] ),
48
		$media['id'],
49
		esc_attr( $media['url'] )
50
	);
51
}
52
53
/**
54
 * Render a video inside a slide
55
 *
56
 * @param array $media  Video information.
57
 *
58
 * @return string
59
 */
60
function render_video( $media ) {
61
	if ( empty( $media['id'] ) || empty( $media['mime'] ) || empty( $media['url'] ) ) {
62
		return __( 'Error retrieving media', 'jetpack' );
63
	}
64
	return sprintf(
65
		'<video
66
			title="%1$s"
67
			type="%2$s"
68
			class="wp-story-video intrinsic-ignore wp-video-%3$s"
69
			data-id="%3$s"
70
			src="%4$s">
71
		</video>',
72
		esc_attr( $media['alt'] ),
73
		esc_attr( $media['mime'] ),
74
		$media['id'],
75
		esc_attr( $media['url'] )
76
	);
77
}
78
79
/**
80
 * Render a slide
81
 *
82
 * @param array $media  Media information.
83
 * @param array $index  Index of the slide, first slide will be displayed by default, others hidden.
84
 *
85
 * @return string
86
 */
87
function render_slide( $media, $index ) {
88
	return sprintf(
89
		'<li class="wp-story-slide" style="display: %s;">
90
			<figure>
91
				%s
92
			</figure>
93
		</li>',
94
		0 === $index ? 'block' : 'none',
95
		'image' === $media['type']
96
			? render_image( $media, $index )
97
			: render_video( $media, $index )
98
	);
99
}
100
101
/**
102
 * Render story block
103
 *
104
 * @param array $attributes  Block attributes.
105
 *
106
 * @return string
107
 */
108
function render_block( $attributes ) {
109
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
110
111
	$settings    = array();
112
	$media_files = isset( $attributes['mediaFiles'] ) ? $attributes['mediaFiles'] : array();
113
114
	return sprintf(
115
		'<div class="%1$s" data-settings="%2$s">
116
			<div style="display: contents;">
117
				<div class="wp-story-container">
118
					<div class="wp-story-meta">
119
						<div class="wp-story-icon">
120
							<img alt="%3$s" src="%4$s" width="32" height=32>
121
						</div>
122
						<div>
123
							<div class="wp-story-title">
124
								%5$s
125
							</div>
126
						</div>
127
						<button class="wp-story-exit-fullscreen jetpack-mdc-icon-button">
128
							<i class="jetpack-material-icons close md-24"></i>
129
						</button>
130
					</div>
131
					<ul class="wp-story-wrapper">
132
						%6$s
133
					</ul>
134
					<div class="wp-story-overlay">
135
						<button class="jetpack-mdc-icon-button circle-icon outlined bordered" aria-label="%7$s" aria-pressed="false">
136
							<i class="jetpack-material-icons play_arrow" style="font-size: 56px;"></i>
137
						</button>
138
					</div>
139
				</div>
140
			</div>
141
		</div>',
142
		esc_attr( Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes, array( 'wp-story', 'aligncenter' ) ) ),
143
		filter_var( wp_json_encode( $settings ), FILTER_SANITIZE_SPECIAL_CHARS ),
144
		__( 'Site icon', 'jetpack' ),
145
		esc_attr( get_site_icon_url( 32, includes_url( 'images/w-logo-blue.png' ) ) ),
146
		esc_html( get_the_title() ),
147
		join( "\n", array_map( __NAMESPACE__ . '\render_slide', $media_files, array_keys( $media_files ) ) ),
148
		__( 'Exit Fullscreen', 'jetpack' )
149
	);
150
}
151