AJAX
2 years ago
Admin_Page
2 years ago
Date_Range
2 years ago
Filter_Lists
2 years ago
Interval
2 years ago
Menu_Bar_Stats
2 years ago
Migrations
2 years ago
Models
2 years ago
Public_API
2 years ago
Rows
2 years ago
Statistics
2 years ago
Tables
2 years ago
Utils
2 years ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
2 years ago
Chart.php
2 years ago
Chart_Geo.php
2 years ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database.php
2 years ago
Database_Manager.php
2 years ago
Email_Chart.php
2 years ago
Email_Reports.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
2 years ago
Filters.php
2 years ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.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
2 years ago
Independent_Analytics.php
2 years ago
Interrupt.php
2 years ago
Known_Referrers.php
2 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
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
2 years ago
Resource_Identifier.php
2 years ago
Settings.php
2 years ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
2 years ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
WooCommerce_Order.php
2 years ago
WooCommerce_Referrer_Meta_Box.php
2 years ago
Email_Reports.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWP\Date_Range\Exact_Date_Range; |
| 6 | use IAWP\Date_Range\Relative_Date_Range; |
| 7 | use IAWP\Rows\Campaigns; |
| 8 | use IAWP\Rows\Countries; |
| 9 | use IAWP\Rows\Pages; |
| 10 | use IAWP\Rows\Referrers; |
| 11 | use IAWP\Statistics\Page_Statistics; |
| 12 | use IAWP\Utils\URL; |
| 13 | /** @internal */ |
| 14 | class Email_Reports |
| 15 | { |
| 16 | public function __construct() |
| 17 | { |
| 18 | \add_filter('cron_schedules', [$this, 'add_monthly_schedule_cron']); |
| 19 | $monitored_options = ['iawp_email_report_time', 'iawp_email_report_email_addresses']; |
| 20 | foreach ($monitored_options as $option) { |
| 21 | \add_action('update_option_' . $option, [$this, 'schedule_email_report'], 10, 0); |
| 22 | \add_action('add_option_' . $option, [$this, 'schedule_email_report'], 10, 0); |
| 23 | } |
| 24 | \add_action('iawp_send_email_report', [$this, 'send_email_report']); |
| 25 | } |
| 26 | public function schedule_email_report() |
| 27 | { |
| 28 | $this->unschedule_email_report(); |
| 29 | if (empty(\IAWPSCOPED\iawp()->get_option('iawp_email_report_email_addresses', []))) { |
| 30 | return; |
| 31 | } |
| 32 | $delivery_time = new \DateTime('first day of +1 month', new \DateTimeZone(\wp_timezone_string())); |
| 33 | $delivery_time->setTime(\IAWPSCOPED\iawp()->get_option('iawp_email_report_time', 9), 0); |
| 34 | \wp_schedule_event($delivery_time->getTimestamp(), 'monthly', 'iawp_send_email_report'); |
| 35 | } |
| 36 | public function unschedule_email_report() |
| 37 | { |
| 38 | $timestamp = \wp_next_scheduled('iawp_send_email_report'); |
| 39 | \wp_unschedule_event($timestamp, 'iawp_send_email_report'); |
| 40 | } |
| 41 | public function add_monthly_schedule_cron($schedules) |
| 42 | { |
| 43 | $schedules['monthly'] = ['interval' => \MONTH_IN_SECONDS, 'display' => \esc_html__('Once a Month', 'independent-analytics')]; |
| 44 | return $schedules; |
| 45 | } |
| 46 | public function send_email_report(bool $test = \false) |
| 47 | { |
| 48 | $to = \IAWPSCOPED\iawp()->get_option('iawp_email_report_email_addresses', []); |
| 49 | if (empty($to)) { |
| 50 | return; |
| 51 | } |
| 52 | $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')); |
| 53 | if ($test) { |
| 54 | $subject = \esc_html__('[Test]', 'independent-analytics') . ' ' . $subject; |
| 55 | } |
| 56 | $body = $this->get_email_body(); |
| 57 | $headers[] = 'From: ' . \get_bloginfo('name') . ' <' . \get_bloginfo('admin_email') . '>'; |
| 58 | $headers[] = 'Content-Type: text/html; charset=UTF-8'; |
| 59 | return \wp_mail($to, $subject, $body, $headers); |
| 60 | } |
| 61 | private function get_email_body() |
| 62 | { |
| 63 | $statistics = new Page_Statistics(new Relative_Date_Range('LAST_MONTH')); |
| 64 | $quick_stats = (new \IAWP\Quick_Stats(null, $statistics))->get_stats(); |
| 65 | $chart = new \IAWP\Email_Chart($statistics); |
| 66 | return \IAWPSCOPED\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' => \IAWPSCOPED\iawp()->get_option('iawp_email_report_colors', ['#5123a0', '#fafafa', '#3a1e6b', '#fafafa', '#5123a0', '#a985e6', '#ece9f2', '#f7f5fa', '#ece9f2', '#dedae6'])]); |
| 67 | } |
| 68 | private function get_top_ten() : array |
| 69 | { |
| 70 | $start = new \DateTime('First day of last month', new \DateTimeZone(\wp_timezone_string())); |
| 71 | $end = new \DateTime('Last day of last month', new \DateTimeZone(\wp_timezone_string())); |
| 72 | $date_range = new Exact_Date_Range($start, $end); |
| 73 | $queries = ['pages' => 'title', 'referrers' => 'referrer', 'countries' => 'country', 'campaigns' => 'title']; |
| 74 | $top_ten = []; |
| 75 | $sort_configuration = new \IAWP\Sort_Configuration('views', 'desc'); |
| 76 | foreach ($queries as $type => $title) { |
| 77 | if ($type === 'pages') { |
| 78 | $query = new Pages($date_range, 10, null, $sort_configuration); |
| 79 | } elseif ($type === 'referrers') { |
| 80 | $query = new Referrers($date_range, 10, null, $sort_configuration); |
| 81 | } elseif ($type === 'countries') { |
| 82 | $query = new Countries($date_range, 10, null, $sort_configuration); |
| 83 | } elseif ($type === 'campaigns') { |
| 84 | $query = new Campaigns($date_range, 10, null, $sort_configuration); |
| 85 | } else { |
| 86 | continue; |
| 87 | } |
| 88 | $rows = \array_map(function ($row, $index) use($title) { |
| 89 | $edited_title = $row->{$title}(); |
| 90 | $edited_title = \mb_strlen($edited_title) > 30 ? \mb_substr($edited_title, 0, 30) . '...' : $edited_title; |
| 91 | return ['title' => $edited_title, 'views' => $row->views()]; |
| 92 | }, $query->rows(), \array_keys($query->rows())); |
| 93 | $top_ten[$type] = $rows; |
| 94 | } |
| 95 | return $top_ten; |
| 96 | } |
| 97 | } |
| 98 |