| Conditions | 7 |
| Paths | 14 |
| Total Lines | 84 |
| 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 | $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 |