Completed
Push — renovate/url-polyfill-1.x ( 01368d...f2aaf6 )
by
unknown
114:05 queued 106:27
created

podcast-player.php ➔ render_player()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 17
nop 2
dl 0
loc 88
rs 7.3284
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 WP_Error;
13
use Jetpack_Gutenberg;
14
use Jetpack_Podcast_Helper;
15
use Jetpack_AMP_Support;
16
17
const FEATURE_NAME = 'podcast-player';
18
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
19
20
if ( ! class_exists( 'Jetpack_Podcast_Helper' ) ) {
21
	\jetpack_require_lib( 'class-jetpack-podcast-helper' );
22
}
23
24
/**
25
 * Registers the block for use in Gutenberg. This is done via an action so that
26
 * we can disable registration if we need to.
27
 */
28
function register_block() {
29
	jetpack_register_block(
30
		BLOCK_NAME,
31
		array(
32
			'attributes'      => array(
33
				'url'                    => array(
34
					'type' => 'url',
35
				),
36
				'itemsToShow'            => array(
37
					'type'    => 'integer',
38
					'default' => 5,
39
				),
40
				'showCoverArt'           => array(
41
					'type'    => 'boolean',
42
					'default' => true,
43
				),
44
				'showEpisodeDescription' => array(
45
					'type'    => 'boolean',
46
					'default' => true,
47
				),
48
			),
49
			'render_callback' => __NAMESPACE__ . '\render_block',
50
		)
51
	);
52
}
53
add_action( 'init', __NAMESPACE__ . '\register_block' );
54
55
/**
56
 * Returns the error message wrapped in HTML if current user
57
 * has the capability to edit the post. Public visitors will
58
 * never see errors.
59
 *
60
 * @param string $message The error message to display.
61
 * @return string
62
 */
63
function render_error( $message ) {
64
	// Suppress errors for users unable to address them.
65
	if ( ! current_user_can( 'edit_posts' ) ) {
66
		return '';
67
	}
68
	return '<p>' . esc_html( $message ) . '</p>';
69
}
70
71
/**
72
 * Podcast Player block registration/dependency declaration.
73
 *
74
 * @param array $attributes Array containing the Podcast Player block attributes.
75
 * @return string
76
 */
77
function render_block( $attributes ) {
78
79
	// Test for empty URLS.
80
	if ( empty( $attributes['url'] ) ) {
81
		return render_error( __( 'No Podcast URL provided. Please enter a valid Podcast RSS feed URL.', 'jetpack' ) );
82
	}
83
84
	// Test for invalid URLs.
85
	if ( ! wp_http_validate_url( $attributes['url'] ) ) {
86
		return render_error( __( 'Your podcast URL is invalid and couldn\'t be embedded. Please double check your URL.', 'jetpack' ) );
87
	}
88
89
	// Sanitize the URL.
90
	$attributes['url'] = esc_url_raw( $attributes['url'] );
91
92
	$player_data = Jetpack_Podcast_Helper::get_player_data( $attributes['url'] );
93
94
	if ( is_wp_error( $player_data ) ) {
95
		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...
96
	}
97
98
	return render_player( $player_data, $attributes );
99
}
100
101
/**
102
 * Renders the HTML for the Podcast player and tracklist.
103
 *
104
 * @param array $player_data The player data details.
105
 * @param array $attributes Array containing the Podcast Player block attributes.
106
 * @return string The HTML for the podcast player.
107
 */
108
function render_player( $player_data, $attributes ) {
109
	// If there are no tracks (it is possible) then display appropriate user facing error message.
110
	if ( empty( $player_data['tracks'] ) ) {
111
		return render_error( __( 'No tracks available to play.', 'jetpack' ) );
112
	}
113
114
	// Only use the amount of tracks requested.
115
	$player_data['tracks'] = array_slice(
116
		$player_data['tracks'],
117
		0,
118
		absint( $attributes['itemsToShow'] )
119
	);
120
121
	// Generate a unique id for the block instance.
122
	$instance_id             = wp_unique_id( 'jetpack-podcast-player-block-' . get_the_ID() . '-' );
123
	$player_data['playerId'] = $instance_id;
124
125
	// Generate object to be used as props for PodcastPlayer.
126
	$player_props = array_merge(
127
		// Add all attributes.
128
		array( 'attributes' => $attributes ),
129
		// Add all player data.
130
		$player_data
131
	);
132
133
	$primary_colors    = get_colors( 'primary', $attributes, 'color' );
134
	$secondary_colors  = get_colors( 'secondary', $attributes, 'color' );
135
	$background_colors = get_colors( 'background', $attributes, 'background-color' );
136
137
	$player_classes_name  = trim( "{$secondary_colors['class']} {$background_colors['class']}" );
138
	$player_inline_style  = trim( "{$secondary_colors['style']} ${background_colors['style']}" );
139
	$player_inline_style .= get_css_vars( $attributes );
140
141
	$block_classname = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes, array( 'is-default' ) );
142
	$is_amp          = ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() );
143
144
	ob_start();
145
	?>
146
	<div class="<?php echo esc_attr( $block_classname ); ?>" id="<?php echo esc_attr( $instance_id ); ?>">
147
		<section
148
			class="jetpack-podcast-player <?php echo esc_attr( $player_classes_name ); ?>"
149
			style="<?php echo esc_attr( $player_inline_style ); ?>"
150
		>
151
			<?php
152
			render(
153
				'podcast-header',
154
				array_merge(
155
					$player_props,
156
					array(
157
						'primary_colors' => $primary_colors,
158
						'player_id'      => $player_data['playerId'],
159
					)
160
				)
161
			);
162
			?>
163
			<?php if ( count( $player_data['tracks'] ) > 1 ) : ?>
164
			<ol class="jetpack-podcast-player__tracks">
165
				<?php foreach ( $player_data['tracks'] as $track_index => $attachment ) : ?>
166
					<?php
167
					render(
168
						'playlist-track',
169
						array(
170
							'is_active'        => 0 === $track_index,
171
							'attachment'       => $attachment,
172
							'primary_colors'   => $primary_colors,
173
							'secondary_colors' => $secondary_colors,
174
						)
175
					);
176
					?>
177
				<?php endforeach; ?>
178
			</ol>
179
			<?php endif; ?>
180
		</section>
181
		<?php if ( ! $is_amp ) : ?>
182
		<script type="application/json"><?php echo wp_json_encode( $player_props ); ?></script>
183
		<?php endif; ?>
184
	</div>
185
	<?php
186
	/**
187
	 * Enqueue necessary scripts and styles.
188
	 */
189
	if ( ! $is_amp ) {
190
		wp_enqueue_style( 'wp-mediaelement' );
191
	}
192
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'mediaelement' ) );
193
194
	return ob_get_clean();
195
}
196
197
/**
198
 * Given the color name, block attributes and the CSS property,
199
 * the function will return an array with the `class` and `style`
200
 * HTML attributes to be used straight in the markup.
201
 *
202
 * @example
203
 * $color = get_colors( 'secondary', $attributes, 'border-color'
204
 *  => array( 'class' => 'has-secondary', 'style' => 'border-color: #333' )
205
 *
206
 * @param string $name     Color attribute name, for instance `primary`, `secondary`, ...
207
 * @param array  $attrs    Block attributes.
208
 * @param string $property Color CSS property, fo instance `color`, `background-color`, ...
209
 * @return array           Colors array.
210
 */
211
function get_colors( $name, $attrs, $property ) {
212
	$attr_color  = "{$name}Color";
213
	$attr_custom = 'custom' . ucfirst( $attr_color );
214
215
	$color        = isset( $attrs[ $attr_color ] ) ? $attrs[ $attr_color ] : null;
216
	$custom_color = isset( $attrs[ $attr_custom ] ) ? $attrs[ $attr_custom ] : null;
217
218
	$colors = array(
219
		'class' => '',
220
		'style' => '',
221
	);
222
223
	if ( $color || $custom_color ) {
224
		$colors['class'] .= "has-{$name}";
225
226
		if ( $color ) {
227
			$colors['class'] .= " has-{$color}-{$property}";
228
		} elseif ( $custom_color ) {
229
			$colors['style'] .= "{$property}: {$custom_color};";
230
		}
231
	}
232
233
	return $colors;
234
}
235
236
/**
237
 * It generates a string with CSS variables according to the
238
 * block colors, prefixing each one with `--jetpack-podcast-player'.
239
 *
240
 * @param array $attrs Podcast Block attributes object.
241
 * @return string      CSS variables depending on block colors.
242
 */
243
function get_css_vars( $attrs ) {
244
	$colors_name = array( 'primary', 'secondary', 'background' );
245
246
	$inline_style = '';
247
	foreach ( $colors_name as $color ) {
248
		$hex_color = 'hex' . ucfirst( $color ) . 'Color';
249
		if ( ! empty( $attrs[ $hex_color ] ) ) {
250
			$inline_style .= " --jetpack-podcast-player-{$color}: {$attrs[ $hex_color ]};";
251
		}
252
	}
253
	return $inline_style;
254
}
255
256
/**
257
 * Render the given template in server-side.
258
 * Important note:
259
 *    The $template_props array will be extracted.
260
 *    This means it will create a var for each array item.
261
 *    Keep it mind when using this param to pass
262
 *    properties to the template.
263
 *
264
 * @param string $name           Template name, available in `./templates` folder.
265
 * @param array  $template_props Template properties. Optional.
266
 * @param bool   $print          Render template. True as default.
267
 * @return false|string          HTML markup or false.
268
 */
269
function render( $name, $template_props = array(), $print = true ) {
270
	if ( ! strpos( $name, '.php' ) ) {
271
		$name = $name . '.php';
272
	}
273
274
	$template_path = dirname( __FILE__ ) . '/templates/' . $name;
275
276
	if ( ! file_exists( $template_path ) ) {
277
		return '';
278
	}
279
280
	/*
281
	 * Optionally provided an assoc array of data to pass to template.
282
	 * IMPORTANT: It will be extracted into variables.
283
	 */
284
	if ( is_array( $template_props ) ) {
285
		/*
286
		 * It ignores the `discouraging` sniffer rule for extract, since it's needed
287
		 * to make the templating system works.
288
		 */
289
		extract( $template_props ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
290
	}
291
292
	if ( $print ) {
293
		include $template_path;
294
	} else {
295
		ob_start();
296
		include $template_path;
297
		$markup = ob_get_contents();
298
		ob_end_clean();
299
300
		return $markup;
301
	}
302
}
303