| Conditions | 10 |
| Paths | 4 |
| 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 |
||
| 38 | if ( empty( $media['id'] ) || empty( $media['url'] ) ) { |
||
| 39 | return __( 'Error retrieving media', 'jetpack' ); |
||
| 40 | } |
||
| 41 | return sprintf( |
||
| 42 | '<img |
||
| 43 | alt="%1$s" |
||
| 44 | class="wp-block-jetpack-story_image wp-story-image wp-image-%2$s" |
||
| 45 | data-id="%2$s" |
||
| 46 | src="%3$s">', |
||
| 47 | esc_attr( $media['alt'] ), |
||
| 48 | $media['id'], |
||
| 49 | esc_attr( $media['url'] ) |
||
| 50 | ); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Render a video inside a slide |
||
| 55 | * |
||
| 56 | * @param array $media Video information. |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | function render_video( $media ) { |
||
| 61 | if ( empty( $media['id'] ) || empty( $media['mime'] ) || empty( $media['url'] ) ) { |
||
| 62 | return __( 'Error retrieving media', 'jetpack' ); |
||
| 63 | } |
||
| 64 | return sprintf( |
||
| 65 | '<video |
||
| 66 | title="%1$s" |
||
| 67 | type="%2$s" |
||
| 68 | class="wp-story-video intrinsic-ignore wp-video-%3$s" |
||
| 69 | data-id="%3$s" |
||
| 70 | src="%4$s"> |
||
| 71 | </video>', |
||
| 72 | esc_attr( $media['alt'] ), |
||
| 73 | esc_attr( $media['mime'] ), |
||
| 74 | $media['id'], |
||
| 75 | esc_attr( $media['url'] ) |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Render a slide |
||
| 81 | * |
||
| 82 | * @param array $media Media information. |
||
| 83 | * @param array $index Index of the slide, first slide will be displayed by default, others hidden. |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | function render_slide( $media, $index ) { |
||
| 88 | return sprintf( |
||
| 89 | '<li class="wp-story-slide" style="display: %s;"> |
||
| 90 | <figure> |
||
| 91 | %s |
||
| 92 | </figure> |
||
| 93 | </li>', |
||
| 94 | 0 === $index ? 'block' : 'none', |
||
| 95 | 'image' === $media['type'] |
||
| 96 | ? render_image( $media, $index ) |
||
| 97 | : render_video( $media, $index ) |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Render story block |
||
| 103 | * |
||
| 104 | * @param array $attributes Block attributes. |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | function render_block( $attributes ) { |
||
| 109 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
||
| 110 | |||
| 111 | $settings = array(); |
||
| 112 | $media_files = isset( $attributes['mediaFiles'] ) ? $attributes['mediaFiles'] : array(); |
||
| 113 | |||
| 114 | return sprintf( |
||
| 115 | '<div class="wp-story aligncenter" data-settings="%s"> |
||
| 116 | <div style="display: contents;"> |
||
| 117 | <div class="wp-story-container"> |
||
| 118 | <div class="wp-story-meta"> |
||
| 119 | <div class="wp-story-icon"> |
||
| 120 | <img alt="%s" src="%s" width="32" height=32> |
||
| 121 | </div> |
||
| 122 | <div> |
||
| 123 | <div class="wp-story-title"> |
||
| 124 | %s |
||
| 125 | </div> |
||
| 126 | </div> |
||
| 127 | <button class="wp-story-exit-fullscreen jetpack-mdc-icon-button"> |
||
| 128 | <i class="jetpack-material-icons close md-24"></i> |
||
| 129 | </button> |
||
| 130 | </div> |
||
| 131 | <ul class="wp-story-wrapper"> |
||
| 150 |