Plugin Directory

Changeset 3364070


Ignore:
Timestamp:
09/18/2025 03:22:26 PM (7 months ago)
Author:
inboundhorizons
Message:

Added ability to choose time of day to send email.
Fixed issue with CRON to running when expected.

Location:
email-reports-for-cfdb7
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • email-reports-for-cfdb7/trunk/email-reports-for-cfdb7.php

    r3362773 r3364070  
    33    Plugin Name: Email Reports for CFDB7
    44    Description: Send scheduled email reports of Contact Form 7 submissions.
    5     Version: 0.0.4
     5    Version: 0.0.5
    66    Author: Inbound Horizons
    77    Author URI: https://www.inboundhorizons.com/
     
    5353        register_deactivation_hook(__FILE__, array($this, 'CRON_DeactivateEmailReport'));
    5454       
    55         // This ensure the CRON will be rescheduled in case the CRON event is ever cancelled by another plugin
    56         add_action('init', array($this, 'CRON_ScheduleNextCRON'), 20);
    5755    }
    5856   
     
    9694        $time_period = isset($settings['time_period']) ? $settings['time_period'] : '';
    9795        $recipients = isset($settings['recipients']) ? $settings['recipients'] : array();
     96        $send_time = isset($settings['send_time']) ? $settings['send_time'] : '0';
    9897       
    9998        $recipients_text = implode(PHP_EOL, $recipients);
     
    103102       
    104103       
    105         $cron_timestamp = wp_next_scheduled($this->CRON_NAME);
    106        
    107         $cron_datetime = "No CRON scheduled yet.";
    108         if ($cron_timestamp) {
    109             // Format it into a readable time using the site's timezone
    110             $dt = DateTimeImmutable::createFromFormat('U', $cron_timestamp);
    111             $dt = $dt->setTimezone(wp_timezone());
    112            
    113             $cron_datetime = $dt->format('g:i A (T)');
    114         }
    115104       
    116105        echo '
     
    146135           
    147136                <h1>'.esc_html($plugin_name).'</h1>
     137                <p>'.esc_html($plugin_description_text).'</p>
    148138           
    149139                <div id="container">
     
    195185                                <tr>
    196186                                    <th scope="row">
    197                                         Scheduled CRON Time
     187                                        Send Emails At
     188                                        <p class="description">Timezone: '.esc_html(wp_timezone_string()).'</p>
    198189                                    </th>
    199190                                    <td>
    200                                         '.esc_html($cron_datetime).'
    201                                         <p class="description">If the scheduled time is incorrect you may need to change the site timezone in Settings->General.</p>
    202                                         <p class="description">Afterwards please click "Save Settings" below to update the next scheduled CRON.</p>
     191                                        <select name="send_time" class="regular-text">
     192                    ';
     193                        $this->HTML_EchoSendAtOptions($send_time);
     194                    echo '
     195                                        </select>
     196                                       
     197                                        <p class="description">Note: Delivery is approximate and may not occur at the exact time shown.</p>
     198                                        <p class="description">Timezone can be changed in <code>Settings->General</code>.</p>
     199                                       
     200                                       
    203201                                    </td>
    204202                                </tr>
     
    341339    }
    342340   
     341    public function HTML_EchoSendAtOptions($selected_value = '0') {
     342       
     343        // Convert to string
     344        $selected_value = (string)$selected_value;
     345       
     346        $hours = array(
     347            '0'  => '12:00 AM (Midnight)',
     348            '1'  => '1:00 AM',
     349            '2'  => '2:00 AM',
     350            '3'  => '3:00 AM',
     351            '4'  => '4:00 AM',
     352            '5'  => '5:00 AM',
     353            '6'  => '6:00 AM',
     354            '7'  => '7:00 AM',
     355            '8'  => '8:00 AM',
     356            '9'  => '9:00 AM',
     357            '10' => '10:00 AM',
     358            '11' => '11:00 AM',
     359            '12' => '12:00 PM (Noon)',
     360            '13' => '1:00 PM',
     361            '14' => '2:00 PM',
     362            '15' => '3:00 PM',
     363            '16' => '4:00 PM',
     364            '17' => '5:00 PM',
     365            '18' => '6:00 PM',
     366            '19' => '7:00 PM',
     367            '20' => '8:00 PM',
     368            '21' => '9:00 PM',
     369            '22' => '10:00 PM',
     370            '23' => '11:00 PM',
     371        );
     372       
     373        foreach ($hours as $option_value => $option_label) {
     374            echo '
     375                <option value="'.esc_attr($option_value).'" '.selected($selected_value, $option_value, false).'>
     376                    '.esc_html($option_label).'
     377                </option>
     378            ';
     379        }
     380    }
     381   
    343382    public function HTML_ContactFormTable() {
    344383       
     
    376415        $time_period = isset($_POST['time_period']) ? sanitize_text_field(wp_unslash($_POST['time_period'])) : 'yesterday';
    377416        $recipients = isset($_POST['recipients']) ? sanitize_textarea_field(wp_unslash($_POST['recipients'])) : '';
     417        $send_time = isset($_POST['send_time']) ? sanitize_text_field(wp_unslash($_POST['send_time'])) : '0';
    378418        $clear_data = isset($_POST['clear_data']) ? filter_var(wp_unslash($_POST['clear_data']), FILTER_VALIDATE_BOOLEAN) : false;
    379            
     419       
    380420        // Split the recipients value by newline, whitespace, or comma
    381421        $recipients = preg_split('/[,\s]+/', (string) $recipients, -1, PREG_SPLIT_NO_EMPTY);
     
    400440            'time_period' => $time_period,
    401441            'recipients' => $recipients,
     442            'send_time' => $send_time,
    402443        );
    403444       
     
    406447        update_option('erf_cfdb7_clear_data', $clear_data);
    407448       
    408         // Schedule the next CRON
    409         $this->CRON_ScheduleNextCRON();
    410        
     449        // Ensure cron is scheduled after saving settings
     450        $this->CRON_ActivateEmailReport();
     451
    411452        wp_safe_redirect($redirect_url);
    412453        exit;
     
    845886   
    846887   
    847     public function GetNextMidnight() {
     888    public function GetNextHour() {
    848889   
    849890        // Define a datetime for right now using this WP site's timezone
     
    851892       
    852893        // Add 1 day and then add 5 seconds
    853         $dt->modify('tomorrow')->setTime(0, 0, 5);
     894        $dt->modify('+1 hour')->setTime(0, 0, 0);
    854895       
    855896        // Convert that local moment to UTC epoch for wp-cron
     
    859900    }
    860901   
    861     public function CRON_ScheduleNextCRON() {
    862        
     902    public function CRON_ActivateEmailReport() {
     903   
    863904        // Clear any old schedule
    864         $timestamp = wp_next_scheduled($this->CRON_NAME);
    865         if ($timestamp) {
    866             wp_unschedule_event($timestamp, $this->CRON_NAME);
    867         }
     905        $this->CRON_DeactivateEmailReport();
     906       
     907        // Ensure this CRON isn't already scheduled
     908        if (!wp_next_scheduled($this->CRON_NAME)) {
     909       
     910            // Schedule the cron for every hour on the hour
     911            wp_schedule_event($this->GetNextHour(), 'hourly', $this->CRON_NAME);   
     912        }
     913    }
     914   
     915    public function CRON_DeactivateEmailReport() {
     916        //$timestamp = wp_next_scheduled($this->CRON_NAME);
     917        //if ($timestamp) {
     918        //  wp_unschedule_event($timestamp, $this->CRON_NAME);
     919        //}
    868920        wp_clear_scheduled_hook($this->CRON_NAME);
    869        
    870         // Ensure this CRON isn't already scheduled
    871         if (!wp_next_scheduled($this->CRON_NAME) ) {
    872        
    873             // Schedule the CRON for midnight
    874             wp_schedule_single_event($this->GetNextMidnight(), $this->CRON_NAME);
    875         }
    876     }
    877    
    878     public function CRON_ActivateEmailReport() {
    879         //if (!wp_next_scheduled($this->CRON_NAME)) {       // If the cron is NOT scheduled...
    880         //  wp_schedule_event(time(), 'daily', $this->CRON_NAME);   // Schedule the cron
    881         //}
    882        
    883         // Clear any old schedule
    884         $timestamp = wp_next_scheduled($this->CRON_NAME);
    885         if ($timestamp) {
    886             wp_unschedule_event($timestamp, $this->CRON_NAME);
    887         }
    888         wp_clear_scheduled_hook($this->CRON_NAME);
    889        
    890         $this->CRON_ScheduleNextCRON();
    891     }
    892    
    893     public function CRON_DeactivateEmailReport() {
    894         $timestamp = wp_next_scheduled($this->CRON_NAME);
    895         if ($timestamp) {
    896             wp_unschedule_event($timestamp, $this->CRON_NAME);
    897         }
    898         wp_clear_scheduled_hook($this->CRON_NAME);
    899921    }
    900922   
    901923    public function RunEmailReportCRON() {
    902924   
    903         // Go ahead and immediately schedule the next CRON before running code
    904         $this->CRON_ScheduleNextCRON();
    905        
    906         // This function will be called DAILY.
    907         // We need to check if an email report is scheduled for TODAY or not.
     925        // This function will be called HOURLY.
     926        // We need to check if an email report is scheduled for TODAY at this HOUR or not.
    908927       
    909928        // Get the settings
     
    912931        // Get the frequency
    913932        $frequency = isset($settings['frequency']) ? $settings['frequency'] : '';
     933        $send_time = isset($settings['send_time']) ? $settings['send_time'] : '0';
    914934       
    915935        // Check if today is the day
    916936        $is_frequency_today = $this->GetIsFrequencyToday($frequency);
    917937       
    918         // Send email if today is the day to send an email
    919         if ($is_frequency_today) {
    920             $this->EMAIL_SendReportEmail();
     938        $now = new DateTime('now', wp_timezone());
     939        $current_hour = (int)$now->format('G'); // (0 to 23)
     940       
     941        // Ensure the current hour matches the "Send At" hour
     942        if (((string)$current_hour) === ((string)$send_time)) {
     943           
     944            // Send email if this is the day and hour to send an email
     945            if ($is_frequency_today) {
     946                $this->EMAIL_SendReportEmail();
     947            }
     948       
    921949        }
    922950    }
  • email-reports-for-cfdb7/trunk/readme.txt

    r3362773 r3364070  
    66License: GPL v2 or later
    77Tested up to: 6.8
    8 Stable tag: 0.0.4
     8Stable tag: 0.0.5
    99
    1010Send scheduled email reports of Contact Form 7 submissions.
     
    2020== Changelog ==
    2121
     22= 0.0.5 - 2025-09-18 =
     23* Added abaility to choose time of day to send email.
     24* Fixed issue with CRON to running when expected.
     25
    2226= 0.0.4 - 2025-09-16 =
    2327* Minor tweaks.
Note: See TracChangeset for help on using the changeset viewer.