Completed
Push — add/sync-health-card ( 00dc7c...8a09d2 )
by
unknown
06:24
created

podcast-player.php ➔ setup_tracks_callback()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 26
rs 9.504
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 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
 * @return string
47
 */
48
function render_block( $attributes ) {
49
50
	// Test for empty URLS.
51
	if ( empty( $attributes['url'] ) ) {
52
		return '<p>' . esc_html__( 'No Podcast URL provided. Please enter a valid Podcast RSS feed URL.', 'jetpack' ) . '</p>';
53
	}
54
55
	// Test for invalid URLs.
56
	if ( ! wp_http_validate_url( $attributes['url'] ) ) {
57
		return '<p>' . esc_html__( 'Your podcast URL is invalid and couldn\'t be embedded. Please double check your URL.', 'jetpack' ) . '</p>';
58
	}
59
60
	// Sanitize the URL.
61
	$attributes['url'] = esc_url_raw( $attributes['url'] );
62
63
	$track_list = get_track_list( $attributes['url'], 10 );
64
65
	if ( is_wp_error( $track_list ) ) {
66
		return '<p>' . esc_html( $track_list->get_error_message() ) . '</p>';
67
	}
68
69
	return render_player( $track_list, $attributes );
70
}
71
72
/**
73
 * Renders the HTML for the Podcast player and tracklist.
74
 *
75
 * @param array $track_list the list of podcast tracks.
76
 * @param array $attributes Array containing the Podcast Player block attributes.
77
 * @return string the HTML for the podcast player.
78
 */
79
function render_player( $track_list, $attributes ) {
80
	// If there are no tracks (it is possible) then display appropriate user facing error message.
81
	if ( empty( $track_list ) ) {
82
		return '<p>' . esc_html__( 'No tracks available to play.', 'jetpack' ) . '</p>';
83
	}
84
85
	$player_data = array(
86
		'type'         => 'audio',
87
		// Don't pass strings to JSON, will be truthy in JS.
88
		'tracklist'    => true,
89
		'tracknumbers' => true,
90
		'images'       => true,
91
		'artists'      => true,
92
		'tracks'       => $track_list,
93
	);
94
95
	$block_classname = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes );
96
97
	ob_start();
98
99
	?>
100
	<div class="<?php echo esc_attr( $block_classname ); ?>">
101
		<?php // Placeholder: block markup is being handled in https://github.com/Automattic/jetpack/pull/14952. ?>
102
		<script type="application/json" class="wp-playlist-script"><?php echo wp_json_encode( $player_data ); ?></script>
103
	</div>
104
	<?php
105
	/*
106
	* Enqueue necessary scripts and styles.
107
	*/
108
	Jetpack_Gutenberg::load_assets_as_required( 'podcast-player' );
109
110
	return ob_get_clean();
111
}
112
113
/**
114
 * Gets a list of tracks for the supplied RSS feed.
115
 *
116
 * @param string $feed the RSS feed to load and list tracks for.
117
 * @param int    $quantity the number of tracks to return.
118
 * @return array|WP_Error the feed's tracks or a error object.
119
 */
120
function get_track_list( $feed, $quantity = 10 ) {
121
	$rss = fetch_feed( $feed );
122
123
	if ( is_wp_error( $rss ) ) {
124
		return new WP_Error( 'invalid_url', __( 'Your podcast couldn\'t be embedded. Please double check your URL.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_url'.

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.

Loading history...
125
	}
126
127
	if ( ! $rss->get_item_quantity() ) {
128
		return new WP_Error( 'no_tracks', __( 'Podcast audio RSS feed has no tracks.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'no_tracks'.

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.

Loading history...
129
	}
130
131
	$track_list = array_map( __NAMESPACE__ . '\setup_tracks_callback', $rss->get_items( 0, $quantity ) );
132
133
	// Remove empty tracks.
134
	return \array_filter( $track_list );
135
}
136
137
/**
138
 * Prepares Episode data to be used with MediaElement.js.
139
 *
140
 * @param \SimplePie_Item $episode SimplePie_Item object, representing a podcast episode.
141
 * @return array
142
 */
143
function setup_tracks_callback( \SimplePie_Item $episode ) {
144
	$enclosure = $episode->get_enclosure();
145
146
	// If there is no link return an empty array. We will filter out later.
147
	if ( ! $enclosure->link ) {
148
		return array();
149
	}
150
151
	// Build track data.
152
	$track = array(
153
		'link'        => esc_url( $episode->get_link() ),
154
		'src'         => esc_url( $enclosure->link ),
155
		'type'        => $enclosure->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