| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Scheduler; |
| 4 |
|
| 5 |
use FluentForm\App\Services\Emogrifier\Emogrifier; |
| 6 |
use FluentForm\View; |
| 7 |
|
| 8 |
class Scheduler |
| 9 |
{ |
| 10 |
public static function processEmailReport() |
| 11 |
{ |
| 12 |
self::cleanUpOldData(); |
| 13 |
$defaults = [ |
| 14 |
'status' => 'yes', |
| 15 |
'send_to_type' => 'admin_email', |
| 16 |
'custom_recipients' => '', |
| 17 |
'sending_day' => 'Mon' |
| 18 |
]; |
| 19 |
$settings = get_option('_fluentform_email_report_summary'); |
| 20 |
if($settings) { |
| 21 |
$settings = wp_parse_args($settings, $defaults); |
| 22 |
} else { |
| 23 |
$settings = $defaults; |
| 24 |
} |
| 25 |
if($settings['status'] == 'no') { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
$currentDay = date('D'); |
| 30 |
$reportingDay = $settings['sending_day']; |
| 31 |
|
| 32 |
$config = apply_filters('fluentform_email_summary_config', [ |
| 33 |
'status' => $currentDay == $reportingDay, |
| 34 |
'days' => 7 |
| 35 |
]); |
| 36 |
|
| 37 |
if (!$config['status']) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$days = $config['days']; |
| 42 |
|
| 43 |
if($settings['send_to_type'] == 'admin_email') { |
| 44 |
$recipients = [get_option('admin_email')]; |
| 45 |
} else { |
| 46 |
$custom_recipients = $settings['custom_recipients']; |
| 47 |
$custom_recipients = explode(',', $custom_recipients); |
| 48 |
$recipients = []; |
| 49 |
foreach ($custom_recipients as $recipient) { |
| 50 |
$recipient = trim($recipient); |
| 51 |
if(is_email($recipient)) { |
| 52 |
$recipients[] = $recipient; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if(!$recipients) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
// Let's grab the reports |
| 63 |
global $wpdb; |
| 64 |
$days = intval($days); |
| 65 |
if(!$days) { |
| 66 |
$days = 7; |
| 67 |
} |
| 68 |
|
| 69 |
$reportDateFrom = date('Y-m-d', time() - $days * 86400); // 7 days |
| 70 |
$submissionCounts = wpFluent()->table('fluentform_submissions') |
| 71 |
->select([ |
| 72 |
wpFluent()->raw("COUNT({$wpdb->prefix}fluentform_submissions.id) as total"), |
| 73 |
'fluentform_submissions.form_id', |
| 74 |
'fluentform_forms.title' |
| 75 |
]) |
| 76 |
->groupBy('fluentform_submissions.form_id') |
| 77 |
->orderBy('total', 'DESC') |
| 78 |
->where('fluentform_submissions.created_at', '>', $reportDateFrom) |
| 79 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_submissions.form_id') |
| 80 |
->limit(15) |
| 81 |
->get(); |
| 82 |
|
| 83 |
foreach ($submissionCounts as $submissionCount) { |
| 84 |
$submissionCount->permalink = admin_url('admin.php?page=fluent_forms&route=entries&form_id='.$submissionCount->form_id); |
| 85 |
} |
| 86 |
|
| 87 |
if(!$submissionCounts) { |
| 88 |
return; // Nothing found |
| 89 |
} |
| 90 |
|
| 91 |
$paymentCounts = []; |
| 92 |
if(defined('FLUENTFORMPRO') && get_option('__fluentform_payment_module_settings')) { |
| 93 |
$paymentCounts = wpFluent()->table('fluentform_transactions') |
| 94 |
->select([ |
| 95 |
wpFluent()->raw("SUM({$wpdb->prefix}fluentform_transactions.payment_total) as total_amount"), |
| 96 |
'fluentform_transactions.form_id', |
| 97 |
'fluentform_transactions.currency', |
| 98 |
'fluentform_forms.title' |
| 99 |
]) |
| 100 |
->groupBy('fluentform_transactions.form_id') |
| 101 |
->orderBy('total_amount', 'DESC') |
| 102 |
->where('fluentform_transactions.created_at', '>', $reportDateFrom) |
| 103 |
->where('fluentform_transactions.status', '=', 'paid') |
| 104 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_transactions.form_id') |
| 105 |
->limit(15) |
| 106 |
->get(); |
| 107 |
foreach ($paymentCounts as $paymentCount) { |
| 108 |
$paymentCount->readable_amount = $paymentCount->currency.' '.number_format($paymentCount->total_amount / 100); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
$data = array( |
| 113 |
'submissions' => $submissionCounts, |
| 114 |
'payments' => $paymentCounts, |
| 115 |
'days' => $days |
| 116 |
); |
| 117 |
$emailBody = View::make('email.report.body', $data); |
| 118 |
|
| 119 |
$emailBody = apply_filters('fluentform_email_summary_body', $emailBody, $data); |
| 120 |
|
| 121 |
$originalEmailBody = $emailBody; |
| 122 |
|
| 123 |
ob_start(); |
| 124 |
try { |
| 125 |
// apply CSS styles inline for picky email clients |
| 126 |
$emogrifier = new Emogrifier($emailBody); |
| 127 |
$emailBody = $emogrifier->emogrify(); |
| 128 |
} catch (\Exception $e) { |
| 129 |
|
| 130 |
} |
| 131 |
$maybeError = ob_get_clean(); |
| 132 |
|
| 133 |
if ($maybeError) { |
| 134 |
$emailBody = $originalEmailBody; |
| 135 |
} |
| 136 |
|
| 137 |
$headers = [ |
| 138 |
'Content-Type: text/html; charset=utf-8' |
| 139 |
]; |
| 140 |
|
| 141 |
$emailSubject = sprintf( esc_html__( 'Email Summary of Your Forms (Last %d Days)', 'my-text-domain' ), $days ); |
| 142 |
|
| 143 |
$emailSubject = apply_filters('fluentform_email_summary_subject', $emailSubject); |
| 144 |
|
| 145 |
$emailResult = wp_mail($recipients, $emailSubject, $emailBody, $headers); |
| 146 |
|
| 147 |
do_action('fluentform_email_summary_details', [ |
| 148 |
'recipients' => $recipients, |
| 149 |
'email_subject' => $emailSubject, |
| 150 |
'email_body' => $emailBody |
| 151 |
], $data, $emailResult); |
| 152 |
|
| 153 |
return $emailResult; |
| 154 |
} |
| 155 |
|
| 156 |
private static function cleanUpOldData() |
| 157 |
{ |
| 158 |
$deleteDaysCount = apply_filters('fluentform_cleanup_days_count', 60); |
| 159 |
if(!$deleteDaysCount) { |
| 160 |
return; |
| 161 |
} |
| 162 |
$seconds = $deleteDaysCount * 86400; |
| 163 |
$deleteTo = date('Y-m-d H:i:s', time() - $seconds); |
| 164 |
// delete 60 days old analytics data |
| 165 |
wpFluent()->table('fluentform_form_analytics') |
| 166 |
->where('created_at', '<', $deleteTo) |
| 167 |
->delete(); |
| 168 |
|
| 169 |
// delete 60 days old scheduled_actions data |
| 170 |
wpFluent()->table('ff_scheduled_actions') |
| 171 |
->where('created_at', '<', $deleteTo) |
| 172 |
->delete(); |
| 173 |
|
| 174 |
} |
| 175 |
} |