Helpers
11 months ago
Integrations
2 weeks ago
RestApi
5 days ago
abilities
3 weeks ago
abstracts
5 days ago
admin
5 days ago
blocks
1 year ago
elementor
2 years ago
export
2 months ago
fields
3 weeks ago
interfaces
8 years ago
libraries
2 years ago
log-handlers
1 year ago
shortcodes
3 weeks ago
stats
5 months ago
templates
3 months ago
traits
3 weeks ago
class-everest-forms.php
5 days ago
class-evf-addon-upsell.php
3 weeks ago
class-evf-ajax.php
3 weeks ago
class-evf-autoloader.php
7 years ago
class-evf-background-process-import-entries.php
2 years ago
class-evf-background-updater.php
7 years ago
class-evf-cache-helper.php
2 months ago
class-evf-cron.php
2 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
5 years ago
class-evf-email-entries-report.php
3 months ago
class-evf-emails.php
3 weeks ago
class-evf-fields.php
3 weeks ago
class-evf-form-handler.php
3 weeks ago
class-evf-form-task.php
3 weeks ago
class-evf-forms-features.php
3 weeks ago
class-evf-frontend-scripts.php
3 weeks ago
class-evf-install.php
2 months ago
class-evf-integrations.php
3 months ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
5 years ago
class-evf-post-types.php
1 year ago
class-evf-privacy.php
6 years ago
class-evf-report-cron.php
2 months ago
class-evf-reporting.php
2 months ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
1 year ago
class-evf-smart-tags.php
9 months ago
class-evf-template-loader.php
1 year ago
class-evf-validation.php
6 years ago
evf-conditional-functions.php
6 years ago
evf-core-functions.php
3 weeks ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
4 months ago
evf-formatting-functions.php
4 years ago
evf-notice-functions.php
4 years ago
evf-template-functions.php
4 years ago
evf-template-hooks.php
7 years ago
evf-update-functions.php
5 years ago
class-evf-report-cron.php
255 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Entries Summary Report Cron |
| 4 | * |
| 5 | * @package EverestForms\Classes |
| 6 | * @since 2.0.9 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Report_Cron Class. |
| 13 | */ |
| 14 | class EVF_Report_Cron { |
| 15 | |
| 16 | /** |
| 17 | * Hook name for the scheduled event. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | const HOOK = 'everest_forms_stats_report_schedule'; |
| 22 | |
| 23 | /** |
| 24 | * Constructor. |
| 25 | * |
| 26 | * @since 2.0.9 |
| 27 | */ |
| 28 | public function __construct() {} |
| 29 | |
| 30 | /** |
| 31 | * Schedule the report cron event. |
| 32 | * |
| 33 | * @since 2.0.9 |
| 34 | * @return bool |
| 35 | */ |
| 36 | public function evf_schedule_add() { |
| 37 | if ( wp_next_scheduled( self::HOOK ) ) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | $frequency = get_option( 'everest_forms_entries_reporting_frequency', '' ); |
| 42 | $send_hour = (int) get_option( 'everest_forms_reporting_send_hour', 8 ); |
| 43 | |
| 44 | switch ( $frequency ) { |
| 45 | case 'Daily': |
| 46 | $offset = '+1 day'; |
| 47 | $recurrence = 'daily'; |
| 48 | break; |
| 49 | |
| 50 | case 'Weekly': |
| 51 | $day = get_option( 'everest_forms_entries_reporting_day', 'monday' ); |
| 52 | $offset = 'next ' . $day; |
| 53 | $recurrence = 'weekly'; |
| 54 | break; |
| 55 | |
| 56 | case 'Monthly': |
| 57 | $offset = 'first day of next month'; |
| 58 | $recurrence = 'monthly'; |
| 59 | break; |
| 60 | |
| 61 | default: |
| 62 | evf_get_logger()->warning( |
| 63 | sprintf( |
| 64 | /* translators: %s: frequency value */ |
| 65 | __( 'EVF Report: unknown frequency "%s", cannot schedule.', 'everest-forms' ), |
| 66 | $frequency |
| 67 | ), |
| 68 | array( 'source' => 'evf-reporting' ) |
| 69 | ); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | $midnight_local = gmdate( 'Y-m-d 00:00:00', strtotime( $offset ) ); |
| 74 | $midnight_utc = get_gmt_from_date( $midnight_local ); |
| 75 | $next_run = strtotime( $midnight_utc ) + ( $send_hour * HOUR_IN_SECONDS ); |
| 76 | |
| 77 | wp_schedule_event( $next_run, $recurrence, self::HOOK ); |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Clear all scheduled report events. |
| 84 | * |
| 85 | * @since 2.0.7 |
| 86 | */ |
| 87 | public function evf_schedule_clear_all() { |
| 88 | $timestamp = wp_next_scheduled( self::HOOK ); |
| 89 | while ( $timestamp ) { |
| 90 | wp_unschedule_event( $timestamp, self::HOOK ); |
| 91 | $timestamp = wp_next_scheduled( self::HOOK ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Clear and reschedule. Called after settings are saved. |
| 97 | * |
| 98 | * @since 2.0.9 |
| 99 | */ |
| 100 | public function evf_reschedule() { |
| 101 | $this->evf_schedule_clear_all(); |
| 102 | |
| 103 | if ( 'yes' === get_option( 'everest_forms_enable_entries_reporting', 'no' ) ) { |
| 104 | $this->evf_schedule_add(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Handle schedule cleanup on plugin deactivation. |
| 110 | * |
| 111 | * @since 2.0.7 |
| 112 | */ |
| 113 | public function deactivate() { |
| 114 | $this->evf_schedule_clear_all(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Build and send the report email. |
| 119 | * |
| 120 | * @since 2.0.9 |
| 121 | * @param bool $is_test Whether this is a manual test send. |
| 122 | * @return bool |
| 123 | */ |
| 124 | public function evf_report_form_statistics_send( $is_test = false ) { |
| 125 | $recipient_option = get_option( 'everest_forms_entries_reporting_email', '{admin_email}' ); |
| 126 | $admin_email = sanitize_email( get_bloginfo( 'admin_email' ) ); |
| 127 | |
| 128 | if ( '{admin_email}' === $recipient_option ) { |
| 129 | $recipient = array( |
| 130 | $admin_email |
| 131 | ); |
| 132 | } else { |
| 133 | $recipient_string = str_replace( '{admin_email}', $admin_email, $recipient_option ); |
| 134 | |
| 135 | $recipient_emails = array(); |
| 136 | foreach ( explode( ',', $recipient_string ) as $email ) { |
| 137 | $email = sanitize_email( trim( $email ) ); |
| 138 | if ( is_email( $email ) ) { |
| 139 | $recipient_emails[] = $email; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | $recipient = ! empty( $recipient_emails ) ? $recipient_emails : array( $admin_email ); |
| 144 | } |
| 145 | |
| 146 | $subject = get_option( 'everest_forms_entries_reporting_subject', __( 'Everest Forms - Entries summary statistics', 'everest-forms' ) ); |
| 147 | if ( empty( trim( $subject ) ) ) { |
| 148 | $subject = __( 'Everest Forms - Entries summary statistics', 'everest-forms' ); |
| 149 | } |
| 150 | |
| 151 | $frequency = get_option( 'everest_forms_entries_reporting_frequency', 'Weekly' ); |
| 152 | if ( ! in_array( $frequency, array( 'Daily', 'Weekly', 'Monthly' ), true ) ) { |
| 153 | evf_get_logger()->warning( |
| 154 | sprintf( |
| 155 | /* translators: %s: frequency value */ |
| 156 | __( 'EVF Report: invalid frequency "%s" at send time.', 'everest-forms' ), |
| 157 | $frequency |
| 158 | ), |
| 159 | array( 'source' => 'evf-reporting' ) |
| 160 | ); |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | $email_builder = new EVF_Email_Entries_Report( $frequency, null, $is_test ); |
| 165 | $entries_data = $email_builder->get_entries_data(); |
| 166 | $html_message = $email_builder->render_html(); |
| 167 | |
| 168 | $headers = array( |
| 169 | 'Content-Type: text/html; charset=UTF-8', |
| 170 | 'From: ' . wp_specialchars_decode( get_bloginfo( 'name' ) ) . ' <' . sanitize_email( get_option( 'admin_email' ) ) . '>', |
| 171 | ); |
| 172 | |
| 173 | $sent = wp_mail( $recipient, $subject, $html_message, $headers ); |
| 174 | |
| 175 | if ( $sent && ! $is_test ) { |
| 176 | $this->log_report_sent( |
| 177 | array( |
| 178 | 'frequency' => $frequency, |
| 179 | 'email' => $recipient, |
| 180 | 'entries' => $entries_data, |
| 181 | 'type' => 'scheduled', |
| 182 | ) |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | return $sent; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Log a report send to the history option. Keeps the last 30 records. |
| 191 | * |
| 192 | * @since 2.0.9 |
| 193 | * @param array $data Keys: frequency, email, entries, type. |
| 194 | */ |
| 195 | public function log_report_sent( $data ) { |
| 196 | $history = get_option( 'everest_forms_report_history', array() ); |
| 197 | if ( ! is_array( $history ) ) { |
| 198 | $history = array(); |
| 199 | } |
| 200 | |
| 201 | $entry_count = ( ! empty( $data['entries'] ) && is_array( $data['entries'] ) ) |
| 202 | ? array_sum( array_column( $data['entries'], 'current' ) ) |
| 203 | : 0; |
| 204 | |
| 205 | $history[] = array( |
| 206 | 'sent_at' => current_time( 'mysql' ), |
| 207 | 'frequency' => isset( $data['frequency'] ) ? $data['frequency'] : '', |
| 208 | 'recipient' => isset( $data['email'] ) ? $data['email'] : '', |
| 209 | 'form_count' => ! empty( $data['entries'] ) ? count( $data['entries'] ) : 0, |
| 210 | 'total_entries' => $entry_count, |
| 211 | 'type' => isset( $data['type'] ) ? $data['type'] : 'scheduled', |
| 212 | ); |
| 213 | |
| 214 | update_option( 'everest_forms_report_history', array_slice( $history, -30 ) ); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Get the schedule health status for display in settings. |
| 219 | * |
| 220 | * @since 2.0.9 |
| 221 | * @return array { status: string, message: string } |
| 222 | */ |
| 223 | public function get_schedule_status() { |
| 224 | $enabled = get_option( 'everest_forms_enable_entries_reporting', 'no' ); |
| 225 | |
| 226 | if ( 'yes' !== $enabled ) { |
| 227 | return array( |
| 228 | 'status' => 'disabled', |
| 229 | 'message' => __( 'Entry reporting is currently disabled.', 'everest-forms' ), |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | $next = wp_next_scheduled( self::HOOK ); |
| 234 | |
| 235 | if ( ! $next ) { |
| 236 | return array( |
| 237 | 'status' => 'not_scheduled', |
| 238 | 'message' => __( 'Reporting is enabled but no report is scheduled. Try saving settings again.', 'everest-forms' ), |
| 239 | ); |
| 240 | } |
| 241 | |
| 242 | return array( |
| 243 | 'status' => 'active', |
| 244 | 'message' => sprintf( |
| 245 | /* translators: %s: next send date/time */ |
| 246 | __( 'Next report scheduled for %s.', 'everest-forms' ), |
| 247 | get_date_from_gmt( |
| 248 | gmdate( 'Y-m-d H:i:s', $next ), |
| 249 | get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) |
| 250 | ) |
| 251 | ), |
| 252 | ); |
| 253 | } |
| 254 | } |
| 255 |