Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EED_Messages 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_Messages, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class EED_Messages extends EED_Module { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * This holds the EE_messages controller |
||
| 26 | * @deprecated 4.9.0 |
||
| 27 | * @var EE_messages $_EEMSG |
||
| 28 | */ |
||
| 29 | protected static $_EEMSG; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @type EE_Message_Resource_Manager $_message_resource_manager |
||
| 33 | */ |
||
| 34 | protected static $_message_resource_manager; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * This holds the EE_Messages_Processor business class. |
||
| 38 | * |
||
| 39 | * @type EE_Messages_Processor |
||
| 40 | */ |
||
| 41 | protected static $_MSG_PROCESSOR; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * holds all the paths for various messages components. |
||
| 45 | * Utilized by autoloader registry |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected static $_MSG_PATHS; |
||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * This will hold an array of messages template packs that are registered in the messages system. |
||
| 55 | * Format is: |
||
| 56 | * array( |
||
| 57 | * 'template_pack_dbref' => EE_Messages_Template_Pack (instance) |
||
| 58 | * ) |
||
| 59 | * |
||
| 60 | * @var EE_Messages_Template_Pack[] |
||
| 61 | */ |
||
| 62 | protected static $_TMP_PACKS = array(); |
||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * @return EED_Module |
||
| 70 | */ |
||
| 71 | public static function instance() { |
||
| 72 | return parent::get_instance( __CLASS__ ); |
||
|
|
|||
| 73 | } |
||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * set_hooks - for hooking into EE Core, other modules, etc |
||
| 80 | * |
||
| 81 | * @since 4.5.0 |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | public static function set_hooks() { |
||
| 86 | //actions |
||
| 87 | add_action( 'AHEE__EE_Payment_Processor__update_txn_based_on_payment', array( 'EED_Messages', 'payment' ), 10, 2 ); |
||
| 88 | add_action( 'AHEE__EE_Registration_Processor__trigger_registration_update_notifications', array( 'EED_Messages', 'maybe_registration' ), 10, 2 ); |
||
| 89 | //filters |
||
| 90 | add_filter( 'FHEE__EE_Registration__receipt_url__receipt_url', array( 'EED_Messages', 'registration_message_trigger_url' ), 10, 4 ); |
||
| 91 | add_filter( 'FHEE__EE_Registration__invoice_url__invoice_url', array( 'EED_Messages', 'registration_message_trigger_url' ), 10, 4 ); |
||
| 92 | //register routes |
||
| 93 | self::_register_routes(); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
||
| 98 | * |
||
| 99 | * @access public |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | public static function set_hooks_admin() { |
||
| 103 | //actions |
||
| 104 | add_action( 'AHEE__EE_Payment_Processor__update_txn_based_on_payment', array( 'EED_Messages', 'payment' ), 10, 2 ); |
||
| 105 | add_action( 'AHEE__Transactions_Admin_Page___send_payment_reminder__process_admin_payment_reminder', array( 'EED_Messages', 'payment_reminder' ), 10 ); |
||
| 106 | add_action( 'AHEE__EE_Registration_Processor__trigger_registration_update_notifications', array( 'EED_Messages', 'maybe_registration' ), 10, 3 ); |
||
| 107 | add_action( 'AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send__with_registrations', array( 'EED_Messages', 'send_newsletter_message' ), 10, 2 ); |
||
| 108 | add_action( 'AHEE__EES_Espresso_Cancelled__process_shortcode__transaction', array( 'EED_Messages', 'cancelled_registration' ), 10 ); |
||
| 109 | add_action( 'AHEE__EE_Admin_Page___process_admin_payment_notification', array( 'EED_Messages', 'process_admin_payment' ), 10, 1 ); |
||
| 110 | //filters |
||
| 111 | add_filter( 'FHEE__EE_Admin_Page___process_resend_registration__success', array( 'EED_Messages', 'process_resend' ), 10, 2 ); |
||
| 112 | add_filter( 'FHEE__EE_Registration__receipt_url__receipt_url', array( 'EED_Messages', 'registration_message_trigger_url' ), 10, 4 ); |
||
| 113 | add_filter( 'FHEE__EE_Registration__invoice_url__invoice_url', array( 'EED_Messages', 'registration_message_trigger_url' ), 10, 4 ); |
||
| 114 | } |
||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * All the message triggers done by route go in here. |
||
| 121 | * |
||
| 122 | * @since 4.5.0 |
||
| 123 | * |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | protected static function _register_routes() { |
||
| 127 | EE_Config::register_route( 'msg_url_trigger', 'Messages', 'run' ); |
||
| 128 | EE_Config::register_route( 'msg_cron_trigger', 'Messages', 'run_cron' ); |
||
| 129 | EE_Config::register_route( 'msg_browser_trigger', 'Messages', 'browser_trigger' ); |
||
| 130 | EE_Config::register_route( 'msg_browser_error_trigger', 'Messages', 'browser_error_trigger' ); |
||
| 131 | do_action( 'AHEE__EED_Messages___register_routes' ); |
||
| 132 | } |
||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * This is called when a browser display trigger is executed. |
||
| 138 | * The browser display trigger is typically used when a already generated message is displayed directly in the browser. |
||
| 139 | * @since 4.9.0 |
||
| 140 | * @param WP $WP |
||
| 141 | */ |
||
| 142 | public function browser_trigger( $WP ) { |
||
| 143 | //ensure controller is loaded |
||
| 144 | self::_load_controller(); |
||
| 145 | $token = EE_Registry::instance()->REQ->get( 'token' ); |
||
| 146 | try { |
||
| 147 | $mtg = new EE_Message_Generated_From_Token( $token, 'html', self::$_message_resource_manager ); |
||
| 148 | self::$_MSG_PROCESSOR->generate_and_send_now( $mtg ); |
||
| 149 | } catch( EE_Error $e ) { |
||
| 150 | $error_msg = __( 'Please note that a system message failed to send due to a technical issue.', 'event_espresso' ); |
||
| 151 | // add specific message for developers if WP_DEBUG in on |
||
| 152 | $error_msg .= '||' . $e->getMessage(); |
||
| 153 | EE_Error::add_error( $error_msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * This is called when a browser error trigger is executed. |
||
| 163 | * When triggered this will grab the EE_Message matching the token in the request and use that to get the error message |
||
| 164 | * and display it. |
||
| 165 | * |
||
| 166 | * @since 4.9.0 |
||
| 167 | * @param $WP |
||
| 168 | */ |
||
| 169 | public function browser_error_trigger( $WP ) { |
||
| 170 | $token = EE_Registry::instance()->REQ->get( 'token' ); |
||
| 171 | if ( $token ) { |
||
| 172 | $message = EEM_Message::instance()->get_one_by_token( $token ); |
||
| 173 | if ( $message instanceof EE_Message ) { |
||
| 174 | header( 'HTTP/1.1 200 OK' ); |
||
| 175 | $error_msg = nl2br( $message->error_message() ); |
||
| 176 | ?> |
||
| 177 | <!DOCTYPE html> |
||
| 178 | <html> |
||
| 179 | <head></head> |
||
| 180 | <body> |
||
| 181 | <?php echo empty( $error_msg ) |
||
| 182 | ? esc_html__( 'Unfortunately, we were unable to capture the error message for this message.', 'event_espresso' ) |
||
| 183 | : wp_kses( |
||
| 184 | $error_msg, |
||
| 185 | array( |
||
| 186 | 'a' => array( |
||
| 187 | 'href' => array(), |
||
| 188 | 'title' => array() |
||
| 189 | ), |
||
| 190 | 'span' => array(), |
||
| 191 | 'div' => array(), |
||
| 192 | 'p' => array(), |
||
| 193 | 'strong' => array(), |
||
| 194 | 'em' => array(), |
||
| 195 | 'br' => array() |
||
| 196 | ) |
||
| 197 | ); ?> |
||
| 198 | </body> |
||
| 199 | </html> |
||
| 200 | <?php |
||
| 201 | exit; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * This runs when the msg_url_trigger route has initiated. |
||
| 211 | * |
||
| 212 | * @since 4.5.0 |
||
| 213 | * @param WP $WP |
||
| 214 | * @throws EE_Error |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | public function run( $WP ) { |
||
| 218 | //ensure controller is loaded |
||
| 219 | self::_load_controller(); |
||
| 220 | // attempt to process message |
||
| 221 | try { |
||
| 222 | /** @type EE_Message_To_Generate_From_Request $message_to_generate */ |
||
| 223 | $message_to_generate = EE_Registry::instance()->load_lib( 'Message_To_Generate_From_Request' ); |
||
| 224 | self::$_MSG_PROCESSOR->generate_and_send_now( $message_to_generate ); |
||
| 225 | } catch ( EE_Error $e ) { |
||
| 226 | $error_msg = __( 'Please note that a system message failed to send due to a technical issue.', 'event_espresso' ); |
||
| 227 | // add specific message for developers if WP_DEBUG in on |
||
| 228 | $error_msg .= '||' . $e->getMessage(); |
||
| 229 | EE_Error::add_error( $error_msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * This is triggered by the 'msg_cron_trigger' route. |
||
| 236 | * @param WP $WP |
||
| 237 | */ |
||
| 238 | public function run_cron( $WP ) { |
||
| 268 | |||
| 269 | |||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * This is used to retrieve the template pack for the given name. |
||
| 274 | * Retrieved packs are cached on the static $_TMP_PACKS array. If there is no class matching the given name then the default template pack is returned. |
||
| 275 | * |
||
| 276 | * @deprecated 4.9.0 @see EEH_MSG_Template::get_template_pack() |
||
| 277 | * |
||
| 278 | * @param string $template_pack_name This should correspond to the dbref of the template pack (which is also used in generating the Pack class name). |
||
| 279 | * |
||
| 280 | * @return EE_Messages_Template_Pack |
||
| 281 | */ |
||
| 282 | public static function get_template_pack( $template_pack_name ) { |
||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Retrieves an array of all template packs. |
||
| 292 | * Array is in the format array( 'dbref' => EE_Messages_Template_Pack ) |
||
| 293 | * @deprecated 4.9.0 @see EEH_MSG_Template_Pack::get_template_pack_collection |
||
| 294 | * |
||
| 295 | * @return EE_Messages_Template_Pack[] |
||
| 296 | */ |
||
| 297 | public static function get_template_packs() { |
||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * This simply makes sure the autoloaders are registered for the EE_messages system. |
||
| 315 | * |
||
| 316 | * @since 4.5.0 |
||
| 317 | * |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | public static function set_autoloaders() { |
||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * Take care of adding all the paths for the messages components to the $_MSG_PATHS property |
||
| 337 | * for use by the Messages Autoloaders |
||
| 338 | * |
||
| 339 | * @since 4.5.0 |
||
| 340 | * |
||
| 341 | * @return void. |
||
| 342 | */ |
||
| 343 | protected static function _set_messages_paths() { |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * Takes care of loading dependencies |
||
| 365 | * |
||
| 366 | * @since 4.5.0 |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | protected static function _load_controller() { |
||
| 378 | |||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * @param EE_Transaction $transaction |
||
| 383 | */ |
||
| 384 | public static function payment_reminder( EE_Transaction $transaction ) { |
||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * Any messages triggers for after successful gateway payments should go in here. |
||
| 394 | * @param EE_Transaction object |
||
| 395 | * @param EE_Payment object |
||
| 396 | * @return void |
||
| 397 | */ |
||
| 398 | public static function payment( EE_Transaction $transaction, EE_Payment $payment ) { |
||
| 407 | |||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * @param EE_Transaction $transaction |
||
| 412 | */ |
||
| 413 | public static function cancelled_registration( EE_Transaction $transaction ) { |
||
| 418 | |||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * Trigger for Registration messages |
||
| 423 | * Note that what registration message type is sent depends on what the reg status is for the registrations on the incoming transaction. |
||
| 424 | * |
||
| 425 | * @param EE_Registration $registration |
||
| 426 | * @param array $extra_details |
||
| 427 | * @return void |
||
| 428 | */ |
||
| 429 | public static function maybe_registration( EE_Registration $registration, $extra_details = array() ) { |
||
| 464 | |||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * This is a helper method used to very whether a registration notification should be sent or |
||
| 469 | * not. Prevents duplicate notifications going out for registration context notifications. |
||
| 470 | * |
||
| 471 | * @param EE_Registration $registration [description] |
||
| 472 | * @param array $extra_details [description] |
||
| 473 | * |
||
| 474 | * @return bool true = send away, false = nope halt the presses. |
||
| 475 | */ |
||
| 476 | protected static function _verify_registration_notification_send( EE_Registration $registration, $extra_details = array() ) { |
||
| 517 | |||
| 518 | |||
| 519 | |||
| 520 | /** |
||
| 521 | * Simply returns an array indexed by Registration Status ID and the related message_type name associated with that status id. |
||
| 522 | * |
||
| 523 | * @deprecated 4.9.0 Use EEH_MSG_Template::reg_status_to_message_type_array() |
||
| 524 | * or EEH_MSG_Template::convert_reg_status_to_message_type |
||
| 525 | * |
||
| 526 | * @param string $reg_status |
||
| 527 | * |
||
| 528 | * @return array |
||
| 529 | */ |
||
| 530 | protected static function _get_reg_status_array( $reg_status = '' ) { |
||
| 536 | |||
| 537 | |||
| 538 | |||
| 539 | /** |
||
| 540 | * Simply returns the payment message type for the given payment status. |
||
| 541 | * |
||
| 542 | * @deprecated 4.9.0 Use EEH_MSG_Template::payment_status_to_message_type_array |
||
| 543 | * or EEH_MSG_Template::convert_payment_status_to_message_type |
||
| 544 | * |
||
| 545 | * @param string $payment_status The payment status being matched. |
||
| 546 | * |
||
| 547 | * @return string|bool The payment message type slug matching the status or false if no match. |
||
| 548 | */ |
||
| 549 | protected static function _get_payment_message_type( $payment_status ) { |
||
| 555 | |||
| 556 | |||
| 557 | |||
| 558 | |||
| 559 | /** |
||
| 560 | * Message triggers for a resending already sent message(s) (via EE_Message list table) |
||
| 561 | * |
||
| 562 | * @access public |
||
| 563 | * @param array $req_data This is the $_POST & $_GET data sent from EE_Admin Pages |
||
| 564 | * @return bool success/fail |
||
| 565 | */ |
||
| 566 | public static function process_resend( $req_data ) { |
||
| 596 | |||
| 597 | |||
| 598 | /** |
||
| 599 | * Message triggers for a resending already sent message(s) (via EE_Message list table) |
||
| 600 | * @return bool |
||
| 601 | */ |
||
| 602 | public static function resend_message() { |
||
| 626 | |||
| 627 | |||
| 628 | |||
| 629 | |||
| 630 | |||
| 631 | /** |
||
| 632 | * Message triggers for manual payment applied by admin |
||
| 633 | * @param EE_Payment $payment EE_payment object |
||
| 634 | * @return bool success/fail |
||
| 635 | */ |
||
| 636 | public static function process_admin_payment( EE_Payment $payment ) { |
||
| 681 | |||
| 682 | |||
| 683 | |||
| 684 | /** |
||
| 685 | * Callback for AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send_with_registrations trigger |
||
| 686 | * |
||
| 687 | * @since 4.3.0 |
||
| 688 | * |
||
| 689 | * @param EE_Registration[] $registrations an array of EE_Registration objects |
||
| 690 | * @param int $grp_id a specific message template group id. |
||
| 691 | * @return void |
||
| 692 | */ |
||
| 693 | public static function send_newsletter_message( $registrations, $grp_id ) { |
||
| 699 | |||
| 700 | |||
| 701 | /** |
||
| 702 | * Callback for FHEE__EE_Registration__invoice_url__invoice_url or FHEE__EE_Registration__receipt_url__receipt_url |
||
| 703 | * |
||
| 704 | * @since 4.3.0 |
||
| 705 | * |
||
| 706 | * @param string $registration_message_trigger_url |
||
| 707 | * @param EE_Registration $registration |
||
| 708 | * @param string $messenger |
||
| 709 | * @param string $message_type |
||
| 710 | * @return string |
||
| 711 | */ |
||
| 712 | public static function registration_message_trigger_url( $registration_message_trigger_url, EE_Registration $registration, $messenger = 'html', $message_type = 'invoice' ) { |
||
| 771 | |||
| 772 | |||
| 773 | |||
| 774 | |||
| 775 | /** |
||
| 776 | * Use to generate and return a message preview! |
||
| 777 | * @param string $type This should correspond with a valid message type |
||
| 778 | * @param string $context This should correspond with a valid context for the message type |
||
| 779 | * @param string $messenger This should correspond with a valid messenger. |
||
| 780 | * @param bool $send true we will do a test send using the messenger delivery, false we just do a regular preview |
||
| 781 | * @return string The body of the message. |
||
| 782 | */ |
||
| 783 | public static function preview_message( $type, $context, $messenger, $send = false ) { |
||
| 799 | |||
| 800 | |||
| 801 | |||
| 802 | |||
| 803 | /** |
||
| 804 | * This is a method that allows for sending a message using a messenger matching the string given and the provided |
||
| 805 | * EE_Message_Queue object. The EE_Message_Queue object is used to create a single aggregate EE_Message via the content |
||
| 806 | * found in the EE_Message objects in the queue. |
||
| 807 | * |
||
| 808 | * @since 4.9.0 |
||
| 809 | * |
||
| 810 | * @param string $messenger a string matching a valid active messenger in the system |
||
| 811 | * @param string $message_type Although it seems contrary to the name of the method, a message type name is |
||
| 812 | * still required to send along the message type to the messenger because this is used |
||
| 813 | * for determining what specific variations might be loaded for the generated message. |
||
| 814 | * @param EE_Messages_Queue $queue |
||
| 815 | * @param string $custom_subject Can be used to set what the custom subject string will be on the aggregate |
||
| 816 | * EE_Message object. |
||
| 817 | * |
||
| 818 | * @return bool success or fail. |
||
| 819 | */ |
||
| 820 | public static function send_message_with_messenger_only( $messenger, $message_type, EE_Messages_Queue $queue, $custom_subject = '' ) { |
||
| 834 | |||
| 835 | |||
| 836 | |||
| 837 | |||
| 838 | /** |
||
| 839 | * Generates Messages immediately for EE_Message IDs (but only for the correct status for generation) |
||
| 840 | * |
||
| 841 | * @since 4.9.0 |
||
| 842 | * @param array $message_ids An array of message ids |
||
| 843 | * @return bool | EE_Messages_Queue false if nothing was generated, EE_Messages_Queue containing generated messages. |
||
| 844 | */ |
||
| 845 | public static function generate_now( $message_ids ) { |
||
| 866 | |||
| 867 | |||
| 868 | |||
| 869 | |||
| 870 | /** |
||
| 871 | * Sends messages immediately for the incoming message_ids that have the status of EEM_Message::status_resend or, |
||
| 872 | * EEM_Message::status_idle |
||
| 873 | * |
||
| 874 | * @since 4.9.0 |
||
| 875 | * @param $message_ids |
||
| 876 | * |
||
| 877 | * @return bool | EE_Messages_Queue false if no messages sent. |
||
| 878 | */ |
||
| 879 | public static function send_now( $message_ids ) { |
||
| 923 | |||
| 924 | |||
| 925 | |||
| 926 | |||
| 927 | |||
| 928 | /** |
||
| 929 | * This will queue the incoming message ids for resending. |
||
| 930 | * Note, only message_ids corresponding to messages with the status of EEM_Message::sent will be queued. |
||
| 931 | * |
||
| 932 | * @since 4.9.0 |
||
| 933 | * @param array $message_ids An array of EE_Message IDs |
||
| 934 | * |
||
| 935 | * @return bool true means messages were successfully queued for resending, false means none were queued for resending. |
||
| 936 | */ |
||
| 937 | public static function queue_for_resending( $message_ids ) { |
||
| 963 | |||
| 964 | |||
| 965 | |||
| 966 | |||
| 967 | |||
| 968 | |||
| 969 | /** |
||
| 970 | * debug |
||
| 971 | * |
||
| 972 | * @param string $class |
||
| 973 | * @param string $func |
||
| 974 | * @param string $line |
||
| 975 | * @param \EE_Transaction $transaction |
||
| 976 | * @param array $info |
||
| 977 | * @param bool $display_request |
||
| 978 | */ |
||
| 979 | View Code Duplication | protected static function log( $class = '', $func = '', $line = '', EE_Transaction $transaction, $info = array(), $display_request = false ) { |
|
| 994 | |||
| 995 | } |
||
| 996 | // End of file EED_Messages.module.php |
||
| 998 |
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.