| Conditions | 9 |
| Paths | 38 |
| Total Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 |