Complex classes like WP_Embed often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WP_Embed, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class WP_Embed { |
||
| 10 | public $handlers = array(); |
||
| 11 | public $post_ID; |
||
| 12 | public $usecache = true; |
||
| 13 | public $linkifunknown = true; |
||
| 14 | public $last_attr = array(); |
||
| 15 | public $last_url = ''; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * When a URL cannot be embedded, return false instead of returning a link |
||
| 19 | * or the URL. |
||
| 20 | * |
||
| 21 | * Bypasses the {@see 'embed_maybe_make_link'} filter. |
||
| 22 | * |
||
| 23 | * @access public |
||
| 24 | * @var bool |
||
| 25 | */ |
||
| 26 | public $return_false_on_fail = false; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Constructor |
||
| 30 | */ |
||
| 31 | public function __construct() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Process the [embed] shortcode. |
||
| 48 | * |
||
| 49 | * Since the [embed] shortcode needs to be run earlier than other shortcodes, |
||
| 50 | * this function removes all existing shortcodes, registers the [embed] shortcode, |
||
| 51 | * calls do_shortcode(), and then re-registers the old shortcodes. |
||
| 52 | * |
||
| 53 | * @global array $shortcode_tags |
||
| 54 | * |
||
| 55 | * @param string $content Content to parse |
||
| 56 | * @return string Content with shortcode parsed |
||
| 57 | */ |
||
| 58 | public function run_shortcode( $content ) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * If a post/page was saved, then output JavaScript to make |
||
| 78 | * an Ajax request that will call WP_Embed::cache_oembed(). |
||
| 79 | */ |
||
| 80 | public function maybe_run_ajax_cache() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Registers an embed handler. |
||
| 97 | * |
||
| 98 | * Do not use this function directly, use wp_embed_register_handler() instead. |
||
| 99 | * |
||
| 100 | * This function should probably also only be used for sites that do not support oEmbed. |
||
| 101 | * |
||
| 102 | * @param string $id An internal ID/name for the handler. Needs to be unique. |
||
| 103 | * @param string $regex The regex that will be used to see if this handler should be used for a URL. |
||
| 104 | * @param callable $callback The callback function that will be called if the regex is matched. |
||
| 105 | * @param int $priority Optional. Used to specify the order in which the registered handlers will be tested (default: 10). Lower numbers correspond with earlier testing, and handlers with the same priority are tested in the order in which they were added to the action. |
||
| 106 | */ |
||
| 107 | public function register_handler( $id, $regex, $callback, $priority = 10 ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Unregisters a previously-registered embed handler. |
||
| 116 | * |
||
| 117 | * Do not use this function directly, use wp_embed_unregister_handler() instead. |
||
| 118 | * |
||
| 119 | * @param string $id The handler ID that should be removed. |
||
| 120 | * @param int $priority Optional. The priority of the handler to be removed (default: 10). |
||
| 121 | */ |
||
| 122 | public function unregister_handler( $id, $priority = 10 ) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The do_shortcode() callback function. |
||
| 128 | * |
||
| 129 | * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of |
||
| 130 | * the registered embed handlers. If none of the regex matches and it's enabled, then the URL |
||
| 131 | * will be given to the WP_oEmbed class. |
||
| 132 | * |
||
| 133 | * @param array $attr { |
||
| 134 | * Shortcode attributes. Optional. |
||
| 135 | * |
||
| 136 | * @type int $width Width of the embed in pixels. |
||
| 137 | * @type int $height Height of the embed in pixels. |
||
| 138 | * } |
||
| 139 | * @param string $url The URL attempting to be embedded. |
||
| 140 | * @return string|false The embed HTML on success, otherwise the original URL. |
||
| 141 | * `->maybe_make_link()` can return false on failure. |
||
| 142 | */ |
||
| 143 | public function shortcode( $attr, $url = '' ) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Delete all oEmbed caches. Unused by core as of 4.0.0. |
||
| 279 | * |
||
| 280 | * @param int $post_ID Post ID to delete the caches for. |
||
| 281 | */ |
||
| 282 | public function delete_oembed_caches( $post_ID ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Triggers a caching of all oEmbed results. |
||
| 295 | * |
||
| 296 | * @param int $post_ID Post ID to do the caching for. |
||
| 297 | */ |
||
| 298 | public function cache_oembed( $post_ID ) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Passes any unlinked URLs that are on their own line to WP_Embed::shortcode() for potential embedding. |
||
| 327 | * |
||
| 328 | * @see WP_Embed::autoembed_callback() |
||
| 329 | * |
||
| 330 | * @param string $content The content to be searched. |
||
| 331 | * @return string Potentially modified $content. |
||
| 332 | */ |
||
| 333 | public function autoembed( $content ) { |
||
| 334 | // Replace line breaks from all HTML elements with placeholders. |
||
| 335 | $content = wp_replace_in_html_tags( $content, array( "\n" => '<!-- wp-line-break -->' ) ); |
||
| 336 | |||
| 337 | if ( preg_match( '#(^|\s|>)https?://#i', $content ) ) { |
||
| 338 | // Find URLs on their own line. |
||
| 339 | $content = preg_replace_callback( '|^(\s*)(https?://[^\s<>"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content ); |
||
| 340 | // Find URLs in their own paragraph. |
||
| 341 | $content = preg_replace_callback( '|(<p(?: [^>]*)?>\s*)(https?://[^\s<>"]+)(\s*<\/p>)|i', array( $this, 'autoembed_callback' ), $content ); |
||
| 342 | } |
||
| 343 | |||
| 344 | // Put the line breaks back. |
||
| 345 | return str_replace( '<!-- wp-line-break -->', "\n", $content ); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Callback function for WP_Embed::autoembed(). |
||
| 350 | * |
||
| 351 | * @param array $match A regex match array. |
||
| 352 | * @return string The embed HTML on success, otherwise the original URL. |
||
| 353 | */ |
||
| 354 | public function autoembed_callback( $match ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Conditionally makes a hyperlink based on an internal class variable. |
||
| 365 | * |
||
| 366 | * @param string $url URL to potentially be linked. |
||
| 367 | * @return false|string Linked URL or the original URL. False if 'return_false_on_fail' is true. |
||
| 368 | */ |
||
| 369 | public function maybe_make_link( $url ) { |
||
| 386 | } |
||
| 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: