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 | 4 | 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() { |
|
60 | |||
61 | // Verify our nonce |
||
62 | if ( ! ( isset( $_REQUEST['cmb2_ajax_nonce'], $_REQUEST['oembed_url'] ) && wp_verify_nonce( $_REQUEST['cmb2_ajax_nonce'], 'ajax_nonce' ) ) ) { |
||
63 | die(); |
||
64 | } |
||
65 | |||
66 | // Sanitize our search string |
||
67 | 1 | $oembed_string = sanitize_text_field( $_REQUEST['oembed_url'] ); |
|
68 | |||
69 | // Send back error if empty |
||
70 | if ( empty( $oembed_string ) ) { |
||
71 | wp_send_json_error( '<p class="ui-state-error-text">' . __( 'Please Try Again', 'cmb2' ) . '</p>' ); |
||
72 | } |
||
73 | |||
74 | // Set width of embed |
||
75 | $embed_width = isset( $_REQUEST['oembed_width'] ) && intval( $_REQUEST['oembed_width'] ) < 640 ? intval( $_REQUEST['oembed_width'] ) : '640'; |
||
76 | |||
77 | // Set url |
||
78 | $oembed_url = esc_url( $oembed_string ); |
||
79 | |||
80 | // Set args |
||
81 | $embed_args = array( 'width' => $embed_width ); |
||
82 | |||
83 | $this->ajax_update = true; |
||
84 | |||
85 | // Get embed code (or fallback link) |
||
86 | $html = $this->get_oembed( array( |
||
87 | 'url' => $oembed_url, |
||
88 | 'object_id' => $_REQUEST['object_id'], |
||
89 | 'object_type' => isset( $_REQUEST['object_type'] ) ? $_REQUEST['object_type'] : 'post', |
||
90 | 'oembed_args' => $embed_args, |
||
91 | 'field_id' => $_REQUEST['field_id'], |
||
92 | ) ); |
||
93 | |||
94 | wp_send_json_success( $html ); |
||
95 | } |
||
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 | 2 | public function get_oembed( $args ) { |
|
104 | |||
105 | 2 | global $wp_embed; |
|
106 | |||
107 | 2 | $oembed_url = esc_url( $args['url'] ); |
|
108 | |||
109 | // Sanitize object_id |
||
110 | 2 | $this->object_id = is_numeric( $args['object_id'] ) ? absint( $args['object_id'] ) : sanitize_text_field( $args['object_id'] ); |
|
111 | |||
112 | 2 | $args = wp_parse_args( $args, array( |
|
113 | 2 | 'object_type' => 'post', |
|
114 | 2 | 'oembed_args' => $this->embed_args, |
|
115 | 2 | 'field_id' => false, |
|
116 | 2 | ) ); |
|
117 | |||
118 | 2 | $this->embed_args =& $args; |
|
119 | |||
120 | /** |
||
121 | * Set the post_ID so oEmbed won't fail |
||
122 | * wp-includes/class-wp-embed.php, WP_Embed::shortcode() |
||
123 | */ |
||
124 | 2 | $wp_embed->post_ID = $this->object_id; |
|
125 | |||
126 | // Special scenario if NOT a post object |
||
127 | 2 | if ( isset( $args['object_type'] ) && 'post' != $args['object_type'] ) { |
|
128 | |||
129 | 1 | if ( 'options-page' == $args['object_type'] ) { |
|
130 | |||
131 | // Bogus id to pass some numeric checks. Issue with a VERY large WP install? |
||
132 | 1 | $wp_embed->post_ID = 1987645321; |
|
133 | 1 | } |
|
134 | |||
135 | // Ok, we need to hijack the oembed cache system |
||
136 | 1 | $this->hijack = true; |
|
137 | 1 | $this->object_type = $args['object_type']; |
|
138 | |||
139 | // Gets ombed cache from our object's meta (vs postmeta) |
||
140 | 1 | add_filter( 'get_post_metadata', array( $this, 'hijack_oembed_cache_get' ), 10, 3 ); |
|
141 | |||
142 | // Sets ombed cache in our object's meta (vs postmeta) |
||
143 | 1 | add_filter( 'update_post_metadata', array( $this, 'hijack_oembed_cache_set' ), 10, 4 ); |
|
144 | |||
145 | 1 | } |
|
146 | |||
147 | 2 | $embed_args = ''; |
|
148 | |||
149 | 2 | foreach ( $args['oembed_args'] as $key => $val ) { |
|
150 | 2 | $embed_args .= " $key=\"$val\""; |
|
151 | 2 | } |
|
152 | |||
153 | // Ping WordPress for an embed |
||
154 | 2 | $check_embed = $wp_embed->run_shortcode( '[embed' . $embed_args . ']' . $oembed_url . '[/embed]' ); |
|
155 | |||
156 | // Fallback that WordPress creates when no oEmbed was found |
||
157 | 2 | $fallback = $wp_embed->maybe_make_link( $oembed_url ); |
|
158 | |||
159 | // Send back our embed |
||
160 | 2 | if ( $check_embed && $check_embed != $fallback ) { |
|
161 | 2 | return '<div class="embed-status">' . $check_embed . '<p class="cmb2-remove-wrapper"><a href="#" class="cmb2-remove-file-button" rel="' . $args['field_id'] . '">' . __( 'Remove Embed', 'cmb2' ) . '</a></p></div>'; |
|
162 | } |
||
163 | |||
164 | // Otherwise, send back error info that no oEmbeds were found |
||
165 | return '<p class="ui-state-error-text">' . sprintf( __( 'No oEmbed Results Found for %s. View more info at', 'cmb2' ), $fallback ) . ' <a href="http://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>.</p>'; |
||
166 | |||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Hijacks retrieving of cached oEmbed. |
||
171 | * Returns cached data from relevant object metadata (vs postmeta) |
||
172 | * |
||
173 | * @since 0.9.5 |
||
174 | * @param boolean $check Whether to retrieve postmeta or override |
||
175 | * @param int $object_id Object ID |
||
176 | * @param string $meta_key Object metakey |
||
177 | * @return mixed Object's oEmbed cached data |
||
178 | */ |
||
179 | 1 | public function hijack_oembed_cache_get( $check, $object_id, $meta_key ) { |
|
180 | 1 | if ( ! $this->hijack || ( $this->object_id != $object_id && 1987645321 !== $object_id ) ) { |
|
181 | return $check; |
||
182 | } |
||
183 | |||
184 | 1 | if ( $this->ajax_update ) { |
|
185 | return false; |
||
186 | } |
||
187 | |||
188 | 1 | return $this->cache_action( $meta_key ); |
|
189 | } |
||
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 | * @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 | * @param mixed $meta_value Value of the postmeta to be saved |
||
200 | * @return boolean Whether to continue setting |
||
201 | */ |
||
202 | 1 | public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_value ) { |
|
203 | |||
204 | if ( |
||
205 | 1 | ! $this->hijack |
|
206 | 1 | || ( $this->object_id != $object_id && 1987645321 !== $object_id ) |
|
207 | // only want to hijack oembed meta values |
||
208 | 1 | || 0 !== strpos( $meta_key, '_oembed_' ) |
|
209 | 1 | ) { |
|
210 | return $check; |
||
211 | } |
||
212 | |||
213 | 1 | $this->cache_action( $meta_key, $meta_value ); |
|
214 | |||
215 | // Anything other than `null` to cancel saving to postmeta |
||
216 | 1 | return true; |
|
217 | } |
||
218 | |||
219 | /** |
||
220 | * Gets/updates the cached oEmbed value from/to relevant object metadata (vs postmeta) |
||
221 | * |
||
222 | * @since 1.3.0 |
||
223 | * @param string $meta_key Postmeta's key |
||
224 | * @param mixed $meta_value (Optional) value of the postmeta to be saved |
||
225 | */ |
||
226 | 1 | protected function cache_action( $meta_key ) { |
|
252 | |||
253 | /** |
||
254 | * Hooks in when options-page data is saved to clean stale |
||
255 | * oembed cache data from the option value. |
||
256 | * @since 2.2.0 |
||
257 | * @param string $option_key The options-page option key |
||
258 | * @return void |
||
259 | */ |
||
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.