1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Podcast Player Block. |
4
|
|
|
* |
5
|
|
|
* @since 8.x |
6
|
|
|
* |
7
|
|
|
* @package Jetpack |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jetpack\Podcast_Player_Block; |
11
|
|
|
|
12
|
|
|
use WP_Error; |
13
|
|
|
use Jetpack_Gutenberg; |
14
|
|
|
|
15
|
|
|
const FEATURE_NAME = 'podcast-player'; |
16
|
|
|
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Registers the block for use in Gutenberg |
20
|
|
|
* This is done via an action so that we can disable |
21
|
|
|
* registration if we need to. |
22
|
|
|
*/ |
23
|
|
|
function register_block() { |
24
|
|
|
jetpack_register_block( |
25
|
|
|
BLOCK_NAME, |
26
|
|
|
array( |
27
|
|
|
'attributes' => array( |
28
|
|
|
'url' => array( |
29
|
|
|
'type' => 'url', |
30
|
|
|
), |
31
|
|
|
'itemsToShow' => array( |
32
|
|
|
'type' => 'integer', |
33
|
|
|
'default' => 5, |
34
|
|
|
), |
35
|
|
|
), |
36
|
|
|
'render_callback' => __NAMESPACE__ . '\render_block', |
37
|
|
|
) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
add_action( 'init', __NAMESPACE__ . '\register_block' ); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Podcast Player block registration/dependency declaration. |
44
|
|
|
* |
45
|
|
|
* @param array $attributes Array containing the Podcast Player block attributes. |
46
|
|
|
* @param string $content String containing the Podcast Player block content. |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
function render_block( $attributes, $content ) { |
51
|
|
|
|
52
|
|
|
// Test for empty URLS. |
53
|
|
|
if ( empty( $attributes['url'] ) ) { |
54
|
|
|
return '<p>' . esc_html__( 'No Podcast URL provided. Please enter a valid Podcast RSS feed URL.', 'jetpack' ) . '</p>'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Test for invalid URLs. |
58
|
|
|
if ( ! wp_http_validate_url( $attributes['url'] ) ) { |
59
|
|
|
return '<p>' . esc_html__( 'Your podcast URL is invalid and couldn\'t be embedded. Please double check your URL.', 'jetpack' ) . '</p>'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Sanitize the URL. |
63
|
|
|
$attributes['url'] = esc_url_raw( $attributes['url'] ); |
64
|
|
|
|
65
|
|
|
$track_list = get_track_list( $attributes['url'], $attributes['itemsToShow'] ); |
66
|
|
|
|
67
|
|
|
if ( is_wp_error( $track_list ) ) { |
68
|
|
|
return '<p>' . esc_html( $track_list->get_error_message() ) . '</p>'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return render_player( $track_list, $attributes ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Renders the HTML for the Podcast player and tracklist. |
76
|
|
|
* |
77
|
|
|
* @param array $track_list the list of podcast tracks. |
78
|
|
|
* @param array $attributes Array containing the Podcast Player block attributes. |
79
|
|
|
* @return string the HTML for the podcast player. |
80
|
|
|
*/ |
81
|
|
|
function render_player( $track_list, $attributes ) { |
82
|
|
|
|
83
|
|
|
$player_data = array( |
84
|
|
|
'type' => 'audio', |
85
|
|
|
// Don't pass strings to JSON, will be truthy in JS. |
86
|
|
|
'tracklist' => true, |
87
|
|
|
'tracknumbers' => true, |
88
|
|
|
'images' => true, |
89
|
|
|
'artists' => true, |
90
|
|
|
'tracks' => $track_list, |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$block_classname = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes ); |
94
|
|
|
|
95
|
|
|
// If there are no tracks (it is possible) then display appropriate user facing error message. |
96
|
|
|
if ( empty( $track_list ) ) { |
97
|
|
|
return '<p>' . esc_html__( 'No tracks available to play.', 'jetpack' ) . '</p>'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
ob_start(); |
101
|
|
|
|
102
|
|
|
?> |
103
|
|
|
<div class="<?php echo esc_attr( $block_classname ); ?>"> |
104
|
|
|
<?php // Placeholder: block markup is being handled in https://github.com/Automattic/jetpack/pull/14952. ?> |
105
|
|
|
<script type="application/json" class="wp-playlist-script"><?php echo wp_json_encode( $player_data ); ?></script> |
106
|
|
|
</div> |
107
|
|
|
<?php |
108
|
|
|
/* |
109
|
|
|
* Enqueue necessary scripts and styles. |
110
|
|
|
*/ |
111
|
|
|
Jetpack_Gutenberg::load_assets_as_required( 'podcast-player' ); |
112
|
|
|
|
113
|
|
|
return ob_get_clean(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Gets a list of tracks for the supplied RSS feed. |
118
|
|
|
* |
119
|
|
|
* @param string $feed the RSS feed to load and list tracks for. |
120
|
|
|
* @param int $quantity the number of tracks to return. |
121
|
|
|
* @return array|WP_Error the feed's tracks or a error object. |
122
|
|
|
*/ |
123
|
|
|
function get_track_list( $feed, $quantity = 5 ) { |
124
|
|
|
if ( empty( $feed ) ) { |
125
|
|
|
return new WP_Error( 'missing_feed', __( 'Podcast audio RSS feed missing.', 'jetpack' ) ); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$rss = fetch_feed( $feed ); |
129
|
|
|
|
130
|
|
|
if ( is_wp_error( $rss ) ) { |
131
|
|
|
return new WP_Error( 'invalid_url', __( 'Your podcast couldn\'t be embedded. Please double check your URL.', 'jetpack' ) ); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ( ! $rss->get_item_quantity() ) { |
135
|
|
|
return new WP_Error( 'no_tracks', __( 'Podcast audio RSS feed has no tracks.', 'jetpack' ) ); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$episodes = $rss->get_items( 0, $quantity ); |
139
|
|
|
|
140
|
|
|
$track_list = array_map( |
141
|
|
|
function( $episode ) { |
142
|
|
|
|
143
|
|
|
$url = ! empty( $episode->data['child']['']['enclosure'][0]['attribs']['']['url'] ) ? $episode->data['child']['']['enclosure'][0]['attribs']['']['url'] : null; |
144
|
|
|
$type = ! empty( $episode->data['child']['']['enclosure'][0]['attribs']['']['type'] ) ? $episode->data['child']['']['enclosure'][0]['attribs']['']['type'] : null; |
145
|
|
|
|
146
|
|
|
// If there is no type return an empty array as the array entry. We will filter out later. |
147
|
|
|
if ( ! $url ) { |
148
|
|
|
return array(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// Build track data. |
152
|
|
|
$track = array( |
153
|
|
|
'link' => esc_url( $episode->get_link() ), |
154
|
|
|
'src' => $url, |
155
|
|
|
'type' => $type, |
156
|
|
|
'caption' => '', |
157
|
|
|
'description' => wp_kses_post( $episode->get_description() ), |
158
|
|
|
'meta' => array(), |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
$track['title'] = esc_html( trim( wp_strip_all_tags( $episode->get_title() ) ) ); |
162
|
|
|
|
163
|
|
|
if ( empty( $track['title'] ) ) { |
164
|
|
|
$track['title'] = esc_html__( '(no title)', 'jetpack' ); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $track; |
168
|
|
|
}, |
169
|
|
|
$episodes |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
// Remove empty tracks. |
173
|
|
|
return \array_filter( $track_list ); |
174
|
|
|
} |
175
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.