PluginProbe
Independent Analytics – WordPress Analytics Plugin / 1.24.0
Independent Analytics – WordPress Analytics Plugin v1.24.0
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 All 120 releases
independent-analytics / IAWP / Email_Reports.php
Email_Reports.php
57 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IAWP_SCOPED\IAWP;
4
5 class Email_Reports
6 {
7 public function __construct()
8 {
9 \add_filter('cron_schedules', [$this, 'add_monthly_schedule_cron']);
10 $monitored_options = ['iawp_email_report_time', 'iawp_email_report_message', 'iawp_email_report_email_addresses'];
11 foreach ($monitored_options as $option) {
12 \add_action('update_option_' . $option, [$this, 'schedule_email_report'], 10, 0);
13 \add_action('add_option_' . $option, [$this, 'schedule_email_report'], 10, 0);
14 }
15 \add_action('iawp_send_email_report', [$this, 'send_email_report']);
16 }
17 public function schedule_email_report()
18 {
19 $this->unschedule_email_report();
20 if (empty(\IAWP_SCOPED\iawp()->get_option('iawp_email_report_email_addresses', []))) {
21 return;
22 }
23 $delivery_time = new \DateTime('first day of +1 month', new \DateTimeZone(\wp_timezone_string()));
24 $delivery_time->setTime(\IAWP_SCOPED\iawp()->get_option('iawp_email_report_time', 9), 0);
25 \wp_schedule_event($delivery_time->getTimestamp(), 'monthly', 'iawp_send_email_report');
26 }
27 public function unschedule_email_report()
28 {
29 $timestamp = \wp_next_scheduled('iawp_send_email_report');
30 \wp_unschedule_event($timestamp, 'iawp_send_email_report');
31 }
32 public function add_monthly_schedule_cron($schedules)
33 {
34 $schedules['monthly'] = ['interval' => \MONTH_IN_SECONDS, 'display' => \esc_html__('Once a Month', 'independent-analytics')];
35 return $schedules;
36 }
37 public function send_email_report(bool $test = \false)
38 {
39 $to = \IAWP_SCOPED\iawp()->get_option('iawp_email_report_email_addresses', []);
40 if (empty($to)) {
41 return;
42 }
43 $pdf = (new PDF())->create_and_save_pdf();
44 if (\is_null($pdf)) {
45 return;
46 }
47 $date = (new \DateTime('-1 month'))->format('F Y');
48 $subject = \sprintf(\esc_html__('Analytics Report for %1$s [%2$s]', 'independent-analytics'), \get_bloginfo('name'), $date);
49 if ($test) {
50 $subject = \esc_html__('[Test]', 'independent-analytics') . ' ' . $subject;
51 }
52 $message = \IAWP_SCOPED\iawp()->get_option('iawp_email_report_message', \esc_html__("Please find the performance report attached to this email. It includes last month's views & visitors, as well as the top pages, referrers, and countries.", 'independent-analytics'));
53 $headers[] = 'From: ' . \get_bloginfo('name') . ' <' . \get_bloginfo('admin_email') . '>';
54 return \wp_mail($to, $subject, $message, $headers, $pdf);
55 }
56 }
57