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 | */ |
||
| 28 | protected static $instance; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Get the singleton instance of this class |
||
| 32 | * @since 2.2.2 |
||
| 33 | * @return object |
||
| 34 | */ |
||
| 35 | 5 | public static function get_instance() { |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor |
||
| 45 | * @since 2.2.0 |
||
| 46 | */ |
||
| 47 | 1 | 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 | 1 | public function oembed_handler() { |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Retrieves oEmbed from url/object ID |
||
| 99 | * @since 0.9.5 |
||
| 100 | * @param array $args Arguments for method |
||
| 101 | * @return string html markup with embed or fallback |
||
| 102 | */ |
||
| 103 | 3 | public function get_oembed_no_edit( $args ) { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Retrieves oEmbed from url/object ID |
||
| 164 | * @since 0.9.5 |
||
| 165 | * @param array $args Arguments for method |
||
| 166 | * @return string html markup with embed or fallback |
||
| 167 | */ |
||
| 168 | 2 | public function get_oembed( $args ) { |
|
| 169 | 2 | $oembed = $this->get_oembed_no_edit( $args ); |
|
| 170 | |||
| 171 | // Send back our embed |
||
| 172 | 2 | if ( $oembed['embed'] && $oembed['embed'] != $oembed['fallback'] ) { |
|
| 173 | 2 | return '<div class="cmb2-oembed embed-status">' . $oembed['embed'] . '<p class="cmb2-remove-wrapper"><a href="#" class="cmb2-remove-file-button" rel="' . $oembed['args']['field_id'] . '">' . __( 'Remove Embed', 'cmb2' ) . '</a></p></div>'; |
|
| 174 | } |
||
| 175 | |||
| 176 | // Otherwise, send back error info that no oEmbeds were found |
||
| 177 | return '<p class="ui-state-error-text">' . sprintf( __( 'No oEmbed Results Found for %s. View more info at', 'cmb2' ), $oembed['fallback'] ) . ' <a href="http://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>.</p>'; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Hijacks retrieving of cached oEmbed. |
||
| 182 | * Returns cached data from relevant object metadata (vs postmeta) |
||
| 183 | * |
||
| 184 | * @since 0.9.5 |
||
| 185 | * @param boolean $check Whether to retrieve postmeta or override |
||
| 186 | * @param int $object_id Object ID |
||
| 187 | * @param string $meta_key Object metakey |
||
| 188 | * @return mixed Object's oEmbed cached data |
||
| 189 | */ |
||
| 190 | 1 | public function hijack_oembed_cache_get( $check, $object_id, $meta_key ) { |
|
| 191 | 1 | if ( ! $this->hijack || ( $this->object_id != $object_id && 1987645321 !== $object_id ) ) { |
|
| 192 | return $check; |
||
| 193 | } |
||
| 194 | |||
| 195 | 1 | if ( $this->ajax_update ) { |
|
| 196 | return false; |
||
| 197 | } |
||
| 198 | |||
| 199 | 1 | return $this->cache_action( $meta_key ); |
|
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Hijacks saving of cached oEmbed. |
||
| 204 | * Saves cached data to relevant object metadata (vs postmeta) |
||
| 205 | * |
||
| 206 | * @since 0.9.5 |
||
| 207 | * @param boolean $check Whether to continue setting postmeta |
||
| 208 | * @param int $object_id Object ID to get postmeta from |
||
| 209 | * @param string $meta_key Postmeta's key |
||
| 210 | * @param mixed $meta_value Value of the postmeta to be saved |
||
| 211 | * @return boolean Whether to continue setting |
||
| 212 | */ |
||
| 213 | 1 | public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_value ) { |
|
| 214 | |||
| 215 | if ( |
||
| 216 | 1 | ! $this->hijack |
|
| 217 | 1 | || ( $this->object_id != $object_id && 1987645321 !== $object_id ) |
|
| 218 | // only want to hijack oembed meta values |
||
| 219 | 1 | || 0 !== strpos( $meta_key, '_oembed_' ) |
|
| 220 | 1 | ) { |
|
| 221 | return $check; |
||
| 222 | } |
||
| 223 | |||
| 224 | 1 | $this->cache_action( $meta_key, $meta_value ); |
|
| 225 | |||
| 226 | // Anything other than `null` to cancel saving to postmeta |
||
| 227 | 1 | return true; |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Gets/updates the cached oEmbed value from/to relevant object metadata (vs postmeta) |
||
| 232 | * |
||
| 233 | * @since 1.3.0 |
||
| 234 | * @param string $meta_key Postmeta's key |
||
| 235 | * @param mixed $meta_value (Optional) value of the postmeta to be saved |
||
| 236 | */ |
||
| 237 | 1 | protected function cache_action( $meta_key ) { |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Hooks in when options-page data is saved to clean stale |
||
| 266 | * oembed cache data from the option value. |
||
| 267 | * @since 2.2.0 |
||
| 268 | * @param string $option_key The options-page option key |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | 1 | public static function clean_stale_options_page_oembeds( $option_key ) { |
|
| 303 | |||
| 304 | } |
||
| 305 |
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.