Completed
Push — fix/jp_api_constants_in_connec... ( de839c...a94bf0 )
by
unknown
07:20
created

podcast-player.php ➔ render_player()   B

Complexity

Conditions 8
Paths 33

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 33
nop 2
dl 0
loc 71
rs 7.3882
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
26
 * This is done via an action so that we can disable
27
 * registration if we need to.
28
 */
29
function register_block() {
30
	jetpack_register_block(
31
		BLOCK_NAME,
32
		array(
33
			'attributes'      => array(
34
				'url'                    => array(
35
					'type' => 'url',
36
				),
37
				'itemsToShow'            => array(
38
					'type'    => 'integer',
39
					'default' => 5,
40
				),
41
				'showCoverArt'           => array(
42
					'type'    => 'boolean',
43
					'default' => true,
44
				),
45
				'showEpisodeDescription' => array(
46
					'type'    => 'boolean',
47
					'default' => true,
48
				),
49
			),
50
			'render_callback' => __NAMESPACE__ . '\render_block',
51
		)
52
	);
53
}
54
add_action( 'init', __NAMESPACE__ . '\register_block' );
55
56
/**
57
 * Podcast Player block registration/dependency declaration.
58
 *
59
 * @param array $attributes Array containing the Podcast Player block attributes.
60
 * @return string
61
 */
62
function render_block( $attributes ) {
63
64
	// Test for empty URLS.
65
	if ( empty( $attributes['url'] ) ) {
66
		return '<p>' . esc_html__( 'No Podcast URL provided. Please enter a valid Podcast RSS feed URL.', 'jetpack' ) . '</p>';
67
	}
68
69
	// Test for invalid URLs.
70
	if ( ! wp_http_validate_url( $attributes['url'] ) ) {
71
		return '<p>' . esc_html__( 'Your podcast URL is invalid and couldn\'t be embedded. Please double check your URL.', 'jetpack' ) . '</p>';
72
	}
73
74
	// Sanitize the URL.
75
	$attributes['url'] = esc_url_raw( $attributes['url'] );
76
77
	$player_data = Jetpack_Podcast_Helper::get_player_data( $attributes['url'] );
78
79
	if ( is_wp_error( $player_data ) ) {
80
		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...
81
	}
82
83
	return render_player( $player_data, $attributes );
84
}
85
86
/**
87
 * Renders the HTML for the Podcast player and tracklist.
88
 *
89
 * @param array $player_data The player data details.
90
 * @param array $attributes Array containing the Podcast Player block attributes.
91
 * @return string The HTML for the podcast player.
92
 */
93
function render_player( $player_data, $attributes ) {
94
	// If there are no tracks (it is possible) then display appropriate user facing error message.
95
	if ( empty( $player_data['tracks'] ) ) {
96
		return '<p>' . esc_html__( 'No tracks available to play.', 'jetpack' ) . '</p>';
97
	}
98
99
	// Only use the amount of tracks requested.
100
	$player_data['tracks'] = array_slice(
101
		$player_data['tracks'],
102
		0,
103
		absint( $attributes['itemsToShow'] )
104
	);
105
106
	// Genereate a unique id for the block instance.
107
	$instance_id             = wp_unique_id( 'jetpack-podcast-player-block-' );
108
	$player_data['playerId'] = $instance_id;
109
110
	// Generate object to be used as props for PodcastPlayer.
111
	$player_props = array_merge(
112
		// Add all attributes.
113
		array( 'attributes' => $attributes ),
114
		// Add all player data.
115
		$player_data
116
	);
117
118
	$block_classname = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes, array( 'is-default' ) );
119
	$is_amp          = ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() );
120
121
	ob_start();
122
	?>
123
	<div class="<?php echo esc_attr( $block_classname ); ?>" id="<?php echo esc_attr( $instance_id ); ?>">
124
		<ol class="jetpack-podcast-player__episodes">
125
			<?php foreach ( $player_data['tracks'] as $attachment ) : ?>
126
			<li class="jetpack-podcast-player__episode">
127
				<a
128
					class="jetpack-podcast-player__episode-link"
129
					href="<?php echo esc_url( $attachment['link'] ); ?>"
130
					role="button"
131
					aria-pressed="false"
132
				>
133
					<span class="jetpack-podcast-player__episode-status-icon"></span>
134
					<span class="jetpack-podcast-player__episode-title"><?php echo esc_html( $attachment['title'] ); ?></span>
135
					<time class="jetpack-podcast-player__episode-duration"><?php echo ( ! empty( $attachment['duration'] ) ? esc_html( $attachment['duration'] ) : '' ); ?></time>
136
				</a>
137
			</li>
138
			<?php endforeach; ?>
139
		</ol>
140
		<?php if ( ! $is_amp ) : ?>
141
		<script type="application/json"><?php echo wp_json_encode( $player_props ); ?></script>
142
		<?php endif; ?>
143
	</div>
144
	<?php if ( ! $is_amp ) : ?>
145
	<script>
146
		( function( instanceId ) {
147
			document.getElementById( instanceId ).classList.remove( 'is-default' );
148
			window.jetpackPodcastPlayers=(window.jetpackPodcastPlayers||[]);
149
			window.jetpackPodcastPlayers.push( instanceId );
150
		} )( <?php echo wp_json_encode( $instance_id ); ?> );
151
	</script>
152
	<?php endif; ?>
153
	<?php
154
	/**
155
	 * Enqueue necessary scripts and styles.
156
	 */
157
	if ( ! $is_amp ) {
158
		wp_enqueue_style( 'mediaelement' );
159
	}
160
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'mediaelement' ) );
161
162
	return ob_get_clean();
163
}
164