| @@ -1,15 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentForm\App\Services\Scheduler; |
| 4 | 4 | |
| 5 | +use FluentForm\App\Models\FormAnalytics; | |
| 6 | +use FluentForm\App\Models\Submission; | |
| 7 | +use FluentForm\App\Modules\Payments\PaymentHelper; | |
| 5 | 8 | use FluentForm\App\Services\Emogrifier\Emogrifier; |
| 6 | -use FluentForm\View; | |
| 7 | 9 | |
| 8 | 10 | class Scheduler |
| 9 | 11 | { |
| 10 | 12 | public static function processEmailReport() |
| 11 | 13 | { |
| 14 | + self::cleanUpOldData(); | |
| 12 | 15 | $defaults = [ |
| 13 | 16 | 'status' => 'yes', |
| 14 | 17 | 'send_to_type' => 'admin_email', |
| 15 | 18 | 'custom_recipients' => '', |
| @@ -20,8 +23,10 @@ | ||
| 20 | 23 | $settings = wp_parse_args($settings, $defaults); |
| 21 | 24 | } else { |
| 22 | 25 | $settings = $defaults; |
| 23 | 26 | } |
| 27 | + $settings = apply_filters('fluentform/email_summary_settings', $settings); | |
| 28 | + | |
| 24 | 29 | if($settings['status'] == 'no') { |
| 25 | 30 | return; |
| 26 | 31 | } |
| 27 | 32 | |
| @@ -26,13 +31,23 @@ | ||
| 26 | 31 | } |
| 27 | 32 | |
| 28 | 33 | $currentDay = date('D'); |
| 29 | 34 | $reportingDay = $settings['sending_day']; |
| 35 | + | |
| 36 | + $config = apply_filters_deprecated( | |
| 37 | + 'fluentform_email_summary_config', | |
| 38 | + [ | |
| 39 | + [ | |
| 40 | + 'status' => $currentDay == $reportingDay, | |
| 41 | + 'days' => 7 | |
| 42 | + ] | |
| 43 | + ], | |
| 44 | + FLUENTFORM_FRAMEWORK_UPGRADE, | |
| 45 | + 'fluentform/email_summary_config', | |
| 46 | + 'Use fluentform/email_summary_config instead of fluentform_email_summary_config.' | |
| 47 | + ); | |
| 30 | 48 | |
| 31 | - $config = apply_filters('fluentform_email_summary_config', [ | |
| 32 | - 'status' => $currentDay == $reportingDay, | |
| 33 | - 'days' => 7 | |
| 34 | - ]); | |
| 49 | + $config = apply_filters('fluentform/email_summary_config', $config); | |
| 35 | 50 | |
| 36 | 51 | if (!$config['status']) { |
| 37 | 52 | return; |
| 38 | 53 | } |
| @@ -65,10 +80,9 @@ | ||
| 65 | 80 | $days = 7; |
| 66 | 81 | } |
| 67 | 82 | |
| 68 | 83 | $reportDateFrom = date('Y-m-d', time() - $days * 86400); // 7 days |
| 69 | - $submissionCounts = wpFluent()->table('fluentform_submissions') | |
| 70 | - ->select([ | |
| 84 | + $submissionCounts = Submission::select([ | |
| 71 | 85 | wpFluent()->raw("COUNT({$wpdb->prefix}fluentform_submissions.id) as total"), |
| 72 | 86 | 'fluentform_submissions.form_id', |
| 73 | 87 | 'fluentform_forms.title' |
| 74 | 88 | ]) |
| @@ -81,17 +95,15 @@ | ||
| 81 | 95 | |
| 82 | 96 | foreach ($submissionCounts as $submissionCount) { |
| 83 | 97 | $submissionCount->permalink = admin_url('admin.php?page=fluent_forms&route=entries&form_id='.$submissionCount->form_id); |
| 84 | 98 | } |
| 85 | - | |
| 86 | - if(!$submissionCounts) { | |
| 99 | + if(!$submissionCounts || count($submissionCounts) === 0) { | |
| 87 | 100 | return; // Nothing found |
| 88 | 101 | } |
| 89 | 102 | |
| 90 | 103 | $paymentCounts = []; |
| 91 | - if(defined('FLUENTFORMPRO') && get_option('__fluentform_payment_module_settings')) { | |
| 92 | - $paymentCounts = wpFluent()->table('fluentform_transactions') | |
| 93 | - ->select([ | |
| 104 | + if(PaymentHelper::hasPaymentSettings()) { | |
| 105 | + $paymentCounts = \FluentForm\App\Models\Transaction::select([ | |
| 94 | 106 | wpFluent()->raw("SUM({$wpdb->prefix}fluentform_transactions.payment_total) as total_amount"), |
| 95 | 107 | 'fluentform_transactions.form_id', |
| 96 | 108 | 'fluentform_transactions.currency', |
| 97 | 109 | 'fluentform_forms.title' |
| @@ -107,14 +119,28 @@ | ||
| 107 | 119 | $paymentCount->readable_amount = $paymentCount->currency.' '.number_format($paymentCount->total_amount / 100); |
| 108 | 120 | } |
| 109 | 121 | } |
| 110 | 122 | |
| 111 | - $emailBody = View::make('email.report.body', array( | |
| 123 | + $data = array( | |
| 112 | 124 | 'submissions' => $submissionCounts, |
| 113 | 125 | 'payments' => $paymentCounts, |
| 114 | 126 | 'days' => $days |
| 115 | - )); | |
| 127 | + ); | |
| 128 | + $emailBody = wpFluentForm('view')->make('email.report.body', $data); | |
| 129 | + | |
| 130 | + $emailBody = apply_filters_deprecated( | |
| 131 | + 'fluentform_email_summary_body', | |
| 132 | + [ | |
| 133 | + $emailBody, | |
| 134 | + $data | |
| 135 | + ], | |
| 136 | + FLUENTFORM_FRAMEWORK_UPGRADE, | |
| 137 | + 'fluentform/email_summary_body', | |
| 138 | + 'Use fluentform/email_summary_body instead of fluentform_email_summary_body.' | |
| 139 | + ); | |
| 116 | 140 | |
| 141 | + $emailBody = apply_filters('fluentform/email_summary_body', $emailBody, $data); | |
| 142 | + | |
| 117 | 143 | $originalEmailBody = $emailBody; |
| 118 | 144 | |
| 119 | 145 | ob_start(); |
| 120 | 146 | try { |
| @@ -132,12 +158,98 @@ | ||
| 132 | 158 | |
| 133 | 159 | $headers = [ |
| 134 | 160 | 'Content-Type: text/html; charset=utf-8' |
| 135 | 161 | ]; |
| 162 | + /* translators: %s is the Number Email Summary Days */ | |
| 163 | + $emailSubject = sprintf(esc_html__('Email Summary of Your Forms (Last %d Days)', 'fluentform'), $days); | |
| 136 | 164 | |
| 137 | - $emailSubject = sprintf( esc_html__( 'Email Summary of Your Forms (Last %d Days)', 'my-text-domain' ), $days ); | |
| 165 | + if (isset($settings['subject']) && $settings['subject']) { | |
| 166 | + $emailSubject = $settings['subject']; | |
| 167 | + } | |
| 168 | + | |
| 169 | + $emailSubject = apply_filters_deprecated( | |
| 170 | + 'fluentform_email_summary_subject', | |
| 171 | + [ | |
| 172 | + $emailSubject | |
| 173 | + ], | |
| 174 | + FLUENTFORM_FRAMEWORK_UPGRADE, | |
| 175 | + 'fluentform/email_summary_subject', | |
| 176 | + 'Use fluentform/email_summary_subject instead of fluentform_email_summary_subject' | |
| 177 | + ); | |
| 178 | + $emailSubject = apply_filters('fluentform/email_summary_subject', $emailSubject); | |
| 138 | 179 | |
| 139 | - $emailSubject = apply_filters('fluentform_email_summary_subject', $emailSubject); | |
| 180 | + $emailResult = wp_mail($recipients, $emailSubject, $emailBody, $headers); | |
| 140 | 181 | |
| 141 | - return wp_mail($recipients, $emailSubject, $emailBody, $headers); | |
| 182 | + do_action_deprecated( | |
| 183 | + 'fluentform_email_summary_details', | |
| 184 | + [ | |
| 185 | + [ | |
| 186 | + 'recipients' => $recipients, | |
| 187 | + 'email_subject' => $emailSubject, | |
| 188 | + 'email_body' => $emailBody | |
| 189 | + ], | |
| 190 | + $data, | |
| 191 | + $emailResult | |
| 192 | + ], | |
| 193 | + FLUENTFORM_FRAMEWORK_UPGRADE, | |
| 194 | + 'fluentform/email_summary_details', | |
| 195 | + 'Use fluentform/email_summary_details instead of fluentform_email_summary_details.' | |
| 196 | + ); | |
| 197 | + | |
| 198 | + do_action('fluentform/email_summary_details', [ | |
| 199 | + 'recipients' => $recipients, | |
| 200 | + 'email_subject' => $emailSubject, | |
| 201 | + 'email_body' => $emailBody | |
| 202 | + ], $data, $emailResult); | |
| 203 | + | |
| 204 | + return $emailResult; | |
| 142 | 205 | } |
| 143 | -} | |
| 206 | + | |
| 207 | + private static function cleanUpOldData() | |
| 208 | + { | |
| 209 | + $days = 60; | |
| 210 | + $days = apply_filters_deprecated( | |
| 211 | + 'fluentform_cleanup_days_count', | |
| 212 | + [ | |
| 213 | + $days | |
| 214 | + ], | |
| 215 | + FLUENTFORM_FRAMEWORK_UPGRADE, | |
| 216 | + 'fluentform/cleanup_days_count', | |
| 217 | + 'Use fluentform/cleanup_days_count instead of fluentform_cleanup_days_count.' | |
| 218 | + ); | |
| 219 | + $deleteDaysCount = apply_filters('fluentform/cleanup_days_count', $days); | |
| 220 | + if(!$deleteDaysCount) { | |
| 221 | + return; | |
| 222 | + } | |
| 223 | + $seconds = $deleteDaysCount * 86400; | |
| 224 | + $deleteTo = date('Y-m-d H:i:s', time() - $seconds); | |
| 225 | + // delete 60 days old analytics data | |
| 226 | + FormAnalytics::where('created_at', '<', $deleteTo) | |
| 227 | + ->delete(); | |
| 228 | + | |
| 229 | + // delete 60 days old scheduled_actions data | |
| 230 | + \FluentForm\App\Models\Scheduler::where('created_at', '<', $deleteTo) | |
| 231 | + ->delete(); | |
| 232 | + | |
| 233 | + // Clean temp uploads. Default threshold is 2 days. Honors the | |
| 234 | + // fluentform/temp_file_delete_time filter so multi-step and | |
| 235 | + // save-and-resume users can extend the window — same filter | |
| 236 | + // Pro's Uploader::removeOldTempFiles() already respects. Without | |
| 237 | + // this, the Scheduler cleanup would wipe temp files at 2 days | |
| 238 | + // regardless of the filter, breaking multi-step uploads. | |
| 239 | + if (defined('FLUENTFORM_UPLOAD_DIR')) { | |
| 240 | + $tempDir = wp_upload_dir()['basedir'] . FLUENTFORM_UPLOAD_DIR . '/temp/'; | |
| 241 | + if (is_dir($tempDir)) { | |
| 242 | + $files = glob($tempDir . '*'); | |
| 243 | + if (!empty($files)) { | |
| 244 | + $maxFileAge = apply_filters('fluentform/temp_file_delete_time', 172800); | |
| 245 | + $cutoff = time() - $maxFileAge; | |
| 246 | + foreach ($files as $file) { | |
| 247 | + if (is_file($file) && basename($file) !== 'index.php' && filemtime($file) < $cutoff) { | |
| 248 | + wp_delete_file($file); | |
| 249 | + } | |
| 250 | + } | |
| 251 | + } | |
| 252 | + } | |
| 253 | + } | |
| 254 | + } | |
| 255 | +} | |