Completed
Push — add/podcast-episodes-block ( 886252...66128b )
by
unknown
09:45
created

podcast-episodes.php ➔ load_assets()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 14
nop 2
dl 0
loc 84
rs 7.4157
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
		$list_item['title'] = esc_html( trim( wp_strip_all_tags( $episode->get_title() ) ) );
71
		if ( empty( $list_item['title'] ) ) {
72
			$list_item['title'] = __( '(no title)', 'jetpack' );
73
		}
74
75
		$list_items[] = $list_item;
76
	}
77
78
	global $content_width;
79
80
	$data = array(
81
		'type'         => 'audio',
82
		// Don't pass strings to JSON, will be truthy in JS.
83
		'tracklist'    => true,
84
		'tracknumbers' => true,
85
		'images'       => true,
86
		'artists'      => true,
87
		'tracks'       => $list_items,
88
	);
89
90
	$outer         = 22; // Default padding and border of wrapper.
91
	$default_width = 640;
92
	$theme_width   = empty( $content_width ) ? $default_width : ( $content_width - $outer );
93
94
	ob_start();
95
	wp_playlist_scripts( 'audio' );
96
	/**
97
	 * Prints and enqueues playlist scripts, styles, and JavaScript templates.
98
	 *
99
	 * @since 3.9.0
100
	 *
101
	 * @param string $type  Type of playlist. Possible values are 'audio' or 'video'.
102
	 * @param string $style The 'theme' for the playlist. Core provides 'light' and 'dark'.
103
	 */
104
	do_action( 'wp_playlist_scripts', 'audio', 'light' );
105
106
	?>
107
	<div class="wp-playlist wp-audio-playlist wp-playlist-light">
108
		<div class="wp-playlist-current-item"></div>
109
		<audio controls="controls" preload="none" width="<?php echo (int) $theme_width; ?>"></audio>
110
		<div class="wp-playlist-next"></div>
111
		<div class="wp-playlist-prev"></div>
112
		<noscript>
113
			<ol>
114
				<?php
115
				foreach ( $list_items as $att_id => $attachment ) :
116
					printf( '<li>%s</li>', esc_url( $attachment['src'] ) );
117
				endforeach;
118
				?>
119
			</ol>
120
		</noscript>
121
		<script type="application/json" class="wp-playlist-script"><?php echo wp_json_encode( $data ); ?></script>
122
	</div>
123
	<?php
124
	/*
125
	 * Enqueue necessary scripts and styles.
126
	 */
127
	\Jetpack_Gutenberg::load_assets_as_required( 'podcast-episodes' );
128
129
	return ob_get_clean();
130
}
131