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 | && EE_Registry::instance()->REQ->get( 'step', '' ) !== '' |
||
| 106 | && ! ( |
||
| 107 | EE_Registry::instance()->REQ->get( 'step', '' ) === 'payment_options' |
||
| 108 | && (boolean) EE_Registry::instance()->REQ->get( 'revisit', false ) === true |
||
| 109 | ) |
||
| 110 | ) { |
||
| 111 | EED_Recaptcha::enqueue_styles_and_scripts(); |
||
| 112 | add_filter( 'FHEE__EED_Single_Page_Checkout__init___continue_reg', array( 'EED_Recaptcha', 'not_a_robot' ), 10 ); |
||
| 113 | add_filter( 'FHEE__EE_SPCO_Reg_Step__set_completed___completed', array( 'EED_Recaptcha', 'not_a_robot' ), 10 ); |
||
| 114 | add_filter( 'FHEE__EE_SPCO_JSON_Response___toString__JSON_response', array( 'EED_Recaptcha', 'recaptcha_response' ), 10, 1 ); |
||
| 115 | } |
||
| 116 | // admin settings |
||
| 117 | add_action( 'AHEE__Extend_Registration_Form_Admin_Page___reg_form_settings_template', array( 'EED_Recaptcha', 'admin_settings' ), 10, 1 ); |
||
| 118 | add_filter( 'FHEE__Extend_Registration_Form_Admin_Page___update_reg_form_settings__CFG_registration', array( 'EED_Recaptcha', 'update_admin_settings' ), 10, 1 ); |
||
| 119 | } |
||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * set_definitions |
||
| 125 | * |
||
| 126 | * @access public |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | public static function set_definitions() { |
||
| 130 | if ( is_user_logged_in() ) { |
||
| 131 | EED_Recaptcha::$_not_a_robot = true; |
||
| 132 | } |
||
| 133 | define( 'RECAPTCHA_BASE_PATH', rtrim( str_replace( array( '\\', '/' ), DS, plugin_dir_path( __FILE__ )), DS ) . DS ); |
||
| 134 | define( 'RECAPTCHA_BASE_URL', plugin_dir_url( __FILE__ )); |
||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * set_late_hooks |
||
| 141 | * |
||
| 142 | * @access public |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | public static function set_late_hooks() { |
||
| 146 | add_filter( |
||
| 147 | 'FHEE__Single_Page_Checkout__translate_js_strings__ajax_submit', |
||
| 148 | array( 'EED_Recaptcha', 'not_a_robot' ) |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * enqueue_styles_and_scripts |
||
| 156 | * |
||
| 157 | * @access public |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | public static function enqueue_styles_and_scripts() { |
||
| 161 | wp_register_script( 'espresso_recaptcha', RECAPTCHA_BASE_URL . 'scripts' . DS . 'espresso_recaptcha.js', array( 'single_page_checkout' ), EVENT_ESPRESSO_VERSION, TRUE ); |
||
| 162 | wp_register_script( 'google_recaptcha', 'https://www.google.com/recaptcha/api.js?hl=' . EE_Registry::instance()->CFG->registration->recaptcha_language, array( 'espresso_recaptcha' ), EVENT_ESPRESSO_VERSION, TRUE ); |
||
| 163 | EE_Registry::$i18n_js_strings['no_SPCO_error'] = __( 'It appears the Single Page Checkout javascript was not loaded properly! Please refresh the page and try again or contact support.', 'event_espresso' ); |
||
| 164 | EE_Registry::$i18n_js_strings['no_recaptcha_error'] = __( 'There appears to be a problem with the reCAPTCHA configuration! Please check the admin settings or contact support.', 'event_espresso' ); |
||
| 165 | EE_Registry::$i18n_js_strings['recaptcha_fail'] = __( 'Please complete the anti-spam test before proceeding.', 'event_espresso' ); |
||
| 166 | } |
||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * run |
||
| 172 | * |
||
| 173 | * @access public |
||
| 174 | * @param \WP $WP |
||
| 175 | */ |
||
| 176 | public function run( $WP ) { |
||
| 177 | } |
||
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * not_a_robot |
||
| 183 | * @return boolean |
||
| 184 | */ |
||
| 185 | public static function not_a_robot() { |
||
| 186 | $not_a_robot = is_bool( EED_Recaptcha::$_not_a_robot ) ? EED_Recaptcha::$_not_a_robot : |
||
| 187 | EED_Recaptcha::recaptcha_passed(); |
||
| 188 | return $not_a_robot; |
||
| 189 | } |
||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * display_recaptcha |
||
| 197 | * |
||
| 198 | * @access public |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | public static function display_recaptcha() { |
||
| 202 | // logged in means you have already passed a turing test of sorts |
||
| 203 | if ( is_user_logged_in() ) { |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | // don't display if not using recaptcha or user is logged in |
||
| 207 | if ( EE_Registry::instance()->CFG->registration->use_captcha ) { |
||
| 208 | // only display if they have NOT passed the test yet |
||
| 209 | if ( ! EED_Recaptcha::$_not_a_robot ) { |
||
| 210 | EEH_Template::display_template( |
||
| 211 | RECAPTCHA_BASE_PATH . DS . 'templates' . DS . 'recaptcha.template.php', |
||
| 212 | array( |
||
| 213 | 'recaptcha_publickey' => EE_Registry::instance()->CFG->registration->recaptcha_publickey, |
||
| 214 | 'recaptcha_theme' => EE_Registry::instance()->CFG->registration->recaptcha_theme, |
||
| 215 | 'recaptcha_type' => EE_Registry::instance()->CFG->registration->recaptcha_type |
||
| 216 | ) |
||
| 217 | ); |
||
| 218 | wp_enqueue_script( 'google_recaptcha' ); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * bypass_recaptcha_for_spco_load_payment_method |
||
| 227 | * |
||
| 228 | * @access public |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public static function bypass_recaptcha_for_spco_load_payment_method() { |
||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * recaptcha_passed |
||
| 243 | * |
||
| 244 | * @access public |
||
| 245 | * @return boolean |
||
| 246 | */ |
||
| 247 | public static function recaptcha_passed() { |
||
| 265 | |||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * recaptcha_response |
||
| 270 | * |
||
| 271 | * @access public |
||
| 272 | * @param array $recaptcha_response |
||
| 273 | * @return boolean |
||
| 274 | */ |
||
| 275 | public static function recaptcha_response( $recaptcha_response = array() ) { |
||
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * _bypass_recaptcha |
||
| 290 | * |
||
| 291 | * @access private |
||
| 292 | * @return boolean |
||
| 293 | */ |
||
| 294 | private static function _bypass_recaptcha() { |
||
| 309 | |||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * process_recaptcha |
||
| 315 | * |
||
| 316 | * @access private |
||
| 317 | * @return boolean |
||
| 318 | */ |
||
| 319 | private static function _get_recaptcha_response() { |
||
| 322 | |||
| 323 | |||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * process_recaptcha |
||
| 328 | * |
||
| 329 | * @access private |
||
| 330 | * @return boolean |
||
| 331 | */ |
||
| 332 | private static function _process_recaptcha_response() { |
||
| 359 | |||
| 360 | |||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | /*************************************** reCAPTCHA ADMIN SETTINGS ***************************************/ |
||
| 365 | |||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * admin_settings |
||
| 370 | * |
||
| 371 | * @access public |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | public static function admin_settings() { |
||
| 377 | |||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * _recaptcha_main_settings |
||
| 382 | * |
||
| 383 | * @access protected |
||
| 384 | * @return EE_Form_Section_Proper |
||
| 385 | */ |
||
| 386 | protected static function _recaptcha_settings_form() { |
||
| 408 | |||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * _recaptcha_main_settings |
||
| 413 | * |
||
| 414 | * @access protected |
||
| 415 | * @return EE_Form_Section_Proper |
||
| 416 | */ |
||
| 417 | protected static function _recaptcha_main_settings() { |
||
| 458 | |||
| 459 | |||
| 460 | |||
| 461 | |||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * _recaptcha_appearance_settings |
||
| 466 | * |
||
| 467 | * @access protected |
||
| 468 | * @return EE_Form_Section_Proper |
||
| 469 | */ |
||
| 470 | protected static function _recaptcha_appearance_settings() { |
||
| 564 | |||
| 565 | |||
| 566 | |||
| 567 | |||
| 568 | |||
| 569 | /** |
||
| 570 | * _recaptcha_example |
||
| 571 | * |
||
| 572 | * @access protected |
||
| 573 | * @return EE_Form_Section_Proper |
||
| 574 | */ |
||
| 575 | protected static function _recaptcha_example() { |
||
| 595 | |||
| 596 | |||
| 597 | /** |
||
| 598 | * admin_settings_template |
||
| 599 | * |
||
| 600 | * @access public |
||
| 601 | * @param EE_Registration_Config $EE_Registration_Config |
||
| 602 | * @return array |
||
| 603 | */ |
||
| 604 | public static function update_admin_settings( EE_Registration_Config $EE_Registration_Config ) { |
||
| 644 | |||
| 645 | |||
| 646 | } |
||
| 647 | // End of file EED_Recaptcha.module.php |
||
| 649 |
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 theSoncalls the wrong method in the parent class.