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 | * @since 2.2.2 |
||
| 26 | * @var object |
||
| 27 | 1 | */ |
|
| 28 | 1 | protected static $instance; |
|
| 29 | 1 | ||
| 30 | /** |
||
| 31 | * Get the singleton instance of this class |
||
| 32 | * @since 2.2.2 |
||
| 33 | * @return object |
||
| 34 | */ |
||
| 35 | public static function get_instance() { |
||
| 42 | |||
| 43 | 1 | /** |
|
| 44 | 1 | * Constructor |
|
| 45 | 1 | * @since 2.2.0 |
|
| 46 | 1 | */ |
|
| 47 | protected function __construct() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Handles our oEmbed ajax request |
||
| 56 | * @since 0.9.5 |
||
| 57 | * @return object oEmbed embed code | fallback | error message |
||
| 58 | */ |
||
| 59 | public function oembed_handler() { |
||
| 96 | |||
| 97 | 2 | /** |
|
| 98 | * Retrieves oEmbed from url/object ID |
||
| 99 | 2 | * @since 0.9.5 |
|
| 100 | * @param array $args Arguments for method |
||
| 101 | 2 | * @return string html markup with embed or fallback |
|
| 102 | */ |
||
| 103 | public function get_oembed( $args ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Hijacks retrieving of cached oEmbed. |
||
| 171 | * Returns cached data from relevant object metadata (vs postmeta) |
||
| 172 | * |
||
| 173 | 68 | * @since 0.9.5 |
|
| 174 | 68 | * @param boolean $check Whether to retrieve postmeta or override |
|
| 175 | 67 | * @param int $object_id Object ID |
|
| 176 | * @param string $meta_key Object metakey |
||
| 177 | * @return mixed Object's oEmbed cached data |
||
| 178 | 2 | */ |
|
| 179 | public function hijack_oembed_cache_get( $check, $object_id, $meta_key ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Hijacks saving of cached oEmbed. |
||
| 193 | * Saves cached data to relevant object metadata (vs postmeta) |
||
| 194 | * |
||
| 195 | * @since 0.9.5 |
||
| 196 | 52 | * @param boolean $check Whether to continue setting postmeta |
|
| 197 | * @param int $object_id Object ID to get postmeta from |
||
| 198 | * @param string $meta_key Postmeta's key |
||
| 199 | 52 | * @param mixed $meta_value Value of the postmeta to be saved |
|
| 200 | 52 | * @return boolean Whether to continue setting |
|
| 201 | */ |
||
| 202 | 52 | public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_value ) { |
|
| 218 | |||
| 219 | /** |
||
| 220 | 2 | * Gets/updates the cached oEmbed value from/to relevant object metadata (vs postmeta) |
|
| 221 | 2 | * |
|
| 222 | 2 | * @since 1.3.0 |
|
| 223 | * @param string $meta_key Postmeta's key |
||
| 224 | 2 | * @param mixed $meta_value (Optional) value of the postmeta to be saved |
|
| 225 | */ |
||
| 226 | 2 | protected function cache_action( $meta_key ) { |
|
| 252 | |||
| 253 | /** |
||
| 254 | 1 | * Hooks in when options-page data is saved to clean stale |
|
| 255 | 1 | * oembed cache data from the option value. |
|
| 256 | 1 | * @since 2.2.0 |
|
| 257 | * @param string $option_key The options-page option key |
||
| 258 | 1 | * @return void |
|
| 259 | 1 | */ |
|
| 260 | 1 | public static function clean_stale_options_page_oembeds( $option_key ) { |
|
| 292 | |||
| 293 | } |
||
| 294 |
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.