Complex classes like CMB2_Ajax 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 CMB2_Ajax, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class CMB2_Ajax { |
||
|
|||
15 | |||
16 | // Whether to hijack the oembed cache system |
||
17 | protected $hijack = false; |
||
18 | protected $object_id = 0; |
||
19 | protected $embed_args = array(); |
||
20 | protected $object_type = 'post'; |
||
21 | protected $ajax_update = false; |
||
22 | |||
23 | /** |
||
24 | * Instance of this class |
||
25 | * |
||
26 | * @since 2.2.2 |
||
27 | * @var object |
||
28 | */ |
||
29 | protected static $instance; |
||
30 | |||
31 | /** |
||
32 | * Get the singleton instance of this class |
||
33 | * |
||
34 | * @since 2.2.2 |
||
35 | * @return CMB2_Ajax |
||
36 | */ |
||
37 | public static function get_instance() { |
||
44 | |||
45 | /** |
||
46 | * Constructor |
||
47 | * |
||
48 | * @since 2.2.0 |
||
49 | */ |
||
50 | protected function __construct() { |
||
56 | |||
57 | /** |
||
58 | * Handles our oEmbed ajax request |
||
59 | * |
||
60 | * @since 0.9.5 |
||
61 | * @return object oEmbed embed code | fallback | error message |
||
62 | */ |
||
63 | public function oembed_handler() { |
||
100 | |||
101 | /** |
||
102 | * Retrieves oEmbed from url/object ID |
||
103 | * |
||
104 | * @since 0.9.5 |
||
105 | * @param array $args Arguments for method |
||
106 | * @return string html markup with embed or fallback |
||
107 | */ |
||
108 | public function get_oembed_no_edit( $args ) { |
||
166 | |||
167 | /** |
||
168 | * Retrieves oEmbed from url/object ID |
||
169 | * |
||
170 | * @since 0.9.5 |
||
171 | * @param array $args Arguments for method |
||
172 | * @return string html markup with embed or fallback |
||
173 | */ |
||
174 | public function get_oembed( $args ) { |
||
193 | |||
194 | /** |
||
195 | * Hijacks retrieving of cached oEmbed. |
||
196 | * Returns cached data from relevant object metadata (vs postmeta) |
||
197 | * |
||
198 | * @since 0.9.5 |
||
199 | * @param boolean $check Whether to retrieve postmeta or override |
||
200 | * @param int $object_id Object ID |
||
201 | * @param string $meta_key Object metakey |
||
202 | * @return mixed Object's oEmbed cached data |
||
203 | */ |
||
204 | public function hijack_oembed_cache_get( $check, $object_id, $meta_key ) { |
||
215 | |||
216 | /** |
||
217 | * Hijacks saving of cached oEmbed. |
||
218 | * Saves cached data to relevant object metadata (vs postmeta) |
||
219 | * |
||
220 | * @since 0.9.5 |
||
221 | * @param boolean $check Whether to continue setting postmeta |
||
222 | * @param int $object_id Object ID to get postmeta from |
||
223 | * @param string $meta_key Postmeta's key |
||
224 | * @param mixed $meta_value Value of the postmeta to be saved |
||
225 | * @return boolean Whether to continue setting |
||
226 | */ |
||
227 | public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_value ) { |
||
243 | |||
244 | /** |
||
245 | * Gets/updates the cached oEmbed value from/to relevant object metadata (vs postmeta) |
||
246 | * |
||
247 | * @since 1.3.0 |
||
248 | * @param string $meta_key Postmeta's key |
||
249 | */ |
||
250 | protected function cache_action( $meta_key ) { |
||
276 | |||
277 | /** |
||
278 | * Hooks in when options-page data is saved to clean stale |
||
279 | * oembed cache data from the option value. |
||
280 | * |
||
281 | * @since 2.2.0 |
||
282 | * @param string $option_key The options-page option key |
||
283 | * @return void |
||
284 | */ |
||
285 | public static function clean_stale_options_page_oembeds( $option_key ) { |
||
317 | |||
318 | } |
||
319 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.