| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Scheduler; |
| 4 |
|
| 5 |
use FluentForm\App\Models\FormAnalytics; |
| 6 |
use FluentForm\App\Models\Submission; |
| 7 |
use FluentForm\App\Modules\Payments\PaymentHelper; |
| 8 |
use FluentForm\App\Services\Emogrifier\Emogrifier; |
| 9 |
|
| 10 |
class Scheduler |
| 11 |
{ |
| 12 |
public static function processEmailReport() |
| 13 |
{ |
| 14 |
self::cleanUpOldData(); |
| 15 |
$defaults = [ |
| 16 |
'status' => 'yes', |
| 17 |
'send_to_type' => 'admin_email', |
| 18 |
'custom_recipients' => '', |
| 19 |
'sending_day' => 'Mon' |
| 20 |
]; |
| 21 |
$settings = get_option('_fluentform_email_report_summary'); |
| 22 |
if($settings) { |
| 23 |
$settings = wp_parse_args($settings, $defaults); |
| 24 |
} else { |
| 25 |
$settings = $defaults; |
| 26 |
} |
| 27 |
$settings = apply_filters('fluentform/email_summary_settings', $settings); |
| 28 |
|
| 29 |
if($settings['status'] == 'no') { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
$currentDay = date('D'); |
| 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 |
); |
| 48 |
|
| 49 |
$config = apply_filters('fluentform/email_summary_config', $config); |
| 50 |
|
| 51 |
if (!$config['status']) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$days = $config['days']; |
| 56 |
|
| 57 |
if($settings['send_to_type'] == 'admin_email') { |
| 58 |
$recipients = [get_option('admin_email')]; |
| 59 |
} else { |
| 60 |
$custom_recipients = $settings['custom_recipients']; |
| 61 |
$custom_recipients = explode(',', $custom_recipients); |
| 62 |
$recipients = []; |
| 63 |
foreach ($custom_recipients as $recipient) { |
| 64 |
$recipient = trim($recipient); |
| 65 |
if(is_email($recipient)) { |
| 66 |
$recipients[] = $recipient; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
if(!$recipients) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
// Let's grab the reports |
| 77 |
global $wpdb; |
| 78 |
$days = intval($days); |
| 79 |
if(!$days) { |
| 80 |
$days = 7; |
| 81 |
} |
| 82 |
|
| 83 |
$reportDateFrom = date('Y-m-d', time() - $days * 86400); // 7 days |
| 84 |
$submissionCounts = Submission::select([ |
| 85 |
wpFluent()->raw("COUNT({$wpdb->prefix}fluentform_submissions.id) as total"), |
| 86 |
'fluentform_submissions.form_id', |
| 87 |
'fluentform_forms.title' |
| 88 |
]) |
| 89 |
->groupBy('fluentform_submissions.form_id') |
| 90 |
->orderBy('total', 'DESC') |
| 91 |
->where('fluentform_submissions.created_at', '>', $reportDateFrom) |
| 92 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_submissions.form_id') |
| 93 |
->limit(15) |
| 94 |
->get(); |
| 95 |
|
| 96 |
foreach ($submissionCounts as $submissionCount) { |
| 97 |
$submissionCount->permalink = admin_url('admin.php?page=fluent_forms&route=entries&form_id='.$submissionCount->form_id); |
| 98 |
} |
| 99 |
if(!$submissionCounts || count($submissionCounts) === 0) { |
| 100 |
return; // Nothing found |
| 101 |
} |
| 102 |
|
| 103 |
$paymentCounts = []; |
| 104 |
if(PaymentHelper::hasPaymentSettings()) { |
| 105 |
$paymentCounts = \FluentForm\App\Models\Transaction::select([ |
| 106 |
wpFluent()->raw("SUM({$wpdb->prefix}fluentform_transactions.payment_total) as total_amount"), |
| 107 |
'fluentform_transactions.form_id', |
| 108 |
'fluentform_transactions.currency', |
| 109 |
'fluentform_forms.title' |
| 110 |
]) |
| 111 |
->groupBy('fluentform_transactions.form_id') |
| 112 |
->orderBy('total_amount', 'DESC') |
| 113 |
->where('fluentform_transactions.created_at', '>', $reportDateFrom) |
| 114 |
->where('fluentform_transactions.status', '=', 'paid') |
| 115 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_transactions.form_id') |
| 116 |
->limit(15) |
| 117 |
->get(); |
| 118 |
foreach ($paymentCounts as $paymentCount) { |
| 119 |
$paymentCount->readable_amount = $paymentCount->currency.' '.number_format($paymentCount->total_amount / 100); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$data = array( |
| 124 |
'submissions' => $submissionCounts, |
| 125 |
'payments' => $paymentCounts, |
| 126 |
'days' => $days |
| 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 |
); |
| 140 |
|
| 141 |
$emailBody = apply_filters('fluentform/email_summary_body', $emailBody, $data); |
| 142 |
|
| 143 |
$originalEmailBody = $emailBody; |
| 144 |
|
| 145 |
ob_start(); |
| 146 |
try { |
| 147 |
// apply CSS styles inline for picky email clients |
| 148 |
$emogrifier = new Emogrifier($emailBody); |
| 149 |
$emailBody = $emogrifier->emogrify(); |
| 150 |
} catch (\Exception $e) { |
| 151 |
|
| 152 |
} |
| 153 |
$maybeError = ob_get_clean(); |
| 154 |
|
| 155 |
if ($maybeError) { |
| 156 |
$emailBody = $originalEmailBody; |
| 157 |
} |
| 158 |
|
| 159 |
$headers = [ |
| 160 |
'Content-Type: text/html; charset=utf-8' |
| 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); |
| 164 |
|
| 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); |
| 179 |
|
| 180 |
$emailResult = wp_mail($recipients, $emailSubject, $emailBody, $headers); |
| 181 |
|
| 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; |
| 205 |
} |
| 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 older than 2 days |
| 234 |
if (defined('FLUENTFORM_UPLOAD_DIR')) { |
| 235 |
$tempDir = wp_upload_dir()['basedir'] . FLUENTFORM_UPLOAD_DIR . '/temp/'; |
| 236 |
if (is_dir($tempDir)) { |
| 237 |
$files = glob($tempDir . '*'); |
| 238 |
if (!empty($files)) { |
| 239 |
$twoDaysAgo = time() - 172800; |
| 240 |
foreach ($files as $file) { |
| 241 |
if (is_file($file) && basename($file) !== 'index.php' && filemtime($file) < $twoDaysAgo) { |
| 242 |
wp_delete_file($file); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|