PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.41
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.41
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / Scheduler / Scheduler.php

Scheduler.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.41, at app/Services/Scheduler/Scheduler.php

164 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $emailBody = View::make('email.report.body', array(
113 'submissions' => $submissionCounts,
114 'payments' => $paymentCounts,
115 'days' => $days
116 ));
117
118 $originalEmailBody = $emailBody;
119
120 ob_start();
121 try {
122 // apply CSS styles inline for picky email clients
123 $emogrifier = new Emogrifier($emailBody);
124 $emailBody = $emogrifier->emogrify();
125 } catch (\Exception $e) {
126
127 }
128 $maybeError = ob_get_clean();
129
130 if ($maybeError) {
131 $emailBody = $originalEmailBody;
132 }
133
134 $headers = [
135 'Content-Type: text/html; charset=utf-8'
136 ];
137
138 $emailSubject = sprintf( esc_html__( 'Email Summary of Your Forms (Last %d Days)', 'my-text-domain' ), $days );
139
140 $emailSubject = apply_filters('fluentform_email_summary_subject', $emailSubject);
141
142 return wp_mail($recipients, $emailSubject, $emailBody, $headers);
143 }
144
145 private static function cleanUpOldData()
146 {
147 $deleteDaysCount = apply_filters('fluentform_cleanup_days_count', 60);
148 if(!$deleteDaysCount) {
149 return;
150 }
151 $seconds = $deleteDaysCount * 86400;
152 $deleteTo = date('Y-m-d H:i:s', time() - $seconds);
153 // delete 60 days old analytics data
154 wpFluent()->table('fluentform_form_analytics')
155 ->where('created_at', '<', $deleteTo)
156 ->delete();
157
158 // delete 60 days old scheduled_actions data
159 wpFluent()->table('ff_scheduled_actions')
160 ->where('created_at', '<', $deleteTo)
161 ->delete();
162
163 }
164 }