Completed
Push — update/podcast-utilities ( 6d8e87 )
by Jeremy
06:56
created

podcast-player.php ➔ setup_tracks_callback()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 1
dl 0
loc 28
rs 9.472
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 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
	$track_list = Jetpack_Podcast_Helper::get_track_list( $attributes['url'], absint( $attributes['itemsToShow'] ) );
77
78
	if ( is_wp_error( $track_list ) ) {
79
		return '<p>' . esc_html( $track_list->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( $track_list, $attributes );
83
}
84
85
/**
86
 * Renders the HTML for the Podcast player and tracklist.
87
 *
88
 * @param array $track_list The list of podcast tracks.
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( $track_list, $attributes ) {
93
	// If there are no tracks (it is possible) then display appropriate user facing error message.
94
	if ( empty( $track_list ) ) {
95
		return '<p>' . esc_html__( 'No tracks available to play.', 'jetpack' ) . '</p>';
96
	}
97
	$instance_id = wp_unique_id( 'podcast-player-block-' );
98
99
	// Generate object to be used as props for PodcastPlayer.
100
	$player_data = array_merge(
101
		// Make all attributes available.
102
		$attributes,
103
		// And add some computed properties.
104
		array(
105
			'tracks'   => $track_list,
106
			'coverArt' => '',
107
		)
108
	);
109
110
	$block_classname = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes );
111
112
	ob_start();
113
	?>
114
	<div class="<?php echo esc_attr( $block_classname ); ?>" id="<?php echo esc_attr( $instance_id ); ?>">
115
		<noscript>
116
			<ol class="podcast-player__episodes">
117
				<?php foreach ( $track_list as $attachment ) : ?>
118
				<li class="podcast-player__episode">
119
					<a
120
						class="podcast-player__episode-link"
121
						href="<?php echo esc_url( $attachment['link'] ); ?>"
122
						role="button"
123
						aria-pressed="false"
124
					>
125
						<span class="podcast-player__episode-status-icon"></span>
126
						<span class="podcast-player__episode-title"><?php echo esc_html( $attachment['title'] ); ?></span>
127
						<time class="podcast-player__episode-duration"><?php echo ( ! empty( $attachment['duration'] ) ? esc_html( $attachment['duration'] ) : '' ); ?></time>
128
					</a>
129
				</li>
130
				<?php endforeach; ?>
131
			</ol>
132
		</noscript>
133
		<script type="application/json"><?php echo wp_json_encode( $player_data ); ?></script>
134
	</div>
135
	<script>window.jetpackPodcastPlayers=(window.jetpackPodcastPlayers||[]);window.jetpackPodcastPlayers.push( <?php echo wp_json_encode( $instance_id ); ?> );</script>
136
	<?php
137
	/**
138
	 * Enqueue necessary scripts and styles.
139
	 */
140
	wp_enqueue_style( 'mediaelement' );
141
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'mediaelement' ) );
142
143
	return ob_get_clean();
144
}
145