SummaryEmailTask.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Tasks\Reports; |
| 4 | |
| 5 | use WPMailSMTP\Tasks\Tasks; |
| 6 | use WPMailSMTP\WP; |
| 7 | use WPMailSMTP\Tasks\Task; |
| 8 | use WPMailSMTP\Reports\Emails\Summary as SummaryReportEmail; |
| 9 | |
| 10 | /** |
| 11 | * Class SummaryEmailTask. |
| 12 | * |
| 13 | * @since 3.0.0 |
| 14 | */ |
| 15 | class SummaryEmailTask extends Task { |
| 16 | |
| 17 | /** |
| 18 | * Action name for this task. |
| 19 | * |
| 20 | * @since 3.0.0 |
| 21 | */ |
| 22 | const ACTION = 'wp_mail_smtp_summary_report_email'; |
| 23 | |
| 24 | /** |
| 25 | * Class constructor. |
| 26 | * |
| 27 | * @since 3.0.0 |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | |
| 31 | parent::__construct( self::ACTION ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Initialize the task with all the proper checks. |
| 36 | * |
| 37 | * @since 3.0.0 |
| 38 | */ |
| 39 | public function init() { |
| 40 | |
| 41 | // Register the action handler. |
| 42 | add_action( self::ACTION, array( $this, 'process' ) ); |
| 43 | |
| 44 | $is_disabled = SummaryReportEmail::is_disabled(); |
| 45 | |
| 46 | // Exit if summary report email is disabled or this task is already scheduled. |
| 47 | if ( ! empty( $is_disabled ) || Tasks::is_scheduled( self::ACTION ) !== false ) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | $date = new \DateTime( 'next monday 2pm', WP::wp_timezone() ); |
| 52 | |
| 53 | // Schedule the task. |
| 54 | $this |
| 55 | ->recurring( $date->getTimestamp(), WEEK_IN_SECONDS ) |
| 56 | ->unique() |
| 57 | ->register(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Process summary report email send. |
| 62 | * |
| 63 | * @since 3.0.0 |
| 64 | * |
| 65 | * @param int $meta_id The Meta ID with the stored task parameters. |
| 66 | */ |
| 67 | public function process( $meta_id ) { |
| 68 | |
| 69 | // Prevent email sending if summary report email is disabled. |
| 70 | if ( SummaryReportEmail::is_disabled() || ! $this->is_allowed() ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Update the last sent week at the top to prevent multiple emails in case of task failure and retry. |
| 75 | update_option( 'wp_mail_smtp_summary_report_email_last_sent_week', current_time( 'W' ) ); |
| 76 | |
| 77 | $reports = wp_mail_smtp()->get_reports(); |
| 78 | |
| 79 | $email = $reports->get_summary_report_email(); |
| 80 | |
| 81 | $email->send(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Check if the summary report email is allowed to be sent. |
| 86 | * |
| 87 | * The email is allowed to be sent if it was not sent in the current week. |
| 88 | * |
| 89 | * @since 4.1.1 |
| 90 | * |
| 91 | * @return bool |
| 92 | */ |
| 93 | private function is_allowed() { |
| 94 | |
| 95 | $last_sent_week = get_option( 'wp_mail_smtp_summary_report_email_last_sent_week' ); |
| 96 | $current_week = current_time( 'W' ); |
| 97 | |
| 98 | if ( $last_sent_week === false || ( (int) $current_week !== (int) $last_sent_week ) ) { |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 |