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() { |
||
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 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 ) { |
||
| 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 ) { |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * This is triggered by the 'msg_cron_trigger' route. |
||
| 236 | * @param WP $WP |
||
| 237 | */ |
||
| 238 | public function run_cron( $WP ) { |
||
| 239 | self::_load_controller(); |
||
| 240 | //get required vars |
||
| 241 | $cron_type = EE_Registry::instance()->REQ->get( 'type' ); |
||
| 242 | $transient_key = EE_Registry::instance()->REQ->get( 'key' ); |
||
| 243 | header( 'HTTP/1.1 200 OK' ); |
||
| 244 | |||
| 245 | //now let's verify transient, if not valid exit immediately |
||
| 246 | if ( ! get_transient( $transient_key ) ) { |
||
| 247 | /** |
||
| 248 | * trigger error so this gets in the error logs. This is important because it happens on a non-user request. |
||
| 249 | */ |
||
| 250 | trigger_error( esc_attr__( 'Invalid Request (Transient does not exist)', 'event_espresso' ) ); |
||
| 251 | } |
||
| 252 | |||
| 253 | //if made it here, lets' delete the transient to keep the db clean |
||
| 254 | delete_transient( $transient_key ); |
||
| 255 | |||
| 256 | $method = 'batch_' . $cron_type . '_from_queue'; |
||
| 257 | if ( method_exists( self::$_MSG_PROCESSOR, $method ) ) { |
||
| 258 | self::$_MSG_PROCESSOR->$method(); |
||
| 259 | } else { |
||
| 260 | //no matching task |
||
| 261 | /** |
||
| 262 | * trigger error so this gets in the error logs. This is important because it happens on a non user request. |
||
| 263 | */ |
||
| 264 | trigger_error( esc_attr( sprintf( __( 'There is no task corresponding to this route %s', 'event_espresso' ), $cron_type ) ) ); |
||
| 265 | } |
||
| 266 | exit(); |
||
| 267 | } |
||
| 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 ) { |
||
| 283 | EE_Registry::instance()->load_helper( 'MSG_Template' ); |
||
| 284 | return EEH_MSG_Template::get_template_pack( $template_pack_name ); |
||
| 285 | } |
||
| 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() { |
||
| 298 | EE_Registry::instance()->load_helper( 'MSG_Template' ); |
||
| 299 | |||
| 300 | //for backward compat, let's make sure this returns in the same format as originally. |
||
| 301 | $template_pack_collection = EEH_MSG_Template::get_template_pack_collection(); |
||
| 302 | $template_pack_collection->rewind(); |
||
| 303 | $template_packs = array(); |
||
| 304 | while ( $template_pack_collection->valid() ) { |
||
| 305 | $template_packs[ $template_pack_collection->current()->dbref ] = $template_pack_collection->current(); |
||
| 306 | $template_pack_collection->next(); |
||
| 307 | } |
||
| 308 | return $template_packs; |
||
| 309 | } |
||
| 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() { |
||
| 321 | if ( empty( self::$_MSG_PATHS ) ) { |
||
| 322 | self::_set_messages_paths(); |
||
| 323 | foreach ( self::$_MSG_PATHS as $path ) { |
||
| 324 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder( $path ); |
||
| 325 | } |
||
| 326 | // add aliases |
||
| 327 | EEH_Autoloader::add_alias( 'EE_messages', 'EE_messages' ); |
||
| 328 | EEH_Autoloader::add_alias( 'EE_messenger', 'EE_messenger' ); |
||
| 329 | } |
||
| 330 | } |
||
| 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() { |
||
| 344 | $dir_ref = array( |
||
| 345 | 'messages/message_type', |
||
| 346 | 'messages/messenger', |
||
| 347 | 'messages/defaults', |
||
| 348 | 'messages/defaults/email', |
||
| 349 | 'messages/data_class', |
||
| 350 | 'messages/validators', |
||
| 351 | 'messages/validators/email', |
||
| 352 | 'messages/validators/html', |
||
| 353 | 'shortcodes', |
||
| 354 | ); |
||
| 355 | $paths = array(); |
||
| 356 | foreach ( $dir_ref as $index => $dir ) { |
||
| 357 | $paths[ $index ] = EE_LIBRARIES . $dir; |
||
| 358 | } |
||
| 359 | self::$_MSG_PATHS = apply_filters( 'FHEE__EED_Messages___set_messages_paths___MSG_PATHS', $paths ); |
||
| 360 | } |
||
| 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() { |
||
| 370 | if ( ! self::$_MSG_PROCESSOR instanceof EE_Messages_Processor ) { |
||
| 371 | EE_Registry::instance()->load_core( 'Request_Handler' ); |
||
| 372 | self::set_autoloaders(); |
||
| 373 | self::$_EEMSG = EE_Registry::instance()->load_lib( 'messages' ); |
||
| 374 | self::$_MSG_PROCESSOR = EE_Registry::instance()->load_lib( 'Messages_Processor' ); |
||
| 375 | self::$_message_resource_manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' ); |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * @param EE_Transaction $transaction |
||
| 383 | */ |
||
| 384 | public static function payment_reminder( EE_Transaction $transaction ) { |
||
| 385 | self::_load_controller(); |
||
| 386 | $data = array( $transaction, null ); |
||
| 387 | self::$_MSG_PROCESSOR->generate_for_all_active_messengers( 'payment_reminder', $data ); |
||
| 388 | } |
||
| 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 ) { |
||
| 399 | self::_load_controller(); |
||
| 400 | $data = array( $transaction, $payment ); |
||
| 401 | EE_Registry::instance()->load_helper( 'MSG_Template' ); |
||
| 402 | $message_type = EEH_MSG_Template::convert_payment_status_to_message_type( $payment->STS_ID() ); |
||
| 403 | //if payment amount is less than 0 then switch to payment_refund message type. |
||
| 404 | $message_type = $payment->amount() < 0 ? 'payment_refund' : $message_type; |
||
| 405 | self::$_MSG_PROCESSOR->generate_for_all_active_messengers( $message_type, $data ); |
||
| 406 | } |
||
| 407 | |||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * @param EE_Transaction $transaction |
||
| 412 | */ |
||
| 413 | public static function cancelled_registration( EE_Transaction $transaction ) { |
||
| 414 | self::_load_controller(); |
||
| 415 | $data = array( $transaction, null ); |
||
| 416 | self::$_MSG_PROCESSOR->generate_for_all_active_messengers( 'cancelled_registration', $data ); |
||
| 417 | } |
||
| 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() ) { |
||
| 430 | |||
| 431 | if ( ! self::_verify_registration_notification_send( $registration, $extra_details ) ) { |
||
| 432 | //no messages please |
||
| 433 | return; |
||
| 434 | } |
||
| 435 | |||
| 436 | |||
| 437 | //get all registrations so we make sure we send messages for the right status. |
||
| 438 | $all_registrations = $registration->transaction()->registrations(); |
||
| 439 | |||
| 440 | //cached array of statuses so we only trigger messages once per status. |
||
| 441 | $statuses_sent = array(); |
||
| 442 | self::_load_controller(); |
||
| 443 | $mtgs = array(); |
||
| 444 | |||
| 445 | //loop through registrations and trigger messages once per status. |
||
| 446 | foreach ( $all_registrations as $reg ) { |
||
| 447 | |||
| 448 | //already triggered? |
||
| 449 | if ( in_array( $reg->status_ID(), $statuses_sent ) ) { |
||
| 450 | continue; |
||
| 451 | } |
||
| 452 | |||
| 453 | $message_type = EEH_MSG_Template::convert_reg_status_to_message_type( $reg->status_ID() ); |
||
| 454 | $mtgs = $mtgs + self::$_MSG_PROCESSOR->setup_mtgs_for_all_active_messengers( $message_type, array( $registration->transaction(), null, $reg->status_ID() ) ); |
||
| 455 | $statuses_sent[] = $reg->status_ID(); |
||
| 456 | } |
||
| 457 | |||
| 458 | $mtgs = $mtgs + self::$_MSG_PROCESSOR->setup_mtgs_for_all_active_messengers( 'registration_summary', array( $registration->transaction(), null ) ); |
||
| 459 | |||
| 460 | //batch queue and initiate request |
||
| 461 | self::$_MSG_PROCESSOR->batch_queue_for_generation_and_persist( $mtgs ); |
||
| 462 | self::$_MSG_PROCESSOR->get_queue()->initiate_request_by_priority(); |
||
| 463 | } |
||
| 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() ) { |
||
| 477 | //self::log( |
||
| 478 | // __CLASS__, __FUNCTION__, __LINE__, |
||
| 479 | // $registration->transaction(), |
||
| 480 | // array( '$extra_details' => $extra_details ) |
||
| 481 | //); |
||
| 482 | // currently only using this to send messages for the primary registrant |
||
| 483 | if ( ! $registration->is_primary_registrant() ) { |
||
| 484 | return false; |
||
| 485 | } |
||
| 486 | // first we check if we're in admin and not doing front ajax |
||
| 487 | if ( is_admin() && ! EE_FRONT_AJAX ) { |
||
| 488 | //make sure appropriate admin params are set for sending messages |
||
| 489 | if ( empty( $_REQUEST['txn_reg_status_change']['send_notifications'] ) || ! absint( $_REQUEST['txn_reg_status_change']['send_notifications'] ) ) { |
||
| 490 | //no messages sent please. |
||
| 491 | return false; |
||
| 492 | } |
||
| 493 | } else { |
||
| 494 | // frontend request (either regular or via AJAX) |
||
| 495 | // TXN is NOT finalized ? |
||
| 496 | if ( ! isset( $extra_details['finalized'] ) || $extra_details['finalized'] === false ) { |
||
| 497 | return false; |
||
| 498 | } |
||
| 499 | // return visit but nothing changed ??? |
||
| 500 | if ( |
||
| 501 | isset( $extra_details['revisit'], $extra_details['status_updates'] ) && |
||
| 502 | $extra_details['revisit'] && ! $extra_details['status_updates'] |
||
| 503 | ) { |
||
| 504 | return false; |
||
| 505 | } |
||
| 506 | // NOT sending messages && reg status is something other than "Not-Approved" |
||
| 507 | if ( |
||
| 508 | ! apply_filters( 'FHEE__EED_Messages___maybe_registration__deliver_notifications', false ) && |
||
| 509 | $registration->status_ID() !== EEM_Registration::status_id_not_approved |
||
| 510 | ) { |
||
| 511 | return false; |
||
| 512 | } |
||
| 513 | } |
||
| 514 | // release the kraken |
||
| 515 | return true; |
||
| 516 | } |
||
| 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 = '' ) { |
||
| 531 | EE_Registry::instance()->load_helper( 'MSG_Template' ); |
||
| 532 | return EEH_MSG_Template::convert_reg_status_to_message_type( $reg_status ) |
||
| 533 | ? EEH_MSG_Template::convert_reg_status_to_message_type( $reg_status ) |
||
| 534 | : EEH_MSG_Template::reg_status_to_message_type_array(); |
||
| 535 | } |
||
| 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 ) { |
||
| 550 | EE_Registry::instance()->load_helper( 'MSG_Template' ); |
||
| 551 | return EEH_MSG_Template::convert_payment_status_to_message_type( $payment_status ) |
||
| 552 | ? EEH_MSG_Template::convert_payment_status_to_message_type( $payment_status ) |
||
| 553 | : false; |
||
| 554 | } |
||
| 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 ) { |
||
| 567 | self::_load_controller(); |
||
| 568 | |||
| 569 | //if $msgID in this request then skip to the new resend_message |
||
| 570 | if ( EE_Registry::instance()->REQ->get( 'MSG_ID' ) ) { |
||
| 571 | return self::resend_message(); |
||
| 572 | } |
||
| 573 | |||
| 574 | //make sure any incoming request data is set on the REQ so that it gets picked up later. |
||
| 575 | $req_data = (array) $req_data; |
||
| 576 | foreach( $req_data as $request_key => $request_value ) { |
||
| 577 | EE_Registry::instance()->REQ->set( $request_key, $request_value ); |
||
| 578 | } |
||
| 579 | |||
| 580 | if ( ! $messages_to_send = self::$_MSG_PROCESSOR->setup_messages_to_generate_from_registration_ids_in_request() ) { |
||
| 581 | return false; |
||
| 582 | } |
||
| 583 | |||
| 584 | try { |
||
| 585 | self::$_MSG_PROCESSOR->batch_queue_for_generation_and_persist( $messages_to_send ); |
||
| 586 | self::$_MSG_PROCESSOR->get_queue()->initiate_request_by_priority(); |
||
| 587 | } catch( EE_Error $e ) { |
||
| 588 | EE_Error::add_error( $e->getMessage(), __FILE__, __FUNCTION__, __LINE__ ); |
||
| 589 | return false; |
||
| 590 | } |
||
| 591 | EE_Error::add_success( |
||
| 592 | __( 'Messages have been successfully queued for generation and sending.', 'event_espresso' ) |
||
| 593 | ); |
||
| 594 | return true; //everything got queued. |
||
| 595 | } |
||
| 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() { |
||
| 603 | self::_load_controller(); |
||
| 604 | |||
| 605 | $msgID = EE_Registry::instance()->REQ->get( 'MSG_ID' ); |
||
| 606 | if ( ! $msgID ) { |
||
| 607 | EE_Error::add_error( __( 'Something went wrong because there is no "MSG_ID" value in the request', 'event_espresso' ), __FILE__, __FUNCTION__, __LINE__ ); |
||
| 608 | return false; |
||
| 609 | } |
||
| 610 | |||
| 611 | self::$_MSG_PROCESSOR->setup_messages_from_ids_and_send( (array) $msgID ); |
||
| 612 | |||
| 613 | //setup success message. |
||
| 614 | $count_ready_for_resend = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue( EEM_Message::status_resend ); |
||
| 615 | EE_Error::add_success( sprintf( |
||
| 616 | _n( |
||
| 617 | 'There was %d message queued for resending.', |
||
| 618 | 'There were %d messages queued for resending.', |
||
| 619 | $count_ready_for_resend, |
||
| 620 | 'event_espresso' |
||
| 621 | ), |
||
| 622 | $count_ready_for_resend |
||
| 623 | ) ); |
||
| 624 | return true; |
||
| 625 | } |
||
| 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 ) { |
||
| 637 | EE_Registry::instance()->load_helper( 'MSG_Template' ); |
||
| 638 | //we need to get the transaction object |
||
| 639 | $transaction = $payment->transaction(); |
||
| 640 | if ( $transaction instanceof EE_Transaction ) { |
||
| 641 | $data = array( $transaction, $payment ); |
||
| 642 | $message_type = EEH_MSG_Template::convert_payment_status_to_message_type( $payment->STS_ID() ); |
||
| 643 | |||
| 644 | //if payment amount is less than 0 then switch to payment_refund message type. |
||
| 645 | $message_type = $payment->amount() < 0 ? 'payment_refund' : $message_type; |
||
| 646 | |||
| 647 | //if payment_refund is selected, but the status is NOT accepted. Then change message type to false so NO message notification goes out. |
||
| 648 | $message_type = $message_type == 'payment_refund' && $payment->STS_ID() != EEM_Payment::status_id_approved ? false : $message_type; |
||
| 649 | |||
| 650 | self::_load_controller(); |
||
| 651 | |||
| 652 | self::$_MSG_PROCESSOR->generate_for_all_active_messengers( $message_type, $data ); |
||
| 653 | |||
| 654 | //get count of queued for generation |
||
| 655 | $count_to_generate = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue( EEM_Message::status_incomplete ); |
||
| 656 | |||
| 657 | if ( $count_to_generate > 0 ) { |
||
| 658 | add_filter( 'FHEE__EE_Admin_Page___process_admin_payment_notification__success', '__return_true' ); |
||
| 659 | return true; |
||
| 660 | } else { |
||
| 661 | $count_failed = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue( EEM_Message::instance()->stati_indicating_failed_sending() ); |
||
| 662 | EE_Error::add_error( sprintf( |
||
| 663 | _n( |
||
| 664 | 'The payment notification generation failed.', |
||
| 665 | '%d payment notifications failed being sent.', |
||
| 666 | $count_failed, |
||
| 667 | 'event_espresso' |
||
| 668 | ), |
||
| 669 | $count_failed |
||
| 670 | ), __FILE__, __FUNCTION__, __LINE__ ); |
||
| 671 | return false; |
||
| 672 | } |
||
| 673 | } else { |
||
| 674 | EE_Error::add_error( |
||
| 675 | 'Unable to generate the payment notification because the given value for the transaction is invalid.', |
||
| 676 | 'event_espresso' |
||
| 677 | ); |
||
| 678 | return false; |
||
| 679 | } |
||
| 680 | } |
||
| 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 ) { |
||
| 694 | //make sure mtp is id and set it in the EE_Request Handler later messages setup. |
||
| 695 | EE_Registry::instance()->REQ->set( 'GRP_ID', (int) $grp_id ); |
||
| 696 | self::_load_controller(); |
||
| 697 | self::$_MSG_PROCESSOR->generate_for_all_active_messengers( 'newsletter', $registrations ); |
||
| 698 | } |
||
| 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' ) { |
||
| 713 | // whitelist $messenger |
||
| 714 | switch ( $messenger ) { |
||
| 715 | case 'pdf' : |
||
| 716 | $sending_messenger = 'pdf'; |
||
| 717 | $generating_messenger = 'html'; |
||
| 718 | break; |
||
| 719 | case 'html' : |
||
| 720 | default : |
||
| 721 | $sending_messenger = 'html'; |
||
| 722 | $generating_messenger = 'html'; |
||
| 723 | break; |
||
| 724 | } |
||
| 725 | // whitelist $message_type |
||
| 726 | switch ( $message_type ) { |
||
| 727 | case 'receipt' : |
||
| 728 | $message_type = 'receipt'; |
||
| 729 | break; |
||
| 730 | case 'invoice' : |
||
| 731 | default : |
||
| 732 | $message_type = 'invoice'; |
||
| 733 | break; |
||
| 734 | } |
||
| 735 | // verify that both the messenger AND the message type are active |
||
| 736 | if ( EEH_MSG_Template::is_messenger_active( $sending_messenger ) && EEH_MSG_Template::is_mt_active( $message_type ) ) { |
||
| 737 | //need to get the correct message template group for this (i.e. is there a custom invoice for the event this registration is registered for?) |
||
| 738 | $template_query_params = array( |
||
| 739 | 'MTP_is_active' => true, |
||
| 740 | 'MTP_messenger' => $generating_messenger, |
||
| 741 | 'MTP_message_type' => $message_type, |
||
| 742 | 'Event.EVT_ID' => $registration->event_ID() |
||
| 743 | ); |
||
| 744 | //get the message template group. |
||
| 745 | $msg_template_group = EEM_Message_Template_Group::instance()->get_one( array( $template_query_params ) ); |
||
| 746 | //if we don't have an EE_Message_Template_Group then return |
||
| 747 | if ( ! $msg_template_group instanceof EE_Message_Template_Group ) { |
||
| 748 | // remove EVT_ID from query params so that global templates get picked up |
||
| 749 | unset( $template_query_params['Event.EVT_ID'] ); |
||
| 750 | //get global template as the fallback |
||
| 751 | $msg_template_group = EEM_Message_Template_Group::instance()->get_one( array( $template_query_params ) ); |
||
| 752 | } |
||
| 753 | //if we don't have an EE_Message_Template_Group then return |
||
| 754 | if ( ! $msg_template_group instanceof EE_Message_Template_Group ) { |
||
| 755 | return ''; |
||
| 756 | } |
||
| 757 | // generate the URL |
||
| 758 | $registration_message_trigger_url = EEH_MSG_Template::generate_url_trigger( |
||
| 759 | $sending_messenger, |
||
| 760 | $generating_messenger, |
||
| 761 | 'purchaser', |
||
| 762 | $message_type, |
||
| 763 | $registration, |
||
| 764 | $msg_template_group->ID(), |
||
| 765 | $registration->transaction_ID() |
||
| 766 | ); |
||
| 767 | |||
| 768 | } |
||
| 769 | return $registration_message_trigger_url; |
||
| 770 | } |
||
| 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 = '' ) { |
||
| 821 | self::_load_controller(); |
||
| 822 | /** @type EE_Message_To_Generate_From_Queue $message_to_generate */ |
||
| 823 | $message_to_generate = EE_Registry::instance()->load_lib( |
||
| 824 | 'Message_To_Generate_From_Queue', |
||
| 825 | array( |
||
| 826 | $messenger, |
||
| 827 | $message_type, |
||
| 828 | $queue, |
||
| 829 | $custom_subject, |
||
| 830 | ) |
||
| 831 | ); |
||
| 832 | return self::$_MSG_PROCESSOR->queue_for_sending( $message_to_generate ); |
||
| 833 | } |
||
| 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 ) { |
|
| 980 | if ( WP_DEBUG && false ) { |
||
| 981 | if ( $transaction instanceof EE_Transaction ) { |
||
| 982 | // don't serialize objects |
||
| 983 | $info = EEH_Debug_Tools::strip_objects( $info ); |
||
| 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.