| Conditions | 22 |
| Paths | 578 |
| Total Lines | 125 |
| Code Lines | 55 |
| Lines | 30 |
| Ratio | 24 % |
| 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 |
||
| 37 | function vimeo_shortcode( $atts ) { |
||
| 38 | global $content_width; |
||
| 39 | |||
| 40 | $attr = array_map( |
||
| 41 | 'intval', |
||
| 42 | shortcode_atts( |
||
| 43 | array( |
||
| 44 | 'id' => 0, |
||
| 45 | 'width' => 0, |
||
| 46 | 'height' => 0, |
||
| 47 | 'autoplay' => 0, |
||
| 48 | 'loop' => 0, |
||
| 49 | ), $atts |
||
| 50 | ) |
||
| 51 | ); |
||
| 52 | |||
| 53 | if ( isset( $atts[0] ) ) { |
||
| 54 | $attr['id'] = jetpack_shortcode_get_vimeo_id( $atts ); |
||
| 55 | } |
||
| 56 | |||
| 57 | if ( ! $attr['id'] ) { |
||
| 58 | return '<!-- vimeo error: not a vimeo video -->'; |
||
| 59 | } |
||
| 60 | |||
| 61 | // [vimeo 141358 h=500&w=350] |
||
| 62 | $params = shortcode_new_to_old_params( $atts ); // h=500&w=350 |
||
| 63 | $params = str_replace( array( '&', '&' ), '&', $params ); |
||
| 64 | parse_str( $params, $args ); |
||
| 65 | |||
| 66 | $width = intval( $attr['width'] ); |
||
| 67 | $height = intval( $attr['height'] ); |
||
| 68 | |||
| 69 | // Support w and h argument as fallback. |
||
| 70 | View Code Duplication | if ( empty( $width ) && isset( $args['w'] ) ) { |
|
| 71 | $width = intval( $args['w'] ); |
||
| 72 | |||
| 73 | if ( empty( $height ) && ! isset( $args['h'] ) ) { |
||
| 74 | // The case where w=300 is specified without h=200, otherwise $height |
||
| 75 | // will always equal the default of 300, no matter what w was set to. |
||
| 76 | $height = round( ( $width / 640 ) * 360 ); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | View Code Duplication | if ( empty( $height ) && isset( $args['h'] ) ) { |
|
| 81 | $height = (int) $args['h']; |
||
| 82 | |||
| 83 | if ( ! isset( $args['w'] ) ) { |
||
| 84 | $width = round( ( $height / 360 ) * 640 ); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | if ( ! $width && ! empty( $content_width ) ) { |
||
| 89 | $width = absint( $content_width ); |
||
| 90 | } |
||
| 91 | |||
| 92 | // If setting the width with content_width has failed, defaulting |
||
| 93 | if ( ! $width ) { |
||
| 94 | $width = 640; |
||
| 95 | } |
||
| 96 | |||
| 97 | if ( ! $height ) { |
||
| 98 | $height = round( ( $width / 640 ) * 360 ); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Filter the Vimeo player width. |
||
| 103 | * |
||
| 104 | * @module shortcodes |
||
| 105 | * |
||
| 106 | * @since 3.4.0 |
||
| 107 | * |
||
| 108 | * @param int $width Width of the Vimeo player in pixels. |
||
| 109 | */ |
||
| 110 | $width = (int) apply_filters( 'vimeo_width', $width ); |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Filter the Vimeo player height. |
||
| 114 | * |
||
| 115 | * @module shortcodes |
||
| 116 | * |
||
| 117 | * @since 3.4.0 |
||
| 118 | * |
||
| 119 | * @param int $height Height of the Vimeo player in pixels. |
||
| 120 | */ |
||
| 121 | $height = (int) apply_filters( 'vimeo_height', $height ); |
||
| 122 | |||
| 123 | $url = esc_url( 'https://player.vimeo.com/video/' . $attr['id'] ); |
||
| 124 | |||
| 125 | // Handle autoplay and loop arguments. |
||
| 126 | View Code Duplication | if ( |
|
| 127 | isset( $args['autoplay'] ) && '1' === $args['autoplay'] // Parsed from the embedded URL. |
||
| 128 | || $attr['autoplay'] // Parsed from shortcode arguments. |
||
| 129 | || in_array( 'autoplay', $atts ) // Catch the argument passed without a value. |
||
| 130 | ) { |
||
| 131 | $url = add_query_arg( 'autoplay', 1, $url ); |
||
| 132 | } |
||
| 133 | |||
| 134 | View Code Duplication | if ( |
|
| 135 | isset( $args['loop'] ) && '1' === $args['loop'] // Parsed from the embedded URL. |
||
| 136 | || $attr['loop'] // Parsed from shortcode arguments. |
||
| 137 | || in_array( 'loop', $atts ) // Catch the argument passed without a value. |
||
| 138 | ) { |
||
| 139 | $url = add_query_arg( 'loop', 1, $url ); |
||
| 140 | } |
||
| 141 | |||
| 142 | $html = sprintf( |
||
| 143 | '<div class="embed-vimeo" style="text-align: center;"><iframe src="%1$s" width="%2$u" height="%3$u" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>', |
||
| 144 | esc_url( $url ), |
||
| 145 | esc_attr( $width ), |
||
| 146 | esc_attr( $height ) |
||
| 147 | ); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Filter the Vimeo player HTML. |
||
| 151 | * |
||
| 152 | * @module shortcodes |
||
| 153 | * |
||
| 154 | * @since 1.2.3 |
||
| 155 | * |
||
| 156 | * @param string $html Embedded Vimeo player HTML. |
||
| 157 | */ |
||
| 158 | $html = apply_filters( 'video_embed_html', $html ); |
||
| 159 | |||
| 160 | return $html; |
||
| 161 | } |
||
| 162 | |||
| 303 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.