| Conditions | 28 |
| Paths | 331 |
| Total Lines | 200 |
| 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 |
||
| 23 | function jetpack_photon_url( $image_url, $args = array(), $scheme = null ) { |
||
| 24 | $image_url = trim( $image_url ); |
||
| 25 | |||
| 26 | if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) { |
||
| 27 | /** |
||
| 28 | * Disables Photon URL processing for local development |
||
| 29 | * |
||
| 30 | * @module photon |
||
| 31 | * |
||
| 32 | * @since 4.1.0 |
||
| 33 | * |
||
| 34 | * @param bool false Result of Automattic\Jetpack\Status->is_development_mode(). |
||
| 35 | */ |
||
| 36 | if ( true === apply_filters( 'jetpack_photon_development_mode', ( new Status() )->is_development_mode() ) ) { |
||
| 37 | return $image_url; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Allow specific image URls to avoid going through Photon. |
||
| 43 | * |
||
| 44 | * @module photon |
||
| 45 | * |
||
| 46 | * @since 3.2.0 |
||
| 47 | * |
||
| 48 | * @param bool false Should the image be returned as is, without going through Photon. Default to false. |
||
| 49 | * @param string $image_url Image URL. |
||
| 50 | * @param array|string $args Array of Photon arguments. |
||
| 51 | * @param string|null $scheme Image scheme. Default to null. |
||
| 52 | */ |
||
| 53 | if ( false !== apply_filters( 'jetpack_photon_skip_for_url', false, $image_url, $args, $scheme ) ) { |
||
| 54 | return $image_url; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Filter the original image URL before it goes through Photon. |
||
| 59 | * |
||
| 60 | * @module photon |
||
| 61 | * |
||
| 62 | * @since 1.9.0 |
||
| 63 | * |
||
| 64 | * @param string $image_url Image URL. |
||
| 65 | * @param array|string $args Array of Photon arguments. |
||
| 66 | * @param string|null $scheme Image scheme. Default to null. |
||
| 67 | */ |
||
| 68 | $image_url = apply_filters( 'jetpack_photon_pre_image_url', $image_url, $args, $scheme ); |
||
| 69 | /** |
||
| 70 | * Filter the original Photon image parameters before Photon is applied to an image. |
||
| 71 | * |
||
| 72 | * @module photon |
||
| 73 | * |
||
| 74 | * @since 1.9.0 |
||
| 75 | * |
||
| 76 | * @param array|string $args Array of Photon arguments. |
||
| 77 | * @param string $image_url Image URL. |
||
| 78 | * @param string|null $scheme Image scheme. Default to null. |
||
| 79 | */ |
||
| 80 | $args = apply_filters( 'jetpack_photon_pre_args', $args, $image_url, $scheme ); |
||
| 81 | |||
| 82 | if ( empty( $image_url ) ) { |
||
| 83 | return $image_url; |
||
| 84 | } |
||
| 85 | |||
| 86 | $image_url_parts = wp_parse_url( $image_url ); |
||
| 87 | |||
| 88 | // Unable to parse. |
||
| 89 | if ( ! is_array( $image_url_parts ) || empty( $image_url_parts['host'] ) || empty( $image_url_parts['path'] ) ) { |
||
| 90 | return $image_url; |
||
| 91 | } |
||
| 92 | |||
| 93 | if ( is_array( $args ) ) { |
||
| 94 | // Convert values that are arrays into strings. |
||
| 95 | foreach ( $args as $arg => $value ) { |
||
| 96 | if ( is_array( $value ) ) { |
||
| 97 | $args[ $arg ] = implode( ',', $value ); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | // Encode values. |
||
| 102 | // See https://core.trac.wordpress.org/ticket/17923 . |
||
| 103 | $args = rawurlencode_deep( $args ); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Don't photon-ize WPCOM hosted images -- we can serve them up from wpcom directly. |
||
| 107 | $is_wpcom_image = false; |
||
| 108 | if ( wp_endswith( strtolower( $image_url_parts['host'] ), '.files.wordpress.com' ) ) { |
||
| 109 | $is_wpcom_image = true; |
||
| 110 | if ( isset( $args['ssl'] ) ) { |
||
| 111 | // Do not send the ssl argument to prevent caching issues. |
||
| 112 | unset( $args['ssl'] ); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | /** This filter is documented below. */ |
||
| 117 | $custom_photon_url = apply_filters( 'jetpack_photon_domain', '', $image_url ); |
||
| 118 | $custom_photon_url = esc_url( $custom_photon_url ); |
||
| 119 | |||
| 120 | // You can't run a Photon URL through Photon again because query strings are stripped. |
||
| 121 | // So if the image is already a Photon URL, append the new arguments to the existing URL. |
||
| 122 | // Alternately, if it's a *.files.wordpress.com url, then keep the domain as is. |
||
| 123 | if ( |
||
| 124 | in_array( $image_url_parts['host'], array( 'i0.wp.com', 'i1.wp.com', 'i2.wp.com' ), true ) |
||
| 125 | || wp_parse_url( $custom_photon_url, PHP_URL_HOST ) === $image_url_parts['host'] |
||
| 126 | || $is_wpcom_image |
||
| 127 | ) { |
||
| 128 | /* |
||
| 129 | * VideoPress Poster images should only keep one param, ssl. |
||
| 130 | */ |
||
| 131 | if ( |
||
| 132 | is_array( $args ) |
||
| 133 | && 'videos.files.wordpress.com' === strtolower( $image_url_parts['host'] ) |
||
| 134 | ) { |
||
| 135 | $args = array_intersect_key( array( 'ssl' => 1 ), $args ); |
||
| 136 | } |
||
| 137 | |||
| 138 | $photon_url = add_query_arg( $args, $image_url ); |
||
| 139 | return jetpack_photon_url_scheme( $photon_url, $scheme ); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Allow Photon to use query strings as well. |
||
| 144 | * By default, Photon doesn't support query strings so we ignore them and look only at the path. |
||
| 145 | * This setting is Photon Server dependent. |
||
| 146 | * |
||
| 147 | * @module photon |
||
| 148 | * |
||
| 149 | * @since 1.9.0 |
||
| 150 | * |
||
| 151 | * @param bool false Should images using query strings go through Photon. Default is false. |
||
| 152 | * @param string $image_url_parts['host'] Image URL's host. |
||
| 153 | */ |
||
| 154 | if ( ! apply_filters( 'jetpack_photon_any_extension_for_domain', false, $image_url_parts['host'] ) ) { |
||
| 155 | // Photon doesn't support query strings so we ignore them and look only at the path. |
||
| 156 | // However some source images are served via PHP so check the no-query-string extension. |
||
| 157 | // For future proofing, this is a blacklist of common issues rather than a whitelist. |
||
| 158 | $extension = pathinfo( $image_url_parts['path'], PATHINFO_EXTENSION ); |
||
| 159 | if ( empty( $extension ) || in_array( $extension, array( 'php', 'ashx' ), true ) ) { |
||
| 160 | return $image_url; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | $image_host_path = $image_url_parts['host'] . $image_url_parts['path']; |
||
| 165 | |||
| 166 | /* |
||
| 167 | * Figure out which CDN subdomain to use. |
||
| 168 | * |
||
| 169 | * The goal is to have the same subdomain for any particular image to prevent multiple runs resulting in multiple |
||
| 170 | * images needing to be downloaded by the browser. |
||
| 171 | * |
||
| 172 | * We are providing our own generated value by taking the modulus of the crc32 value of the URL. |
||
| 173 | * |
||
| 174 | * Valid values are 0, 1, and 2. |
||
| 175 | */ |
||
| 176 | $subdomain = abs( crc32( $image_host_path ) % 3 ); |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Filters the domain used by the Photon module. |
||
| 180 | * |
||
| 181 | * @module photon |
||
| 182 | * |
||
| 183 | * @since 3.4.2 |
||
| 184 | * |
||
| 185 | * @param string https://i{$subdomain}.wp.com Domain used by Photon. $subdomain is a random number between 0 and 2. |
||
| 186 | * @param string $image_url URL of the image to be photonized. |
||
| 187 | */ |
||
| 188 | $photon_domain = apply_filters( 'jetpack_photon_domain', "https://i{$subdomain}.wp.com", $image_url ); |
||
| 189 | $photon_domain = trailingslashit( esc_url( $photon_domain ) ); |
||
| 190 | $photon_url = $photon_domain . $image_host_path; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Add query strings to Photon URL. |
||
| 194 | * By default, Photon doesn't support query strings so we ignore them. |
||
| 195 | * This setting is Photon Server dependent. |
||
| 196 | * |
||
| 197 | * @module photon |
||
| 198 | * |
||
| 199 | * @since 1.9.0 |
||
| 200 | * |
||
| 201 | * @param bool false Should query strings be added to the image URL. Default is false. |
||
| 202 | * @param string $image_url_parts['host'] Image URL's host. |
||
| 203 | */ |
||
| 204 | if ( isset( $image_url_parts['query'] ) && apply_filters( 'jetpack_photon_add_query_string_to_domain', false, $image_url_parts['host'] ) ) { |
||
| 205 | $photon_url .= '?q=' . rawurlencode( $image_url_parts['query'] ); |
||
| 206 | } |
||
| 207 | |||
| 208 | if ( $args ) { |
||
| 209 | if ( is_array( $args ) ) { |
||
| 210 | $photon_url = add_query_arg( $args, $photon_url ); |
||
| 211 | } else { |
||
| 212 | // You can pass a query string for complicated requests but where you still want CDN subdomain help, etc. |
||
| 213 | $photon_url .= '?' . $args; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | if ( isset( $image_url_parts['scheme'] ) && 'https' === $image_url_parts['scheme'] ) { |
||
| 218 | $photon_url = add_query_arg( array( 'ssl' => 1 ), $photon_url ); |
||
| 219 | } |
||
| 220 | |||
| 221 | return jetpack_photon_url_scheme( $photon_url, $scheme ); |
||
| 222 | } |
||
| 223 | |||
| 383 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: