Completed
Push — updates/infinity-vanilla-js ( fda899...9466ff )
by
unknown
07:55
created

podcast-player.php ➔ render_player()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 60
rs 8.8727
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
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
25
 * This is done via an action so that we can disable
26
 * 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
 * Podcast Player block registration/dependency declaration.
57
 *
58
 * @param array $attributes Array containing the Podcast Player block attributes.
59
 * @return string
60
 */
61
function render_block( $attributes ) {
62
63
	// Test for empty URLS.
64
	if ( empty( $attributes['url'] ) ) {
65
		return '<p>' . esc_html__( 'No Podcast URL provided. Please enter a valid Podcast RSS feed URL.', 'jetpack' ) . '</p>';
66
	}
67
68
	// Test for invalid URLs.
69
	if ( ! wp_http_validate_url( $attributes['url'] ) ) {
70
		return '<p>' . esc_html__( 'Your podcast URL is invalid and couldn\'t be embedded. Please double check your URL.', 'jetpack' ) . '</p>';
71
	}
72
73
	// Sanitize the URL.
74
	$attributes['url'] = esc_url_raw( $attributes['url'] );
75
76
	$player_data = Jetpack_Podcast_Helper::get_player_data( $attributes['url'] );
77
78
	if ( is_wp_error( $player_data ) ) {
79
		return '<p>' . esc_html( $player_data->get_error_message() ) . '</p>';
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...
80
	}
81
82
	return render_player( $player_data, $attributes );
83
}
84
85
/**
86
 * Renders the HTML for the Podcast player and tracklist.
87
 *
88
 * @param array $player_data The player data details.
89
 * @param array $attributes Array containing the Podcast Player block attributes.
90
 * @return string The HTML for the podcast player.
91
 */
92
function render_player( $player_data, $attributes ) {
93
	// If there are no tracks (it is possible) then display appropriate user facing error message.
94
	if ( empty( $player_data['tracks'] ) ) {
95
		return '<p>' . esc_html__( 'No tracks available to play.', 'jetpack' ) . '</p>';
96
	}
97
98
	// Only use the amount of tracks requested.
99
	$player_data['tracks'] = array_slice(
100
		$player_data['tracks'],
101
		0,
102
		absint( $attributes['itemsToShow'] )
103
	);
104
105
	// Genereate a unique id for the block instance.
106
	$instance_id             = wp_unique_id( 'jetpack-podcast-player-block-' );
107
	$player_data['playerId'] = $instance_id;
108
109
	// Generate object to be used as props for PodcastPlayer.
110
	$player_props = array_merge(
111
		// Make all attributes available.
112
		$attributes,
113
		// Add all player data.
114
		$player_data
115
	);
116
117
	$block_classname = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes );
118
119
	ob_start();
120
	?>
121
	<div class="<?php echo esc_attr( $block_classname ); ?>" id="<?php echo esc_attr( $instance_id ); ?>">
122
		<noscript>
123
			<ol class="jetpack-podcast-player__episodes">
124
				<?php foreach ( $player_data['tracks'] as $attachment ) : ?>
125
				<li class="jetpack-podcast-player__episode">
126
					<a
127
						class="jetpack-podcast-player__episode-link"
128
						href="<?php echo esc_url( $attachment['link'] ); ?>"
129
						role="button"
130
						aria-pressed="false"
131
					>
132
						<span class="jetpack-podcast-player__episode-status-icon"></span>
133
						<span class="jetpack-podcast-player__episode-title"><?php echo esc_html( $attachment['title'] ); ?></span>
134
						<time class="jetpack-podcast-player__episode-duration"><?php echo ( ! empty( $attachment['duration'] ) ? esc_html( $attachment['duration'] ) : '' ); ?></time>
135
					</a>
136
				</li>
137
				<?php endforeach; ?>
138
			</ol>
139
		</noscript>
140
		<script type="application/json"><?php echo wp_json_encode( $player_props ); ?></script>
141
	</div>
142
	<script>window.jetpackPodcastPlayers=(window.jetpackPodcastPlayers||[]);window.jetpackPodcastPlayers.push( <?php echo wp_json_encode( $instance_id ); ?> );</script>
143
	<?php
144
	/**
145
	 * Enqueue necessary scripts and styles.
146
	 */
147
	wp_enqueue_style( 'mediaelement' );
148
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'mediaelement' ) );
149
150
	return ob_get_clean();
151
}
152