Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 4 |
CRAP | n/a |
0 / 0 |
|
| wpcomsh_smtp_add_priority_header | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| wpcomsh_smtp_set_priority_local_priority | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| wpcomsh_smtp_add_local_priority_filter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| wpcomsh_smtp_add_local_priority_filter_within_filter | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Set up custom priority headers for outbound email sent via SMTP. |
| 4 | * See pMz3w-gAF-p2#comment-104972 for the specific header structure and |
| 5 | * background discussion for our initial use cases. |
| 6 | * |
| 7 | * @since 3.5.36 |
| 8 | * @package wpcomsh |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * Maximum priority level. |
| 13 | */ |
| 14 | const WPCOMSH_SMTP_LEVEL_MAX = 9; |
| 15 | /** |
| 16 | * Minimum priority level. |
| 17 | */ |
| 18 | const WPCOMSH_SMTP_LEVEL_MIN = 1; |
| 19 | /** |
| 20 | * The default level for users with reduced email priority. |
| 21 | */ |
| 22 | const WPCOMSH_SMTP_LEVEL_REDUCED = 2; |
| 23 | /** |
| 24 | * A slightly increased level for emails that should get priority, like password resets and new user registrations. |
| 25 | */ |
| 26 | const WPCOMSH_SMTP_LEVEL_LOCAL_PRIORITY = 5; |
| 27 | |
| 28 | /** |
| 29 | * Add the custom email priority SMTP header if that's something we need for this site. |
| 30 | * |
| 31 | * @param \PHPMailer\PHPMailer\PHPMailer $phpmailer The current PHPMailer instance, which is passed by reference. |
| 32 | * @return void |
| 33 | * @throws \PHPMailer\PHPMailer\Exception Exception may be thrown by the {@see PHP_Mailer::addCustomHeader()} function. |
| 34 | */ |
| 35 | function wpcomsh_smtp_add_priority_header( $phpmailer ): void { |
| 36 | if ( ! wpcom_site_has_feature( \WPCOM_Features::REDUCED_ATOMIC_EMAIL_PRIORITY ) ) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Filters the SMTP email priority. |
| 42 | * |
| 43 | * @since 3.5.36 |
| 44 | * |
| 45 | * @param int|null $priority The priority we should apply. Default is 2. |
| 46 | * @return int|null Return null if you want no priority. Otherwise the returned value must be 0-9. |
| 47 | */ |
| 48 | $atomic_email_priority = apply_filters( 'wpcomsh_smtp_email_priority', WPCOMSH_SMTP_LEVEL_REDUCED ); |
| 49 | |
| 50 | // If the priority is null, assume we don't want any priority. |
| 51 | if ( null === $atomic_email_priority ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Ensure we have an integer between 1-9; if we don't have something int-like, use the default priority. |
| 56 | if ( ! is_int( $atomic_email_priority ) ) { |
| 57 | $atomic_email_priority = WPCOMSH_SMTP_LEVEL_REDUCED; |
| 58 | } |
| 59 | $atomic_email_priority = min( WPCOMSH_SMTP_LEVEL_MAX, $atomic_email_priority ); |
| 60 | $atomic_email_priority = max( WPCOMSH_SMTP_LEVEL_MIN, $atomic_email_priority ); |
| 61 | |
| 62 | $phpmailer->addCustomHeader( 'X-Atomic-Email-Level', $atomic_email_priority ); |
| 63 | } |
| 64 | |
| 65 | // Run this relatively late to make sure standard SMTP configuration has already been completed |
| 66 | add_action( 'phpmailer_init', 'wpcomsh_smtp_add_priority_header', 100 ); |
| 67 | |
| 68 | /** |
| 69 | * Helper function to return the local priority SMTP level - {@see WPCOMSH_SMTP_LEVEL_LOCAL_PRIORITY}. |
| 70 | * |
| 71 | * @since 3.5.36 |
| 72 | */ |
| 73 | function wpcomsh_smtp_set_priority_local_priority(): int { |
| 74 | return WPCOMSH_SMTP_LEVEL_LOCAL_PRIORITY; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Helper function to ensure that we add a filter that sets the SMTP email priority to {@see WPCOMSH_SMTP_LEVEL_LOCAL_PRIORITY}; |
| 79 | * |
| 80 | * @since 3.5.36 |
| 81 | */ |
| 82 | function wpcomsh_smtp_add_local_priority_filter(): void { |
| 83 | add_filter( 'wpcomsh_smtp_email_priority', 'wpcomsh_smtp_set_priority_local_priority' ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Helper function to allow us to set up local priority from within a filter. |
| 88 | * {@see wpcomsh_smtp_add_local_priority_filter()}. |
| 89 | * |
| 90 | * @since 3.5.36 |
| 91 | * |
| 92 | * @param mixed $filtered_value The value for the filter. We return this as-is from the function. |
| 93 | * @return mixed |
| 94 | */ |
| 95 | function wpcomsh_smtp_add_local_priority_filter_within_filter( $filtered_value ) { |
| 96 | wpcomsh_smtp_add_local_priority_filter(); |
| 97 | |
| 98 | return $filtered_value; |
| 99 | } |
| 100 | |
| 101 | // Ensure emails sent as a result of password resets get local priority. |
| 102 | add_action( 'password_reset', 'wpcomsh_smtp_add_local_priority_filter' ); |
| 103 | // Ensure emails sent as a result of a new user registration get local priority. |
| 104 | add_action( 'register_new_user', 'wpcomsh_smtp_add_local_priority_filter' ); |
| 105 | // Ensure emails sent as part of a password retrieval/reset get local priority. |
| 106 | add_filter( 'retrieve_password_notification_email', 'wpcomsh_smtp_add_local_priority_filter_within_filter' ); |