|
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>'; |
|
|
|
|
|
|
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
|
|
|
<?php render_podcast_header( $player_data, $attributes ); ?> |
|
125
|
|
|
|
|
126
|
|
|
<ol class="jetpack-podcast-player__episodes"> |
|
127
|
|
|
<?php foreach ( $player_data['tracks'] as $attachment ) : ?> |
|
128
|
|
|
<li class="jetpack-podcast-player__episode"> |
|
129
|
|
|
<a |
|
130
|
|
|
class="jetpack-podcast-player__episode-link" |
|
131
|
|
|
href="<?php echo esc_url( $attachment['link'] ); ?>" |
|
132
|
|
|
role="button" |
|
133
|
|
|
aria-pressed="false" |
|
134
|
|
|
> |
|
135
|
|
|
<span class="jetpack-podcast-player__episode-status-icon"></span> |
|
136
|
|
|
<span class="jetpack-podcast-player__episode-title"><?php echo esc_html( $attachment['title'] ); ?></span> |
|
137
|
|
|
<time class="jetpack-podcast-player__episode-duration"><?php echo ( ! empty( $attachment['duration'] ) ? esc_html( $attachment['duration'] ) : '' ); ?></time> |
|
138
|
|
|
</a> |
|
139
|
|
|
</li> |
|
140
|
|
|
<?php endforeach; ?> |
|
141
|
|
|
</ol> |
|
142
|
|
|
<?php if ( ! $is_amp ) : ?> |
|
143
|
|
|
<script type="application/json"><?php echo wp_json_encode( $player_props ); ?></script> |
|
144
|
|
|
<?php endif; ?> |
|
145
|
|
|
</div> |
|
146
|
|
|
<?php if ( ! $is_amp ) : ?> |
|
147
|
|
|
<script> |
|
148
|
|
|
( function( instanceId ) { |
|
149
|
|
|
document.getElementById( instanceId ).classList.remove( 'is-default' ); |
|
150
|
|
|
window.jetpackPodcastPlayers=(window.jetpackPodcastPlayers||[]); |
|
151
|
|
|
window.jetpackPodcastPlayers.push( instanceId ); |
|
152
|
|
|
} )( <?php echo wp_json_encode( $instance_id ); ?> ); |
|
153
|
|
|
</script> |
|
154
|
|
|
<?php endif; ?> |
|
155
|
|
|
<?php |
|
156
|
|
|
/** |
|
157
|
|
|
* Enqueue necessary scripts and styles. |
|
158
|
|
|
*/ |
|
159
|
|
|
if ( ! $is_amp ) { |
|
160
|
|
|
wp_enqueue_style( 'mediaelement' ); |
|
161
|
|
|
} |
|
162
|
|
|
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'mediaelement' ) ); |
|
163
|
|
|
|
|
164
|
|
|
return ob_get_clean(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Helper function to render the podcast title. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $title Podcast title. |
|
171
|
|
|
* @param string $link Podcast link. |
|
172
|
|
|
*/ |
|
173
|
|
|
function render_podcast_title( $title, $link ) { |
|
174
|
|
|
?> |
|
175
|
|
|
<span class="jetpack-podcast-player__title"> |
|
176
|
|
|
<?php if ( $link ) : ?> |
|
177
|
|
|
<a class="jetpack-podcast-player__title-link" href="<?php echo esc_url( $link ); ?>"> |
|
178
|
|
|
<?php echo esc_html( $title ); ?> |
|
179
|
|
|
</a> |
|
180
|
|
|
<?php |
|
181
|
|
|
else : |
|
182
|
|
|
echo esc_html( $title ); |
|
183
|
|
|
endif; |
|
184
|
|
|
?> |
|
185
|
|
|
</span> |
|
186
|
|
|
<?php |
|
187
|
|
|
}; |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Renders the podcast title. |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $player_id Podcast player instance ID. |
|
193
|
|
|
* @param string $title Podcast title. |
|
194
|
|
|
* @param string $link Podcast link. |
|
195
|
|
|
* @param array $track Track array. Usually it expects the first one. |
|
196
|
|
|
*/ |
|
197
|
|
|
function render_title( $player_id, $title, $link, $track ) { |
|
198
|
|
|
?> |
|
199
|
|
|
<h2 id="<?php echo esc_attr( "${player_id}__title" ); ?>" class="jetpack-podcast-player__titles"> |
|
200
|
|
|
<?php if ( ! empty( $track['title'] ) ) : ?> |
|
201
|
|
|
<span class="jetpack-podcast-player__track-title"> |
|
202
|
|
|
<?php echo esc_html( $track['title'] ); ?> |
|
203
|
|
|
</span> |
|
204
|
|
|
|
|
205
|
|
|
<?php if ( ! $title ) : ?> |
|
206
|
|
|
<span class="jetpack-podcast-player--visually-hidden"> &endash; </span> |
|
207
|
|
|
<?php endif; ?> |
|
208
|
|
|
<?php |
|
209
|
|
|
endif; |
|
210
|
|
|
|
|
211
|
|
|
if ( $title ) : |
|
212
|
|
|
render_podcast_title( $title, $link ); |
|
213
|
|
|
endif; |
|
214
|
|
|
?> |
|
215
|
|
|
</h2> |
|
216
|
|
|
<?php |
|
217
|
|
|
}; |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Renders the podcast header. |
|
221
|
|
|
* |
|
222
|
|
|
* @param array $data Player data. |
|
223
|
|
|
* @param array $attributes Block attributes. |
|
224
|
|
|
*/ |
|
225
|
|
|
function render_podcast_header( $data, $attributes ) { |
|
226
|
|
|
$show_cover_art = (bool) $attributes['showCoverArt']; |
|
227
|
|
|
$show_description = (bool) $attributes['showEpisodeDescription']; |
|
228
|
|
|
|
|
229
|
|
|
$player_id = $data['playerId']; |
|
230
|
|
|
$title = $data['title']; |
|
231
|
|
|
$link = $data['link']; |
|
232
|
|
|
$cover = $data['cover']; |
|
233
|
|
|
$track = ! empty( $data['tracks'][0] ) ? $data['tracks'][0] : array(); |
|
234
|
|
|
?> |
|
235
|
|
|
<div class="jetpack-podcast-player__header-wrapper"> |
|
236
|
|
|
<div class="jetpack-podcast-player__header" aria-live="polite"> |
|
237
|
|
|
<?php if ( $show_cover_art && $cover ) : ?> |
|
238
|
|
|
<div class="jetpack-podcast-player__track-image-wrapper"> |
|
239
|
|
|
<img class="jetpack-podcast-player__track-image" src="<?php echo esc_url( $cover ); ?>" alt=""/> |
|
240
|
|
|
</div> |
|
241
|
|
|
<?php endif; ?> |
|
242
|
|
|
|
|
243
|
|
|
<?php if ( $title && ! empty( $track['title'] ) ) : ?> |
|
244
|
|
|
<div class="jetpack-podcast-player__titles"> |
|
245
|
|
|
<?php render_title( $player_id, $title, $link, $track ); ?> |
|
246
|
|
|
</div> |
|
247
|
|
|
<?php endif; ?> |
|
248
|
|
|
</div> |
|
249
|
|
|
|
|
250
|
|
|
<?php if ( $show_description && ! empty( $track['description'] ) ) : ?> |
|
251
|
|
|
<div |
|
252
|
|
|
id="<?php echo esc_attr( "${player_id}__track-description" ); ?>" |
|
253
|
|
|
class="jetpack-podcast-player__track-description" |
|
254
|
|
|
> |
|
255
|
|
|
<?php echo esc_html( $track['description'] ); ?> |
|
256
|
|
|
</div> |
|
257
|
|
|
<?php endif; ?> |
|
258
|
|
|
</div> |
|
259
|
|
|
<?php |
|
260
|
|
|
} |
|
261
|
|
|
|
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.