Completed
Push — fix/14559-block-style ( 31537f...285323 )
by Gary
06:50
created

blocks/podcast-player/podcast-player.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
108
	// Generate object to be used as props for PodcastPlayer.
109
	$player_props = array_merge(
110
		// Make all attributes available.
111
		$attributes,
112
		// Add all player data.
113
		$player_data
114
	);
115
116
	$block_classname = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes );
117
118
	ob_start();
119
	?>
120
	<div class="<?php echo esc_attr( $block_classname ); ?>" id="<?php echo esc_attr( $instance_id ); ?>">
121
		<noscript>
122
			<ol class="jetpack-podcast-player__episodes">
123
				<?php foreach ( $player_data['tracks'] as $attachment ) : ?>
124
				<li class="jetpack-podcast-player__episode">
125
					<a
126
						class="jetpack-podcast-player__episode-link"
127
						href="<?php echo esc_url( $attachment['link'] ); ?>"
128
						role="button"
129
						aria-pressed="false"
130
					>
131
						<span class="jetpack-podcast-player__episode-status-icon"></span>
132
						<span class="jetpack-podcast-player__episode-title"><?php echo esc_html( $attachment['title'] ); ?></span>
133
						<time class="jetpack-podcast-player__episode-duration"><?php echo ( ! empty( $attachment['duration'] ) ? esc_html( $attachment['duration'] ) : '' ); ?></time>
134
					</a>
135
				</li>
136
				<?php endforeach; ?>
137
			</ol>
138
		</noscript>
139
		<script type="application/json"><?php echo wp_json_encode( $player_props ); ?></script>
140
	</div>
141
	<script>window.jetpackPodcastPlayers=(window.jetpackPodcastPlayers||[]);window.jetpackPodcastPlayers.push( <?php echo wp_json_encode( $instance_id ); ?> );</script>
142
	<?php
143
	/**
144
	 * Enqueue necessary scripts and styles.
145
	 */
146
	wp_enqueue_style( 'mediaelement' );
147
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'mediaelement' ) );
148
149
	return ob_get_clean();
150
}
151