1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Helper to massage Podcast data to be used in the Podcast block. |
4
|
|
|
* |
5
|
|
|
* @package jetpack |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Jetpack_Podcast_Helper |
10
|
|
|
*/ |
11
|
|
|
class Jetpack_Podcast_Helper { |
12
|
|
|
/** |
13
|
|
|
* Gets a list of tracks for the supplied RSS feed. This function is used |
14
|
|
|
* in both server-side block rendering and in API `WPCOM_REST_API_V2_Endpoint_Podcast_Player`. |
15
|
|
|
* |
16
|
|
|
* @param string $feed The RSS feed to load and list tracks for. |
17
|
|
|
* @param int $quantity Optional. The number of tracks to return. |
18
|
|
|
* @return array|WP_Error The feed's tracks or a error object. |
19
|
|
|
*/ |
20
|
|
|
public static function get_track_list( $feed, $quantity = 10 ) { |
21
|
|
|
$rss = fetch_feed( $feed ); |
22
|
|
|
|
23
|
|
|
if ( is_wp_error( $rss ) ) { |
24
|
|
|
return new WP_Error( 'invalid_url', __( 'Your podcast couldn\'t be embedded. Please double check your URL.', 'jetpack' ) ); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if ( ! $rss->get_item_quantity() ) { |
28
|
|
|
return new WP_Error( 'no_tracks', __( 'Podcast audio RSS feed has no tracks.', 'jetpack' ) ); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$track_list = array_map( array( __CLASS__, 'setup_tracks_callback' ), $rss->get_items( 0, $quantity ) ); |
32
|
|
|
|
33
|
|
|
// Remove empty tracks. |
34
|
|
|
return array_filter( $track_list ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Prepares Episode data to be used with MediaElement.js. |
39
|
|
|
* |
40
|
|
|
* @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode. |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
private static function setup_tracks_callback( SimplePie_Item $episode ) { |
44
|
|
|
$enclosure = self::get_audio_enclosure( $episode ); |
45
|
|
|
|
46
|
|
|
// If there is no link return an empty array. We will filter out later. |
47
|
|
|
if ( empty( $enclosure->link ) ) { |
48
|
|
|
return array(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Build track data. |
52
|
|
|
$track = array( |
53
|
|
|
'id' => wp_unique_id( 'podcast-track-' ), |
54
|
|
|
'link' => esc_url( $episode->get_link() ), |
55
|
|
|
'src' => esc_url( $enclosure->link ), |
56
|
|
|
'type' => esc_attr( $enclosure->type ), |
57
|
|
|
'description' => wp_kses_post( $episode->get_description() ), |
58
|
|
|
'title' => esc_html( trim( wp_strip_all_tags( $episode->get_title() ) ) ), |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
if ( empty( $track['title'] ) ) { |
62
|
|
|
$track['title'] = esc_html__( '(no title)', 'jetpack' ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ( ! empty( $enclosure->duration ) ) { |
66
|
|
|
$track['duration'] = self::format_track_duration( $enclosure->duration ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $track; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Retrieves an audio enclosure. |
74
|
|
|
* |
75
|
|
|
* @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode. |
76
|
|
|
* @return SimplePie_Enclosure|null |
77
|
|
|
*/ |
78
|
|
|
private static function get_audio_enclosure( SimplePie_Item $episode ) { |
79
|
|
|
foreach ( (array) $episode->get_enclosures() as $enclosure ) { |
80
|
|
|
if ( 0 === strpos( $enclosure->type, 'audio/' ) ) { |
81
|
|
|
return $enclosure; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Default to empty SimplePie_Enclosure object. |
86
|
|
|
return $episode->get_enclosure(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the track duration as a formatted string. |
91
|
|
|
* |
92
|
|
|
* @param number $duration of the track in seconds. |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
private static function format_track_duration( $duration ) { |
96
|
|
|
$format = $duration > HOUR_IN_SECONDS ? 'H:i:s' : 'i:s'; |
97
|
|
|
|
98
|
|
|
return date_i18n( $format, $duration ); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.