Changeset 3364070
- Timestamp:
- 09/18/2025 03:22:26 PM (7 months ago)
- Location:
- email-reports-for-cfdb7
- Files:
-
- 6 added
- 2 edited
-
tags/0.0.5 (added)
-
tags/0.0.5/assets (added)
-
tags/0.0.5/assets/Email-Reports-for-CFDB7.jpg (added)
-
tags/0.0.5/email-reports-for-cfdb7.php (added)
-
tags/0.0.5/readme.txt (added)
-
tags/0.0.5/uninstall.php (added)
-
trunk/email-reports-for-cfdb7.php (modified) (14 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
email-reports-for-cfdb7/trunk/email-reports-for-cfdb7.php
r3362773 r3364070 3 3 Plugin Name: Email Reports for CFDB7 4 4 Description: Send scheduled email reports of Contact Form 7 submissions. 5 Version: 0.0. 45 Version: 0.0.5 6 6 Author: Inbound Horizons 7 7 Author URI: https://www.inboundhorizons.com/ … … 53 53 register_deactivation_hook(__FILE__, array($this, 'CRON_DeactivateEmailReport')); 54 54 55 // This ensure the CRON will be rescheduled in case the CRON event is ever cancelled by another plugin56 add_action('init', array($this, 'CRON_ScheduleNextCRON'), 20);57 55 } 58 56 … … 96 94 $time_period = isset($settings['time_period']) ? $settings['time_period'] : ''; 97 95 $recipients = isset($settings['recipients']) ? $settings['recipients'] : array(); 96 $send_time = isset($settings['send_time']) ? $settings['send_time'] : '0'; 98 97 99 98 $recipients_text = implode(PHP_EOL, $recipients); … … 103 102 104 103 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 timezone110 $dt = DateTimeImmutable::createFromFormat('U', $cron_timestamp);111 $dt = $dt->setTimezone(wp_timezone());112 113 $cron_datetime = $dt->format('g:i A (T)');114 }115 104 116 105 echo ' … … 146 135 147 136 <h1>'.esc_html($plugin_name).'</h1> 137 <p>'.esc_html($plugin_description_text).'</p> 148 138 149 139 <div id="container"> … … 195 185 <tr> 196 186 <th scope="row"> 197 Scheduled CRON Time 187 Send Emails At 188 <p class="description">Timezone: '.esc_html(wp_timezone_string()).'</p> 198 189 </th> 199 190 <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 203 201 </td> 204 202 </tr> … … 341 339 } 342 340 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 343 382 public function HTML_ContactFormTable() { 344 383 … … 376 415 $time_period = isset($_POST['time_period']) ? sanitize_text_field(wp_unslash($_POST['time_period'])) : 'yesterday'; 377 416 $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'; 378 418 $clear_data = isset($_POST['clear_data']) ? filter_var(wp_unslash($_POST['clear_data']), FILTER_VALIDATE_BOOLEAN) : false; 379 419 380 420 // Split the recipients value by newline, whitespace, or comma 381 421 $recipients = preg_split('/[,\s]+/', (string) $recipients, -1, PREG_SPLIT_NO_EMPTY); … … 400 440 'time_period' => $time_period, 401 441 'recipients' => $recipients, 442 'send_time' => $send_time, 402 443 ); 403 444 … … 406 447 update_option('erf_cfdb7_clear_data', $clear_data); 407 448 408 // Schedule the next CRON409 $this->CRON_ ScheduleNextCRON();410 449 // Ensure cron is scheduled after saving settings 450 $this->CRON_ActivateEmailReport(); 451 411 452 wp_safe_redirect($redirect_url); 412 453 exit; … … 845 886 846 887 847 public function GetNext Midnight() {888 public function GetNextHour() { 848 889 849 890 // Define a datetime for right now using this WP site's timezone … … 851 892 852 893 // 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); 854 895 855 896 // Convert that local moment to UTC epoch for wp-cron … … 859 900 } 860 901 861 public function CRON_ ScheduleNextCRON() {862 902 public function CRON_ActivateEmailReport() { 903 863 904 // 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 //} 868 920 wp_clear_scheduled_hook($this->CRON_NAME); 869 870 // Ensure this CRON isn't already scheduled871 if (!wp_next_scheduled($this->CRON_NAME) ) {872 873 // Schedule the CRON for midnight874 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 cron881 //}882 883 // Clear any old schedule884 $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);899 921 } 900 922 901 923 public function RunEmailReportCRON() { 902 924 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. 908 927 909 928 // Get the settings … … 912 931 // Get the frequency 913 932 $frequency = isset($settings['frequency']) ? $settings['frequency'] : ''; 933 $send_time = isset($settings['send_time']) ? $settings['send_time'] : '0'; 914 934 915 935 // Check if today is the day 916 936 $is_frequency_today = $this->GetIsFrequencyToday($frequency); 917 937 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 921 949 } 922 950 } -
email-reports-for-cfdb7/trunk/readme.txt
r3362773 r3364070 6 6 License: GPL v2 or later 7 7 Tested up to: 6.8 8 Stable tag: 0.0. 48 Stable tag: 0.0.5 9 9 10 10 Send scheduled email reports of Contact Form 7 submissions. … … 20 20 == Changelog == 21 21 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 22 26 = 0.0.4 - 2025-09-16 = 23 27 * Minor tweaks.
Note: See TracChangeset
for help on using the changeset viewer.