templates
1 year ago
class-mail-admin.php
1 year ago
class-mail.php
1 year ago
index.php
2 years ago
class-mail-admin.php
138 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 | |
| 4 | /** |
| 5 | * Class to send an e-mail |
| 6 | */ |
| 7 | |
| 8 | if ( !class_exists('rsssl_mailer_admin') ) { |
| 9 | class rsssl_mailer_admin { |
| 10 | |
| 11 | public function __construct() { |
| 12 | add_filter( 'rsssl_five_minutes_cron', array( $this, 'maybe_send_mail' ) ); |
| 13 | add_filter( 'rsssl_five_minutes_cron', array( $this, 'rsssl_clear_expired_tokens' ) ); |
| 14 | add_action( 'admin_init', array( $this, 'maybe_verify_user_email' ) ); |
| 15 | add_action( 'rsssl_after_save_field', array( $this, 'maybe_allow_restart_email_verification' ), 10, 4 ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * @return void |
| 20 | * |
| 21 | * Clear expired verification tokens from DB |
| 22 | */ |
| 23 | public function rsssl_clear_expired_tokens() { |
| 24 | |
| 25 | $token_expiration = get_option( 'rsssl_email_verification_code_expiration' ); |
| 26 | if ( $token_expiration > time() ) { |
| 27 | delete_option( 'rsssl_email_verification_code' ); |
| 28 | delete_option( 'rsssl_email_verification_code_expiration' ); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @return void |
| 34 | * |
| 35 | * Verify user e-mail |
| 36 | */ |
| 37 | public function maybe_verify_user_email() { |
| 38 | |
| 39 | if ( ! rsssl_user_can_manage() ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if ( ! isset( $_GET['rsssl_verification_code'] ) || ! isset( $_GET['rsssl_nonce'] ) ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | // verify code |
| 48 | $user_id = get_current_user_id(); |
| 49 | $nonce = $_GET['rsssl_nonce']; |
| 50 | if ( ! wp_verify_nonce( $nonce, 'rsssl_email_verification_' . $user_id ) ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if ( isset($_GET['rsssl_force_verification'] ) ){ |
| 55 | update_option( 'rsssl_email_verification_status', 'completed', false ); |
| 56 | } |
| 57 | |
| 58 | // Handle e-mail verification |
| 59 | $verification_code = $_GET['rsssl_verification_code']; |
| 60 | $verification_code = preg_replace( "/[^0-9]/", "", $verification_code ); |
| 61 | $verification_code = substr( $verification_code, 0, 6 ); |
| 62 | |
| 63 | $current_time = time(); |
| 64 | $saved_verification_code = get_option('rsssl_email_verification_code'); |
| 65 | $saved_verification_expiration = get_option('rsssl_email_verification_code_expiration'); |
| 66 | |
| 67 | if ( $verification_code === $saved_verification_code && $saved_verification_expiration && $current_time < $saved_verification_expiration ) { |
| 68 | // If the verification code is correct and hasn't expired, update the verification status |
| 69 | update_option( 'rsssl_email_verification_status', 'completed', false ); |
| 70 | set_transient('rsssl_redirect_to_settings_page', true, HOUR_IN_SECONDS ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return void |
| 76 | */ |
| 77 | public function maybe_send_mail() { |
| 78 | if ( ! rsssl_get_option( 'send_notifications_email' ) ) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | $fields = get_option( 'rsssl_email_warning_fields', [] ); |
| 83 | $time_saved = get_option( 'rsssl_email_warning_fields_saved' ); |
| 84 | if ( ! $time_saved ) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | $thirty_minutes_ago = $time_saved < strtotime( "-10 minutes" ); |
| 89 | $warning_blocks = array_column( $fields, 'email' ); |
| 90 | if ( $thirty_minutes_ago && count( $warning_blocks ) > 0 ) { |
| 91 | //clear the option |
| 92 | delete_option( 'rsssl_email_warning_fields', [] ); |
| 93 | delete_option( 'rsssl_email_warning_fields_saved' ); |
| 94 | $mailer = new rsssl_mailer(); |
| 95 | $mailer->warning_blocks = $warning_blocks; |
| 96 | $mailer->send_mail(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return bool|void |
| 102 | * |
| 103 | * E-mail verification status callback |
| 104 | */ |
| 105 | public function email_verification_completed() { |
| 106 | $status = get_option( 'rsssl_email_verification_status' ); |
| 107 | |
| 108 | if ( $status === 'started' ) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | if ( $status === 'completed' ) { |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | if ( $status === 'email_changed' ) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param $field_id |
| 124 | * @param $field_value |
| 125 | * @param $prev_value |
| 126 | * @param $field_type |
| 127 | * |
| 128 | * @return void |
| 129 | * |
| 130 | * Maybe allow the user to re-verify their e-mail address after the notifications e-mail address has changed |
| 131 | */ |
| 132 | public function maybe_allow_restart_email_verification( $field_id, $field_value, $prev_value, $field_type ) { |
| 133 | if ( $field_id === 'notifications_email_address' && $field_value !== $prev_value && rsssl_user_can_manage() ) { |
| 134 | update_option( 'rsssl_email_verification_status', 'email_changed' ); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |