AJAX
3 years ago
Date_Range
3 years ago
Interval
3 years ago
Menu_Bar_Stats
3 years ago
Migrations
3 years ago
Models
3 years ago
Queries
3 years ago
Statistics
3 years ago
Tables
3 years ago
Utils
3 years ago
Campaign_Builder.php
3 years ago
Capability_Manager.php
3 years ago
Chart.php
3 years ago
Chart_Geo.php
3 years ago
Chart_SVG.php
3 years ago
Dashboard_Options.php
3 years ago
Dashboard_Widget.php
3 years ago
Email_Reports.php
3 years ago
Filters.php
3 years ago
Geo_Database.php
3 years ago
Geo_Database_Download_Job.php
3 years ago
Health_Check.php
3 years ago
Illuminate_Builder.php
3 years ago
Independent_Analytics.php
3 years ago
Known_Referrers.php
3 years ago
PDF.php
3 years ago
Query.php
3 years ago
Quick_Stats.php
3 years ago
REST_API.php
3 years ago
Real_Time.php
3 years ago
Reset_Database.php
3 years ago
Resource_Identifier.php
3 years ago
Settings.php
3 years ago
Track_Resource_Changes.php
3 years ago
View.php
3 years ago
View_Counter.php
3 years ago
WP_Option_Cache_Bust.php
3 years ago
WooCommerce_Order.php
3 years ago
Email_Reports.php
57 lines
| 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 |