AJAX
2 years ago
Date_Range
2 years ago
Interval
3 years ago
Menu_Bar_Stats
2 years ago
Migrations
2 years ago
Models
2 years ago
Queries
2 years ago
Statistics
2 years ago
Tables
2 years ago
Utils
2 years ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
3 years ago
Chart.php
3 years ago
Chart_Geo.php
2 years ago
Chart_SVG.php
3 years ago
City_To_Country_Converter.php
3 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
3 years ago
Email_Chart.php
2 years ago
Email_Reports.php
2 years ago
Env.php
2 years ago
Filters.php
2 years ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Downloader.php
2 years ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
3 years ago
Independent_Analytics.php
2 years ago
Interrupt.php
2 years ago
Known_Referrers.php
3 years ago
Plugin_Conflict_Detector.php
2 years ago
Query.php
2 years ago
Quick_Stats.php
2 years ago
REST_API.php
2 years ago
Real_Time.php
2 years ago
Reset_Database.php
3 years ago
Resource_Identifier.php
3 years ago
Settings.php
2 years ago
Sort_Configuration.php
3 years ago
Track_Resource_Changes.php
3 years ago
View.php
2 years ago
View_Counter.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
WooCommerce_Order.php
2 years ago
Email_Reports.php
109 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP; |
| 4 | |
| 5 | use IAWP_SCOPED\IAWP\Date_Range\Exact_Date_Range; |
| 6 | use IAWP_SCOPED\IAWP\Date_Range\Relative_Date_Range; |
| 7 | use IAWP_SCOPED\IAWP\Queries\Campaigns; |
| 8 | use IAWP_SCOPED\IAWP\Queries\Countries; |
| 9 | use IAWP_SCOPED\IAWP\Queries\Referrers; |
| 10 | use IAWP_SCOPED\IAWP\Queries\Resources; |
| 11 | use IAWP_SCOPED\IAWP\Statistics\Page_Statistics; |
| 12 | use IAWP_SCOPED\IAWP\Utils\URL; |
| 13 | class Email_Reports |
| 14 | { |
| 15 | public function __construct() |
| 16 | { |
| 17 | \add_filter('cron_schedules', [$this, 'add_monthly_schedule_cron']); |
| 18 | $monitored_options = ['iawp_email_report_time', 'iawp_email_report_email_addresses']; |
| 19 | foreach ($monitored_options as $option) { |
| 20 | \add_action('update_option_' . $option, [$this, 'schedule_email_report'], 10, 0); |
| 21 | \add_action('add_option_' . $option, [$this, 'schedule_email_report'], 10, 0); |
| 22 | } |
| 23 | \add_action('iawp_send_email_report', [$this, 'send_email_report']); |
| 24 | } |
| 25 | public function schedule_email_report() |
| 26 | { |
| 27 | $this->unschedule_email_report(); |
| 28 | if (empty(\IAWP_SCOPED\iawp()->get_option('iawp_email_report_email_addresses', []))) { |
| 29 | return; |
| 30 | } |
| 31 | $delivery_time = new \DateTime('first day of +1 month', new \DateTimeZone(\wp_timezone_string())); |
| 32 | $delivery_time->setTime(\IAWP_SCOPED\iawp()->get_option('iawp_email_report_time', 9), 0); |
| 33 | \wp_schedule_event($delivery_time->getTimestamp(), 'monthly', 'iawp_send_email_report'); |
| 34 | } |
| 35 | public function unschedule_email_report() |
| 36 | { |
| 37 | $timestamp = \wp_next_scheduled('iawp_send_email_report'); |
| 38 | \wp_unschedule_event($timestamp, 'iawp_send_email_report'); |
| 39 | } |
| 40 | public function add_monthly_schedule_cron($schedules) |
| 41 | { |
| 42 | $schedules['monthly'] = ['interval' => \MONTH_IN_SECONDS, 'display' => \esc_html__('Once a Month', 'independent-analytics')]; |
| 43 | return $schedules; |
| 44 | } |
| 45 | public function send_email_report(bool $test = \false) |
| 46 | { |
| 47 | $to = \IAWP_SCOPED\iawp()->get_option('iawp_email_report_email_addresses', []); |
| 48 | if (empty($to)) { |
| 49 | return; |
| 50 | } |
| 51 | $subject = \sprintf(\esc_html__('Analytics Report for %1$s [%2$s]', 'independent-analytics'), \get_bloginfo('name'), (new \DateTime('-1 month', new \DateTimeZone(\wp_timezone_string())))->format('F Y')); |
| 52 | if ($test) { |
| 53 | $subject = \esc_html__('[Test]', 'independent-analytics') . ' ' . $subject; |
| 54 | } |
| 55 | $body = $this->get_email_body(); |
| 56 | $headers[] = 'From: ' . \get_bloginfo('name') . ' <' . \get_bloginfo('admin_email') . '>'; |
| 57 | $headers[] = 'Content-Type: text/html; charset=UTF-8'; |
| 58 | return \wp_mail($to, $subject, $body, $headers); |
| 59 | } |
| 60 | private function get_email_body() |
| 61 | { |
| 62 | $statistics = new Page_Statistics(new Relative_Date_Range('LAST_MONTH')); |
| 63 | $quick_stats = (new Quick_Stats(null, $statistics))->get_stats(); |
| 64 | $chart = new Email_Chart($statistics); |
| 65 | return \IAWP_SCOPED\iawp_blade()->run('email.email', ['site_title' => \get_bloginfo('name'), 'site_url' => (new URL(\get_site_url()))->get_domain(), 'date' => (new \DateTime('Last month', new \DateTimeZone(\wp_timezone_string())))->format('F Y'), 'stats' => $quick_stats, 'top_ten' => $this->get_top_ten(), 'chart_views' => $chart->daily_views, 'most_views' => $chart->most_views, 'y_labels' => $chart->y_labels, 'x_labels' => $chart->x_labels, 'colors' => \get_option('iawp_email_report_colors', ['#5123a0', '#fafafa', '#3a1e6b', '#fafafa', '#5123a0', '#a985e6'])]); |
| 66 | } |
| 67 | private function get_top_ten() : array |
| 68 | { |
| 69 | $start = new \DateTime('First day of last month', new \DateTimeZone(\wp_timezone_string())); |
| 70 | $end = new \DateTime('Last day of last month', new \DateTimeZone(\wp_timezone_string())); |
| 71 | $date_range = new Exact_Date_Range($start, $end); |
| 72 | $queries = ['pages' => 'title', 'referrers' => 'referrer', 'countries' => 'country', 'campaigns' => 'title']; |
| 73 | $top_ten = []; |
| 74 | foreach ($queries as $type => $title) { |
| 75 | if ($type === 'pages') { |
| 76 | $query = new Resources($date_range); |
| 77 | } elseif ($type === 'referrers') { |
| 78 | $query = new Referrers($date_range); |
| 79 | } elseif ($type === 'countries') { |
| 80 | $query = new Countries($date_range); |
| 81 | } elseif ($type === 'campaigns') { |
| 82 | $query = new Campaigns($date_range); |
| 83 | } else { |
| 84 | continue; |
| 85 | } |
| 86 | $data = $query->rows(); |
| 87 | \usort($data, function ($a, $b) { |
| 88 | $a = $a->views(); |
| 89 | $b = $b->views(); |
| 90 | if ($a < $b) { |
| 91 | return 1; |
| 92 | } elseif ($a > $b) { |
| 93 | return -1; |
| 94 | } else { |
| 95 | return 0; |
| 96 | } |
| 97 | }); |
| 98 | $data = \array_slice($data, 0, 10); |
| 99 | $rows = \array_map(function ($row, $index) use($title) { |
| 100 | $edited_title = $row->{$title}(); |
| 101 | $edited_title = \mb_strlen($edited_title) > 30 ? \mb_substr($edited_title, 0, 30) . '...' : $edited_title; |
| 102 | return ['title' => $edited_title, 'views' => $row->views()]; |
| 103 | }, $data, \array_keys($data)); |
| 104 | $top_ten[$type] = $rows; |
| 105 | } |
| 106 | return $top_ten; |
| 107 | } |
| 108 | } |
| 109 |