PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.4.2
Independent Analytics – WordPress Analytics Plugin v2.4.2
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 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Email_Reports.php
independent-analytics / IAWP Last commit date
AJAX 2 years ago Admin_Page 2 years ago Custom_WordPress_Columns 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
208 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\Device_Types;
10 use IAWP\Rows\Pages;
11 use IAWP\Rows\Referrers;
12 use IAWP\Statistics\Intervals\Hourly;
13 use IAWP\Statistics\Page_Statistics;
14 use IAWP\Utils\URL;
15 /** @internal */
16 class Email_Reports
17 {
18 public function __construct()
19 {
20 \add_filter('cron_schedules', [$this, 'add_monthly_schedule_cron']);
21 $monitored_options = ['iawp_email_report_interval', 'iawp_email_report_time', 'iawp_email_report_email_addresses'];
22 foreach ($monitored_options as $option) {
23 \add_action('update_option_' . $option, [$this, 'schedule_email_report'], 10, 0);
24 \add_action('add_option_' . $option, [$this, 'schedule_email_report'], 10, 0);
25 }
26 // Maybe reschedule when starting day of the week is changed
27 \add_action('update_option_iawp_dow', [$this, 'maybe_reschedule_email_report'], 10, 0);
28 \add_action('add_option_iawp_dow', [$this, 'maybe_reschedule_email_report'], 10, 0);
29 \add_action('iawp_send_email_report', [$this, 'send_email_report']);
30 }
31 public function schedule_email_report()
32 {
33 $this->unschedule_email_report();
34 if (empty(\IAWPSCOPED\iawp()->get_option('iawp_email_report_email_addresses', []))) {
35 return;
36 }
37 $interval = \IAWPSCOPED\iawp()->get_option('iawp_email_report_interval', 'monthly');
38 $delivery_time = $this->get_delivery_time();
39 \wp_schedule_event($delivery_time->getTimestamp(), $interval, 'iawp_send_email_report');
40 }
41 public function get_delivery_time()
42 {
43 $interval = \IAWPSCOPED\iawp()->get_option('iawp_email_report_interval', 'monthly');
44 $date_string = 'first day of +1 month';
45 if ($interval == 'weekly') {
46 $first_dow = \IAWPSCOPED\iawp()->get_option('iawp_dow', 0);
47 $date_string = 'next ' . $this->days_of_week()[$first_dow];
48 } elseif ($interval == 'daily') {
49 $date_string = 'tomorrow';
50 }
51 $delivery_time = new \DateTime($date_string, new \DateTimeZone(\wp_timezone_string()));
52 $delivery_time->setTime(\IAWPSCOPED\iawp()->get_option('iawp_email_report_time', 9), 0);
53 return $delivery_time;
54 }
55 public function unschedule_email_report()
56 {
57 $timestamp = \wp_next_scheduled('iawp_send_email_report');
58 \wp_unschedule_event($timestamp, 'iawp_send_email_report');
59 }
60 /* $use_cron is normally true so we report on the date from the cron event.
61 ** It is set to false for testing purposes. This allows the date to be reconstructed and compared to the cron event date */
62 public function get_next_scheduled_email_date_formatted($use_cron = \true)
63 {
64 if (!\wp_next_scheduled('iawp_send_email_report')) {
65 return \esc_html__('There is no email scheduled.', 'independent-analytics');
66 }
67 if ($use_cron) {
68 $date = new \DateTime('now', new \DateTimeZone(\wp_timezone_string()));
69 $date->setTimestamp(\wp_next_scheduled('iawp_send_email_report'));
70 $date->setTime(\IAWPSCOPED\iawp()->get_option('iawp_email_report_time', 9), 0);
71 } else {
72 $date = $this->get_delivery_time();
73 }
74 $day = $date->format(\get_option('date_format'));
75 $time = $date->format(\get_option('time_format'));
76 return \sprintf(\__('Next email scheduled for %s at %s.', 'independent-analytics'), '<span>' . $day . '</span>', '<span>' . $time . '</span>');
77 }
78 public function add_monthly_schedule_cron($schedules)
79 {
80 $schedules['monthly'] = ['interval' => \MONTH_IN_SECONDS, 'display' => \esc_html__('Once a Month', 'independent-analytics')];
81 return $schedules;
82 }
83 public function maybe_reschedule_email_report()
84 {
85 if (!\wp_next_scheduled('iawp_send_email_report')) {
86 return;
87 }
88 if (\IAWPSCOPED\iawp()->get_option('iawp_email_report_interval', 'monthly') != 'weekly') {
89 return;
90 }
91 $this->schedule_email_report();
92 }
93 public function send_email_report(bool $test = \false)
94 {
95 $to = \IAWPSCOPED\iawp()->get_option('iawp_email_report_email_addresses', []);
96 if (empty($to)) {
97 return;
98 }
99 $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'));
100 if ($test) {
101 $subject = \esc_html__('[Test]', 'independent-analytics') . ' ' . $subject;
102 }
103 $body = $this->get_email_body();
104 $headers[] = 'From: ' . \get_bloginfo('name') . ' <' . \get_bloginfo('admin_email') . '>';
105 $headers[] = 'Content-Type: text/html; charset=UTF-8';
106 return \wp_mail($to, $subject, $body, $headers);
107 }
108 public function get_email_preview($colors)
109 {
110 return $this->get_email_body($colors);
111 }
112 private function days_of_week()
113 {
114 return ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
115 }
116 private function get_email_body($colors = '')
117 {
118 $interval = \IAWPSCOPED\iawp()->get_option('iawp_email_report_interval', 'monthly');
119 if ($interval == 'monthly') {
120 $statistics = new Page_Statistics(new Relative_Date_Range('LAST_MONTH'));
121 } elseif ($interval == 'weekly') {
122 $statistics = new Page_Statistics(new Relative_Date_Range('LAST_WEEK'));
123 } else {
124 $statistics = new Page_Statistics(new Relative_Date_Range('YESTERDAY'), null, new Hourly());
125 }
126 $quick_stats = (new \IAWP\Quick_Stats(null, $statistics))->get_stats();
127 $chart = new \IAWP\Email_Chart($statistics);
128 $colors = $colors == '' ? \IAWPSCOPED\iawp()->get_option('iawp_email_report_colors', ['#5123a0', '#fafafa', '#3a1e6b', '#fafafa', '#5123a0', '#a985e6', '#ece9f2', '#f7f5fa', '#ece9f2', '#dedae6']) : \explode(',', $colors);
129 return \IAWPSCOPED\iawp_blade()->run('email.email', ['site_title' => \get_bloginfo('name'), 'site_url' => (new URL(\get_site_url()))->get_domain(), 'date' => $this->get_email_date_subheading(), 'stats' => $quick_stats, 'top_ten' => $this->get_top_ten(), 'chart_views' => $chart->views, 'most_views' => $chart->most_views, 'y_labels' => $chart->y_labels, 'x_labels' => $chart->x_labels, 'colors' => $colors]);
130 }
131 private function get_email_start_end_dates()
132 {
133 $interval = \IAWPSCOPED\iawp()->get_option('iawp_email_report_interval', 'monthly');
134 $start = new \DateTime('First day of last month', new \DateTimeZone(\wp_timezone_string()));
135 $end = new \DateTime('Last day of last month', new \DateTimeZone(\wp_timezone_string()));
136 if ($interval == 'weekly') {
137 $date_string = 'last ' . $this->days_of_week()[\IAWPSCOPED\iawp()->get_option('iawp_dow', 0)];
138 $start = new \DateTime($date_string, new \DateTimeZone(\wp_timezone_string()));
139 // -1 week if today isn't the chosen day of the week
140 if ((new \DateTime('now'))->format('w') != \IAWPSCOPED\iawp()->get_option('iawp_dow', 0)) {
141 $start->modify('-7 days');
142 }
143 $end = clone $start;
144 $end->modify('+6 days');
145 } elseif ($interval == 'daily') {
146 $start = new \DateTime('Yesterday', new \DateTimeZone(\wp_timezone_string()));
147 $end = clone $start;
148 }
149 return [$start, $end];
150 }
151 private function get_email_date_subheading()
152 {
153 $dates = $this->get_email_start_end_dates();
154 $interval = \IAWPSCOPED\iawp()->get_option('iawp_email_report_interval', 'monthly');
155 $format = \get_option('date_format');
156 $string = $dates[0]->format('F Y');
157 if ($interval == 'weekly') {
158 $string = $dates[0]->format($format) . ' - ' . $dates[1]->format($format);
159 } elseif ($interval == 'daily') {
160 $string = $dates[0]->format($format);
161 }
162 return $string;
163 }
164 private function get_top_ten() : array
165 {
166 $dates = $this->get_email_start_end_dates();
167 $date_range = new Exact_Date_Range($dates[0], $dates[1]);
168 $queries = ['pages' => 'title', 'referrers' => 'referrer', 'countries' => 'country', 'devices' => 'device_type', 'campaigns' => 'title', 'landing_pages' => 'title', 'exit_pages' => 'title'];
169 $top_ten = [];
170 $sort_configuration = new \IAWP\Sort_Configuration('views', 'desc');
171 foreach ($queries as $type => $title) {
172 if ($type === 'pages') {
173 $query = new Pages($date_range, 10, null, $sort_configuration);
174 } elseif ($type === 'referrers') {
175 $query = new Referrers($date_range, 10, null, $sort_configuration);
176 } elseif ($type === 'countries') {
177 $query = new Countries($date_range, 10, null, $sort_configuration);
178 } elseif ($type === 'devices') {
179 $query = new Device_Types($date_range, 10, null, $sort_configuration);
180 } elseif ($type === 'campaigns') {
181 $query = new Campaigns($date_range, 10, null, $sort_configuration);
182 } elseif ($type === 'landing_pages') {
183 $query = new Pages($date_range, 10, null, new \IAWP\Sort_Configuration('entrances', 'desc'));
184 } elseif ($type === 'exit_pages') {
185 $query = new Pages($date_range, 10, null, new \IAWP\Sort_Configuration('exits', 'desc'));
186 } else {
187 continue;
188 }
189 $rows = \array_map(function ($row, $index) use($type, $title) {
190 $edited_title = $row->{$title}();
191 $edited_title = \mb_strlen($edited_title) > 30 ? \mb_substr($edited_title, 0, 30) . '...' : $edited_title;
192 $metric = 'views';
193 if ($type == 'landing_pages') {
194 $metric = 'entrances';
195 } elseif ($type == 'exit_pages') {
196 $metric = 'exits';
197 }
198 return ['title' => $edited_title, 'views' => $row->{$metric}()];
199 }, $query->rows(), \array_keys($query->rows()));
200 if (\count($rows) == 0) {
201 continue;
202 }
203 $top_ten[$type] = $rows;
204 }
205 return $top_ten;
206 }
207 }
208