Completed
Push — update/podcast-player-single-e... ( 29bbed...c67876 )
by
unknown
558:12 queued 548:14
created

podcast-player.php ➔ render_block()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 9
nop 2
dl 0
loc 36
rs 8.0995
c 0
b 0
f 0
1
<?php
2
/**
3
 * Podcast Player Block.
4
 *
5
 * @since 8.4.0
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Automattic\Jetpack\Extensions\Podcast_Player;
11
12
use Automattic\Jetpack\Blocks;
13
use Jetpack_Gutenberg;
14
use Jetpack_Podcast_Helper;
15
16
const FEATURE_NAME = 'podcast-player';
17
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
18
19
if ( ! class_exists( 'Jetpack_Podcast_Helper' ) ) {
20
	\jetpack_require_lib( 'class-jetpack-podcast-helper' );
21
}
22
23
/**
24
 * Registers the block for use in Gutenberg. This is done via an action so that
25
 * we can disable registration if we need to.
26
 */
27
function register_block() {
28
	Blocks::jetpack_register_block(
29
		BLOCK_NAME,
30
		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
				'singleEpisode'          => array(
52
					'type' => 'object',
53
				),
54
			),
55
			'render_callback' => __NAMESPACE__ . '\render_block',
56
		)
57
	);
58
}
59
add_action( 'init', __NAMESPACE__ . '\register_block' );
60
61
/**
62
 * Returns the error message wrapped in HTML if current user
63
 * has the capability to edit the post. Public visitors will
64
 * never see errors.
65
 *
66
 * @param string $message The error message to display.
67
 * @return string
68
 */
69
function render_error( $message ) {
70
	// Suppress errors for users unable to address them.
71
	if ( ! current_user_can( 'edit_posts' ) ) {
72
		return '';
73
	}
74
	return '<p>' . esc_html( $message ) . '</p>';
75
}
76
77
/**
78
 * Podcast Player block registration/dependency declaration.
79
 *
80
 * @param array  $attributes Array containing the Podcast Player block attributes.
81
 * @param string $content    Fallback content - a direct link to RSS, as rendered by save.js.
82
 * @return string
83
 */
84
function render_block( $attributes, $content ) {
85
	// Don't render an interactive version of the block outside the frontend context.
86
	if ( ! jetpack_is_frontend() ) {
87
		return $content;
88
	}
89
90
	// Test for empty URLS.
91
	if ( empty( $attributes['url'] ) ) {
92
		return render_error( __( 'No Podcast URL provided. Please enter a valid Podcast RSS feed URL.', 'jetpack' ) );
93
	}
94
95
	// Test for invalid URLs.
96
	if ( ! wp_http_validate_url( $attributes['url'] ) ) {
97
		return render_error( __( 'Your podcast URL is invalid and couldn\'t be embedded. Please double check your URL.', 'jetpack' ) );
98
	}
99
100
	// Sanitize the URL.
101
	$attributes['url'] = esc_url_raw( $attributes['url'] );
102
	$helper            = new Jetpack_Podcast_Helper( $attributes['url'] );
103
	if ( isset( $attributes['singleEpisode']['guid'] ) ) {
104
		$player_data = $helper->get_player_data( array( 'guid' => $attributes['singleEpisode']['guid'] ) );
105
	} else {
106
		$player_data = $helper->get_player_data();
107
	}
108
109
	if ( is_wp_error( $player_data ) ) {
110
		return render_error( $player_data->get_error_message() );
0 ignored issues
show
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
	}
112
113
	// If we haven't managed to get the episode details fallback to what is saved in the attribute.
114
	if ( empty( $player_data['tracks'] ) && isset( $attributes['singleEpisode'] ) ) {
115
		$player_data['tracks'] = $attributes['singleEpisode'];
116
	}
117
118
	return render_player( $player_data, $attributes );
119
}
120
121
/**
122
 * Renders the HTML for the Podcast player and tracklist.
123
 *
124
 * @param array $player_data The player data details.
125
 * @param array $attributes Array containing the Podcast Player block attributes.
126
 * @return string The HTML for the podcast player.
127
 */
128
function render_player( $player_data, $attributes ) {
129
	// If there are no tracks (it is possible) then display appropriate user facing error message.
130
	if ( empty( $player_data['tracks'] ) ) {
131
		return render_error( __( 'No tracks available to play.', 'jetpack' ) );
132
	}
133
134
	// Only use the amount of tracks requested.
135
	$player_data['tracks'] = array_slice(
136
		$player_data['tracks'],
137
		0,
138
		absint( $attributes['itemsToShow'] )
139
	);
140
141
	// Generate a unique id for the block instance.
142
	$instance_id             = wp_unique_id( 'jetpack-podcast-player-block-' . get_the_ID() . '-' );
143
	$player_data['playerId'] = $instance_id;
144
145
	// Generate object to be used as props for PodcastPlayer.
146
	$player_props = array_merge(
147
		// Add all attributes.
148
		array( 'attributes' => $attributes ),
149
		// Add all player data.
150
		$player_data
151
	);
152
153
	$primary_colors    = get_colors( 'primary', $attributes, 'color' );
154
	$secondary_colors  = get_colors( 'secondary', $attributes, 'color' );
155
	$background_colors = get_colors( 'background', $attributes, 'background-color' );
156
157
	$player_classes_name  = trim( "{$secondary_colors['class']} {$background_colors['class']}" );
158
	$player_inline_style  = trim( "{$secondary_colors['style']} ${background_colors['style']}" );
159
	$player_inline_style .= get_css_vars( $attributes );
160
161
	$block_classname = Blocks::classes( FEATURE_NAME, $attributes, array( 'is-default' ) );
162
	$is_amp          = Blocks::is_amp_request();
163
164
	ob_start();
165
	?>
166
	<div class="<?php echo esc_attr( $block_classname ); ?>" id="<?php echo esc_attr( $instance_id ); ?>">
167
		<section
168
			class="jetpack-podcast-player <?php echo esc_attr( $player_classes_name ); ?>"
169
			style="<?php echo esc_attr( $player_inline_style ); ?>"
170
		>
171
			<?php
172
			render(
173
				'podcast-header',
174
				array_merge(
175
					$player_props,
176
					array(
177
						'primary_colors' => $primary_colors,
178
						'player_id'      => $player_data['playerId'],
179
					)
180
				)
181
			);
182
			?>
183
			<?php if ( count( $player_data['tracks'] ) > 1 ) : ?>
184
			<ol class="jetpack-podcast-player__tracks">
185
				<?php foreach ( $player_data['tracks'] as $track_index => $attachment ) : ?>
186
					<?php
187
					render(
188
						'playlist-track',
189
						array(
190
							'is_active'        => 0 === $track_index,
191
							'attachment'       => $attachment,
192
							'primary_colors'   => $primary_colors,
193
							'secondary_colors' => $secondary_colors,
194
						)
195
					);
196
					?>
197
				<?php endforeach; ?>
198
			</ol>
199
			<?php endif; ?>
200
		</section>
201
		<?php if ( ! $is_amp ) : ?>
202
		<script type="application/json"><?php echo wp_json_encode( $player_props ); ?></script>
203
		<?php endif; ?>
204
	</div>
205
	<?php
206
	/**
207
	 * Enqueue necessary scripts and styles.
208
	 */
209
	if ( ! $is_amp ) {
210
		wp_enqueue_style( 'wp-mediaelement' );
211
	}
212
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'mediaelement' ) );
213
214
	return ob_get_clean();
215
}
216
217
/**
218
 * Given the color name, block attributes and the CSS property,
219
 * the function will return an array with the `class` and `style`
220
 * HTML attributes to be used straight in the markup.
221
 *
222
 * @example
223
 * $color = get_colors( 'secondary', $attributes, 'border-color'
224
 *  => array( 'class' => 'has-secondary', 'style' => 'border-color: #333' )
225
 *
226
 * @param string $name     Color attribute name, for instance `primary`, `secondary`, ...
227
 * @param array  $attrs    Block attributes.
228
 * @param string $property Color CSS property, fo instance `color`, `background-color`, ...
229
 * @return array           Colors array.
230
 */
231
function get_colors( $name, $attrs, $property ) {
232
	$attr_color  = "{$name}Color";
233
	$attr_custom = 'custom' . ucfirst( $attr_color );
234
235
	$color        = isset( $attrs[ $attr_color ] ) ? $attrs[ $attr_color ] : null;
236
	$custom_color = isset( $attrs[ $attr_custom ] ) ? $attrs[ $attr_custom ] : null;
237
238
	$colors = array(
239
		'class' => '',
240
		'style' => '',
241
	);
242
243
	if ( $color || $custom_color ) {
244
		$colors['class'] .= "has-{$name}";
245
246
		if ( $color ) {
247
			$colors['class'] .= " has-{$color}-{$property}";
248
		} elseif ( $custom_color ) {
249
			$colors['style'] .= "{$property}: {$custom_color};";
250
		}
251
	}
252
253
	return $colors;
254
}
255
256
/**
257
 * It generates a string with CSS variables according to the
258
 * block colors, prefixing each one with `--jetpack-podcast-player'.
259
 *
260
 * @param array $attrs Podcast Block attributes object.
261
 * @return string      CSS variables depending on block colors.
262
 */
263
function get_css_vars( $attrs ) {
264
	$colors_name = array( 'primary', 'secondary', 'background' );
265
266
	$inline_style = '';
267
	foreach ( $colors_name as $color ) {
268
		$hex_color = 'hex' . ucfirst( $color ) . 'Color';
269
		if ( ! empty( $attrs[ $hex_color ] ) ) {
270
			$inline_style .= " --jetpack-podcast-player-{$color}: {$attrs[ $hex_color ]};";
271
		}
272
	}
273
	return $inline_style;
274
}
275
276
/**
277
 * Render the given template in server-side.
278
 * Important note:
279
 *    The $template_props array will be extracted.
280
 *    This means it will create a var for each array item.
281
 *    Keep it mind when using this param to pass
282
 *    properties to the template.
283
 *
284
 * @param string $name           Template name, available in `./templates` folder.
285
 * @param array  $template_props Template properties. Optional.
286
 * @param bool   $print          Render template. True as default.
287
 * @return false|string          HTML markup or false.
288
 */
289
function render( $name, $template_props = array(), $print = true ) {
290
	if ( ! strpos( $name, '.php' ) ) {
291
		$name = $name . '.php';
292
	}
293
294
	$template_path = __DIR__ . '/templates/' . $name;
295
296
	if ( ! file_exists( $template_path ) ) {
297
		return '';
298
	}
299
300
	/*
301
	 * Optionally provided an assoc array of data to pass to template.
302
	 * IMPORTANT: It will be extracted into variables.
303
	 */
304
	if ( is_array( $template_props ) ) {
305
		/*
306
		 * It ignores the `discouraging` sniffer rule for extract, since it's needed
307
		 * to make the templating system works.
308
		 */
309
		extract( $template_props ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
310
	}
311
312
	if ( $print ) {
313
		include $template_path;
314
	} else {
315
		ob_start();
316
		include $template_path;
317
		$markup = ob_get_contents();
318
		ob_end_clean();
319
320
		return $markup;
321
	}
322
}
323