Complex classes like EED_Recaptcha 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 EED_Recaptcha, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
24 | class EED_Recaptcha extends EED_Module { |
||
25 | |||
26 | |||
27 | |||
28 | /** |
||
29 | * @type bool $_not_a_robot |
||
30 | */ |
||
31 | private static $_not_a_robot; |
||
32 | |||
33 | |||
34 | |||
35 | /** |
||
36 | * @type string $_recaptcha_response |
||
37 | */ |
||
38 | private static $_recaptcha_response; |
||
39 | |||
40 | |||
41 | |||
42 | /** |
||
43 | * @return EED_Recaptcha |
||
44 | */ |
||
45 | public static function instance() { |
||
48 | |||
49 | |||
50 | |||
51 | /** |
||
52 | * set_hooks - for hooking into EE Core, other modules, etc |
||
53 | * |
||
54 | * @access public |
||
55 | * @return void |
||
56 | */ |
||
57 | public static function set_hooks() { |
||
58 | // use_captcha ? |
||
59 | if ( |
||
60 | EE_Registry::instance()->CFG->registration->use_captcha |
||
61 | && ! ( |
||
62 | EE_Registry::instance()->REQ->get( 'step', '' ) === 'payment_options' |
||
63 | && (boolean) EE_Registry::instance()->REQ->get( 'revisit', false ) === true |
||
64 | ) |
||
65 | ) { |
||
66 | EED_Recaptcha::set_definitions(); |
||
67 | EED_Recaptcha::enqueue_styles_and_scripts(); |
||
68 | add_action( 'wp', array( 'EED_Recaptcha', 'set_late_hooks' ), 1, 0 ); |
||
69 | add_action( |
||
70 | 'AHEE__before_spco_whats_next_buttons', |
||
71 | array( 'EED_Recaptcha', 'display_recaptcha' ), 10, 0 |
||
72 | ); |
||
73 | add_filter( |
||
74 | 'FHEE__EED_Single_Page_Checkout__init___continue_reg', |
||
75 | array( 'EED_Recaptcha', 'not_a_robot' ), 10 |
||
76 | ); |
||
77 | add_filter( |
||
78 | 'FHEE__EE_SPCO_Reg_Step__set_completed___completed', |
||
79 | array( 'EED_Recaptcha', 'not_a_robot' ), 10 |
||
80 | ); |
||
81 | add_filter( |
||
82 | 'FHEE__EE_SPCO_JSON_Response___toString__JSON_response', |
||
83 | array( 'EED_Recaptcha', 'recaptcha_response' ), 10, 1 |
||
84 | ); |
||
85 | add_filter( |
||
86 | 'FHEE__EED_Recaptcha___bypass_recaptcha__bypass_request_params_array', |
||
87 | array( 'EED_Recaptcha', 'bypass_recaptcha_for_spco_load_payment_method' ), 10, 1 |
||
88 | ); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | |||
93 | |||
94 | /** |
||
95 | * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
||
96 | * |
||
97 | * @access public |
||
98 | * @return void |
||
99 | */ |
||
100 | public static function set_hooks_admin() { |
||
101 | EED_Recaptcha::set_definitions(); |
||
102 | // use_captcha ? |
||
103 | if ( |
||
104 | EE_Registry::instance()->CFG->registration->use_captcha |
||
105 | && ! ( |
||
106 | EE_Registry::instance()->REQ->get( 'step', '' ) === 'payment_options' |
||
107 | && (boolean) EE_Registry::instance()->REQ->get( 'revisit', false ) === true |
||
108 | ) |
||
109 | ) { |
||
110 | EED_Recaptcha::enqueue_styles_and_scripts(); |
||
111 | add_filter( 'FHEE__EED_Single_Page_Checkout__init___continue_reg', array( 'EED_Recaptcha', 'not_a_robot' ), 10 ); |
||
112 | add_filter( 'FHEE__EE_SPCO_Reg_Step__set_completed___completed', array( 'EED_Recaptcha', 'not_a_robot' ), 10 ); |
||
113 | add_filter( 'FHEE__EE_SPCO_JSON_Response___toString__JSON_response', array( 'EED_Recaptcha', 'recaptcha_response' ), 10, 1 ); |
||
114 | } |
||
115 | // admin settings |
||
116 | add_action( 'AHEE__Extend_Registration_Form_Admin_Page___reg_form_settings_template', array( 'EED_Recaptcha', 'admin_settings' ), 10, 1 ); |
||
117 | add_filter( 'FHEE__Extend_Registration_Form_Admin_Page___update_reg_form_settings__CFG_registration', array( 'EED_Recaptcha', 'update_admin_settings' ), 10, 1 ); |
||
118 | } |
||
119 | |||
120 | |||
121 | |||
122 | /** |
||
123 | * set_definitions |
||
124 | * |
||
125 | * @access public |
||
126 | * @return void |
||
127 | */ |
||
128 | public static function set_definitions() { |
||
135 | |||
136 | |||
137 | |||
138 | /** |
||
139 | * set_late_hooks |
||
140 | * |
||
141 | * @access public |
||
142 | * @return void |
||
143 | */ |
||
144 | public static function set_late_hooks() { |
||
150 | |||
151 | |||
152 | |||
153 | /** |
||
154 | * enqueue_styles_and_scripts |
||
155 | * |
||
156 | * @access public |
||
157 | * @return void |
||
158 | */ |
||
159 | public static function enqueue_styles_and_scripts() { |
||
166 | |||
167 | |||
168 | |||
169 | /** |
||
170 | * run |
||
171 | * |
||
172 | * @access public |
||
173 | * @param \WP $WP |
||
174 | */ |
||
175 | public function run( $WP ) { |
||
177 | |||
178 | |||
179 | |||
180 | /** |
||
181 | * not_a_robot |
||
182 | * @return boolean |
||
183 | */ |
||
184 | public static function not_a_robot() { |
||
189 | |||
190 | |||
191 | |||
192 | |||
193 | |||
194 | /** |
||
195 | * display_recaptcha |
||
196 | * |
||
197 | * @access public |
||
198 | * @return void |
||
199 | */ |
||
200 | public static function display_recaptcha() { |
||
201 | // logged in means you have already passed a turing test of sorts |
||
202 | if ( is_user_logged_in() ) { |
||
203 | return; |
||
204 | } |
||
205 | // don't display if not using recaptcha or user is logged in |
||
206 | if ( EE_Registry::instance()->CFG->registration->use_captcha ) { |
||
207 | // only display if they have NOT passed the test yet |
||
208 | if ( ! EED_Recaptcha::$_not_a_robot ) { |
||
209 | EEH_Template::display_template( |
||
210 | RECAPTCHA_BASE_PATH . DS . 'templates' . DS . 'recaptcha.template.php', |
||
211 | array( |
||
212 | 'recaptcha_publickey' => EE_Registry::instance()->CFG->registration->recaptcha_publickey, |
||
213 | 'recaptcha_theme' => EE_Registry::instance()->CFG->registration->recaptcha_theme, |
||
214 | 'recaptcha_type' => EE_Registry::instance()->CFG->registration->recaptcha_type |
||
215 | ) |
||
216 | ); |
||
217 | wp_enqueue_script( 'google_recaptcha' ); |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | |||
222 | |||
223 | |||
224 | /** |
||
225 | * bypass_recaptcha_for_spco_load_payment_method |
||
226 | * |
||
227 | * @access public |
||
228 | * @return string |
||
229 | */ |
||
230 | public static function bypass_recaptcha_for_spco_load_payment_method() { |
||
237 | |||
238 | |||
239 | |||
240 | /** |
||
241 | * recaptcha_passed |
||
242 | * |
||
243 | * @access public |
||
244 | * @return boolean |
||
245 | */ |
||
246 | public static function recaptcha_passed() { |
||
264 | |||
265 | |||
266 | |||
267 | /** |
||
268 | * recaptcha_response |
||
269 | * |
||
270 | * @access public |
||
271 | * @param array $recaptcha_response |
||
272 | * @return boolean |
||
273 | */ |
||
274 | public static function recaptcha_response( $recaptcha_response = array() ) { |
||
283 | |||
284 | |||
285 | |||
286 | |||
287 | /** |
||
288 | * _bypass_recaptcha |
||
289 | * |
||
290 | * @access private |
||
291 | * @return boolean |
||
292 | */ |
||
293 | private static function _bypass_recaptcha() { |
||
308 | |||
309 | |||
310 | |||
311 | |||
312 | /** |
||
313 | * process_recaptcha |
||
314 | * |
||
315 | * @access private |
||
316 | * @return boolean |
||
317 | */ |
||
318 | private static function _get_recaptcha_response() { |
||
321 | |||
322 | |||
323 | |||
324 | |||
325 | /** |
||
326 | * process_recaptcha |
||
327 | * |
||
328 | * @access private |
||
329 | * @return boolean |
||
330 | */ |
||
331 | private static function _process_recaptcha_response() { |
||
358 | |||
359 | |||
360 | |||
361 | |||
362 | |||
363 | /*************************************** reCAPTCHA ADMIN SETTINGS ***************************************/ |
||
364 | |||
365 | |||
366 | |||
367 | /** |
||
368 | * admin_settings |
||
369 | * |
||
370 | * @access public |
||
371 | * @return array |
||
372 | */ |
||
373 | public static function admin_settings() { |
||
376 | |||
377 | |||
378 | |||
379 | /** |
||
380 | * _recaptcha_main_settings |
||
381 | * |
||
382 | * @access protected |
||
383 | * @return EE_Form_Section_Proper |
||
384 | */ |
||
385 | protected static function _recaptcha_settings_form() { |
||
386 | |||
387 | |||
388 | return new EE_Form_Section_Proper( |
||
389 | array( |
||
390 | 'name' => 'recaptcha_settings_form', |
||
391 | 'html_id' => 'recaptcha_settings_form', |
||
392 | 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
||
393 | 'subsections' => apply_filters( |
||
394 | 'FHEE__EED_Recaptcha___recaptcha_settings_form__form_subsections', |
||
395 | array( |
||
396 | 'main_settings_hdr' => new EE_Form_Section_HTML( EEH_HTML::h2( __( 'reCAPTCHA Anti-spam Settings', 'event_espresso' ) . EEH_Template::get_help_tab_link( 'recaptcha_info' ))), |
||
397 | 'main_settings' => EED_Recaptcha::_recaptcha_main_settings(), |
||
398 | 'appearance_settings_hdr' => new EE_Form_Section_HTML( EEH_HTML::h2( __( 'reCAPTCHA Appearance', 'event_espresso' ) )), |
||
399 | 'appearance_settings' => EED_Recaptcha::_recaptcha_appearance_settings(), |
||
400 | // 'recaptcha_example' => new EE_Form_Section_HTML( EED_Recaptcha::display_recaptcha() ), |
||
401 | 'required_fields_note' => new EE_Form_Section_HTML( EEH_HTML::p( __( 'All fields marked with a * are required fields', 'event_espresso' ), '', 'grey-text' )) |
||
402 | ) |
||
403 | ) |
||
404 | ) |
||
405 | ); |
||
406 | } |
||
407 | |||
408 | |||
409 | |||
410 | /** |
||
411 | * _recaptcha_main_settings |
||
412 | * |
||
413 | * @access protected |
||
414 | * @return EE_Form_Section_Proper |
||
415 | */ |
||
416 | protected static function _recaptcha_main_settings() { |
||
457 | |||
458 | |||
459 | |||
460 | |||
461 | |||
462 | |||
463 | /** |
||
464 | * _recaptcha_appearance_settings |
||
465 | * |
||
466 | * @access protected |
||
467 | * @return EE_Form_Section_Proper |
||
468 | */ |
||
469 | protected static function _recaptcha_appearance_settings() { |
||
563 | |||
564 | |||
565 | |||
566 | |||
567 | |||
568 | /** |
||
569 | * _recaptcha_example |
||
570 | * |
||
571 | * @access protected |
||
572 | * @return EE_Form_Section_Proper |
||
573 | */ |
||
574 | protected static function _recaptcha_example() { |
||
594 | |||
595 | |||
596 | /** |
||
597 | * admin_settings_template |
||
598 | * |
||
599 | * @access public |
||
600 | * @param EE_Registration_Config $EE_Registration_Config |
||
601 | * @return array |
||
602 | */ |
||
603 | public static function update_admin_settings( EE_Registration_Config $EE_Registration_Config ) { |
||
643 | |||
644 | |||
645 | } |
||
646 | // End of file EED_Recaptcha.module.php |
||
647 | // Location: /modules/recaptcha/EED_Recaptcha.module.php |
||
648 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.