Completed
Push — add/podcast-episodes-block ( 66128b...6a22bf )
by
unknown
06:48
created

podcast-episodes.php ➔ load_assets()   C

Complexity

Conditions 9
Paths 38

Size

Total Lines 93

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 38
nop 2
dl 0
loc 93
rs 6.5971
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Podcast Episodes Block.
4
 *
5
 * @since 8.x
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Jetpack\Podcast_Episodes_Block;
11
12
const FEATURE_NAME = 'podcast-episodes';
13
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
14
15
/**
16
 * Registers the block for use in Gutenberg
17
 * This is done via an action so that we can disable
18
 * registration if we need to.
19
 */
20
function register_block() {
21
	jetpack_register_block(
22
		BLOCK_NAME,
23
		array(
24
			'attributes'      => array(
25
				'url'         => array(
26
					'type' => 'url',
27
				),
28
				'itemsToShow' => array(
29
					'type'    => 'integer',
30
					'default' => 5,
31
				),
32
			),
33
			'render_callback' => __NAMESPACE__ . '\load_assets',
34
		)
35
	);
36
}
37
add_action( 'init', __NAMESPACE__ . '\register_block' );
38
39
/**
40
 * Podcast Episodes block registration/dependency declaration.
41
 *
42
 * @param array  $attributes Array containing the Podcast Episodes block attributes.
43
 * @param string $content String containing the Podcast Episodes block content.
44
 *
45
 * @return string
46
 */
47
function load_assets( $attributes, $content ) {
48
	$rss = fetch_feed( 'https://anchor.fm/s/9400d7c/podcast/rss' );
49
50
	if ( is_wp_error( $rss ) ) {
51
		return '<div class="components-placeholder"><div class="notice notice-error"><strong>' . __( 'RSS Error:', 'jetpack' ) . '</strong> ' . $rss->get_error_message() . '</div></div>';
52
	}
53
54
	if ( ! $rss->get_item_quantity() ) {
55
		return '<div class="components-placeholder"><div class="notice notice-error">' . __( 'An error has occurred, which probably means the feed is down. Try again later.', 'jetpack' ) . '</div></div>';
56
	}
57
58
	$episodes   = $rss->get_items( 0, $attributes['itemsToShow'] );
59
	$list_items = array();
60
61
	foreach ( $episodes as $episode ) {
62
		$list_item = array(
63
			'src'         => esc_url( $episode->data['child']['']['enclosure'][0]['attribs']['']['url'] ),
64
			'type'        => esc_attr( $episode->data['child']['']['enclosure'][0]['attribs']['']['type'] ),
65
			'caption'     => '',
66
			'description' => wp_kses_post( $episode->get_description() ),
67
			'meta'        => array(),
68
		);
69
70
		if ( ! empty( $episode->data['child']['http://www.itunes.com/dtds/podcast-1.0.dtd']['image'][0]['attribs']['']['href'] ) ) {
71
			$list_item['image']['src'] = esc_url( $episode->data['child']['http://www.itunes.com/dtds/podcast-1.0.dtd']['image'][0]['attribs']['']['href'] );
72
			$list_item['thumb']['src'] = $list_item['image']['src'];
73
		}
74
75
		if ( ! empty( $episode->data['child']['']['enclosure'][0]['attribs']['']['length'] ) ) {
76
			$list_item['meta']['length_formatted'] = date_i18n( 'H:i:s', $episode->data['child']['http://www.itunes.com/dtds/podcast-1.0.dtd']['duration'][0]['data'] );
77
		}
78
79
		$list_item['title'] = esc_html( trim( wp_strip_all_tags( $episode->get_title() ) ) );
80
		if ( empty( $list_item['title'] ) ) {
81
			$list_item['title'] = __( '(no title)', 'jetpack' );
82
		}
83
84
		$list_items[] = $list_item;
85
	}
86
87
	global $content_width;
88
89
	$data = array(
90
		'type'         => 'audio',
91
		// Don't pass strings to JSON, will be truthy in JS.
92
		'tracklist'    => true,
93
		'tracknumbers' => true,
94
		'images'       => true,
95
		'artists'      => true,
96
		'tracks'       => $list_items,
97
	);
98
99
	$outer         = 22; // Default padding and border of wrapper.
100
	$default_width = 640;
101
	$theme_width   = empty( $content_width ) ? $default_width : ( $content_width - $outer );
102
103
	ob_start();
104
	wp_playlist_scripts( 'audio' );
105
	/**
106
	 * Prints and enqueues playlist scripts, styles, and JavaScript templates.
107
	 *
108
	 * @since 3.9.0
109
	 *
110
	 * @param string $type  Type of playlist. Possible values are 'audio' or 'video'.
111
	 * @param string $style The 'theme' for the playlist. Core provides 'light' and 'dark'.
112
	 */
113
	do_action( 'wp_playlist_scripts', 'audio', 'light' );
114
115
	?>
116
	<div class="wp-playlist wp-audio-playlist wp-playlist-light">
117
		<div class="wp-playlist-current-item"></div>
118
		<audio controls="controls" preload="none" width="<?php echo (int) $theme_width; ?>"></audio>
119
		<div class="wp-playlist-next"></div>
120
		<div class="wp-playlist-prev"></div>
121
		<noscript>
122
			<ol>
123
				<?php
124
				foreach ( $list_items as $att_id => $attachment ) :
125
					printf( '<li>%s</li>', esc_url( $attachment['src'] ) );
126
				endforeach;
127
				?>
128
			</ol>
129
		</noscript>
130
		<script type="application/json" class="wp-playlist-script"><?php echo wp_json_encode( $data ); ?></script>
131
	</div>
132
	<?php
133
	/*
134
	 * Enqueue necessary scripts and styles.
135
	 */
136
	\Jetpack_Gutenberg::load_assets_as_required( 'podcast-episodes' );
137
138
	return ob_get_clean();
139
}
140