PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
← All changes | extensions/blocks/podcast-player/podcast-player.php +22 -274 12.5.2 → 16.3-beta View file →
@@ -9,16 +9,11 @@
9 9
10 10 namespace Automattic\Jetpack\Extensions\Podcast_Player;
11 11
12 12 use Automattic\Jetpack\Blocks;
13 -use Jetpack_Gutenberg;
14 -use Jetpack_Podcast_Helper;
15 13
16 -const FEATURE_NAME = 'podcast-player';
17 -const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
18 -
19 -if ( ! class_exists( 'Jetpack_Podcast_Helper' ) ) {
20 - require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-podcast-helper.php';
14 +if ( ! defined( 'ABSPATH' ) ) {
15 + exit( 0 );
21 16 }
22 17
23 18 /**
24 19 * Registers the block for use in Gutenberg. This is done via an action so that
@@ -25,42 +20,14 @@
25 20 * we can disable registration if we need to.
26 21 */
27 22 function register_block() {
28 23 Blocks::jetpack_register_block(
29 - BLOCK_NAME,
24 + __DIR__,
30 25 array(
31 - 'attributes' => array(
32 - 'url' => array(
33 - 'type' => 'string',
34 - ),
35 - 'itemsToShow' => array(
36 - 'type' => 'integer',
37 - 'default' => 5,
38 - ),
39 - 'showCoverArt' => array(
40 - 'type' => 'boolean',
41 - 'default' => true,
42 - ),
43 - 'showEpisodeTitle' => array(
44 - 'type' => 'boolean',
45 - 'default' => true,
46 - ),
47 - 'showEpisodeDescription' => array(
48 - 'type' => 'boolean',
49 - 'default' => true,
50 - ),
51 - ),
52 - 'render_callback' => __NAMESPACE__ . '\render_block',
53 - 'supports' => array(
54 - 'align' => array( 'wide', 'full' ),
55 - 'spacing' => array(
56 - 'padding' => true,
57 - 'margin' => true,
58 - ),
59 - ),
26 + 'render_callback' => __NAMESPACE__ . '\render_block',
60 27 // Since Gutenberg #31873.
61 - 'style' => 'wp-mediaelement',
62 -
28 + 'style' => 'wp-mediaelement',
29 + 'render_email_callback' => __NAMESPACE__ . '\render_email',
63 30 )
64 31 );
65 32 }
66 33 add_action( 'init', __NAMESPACE__ . '\register_block' );
@@ -65,25 +32,12 @@
65 32 }
66 33 add_action( 'init', __NAMESPACE__ . '\register_block' );
67 34
68 35 /**
69 - * Returns the error message wrapped in HTML if current user
70 - * has the capability to edit the post. Public visitors will
71 - * never see errors.
36 + * Podcast Player block registration/dependency declaration.
72 37 *
73 - * @param string $message The error message to display.
74 - * @return string
75 - */
76 -function render_error( $message ) {
77 - // Suppress errors for users unable to address them.
78 - if ( ! current_user_can( 'edit_posts' ) ) {
79 - return '';
80 - }
81 - return '<p>' . esc_html( $message ) . '</p>';
82 -}
83 -
84 -/**
85 - * Podcast Player block registration/dependency declaration.
38 + * The render implementation lives in render.php and is only loaded when the
39 + * block is actually rendered, keeping it out of the eager front-end path.
86 40 *
87 41 * @param array $attributes Array containing the Podcast Player block attributes.
88 42 * @param string $content Fallback content - a direct link to RSS, as rendered by save.js.
89 43 * @return string
@@ -88,232 +42,26 @@
88 42 * @param string $content Fallback content - a direct link to RSS, as rendered by save.js.
89 43 * @return string
90 44 */
91 45 function render_block( $attributes, $content ) {
92 - // Don't render an interactive version of the block outside the frontend context.
93 - if ( ! jetpack_is_frontend() ) {
94 - return $content;
95 - }
96 -
97 - // Test for empty URLS.
98 - if ( empty( $attributes['url'] ) ) {
99 - return render_error( __( 'No Podcast URL provided. Please enter a valid Podcast RSS feed URL.', 'jetpack' ) );
100 - }
101 -
102 - // Test for invalid URLs.
103 - if ( ! wp_http_validate_url( $attributes['url'] ) ) {
104 - return render_error( __( 'Your podcast URL is invalid and couldn\'t be embedded. Please double check your URL.', 'jetpack' ) );
105 - }
106 -
107 - if ( ! empty( $attributes['selectedEpisodes'] ) ) {
108 - $guids = array_map(
109 - function ( $episode ) {
110 - return $episode['guid'];
111 - },
112 - $attributes['selectedEpisodes']
113 - );
114 - $player_args = array( 'guids' => $guids );
115 - } else {
116 - $player_args = array();
117 - }
118 -
119 - // Sanitize the URL.
120 - $attributes['url'] = esc_url_raw( $attributes['url'] );
121 - $player_data = ( new Jetpack_Podcast_Helper( $attributes['url'] ) )->get_player_data( $player_args );
122 -
123 - if ( is_wp_error( $player_data ) ) {
124 - return render_error( $player_data->get_error_message() );
125 - }
126 -
127 - return render_player( $player_data, $attributes );
46 + require_once __DIR__ . '/render.php';
47 + return render_block_implementation( $attributes, $content );
128 48 }
129 49
130 50 /**
131 - * Renders the HTML for the Podcast player and tracklist.
51 + * Render podcast player block for email.
132 52 *
133 - * @param array $player_data The player data details.
134 - * @param array $attributes Array containing the Podcast Player block attributes.
135 - * @return string The HTML for the podcast player.
136 - */
137 -function render_player( $player_data, $attributes ) {
138 - // If there are no tracks (it is possible) then display appropriate user facing error message.
139 - if ( empty( $player_data['tracks'] ) ) {
140 - return render_error( __( 'No tracks available to play.', 'jetpack' ) );
141 - }
142 -
143 - // Only use the amount of tracks requested.
144 - $player_data['tracks'] = array_slice(
145 - $player_data['tracks'],
146 - 0,
147 - absint( $attributes['itemsToShow'] )
148 - );
149 -
150 - // Generate a unique id for the block instance.
151 - $instance_id = wp_unique_id( 'jetpack-podcast-player-block-' . get_the_ID() . '-' );
152 - $player_data['playerId'] = $instance_id;
153 -
154 - // Generate object to be used as props for PodcastPlayer.
155 - $player_props = array_merge(
156 - // Add all attributes.
157 - array( 'attributes' => $attributes ),
158 - // Add all player data.
159 - $player_data
160 - );
161 -
162 - $primary_colors = get_colors( 'primary', $attributes, 'color' );
163 - $secondary_colors = get_colors( 'secondary', $attributes, 'color' );
164 - $background_colors = get_colors( 'background', $attributes, 'background-color' );
165 -
166 - $player_classes_name = trim( "{$secondary_colors['class']} {$background_colors['class']}" );
167 - $player_inline_style = trim( "{$secondary_colors['style']} {$background_colors['style']}" );
168 - $player_inline_style .= get_css_vars( $attributes );
169 - $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
170 - $block_classname = Blocks::classes( FEATURE_NAME, $attributes, array( 'is-default' ) );
171 - $is_amp = Blocks::is_amp_request();
172 -
173 - ob_start();
174 - ?>
175 - <div class="<?php echo esc_attr( $block_classname ); ?>"<?php echo ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : ''; ?> id="<?php echo esc_attr( $instance_id ); ?>">
176 - <section
177 - class="jetpack-podcast-player <?php echo esc_attr( $player_classes_name ); ?>"
178 - style="<?php echo esc_attr( $player_inline_style ); ?>"
179 - >
180 - <?php
181 - render(
182 - 'podcast-header',
183 - array_merge(
184 - $player_props,
185 - array(
186 - 'primary_colors' => $primary_colors,
187 - 'player_id' => $player_data['playerId'],
188 - )
189 - )
190 - );
191 - ?>
192 - <?php if ( count( $player_data['tracks'] ) > 1 ) : ?>
193 - <ol class="jetpack-podcast-player__tracks">
194 - <?php foreach ( $player_data['tracks'] as $track_index => $attachment ) : ?>
195 - <?php
196 - render(
197 - 'playlist-track',
198 - array(
199 - 'is_active' => 0 === $track_index,
200 - 'attachment' => $attachment,
201 - 'primary_colors' => $primary_colors,
202 - 'secondary_colors' => $secondary_colors,
203 - )
204 - );
205 - ?>
206 - <?php endforeach; ?>
207 - </ol>
208 - <?php endif; ?>
209 - </section>
210 - <?php if ( ! $is_amp ) : ?>
211 - <script type="application/json"><?php echo wp_json_encode( $player_props ); ?></script>
212 - <?php endif; ?>
213 - </div>
214 - <?php
215 - /**
216 - * Enqueue necessary scripts and styles.
217 - */
218 - if ( ! $is_amp ) {
219 - wp_enqueue_style( 'wp-mediaelement' );
220 - }
221 - Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'mediaelement' ) );
222 -
223 - return ob_get_clean();
224 -}
225 -
226 -/**
227 - * Given the color name, block attributes and the CSS property,
228 - * the function will return an array with the `class` and `style`
229 - * HTML attributes to be used straight in the markup.
53 + * The render implementation lives in render.php and is only loaded when the
54 + * block is actually rendered, keeping it out of the eager front-end path.
230 55 *
231 - * @example
232 - * $color = get_colors( 'secondary', $attributes, 'border-color'
233 - * => array( 'class' => 'has-secondary', 'style' => 'border-color: #333' )
56 + * @since 15.0
234 57 *
235 - * @param string $name Color attribute name, for instance `primary`, `secondary`, ...
236 - * @param array $attrs Block attributes.
237 - * @param string $property Color CSS property, fo instance `color`, `background-color`, ...
238 - * @return array Colors array.
239 - */
240 -function get_colors( $name, $attrs, $property ) {
241 - $attr_color = "{$name}Color";
242 - $attr_custom = 'custom' . ucfirst( $attr_color );
243 -
244 - $color = isset( $attrs[ $attr_color ] ) ? $attrs[ $attr_color ] : null;
245 - $custom_color = isset( $attrs[ $attr_custom ] ) ? $attrs[ $attr_custom ] : null;
246 -
247 - $colors = array(
248 - 'class' => '',
249 - 'style' => '',
250 - );
251 -
252 - if ( $color || $custom_color ) {
253 - $colors['class'] .= "has-{$name}";
254 -
255 - if ( $color ) {
256 - $colors['class'] .= " has-{$color}-{$property}";
257 - } elseif ( $custom_color ) {
258 - $colors['style'] .= "{$property}: {$custom_color};";
259 - }
260 - }
261 -
262 - return $colors;
263 -}
264 -
265 -/**
266 - * It generates a string with CSS variables according to the
267 - * block colors, prefixing each one with `--jetpack-podcast-player'.
58 + * @param string $block_content The original block HTML content.
59 + * @param array $parsed_block The parsed block data including attributes.
60 + * @param object $rendering_context Email rendering context.
268 61 *
269 - * @param array $attrs Podcast Block attributes object.
270 - * @return string CSS variables depending on block colors.
62 + * @return string
271 63 */
272 -function get_css_vars( $attrs ) {
273 - $colors_name = array( 'primary', 'secondary', 'background' );
274 -
275 - $inline_style = '';
276 - foreach ( $colors_name as $color ) {
277 - $hex_color = 'hex' . ucfirst( $color ) . 'Color';
278 - if ( ! empty( $attrs[ $hex_color ] ) ) {
279 - $inline_style .= " --jetpack-podcast-player-{$color}: {$attrs[ $hex_color ]};";
280 - }
281 - }
282 - return $inline_style;
283 -}
284 -
285 -/**
286 - * Render the given template in server-side.
287 - * Important note:
288 - * The $template_props array will be extracted.
289 - * This means it will create a var for each array item.
290 - * Keep it mind when using this param to pass
291 - * properties to the template.
292 - *
293 - * @param string $name Template name, available in `./templates` folder.
294 - * @param array $template_props Template properties. Optional.
295 - * @param bool $print Render template. True as default.
296 - * @return false|string HTML markup or false.
297 - */
298 -function render( $name, $template_props = array(), $print = true ) {
299 - if ( ! strpos( $name, '.php' ) ) {
300 - $name = $name . '.php';
301 - }
302 -
303 - $template_path = __DIR__ . '/templates/' . $name;
304 -
305 - if ( ! file_exists( $template_path ) ) {
306 - return '';
307 - }
308 -
309 - if ( $print ) {
310 - include $template_path;
311 - } else {
312 - ob_start();
313 - include $template_path;
314 - $markup = ob_get_contents();
315 - ob_end_clean();
316 -
317 - return $markup;
318 - }
64 +function render_email( $block_content, array $parsed_block, $rendering_context ) {
65 + require_once __DIR__ . '/render.php';
66 + return render_email_implementation( $block_content, $parsed_block, $rendering_context );
319 67 }