| Conditions | 20 |
| Paths | 932 |
| Total Lines | 133 |
| Code Lines | 46 |
| 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 |
||
| 143 | public function shortcode( $attr, $url = '' ) { |
||
| 144 | $post = get_post(); |
||
| 145 | |||
| 146 | if ( empty( $url ) && ! empty( $attr['src'] ) ) { |
||
| 147 | $url = $attr['src']; |
||
| 148 | } |
||
| 149 | |||
| 150 | $this->last_url = $url; |
||
| 151 | |||
| 152 | if ( empty( $url ) ) { |
||
| 153 | $this->last_attr = $attr; |
||
| 154 | return ''; |
||
| 155 | } |
||
| 156 | |||
| 157 | $rawattr = $attr; |
||
| 158 | $attr = wp_parse_args( $attr, wp_embed_defaults( $url ) ); |
||
| 159 | |||
| 160 | $this->last_attr = $attr; |
||
| 161 | |||
| 162 | // kses converts & into & and we need to undo this |
||
| 163 | // See https://core.trac.wordpress.org/ticket/11311 |
||
| 164 | $url = str_replace( '&', '&', $url ); |
||
| 165 | |||
| 166 | // Look for known internal handlers |
||
| 167 | ksort( $this->handlers ); |
||
| 168 | foreach ( $this->handlers as $priority => $handlers ) { |
||
| 169 | foreach ( $handlers as $id => $handler ) { |
||
| 170 | if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) { |
||
| 171 | if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) ) |
||
| 172 | /** |
||
| 173 | * Filters the returned embed handler. |
||
| 174 | * |
||
| 175 | * @since 2.9.0 |
||
| 176 | * |
||
| 177 | * @see WP_Embed::shortcode() |
||
| 178 | * |
||
| 179 | * @param mixed $return The shortcode callback function to call. |
||
| 180 | * @param string $url The attempted embed URL. |
||
| 181 | * @param array $attr An array of shortcode attributes. |
||
| 182 | */ |
||
| 183 | return apply_filters( 'embed_handler_html', $return, $url, $attr ); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | $post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null; |
||
| 189 | if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed() |
||
| 190 | $post_ID = $this->post_ID; |
||
| 191 | |||
| 192 | // Unknown URL format. Let oEmbed have a go. |
||
| 193 | if ( $post_ID ) { |
||
| 194 | |||
| 195 | // Check for a cached result (stored in the post meta) |
||
| 196 | $key_suffix = md5( $url . serialize( $attr ) ); |
||
| 197 | $cachekey = '_oembed_' . $key_suffix; |
||
| 198 | $cachekey_time = '_oembed_time_' . $key_suffix; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Filters the oEmbed TTL value (time to live). |
||
| 202 | * |
||
| 203 | * @since 4.0.0 |
||
| 204 | * |
||
| 205 | * @param int $time Time to live (in seconds). |
||
| 206 | * @param string $url The attempted embed URL. |
||
| 207 | * @param array $attr An array of shortcode attributes. |
||
| 208 | * @param int $post_ID Post ID. |
||
| 209 | */ |
||
| 210 | $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID ); |
||
| 211 | |||
| 212 | $cache = get_post_meta( $post_ID, $cachekey, true ); |
||
| 213 | $cache_time = get_post_meta( $post_ID, $cachekey_time, true ); |
||
| 214 | |||
| 215 | if ( ! $cache_time ) { |
||
| 216 | $cache_time = 0; |
||
| 217 | } |
||
| 218 | |||
| 219 | $cached_recently = ( time() - $cache_time ) < $ttl; |
||
| 220 | |||
| 221 | if ( $this->usecache || $cached_recently ) { |
||
| 222 | // Failures are cached. Serve one if we're using the cache. |
||
| 223 | if ( '{{unknown}}' === $cache ) |
||
| 224 | return $this->maybe_make_link( $url ); |
||
| 225 | |||
| 226 | if ( ! empty( $cache ) ) { |
||
| 227 | /** |
||
| 228 | * Filters the cached oEmbed HTML. |
||
| 229 | * |
||
| 230 | * @since 2.9.0 |
||
| 231 | * |
||
| 232 | * @see WP_Embed::shortcode() |
||
| 233 | * |
||
| 234 | * @param mixed $cache The cached HTML result, stored in post meta. |
||
| 235 | * @param string $url The attempted embed URL. |
||
| 236 | * @param array $attr An array of shortcode attributes. |
||
| 237 | * @param int $post_ID Post ID. |
||
| 238 | */ |
||
| 239 | return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID ); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Filters whether to inspect the given URL for discoverable link tags. |
||
| 245 | * |
||
| 246 | * @since 2.9.0 |
||
| 247 | * @since 4.4.0 The default value changed to true. |
||
| 248 | * |
||
| 249 | * @see WP_oEmbed::discover() |
||
| 250 | * |
||
| 251 | * @param bool $enable Whether to enable `<link>` tag discovery. Default true. |
||
| 252 | */ |
||
| 253 | $attr['discover'] = ( apply_filters( 'embed_oembed_discover', true ) ); |
||
| 254 | |||
| 255 | // Use oEmbed to get the HTML |
||
| 256 | $html = wp_oembed_get( $url, $attr ); |
||
| 257 | |||
| 258 | // Maybe cache the result |
||
| 259 | if ( $html ) { |
||
|
|
|||
| 260 | update_post_meta( $post_ID, $cachekey, $html ); |
||
| 261 | update_post_meta( $post_ID, $cachekey_time, time() ); |
||
| 262 | } elseif ( ! $cache ) { |
||
| 263 | update_post_meta( $post_ID, $cachekey, '{{unknown}}' ); |
||
| 264 | } |
||
| 265 | |||
| 266 | // If there was a result, return it |
||
| 267 | if ( $html ) { |
||
| 268 | /** This filter is documented in wp-includes/class-wp-embed.php */ |
||
| 269 | return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID ); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | // Still unknown |
||
| 274 | return $this->maybe_make_link( $url ); |
||
| 275 | } |
||
| 276 | |||
| 387 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: