Complex classes like oEmbed 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 oEmbed, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class oEmbed { |
||
| 13 | public static $provider_url = ''; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Initialize. |
||
| 17 | * |
||
| 18 | * Register the oEmbed handler and the provider. |
||
| 19 | * Fire off the provider handler if detected. |
||
| 20 | * |
||
| 21 | * @return void |
||
| 22 | */ |
||
| 23 | public static function init() { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Output a response as a provider for an entry oEmbed URL. |
||
| 38 | * |
||
| 39 | * For now we only output the JSON format and don't care about the size (width, height). |
||
| 40 | * Our only current use-case is for it to provide output to the Add Media / From URL box |
||
| 41 | * in WordPress 4.8. |
||
| 42 | * |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | public static function render_provider_request() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Output the embed HTML. |
||
| 74 | * |
||
| 75 | * @param array $matches The regex matches from the provided regex when calling wp_embed_register_handler() |
||
| 76 | * @param array $attr Embed attributes. |
||
| 77 | * @param string $url The original URL that was matched by the regex. |
||
| 78 | * @param array $rawattr The original unmodified attributes. |
||
| 79 | * |
||
| 80 | * @return string The embed HTML. |
||
| 81 | */ |
||
| 82 | 2 | public static function render( $matches, $attr, $url, $rawattr ) { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Parse oEmbed regex matches and return View and Entry. |
||
| 106 | * |
||
| 107 | * @param array $matches The regex matches. |
||
| 108 | * @param string $url The URL of the embed. |
||
| 109 | * |
||
| 110 | * @return array (\GV\View, \GV\Entry) |
||
| 111 | */ |
||
| 112 | 1 | private static function parse_matches( $matches, $url ) { |
|
| 113 | // If not using permalinks, re-assign values for matching groups |
||
| 114 | 1 | if ( ! empty( $matches['entry_slug2'] ) ) { |
|
| 115 | $matches['is_cpt'] = $matches['is_cpt2']; |
||
| 116 | $matches['slug'] = $matches['slug2']; |
||
| 117 | $matches['entry_slug'] = $matches['entry_slug2']; |
||
| 118 | unset( $matches['is_cpt2'], $matches['slug2'], $matches['entry_slug2'] ); |
||
| 119 | } |
||
| 120 | |||
| 121 | 1 | if ( empty( $matches['entry_slug'] ) ) { |
|
| 122 | gravityview()->log->error( 'Entry slug not parsed by regex.', array( 'data' => $matches ) ); |
||
| 123 | return null; |
||
| 124 | } else { |
||
| 125 | 1 | $entry_id = $matches['entry_slug']; |
|
| 126 | } |
||
| 127 | |||
| 128 | 1 | if ( ! $entry = \GV\GF_Entry::by_id( $entry_id ) ) { |
|
| 129 | gravityview()->log->error( 'Invalid entry ID {entry_id}', array( 'entry_id' => $entry_id ) ); |
||
| 130 | return null; |
||
| 131 | } |
||
| 132 | |||
| 133 | 1 | $view = null; |
|
| 134 | |||
| 135 | 1 | if ( $view_id = url_to_postid( $url ) ) { |
|
| 136 | $view = \GV\View::by_id( $view_id ); |
||
| 137 | } |
||
| 138 | |||
| 139 | // Maybe it failed to find a GravityView CPT |
||
| 140 | 1 | if ( ! $view ) { |
|
| 141 | |||
| 142 | // If the slug doesn't work, maybe using Plain permalinks and not the slug, only ID |
||
| 143 | 1 | if( is_numeric( $matches['slug'] ) ) { |
|
| 144 | $view = \GV\View::by_id( $matches['slug'] ); |
||
| 145 | } |
||
| 146 | |||
| 147 | 1 | if( ! $view ) { |
|
| 148 | 1 | $view = \GV\View::from_post( get_page_by_path( $matches['slug'], OBJECT, 'gravityview' ) ); |
|
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | 1 | if ( ! $view ) { |
|
| 153 | 1 | gravityview()->log->error( 'Could not detect View from URL {url}', array( 'url' => $url, 'data' => $matches ) ); |
|
| 154 | 1 | return null; |
|
| 155 | } |
||
| 156 | |||
| 157 | 1 | return array( $view, $entry ); |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Display a nice placeholder in the admin for the entry. |
||
| 162 | * |
||
| 163 | * @param \GV\View $view The View. |
||
| 164 | * @param \GV\Entry $entry The Entry. |
||
| 165 | * |
||
| 166 | * @return string A placeholder, with Mr. Floaty :) |
||
| 167 | */ |
||
| 168 | private static function render_admin( $view, $entry ) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Generate a warning to users when previewing oEmbed in the Add Media modal. |
||
| 189 | * |
||
| 190 | * @return string HTML notice |
||
| 191 | */ |
||
| 192 | private static function render_preview_notice() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Render the entry as an oEmbed. |
||
| 201 | * |
||
| 202 | * @param \GV\View $view The View. |
||
| 203 | * @param \GV\Entry $entry The Entry. |
||
| 204 | * |
||
| 205 | * @return string The rendered oEmbed. |
||
| 206 | */ |
||
| 207 | 1 | private static function render_frontend( $view, $entry ) { |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Generate the Regular expression that matches embedded entries. |
||
| 260 | * |
||
| 261 | * Generates different regex if using permalinks and if not using permalinks |
||
| 262 | * |
||
| 263 | * @return string Regex code |
||
| 264 | */ |
||
| 265 | private static function get_entry_regex() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Internal oEmbed output, shortcircuit without proxying to the provider. |
||
| 291 | */ |
||
| 292 | public static function pre_oembed_result( $result, $url, $args ) { |
||
| 306 | } |
||
| 307 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.