Complex classes like GravityView_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 GravityView_oEmbed, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class GravityView_oEmbed { |
||
23 | |||
24 | protected $output = array(); |
||
25 | protected $entry_id = NULL; |
||
26 | protected $view_id = NULL; |
||
27 | protected $is_full_oembed_preview = false; |
||
28 | |||
29 | static $instance = NULL; |
||
30 | |||
31 | private function __construct() {} |
||
32 | |||
33 | private function initialize() { |
||
42 | |||
43 | /** |
||
44 | * @return GravityView_oEmbed |
||
45 | * @since 1.6 |
||
46 | */ |
||
47 | static function getInstance() { |
||
57 | |||
58 | /** |
||
59 | * Register the oEmbed handler |
||
60 | * |
||
61 | * @since 1.6 |
||
62 | * @uses get_handler_regex |
||
63 | */ |
||
64 | function register_handler() { |
||
69 | |||
70 | /** |
||
71 | * Become an oEmbed provider for GravityView. |
||
72 | * |
||
73 | * @since 1.21.5.3 |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | function add_provider() { |
||
80 | |||
81 | /** |
||
82 | * Output a response as a provider for an entry oEmbed URL. |
||
83 | * |
||
84 | * For now we only output the JSON format and don't care about the size (width, height). |
||
85 | * Our only current use-case is for it to provide output to the Add Media / From URL box |
||
86 | * in WordPress 4.8. |
||
87 | * |
||
88 | * @since 1.21.5.3 |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | function render_provider_request() { |
||
128 | |||
129 | /** |
||
130 | * Generate the Regular expression that matches embedded entries. |
||
131 | * |
||
132 | * Generates different regex if using permalinks and if not using permalinks |
||
133 | * |
||
134 | * @since 1.6 |
||
135 | * |
||
136 | * @return string Regex code |
||
137 | */ |
||
138 | private function get_handler_regex() { |
||
167 | |||
168 | /** |
||
169 | * Get the post ID from an URL |
||
170 | * |
||
171 | * This is necessary because url_to_postid() doesn't work with permalinks off for custom post types |
||
172 | * |
||
173 | * @uses url_to_postid() |
||
174 | * @since 1.6 |
||
175 | * |
||
176 | * @param string $url URL to get the post ID from |
||
177 | * @param string $slug The name of a post, used as backup way of checking for post ID |
||
178 | * @return int 0 if not found; int of URL post ID otherwise |
||
179 | */ |
||
180 | private function get_postid_from_url_and_slug( $url = '', $slug = '' ) { |
||
201 | |||
202 | /** |
||
203 | * Get the entry id for the current oEmbedded entry |
||
204 | * |
||
205 | * @since 1.6 |
||
206 | * |
||
207 | * @return int|null |
||
208 | */ |
||
209 | public function get_entry_id() { |
||
212 | |||
213 | /** |
||
214 | * |
||
215 | * |
||
216 | * @since 1.6 |
||
217 | * @see GravityView_oEmbed::add_providers() for the regex |
||
218 | * |
||
219 | * @param array $matches The regex matches from the provided regex when calling wp_embed_register_handler() |
||
220 | * @param array $attr Embed attributes. |
||
221 | * @param string $url The original URL that was matched by the regex. |
||
222 | * @param array $rawattr The original unmodified attributes. |
||
223 | * @return string The embed HTML. |
||
224 | */ |
||
225 | public function render_handler( $matches, $attr, $url, $rawattr ) { |
||
261 | |||
262 | |||
263 | /** |
||
264 | * Generate a warning to users when previewing oEmbed in the Add Media modal |
||
265 | * |
||
266 | * @return string HTML notice |
||
267 | */ |
||
268 | private function generate_preview_notice() { |
||
274 | |||
275 | /** |
||
276 | * Set entry_id and view_id from the data sent to render_handler |
||
277 | * |
||
278 | * @var $entry_id |
||
279 | * @var $view_id |
||
280 | * |
||
281 | * @see render_handler |
||
282 | */ |
||
283 | 1 | private function set_vars( $matches, $attr, $url, $rawattr ) { |
|
321 | |||
322 | /** |
||
323 | * Display a nice placeholder in the admin for the entry |
||
324 | * |
||
325 | * @param array $matches The regex matches from the provided regex when calling wp_embed_register_handler() |
||
326 | * @param array $attr Embed attributes. |
||
327 | * @param string $url The original URL that was matched by the regex. |
||
328 | * @param array $rawattr The original unmodified attributes. |
||
329 | * @return string The embed HTML. |
||
330 | */ |
||
331 | private function render_admin( $matches, $attr, $url, $rawattr ) { |
||
351 | |||
352 | private function generate_entry_output() { |
||
373 | |||
374 | /** |
||
375 | * Tell get_gravityview() to display a single entry |
||
376 | * |
||
377 | * REQUIRED FOR THE VIEW TO OUTPUT A SINGLE ENTRY |
||
378 | * |
||
379 | * @param bool|int $is_single_entry Existing single entry. False, because GV thinks we're in a post or page. |
||
380 | * |
||
381 | * @return int The current entry ID |
||
382 | */ |
||
383 | public function set_single_entry_id( $is_single_entry = false ) { |
||
387 | |||
388 | /** |
||
389 | * GravityView embed entry handler |
||
390 | * |
||
391 | * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}. |
||
392 | * @param array $attr Embed attributes. |
||
393 | * @param string $url The original URL that was matched by the regex. |
||
394 | * @param array $rawattr The original unmodified attributes. |
||
395 | * @return string The embed HTML. |
||
396 | */ |
||
397 | private function render_frontend( $matches, $attr, $url, $rawattr ) { |
||
429 | |||
430 | } |
||
431 | |||
432 | GravityView_oEmbed::getInstance(); |
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.