PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.3.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.3.5
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / admin / reports / class-betterdocs-report-email.php

class-betterdocs-report-email.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 2.3.5, at admin/reports/class-betterdocs-report-email.php

369 lines 14.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This class is responsible for sending weekly email with reports
4 *
5 * @since 1.4.4
6 */
7 class BetterDocs_Report_Email {
8
9 use BetterDocs_Email_Template;
10
11 /**
12 * Get a single Instance of Analytics
13 * @var Report_Email
14 */
15 public $settings = null;
16
17 private static $current_timestamp = null;
18 private static $current_date = null;
19
20 /**
21 * Initially Invoked by Default.
22 */
23 public function __construct() {
24 if ( BetterDocs_DB::get_settings('enable_reporting') == 'off' ) {
25 $this->mail_report_deactivation( 'betterdocs_daily_email_reporting' );
26 $this->mail_report_deactivation( 'betterdocs_weekly_email_reporting' );
27 $this->mail_report_deactivation( 'betterdocs_monthly_email_reporting' );
28 return;
29 }
30
31 add_filter( 'cron_schedules', array( $this, 'schedules_cron' ) );
32 add_action('admin_init', array( $this, 'mail_report_activation' ));
33 add_action('betterdocs_weekly_email_reporting', array( $this, 'send_email' ));
34 }
35
36 public function reporting( $request ){
37 if( ! boolval($request->get_param('disable_reporting')) ) {
38 if( $request->has_param('reporting_email') ) {
39 $email = $request->get_param( 'reporting_email' );
40 }
41 $email = $this->receiver_email_address( $email );
42
43 if( ! empty( $email ) ) {
44 $is_send = $this->send_email( $request->get_param('reporting_frequency'), true, $email );
45 if( $is_send && ! is_wp_error( $is_send ) ) {
46 return [ 'message' => __( 'Successfully Sent an Email', 'betterdocs' ) ];
47 } else if ( is_wp_error( $is_send ) ) {
48 return $is_send;
49 } else {
50 new \WP_Error('betterdocs_unknown_reason', __( 'Email cannot be sent for some reason.', 'betterdocs' ) );
51 }
52 }
53 new \WP_Error('betterdocs_unknown_reason', __( 'Invalid email address.', 'betterdocs' ) );
54 } else {
55 return new \WP_Error('betterdocs_disabled_reporting', __( 'You have to enable Reporting first.', 'betterdocs' ) );
56 }
57 }
58
59 private static function timestamps( $date = false ){
60 if( is_null( self::$current_timestamp ) ) {
61 self::$current_timestamp = current_time('timestamp');
62 }
63 if( $date ) {
64 if( is_null( self::$current_date ) ) {
65 self::$current_date = current_time('Y-m-d');
66 }
67 return self::$current_date;
68 }
69 return self::$current_timestamp;
70 }
71
72 public function create_date($count = '-7days'){
73 return date('Y-m-d', strtotime($count, self::timestamps()));
74 }
75
76 public function get_views( $start_date, $end_date = null ) {
77 global $wpdb;
78
79 $query = $wpdb->get_results("
80 SELECT sum(impressions) as views, sum(unique_visit) as unique_visit, sum(happy + sad + normal) as reactions
81 FROM {$wpdb->prefix}betterdocs_analytics
82 WHERE (created_at BETWEEN '".$start_date."' AND '".$end_date."')
83 ");
84 return $query;
85 }
86
87 public function get_search( $start_date, $end_date = null ) {
88 global $wpdb;
89
90 $query = $wpdb->get_results("
91 SELECT SUM(count + not_found_count) as search_count, SUM(count) as search_found, SUM(not_found_count) as search_not_found_count
92 FROM {$wpdb->prefix}betterdocs_search_log as search_log
93 WHERE (search_log.created_at BETWEEN '".$start_date."' AND '".$end_date."')
94 ");
95
96 return $query;
97 }
98
99 public function count_new_docs( $start_date, $end_date = null ) {
100 $args = array(
101 'post_type' => 'docs',
102 'post_status' => 'publish',
103 'posts_per_page' => -1,
104 'date_query' => array(
105 array(
106 'after' => $start_date,
107 'before' => $end_date,
108 'inclusive' => true,
109 ),
110 ),
111 );
112 $query = new WP_Query( $args );
113
114 return $query->found_posts;
115 }
116
117 public function get_search_keywords( $start_date, $end_date = null ) {
118 global $wpdb;
119 $select = "SELECT search_keyword.keyword, search_log.keyword_id, SUM(search_log.count + search_log.not_found_count) as total_search, SUM(search_log.count) as count, SUM(search_log.not_found_count) as not_found";
120 $join = "FROM {$wpdb->prefix}betterdocs_search_keyword as search_keyword
121 JOIN {$wpdb->prefix}betterdocs_search_log as search_log on search_keyword.id = search_log.keyword_id";
122
123 $query = $wpdb->get_results("
124 {$select}
125 {$join}
126 WHERE (search_log.created_at BETWEEN '".$start_date."' AND '".$end_date."')
127 GROUP BY search_log.keyword_id
128 ORDER BY count DESC LIMIT 5
129 ");
130
131 return $query;
132 }
133
134 public function get_docs_items( $request ) {
135 $prepared_post = new stdClass();
136
137 if ( isset( $request->ID ) ) {
138 $prepared_post->ID = $request->ID;
139 }
140
141 if ( isset( $request->post_title ) ) {
142 $prepared_post->title = wp_kses($request->post_title, BETTERDOCS_KSES_ALLOWED_HTML);
143 }
144
145 if ( isset( $request->total_views ) ) {
146 $prepared_post->total_views = $request->total_views;
147 }
148
149 if ( isset( $request->total_unique_visit ) ) {
150 $prepared_post->total_unique_visit = $request->total_unique_visit;
151 }
152
153 if ( isset( $request->total_reactions ) ) {
154 $prepared_post->total_reactions = $request->total_reactions;
155 }
156
157 if ( isset( $request->ID ) ) {
158 $prepared_post->link = get_permalink($request->ID);
159 }
160
161 return $prepared_post;
162 }
163
164 public function get_leading_docs( $start_date, $end_date = null ) {
165 global $wpdb;
166
167 $select = "SELECT docs.ID, docs.post_author, docs.post_title, SUM(analytics.impressions) as total_views, SUM(analytics.unique_visit) as total_unique_visit, SUM(analytics.happy + analytics.sad + analytics.normal) as total_reactions";
168 $join = "FROM {$wpdb->prefix}posts as docs
169 JOIN {$wpdb->prefix}betterdocs_analytics as analytics on docs.ID = analytics.post_id";
170
171 $query = $wpdb->get_results("
172 {$select}
173 {$join}
174 WHERE post_type = 'docs' AND post_status = 'publish' AND (analytics.created_at BETWEEN '".$start_date."' AND '".$end_date."')
175 GROUP BY analytics.post_id
176 ORDER BY total_views DESC LIMIT 5
177 ");
178
179 $docs = array();
180 foreach ($query as $key=>$value) {
181 $docs[$key] = $this->get_docs_items( $value );
182 }
183
184 return $docs;
185 }
186
187 /**
188 * Calculate Total NotificationX Views
189 * @return int
190 */
191 public function get_data( $frequency = 'betterdocs_weekly'){
192
193 if( $frequency == 'betterdocs_daily' ) {
194 $start_date = $this->create_date('last day');
195 $end_date = $this->create_date('last day');
196 $previous_start_date = $this->create_date('last day last day');
197 $previous_end_date = $this->create_date('last day last day');
198 }
199
200 if( $frequency == 'betterdocs_weekly' ) {
201 $start_date = $this->create_date('-7days');
202 $end_date = $this->create_date('last day');
203 $previous_start_date = $this->create_date('-14days');
204 $previous_end_date = $this->create_date('-7days');
205 }
206 if( $frequency == 'betterdocs_monthly' ) {
207 $previous_start_date = $this->create_date('first day of last month last month');
208 $previous_end_date = $this->create_date('last day of last month last month');
209 $start_date = $this->create_date('first day of last month');
210 $end_date = $this->create_date('last day of last month');
211 }
212
213 $views_current_data = $this->get_views( $start_date, $end_date );
214 $views_previous_data = $this->get_views( $previous_start_date, $previous_end_date );
215 $search_current_data = $this->get_search( $start_date, $end_date );
216 $search_previous_data = $this->get_search( $previous_start_date, $previous_end_date );
217 $docs_current_data = $this->get_leading_docs( $start_date, $end_date );
218 $docs_total_current_reactions = array_sum(array_column($docs_current_data, 'total_reactions'));
219 $search_keyword_data = $this->get_search_keywords( $start_date, $end_date );
220 $count_new_docs_current = $this->count_new_docs( $start_date, $end_date );
221 $count_new_docs_previous = $this->count_new_docs( $previous_start_date, $previous_end_date );
222
223 $from_date = $start_date;
224 $to_date = $frequency == 'betterdocs_daily' ? $start_date : $end_date;
225
226 $data = [
227 'views' => [
228 'from_date' => $from_date,
229 'to_date' => $to_date,
230 'current_data' => $views_current_data,
231 'previous_data' => $views_previous_data,
232 ],
233 'search' => [
234 'from_date' => $from_date,
235 'to_date' => $to_date,
236 'current_data' => $search_current_data,
237 'previous_data' => $search_previous_data,
238 'keywords' => $search_keyword_data,
239 ],
240 'new_docs' => [
241 'from_date' => $from_date,
242 'to_date' => $to_date,
243 'current_data' => $count_new_docs_current,
244 'previous_data' => $count_new_docs_previous
245 ],
246 'docs' => [
247 'from_date' => $from_date,
248 'to_date' => $to_date,
249 'current_data' => $docs_current_data,
250 'total_current_reactions' => $docs_total_current_reactions
251 ]
252 ];
253 return $data;
254 }
255
256
257 /**
258 * Adds a custom cron schedule for Weekly.
259 *
260 * @param array $schedules An array of non-default cron schedules.
261 * @return array Filtered array of non-default cron schedules.
262 */
263 function schedules_cron( $schedules = array() ) {
264 $schedules['betterdocs_weekly'] = array(
265 'interval' => 604800,
266 'display' => __( 'Once Weekly', 'betterdocs' )
267 );
268 $schedules['betterdocs_daily'] = array(
269 'interval' => 86400,
270 'display' => __( 'Once Daily', 'betterdocs' )
271 );
272 $schedules['betterdocs_monthly'] = array(
273 'interval' => 2635200,
274 'display' => __( 'Once Monthly', 'betterdocs' )
275 );
276 return $schedules;
277 }
278
279 /**
280 * Set Email Receiver mail address
281 * By Default, Admin Email Address
282 * Admin can set Custom email from NotificationX Advanced Settings Panel
283 * @return email||String
284 */
285 public function receiver_email_address( $email = '' ) {
286 if( empty( $email ) ) {
287 $email = BetterDocs_DB::get_settings('reporting_email');
288 if( empty( $email ) ) {
289 $email = get_option( 'admin_email' );
290 }
291 }
292 if( strpos( $email, ',' ) !== false ) {
293 $email = str_replace( ' ', '', $email );
294 $email = explode(',', $email );
295 } else {
296 $email = trim( $email );
297 }
298 return $email;
299 }
300
301 public function reporting_frequency() {
302 $reporting_frequency = BetterDocs_DB::get_settings('reporting_frequency');
303 return ($reporting_frequency !== 'off') ? $reporting_frequency : 'betterdocs_weekly';
304 }
305
306 /**
307 * Enable Cron Function
308 * Hook: admin_init
309 */
310 function mail_report_activation() {
311 $day = "monday";
312 $reporting_day = BetterDocs_DB::get_settings('reporting_day');
313 $reporting_day_opt = get_option( 'betterdocs_weekly_reporting_day' );
314 if( isset( $reporting_day ) ) {
315 if ( $reporting_day !== $reporting_day_opt ) {
316 $this->mail_report_deactivation( 'betterdocs_weekly_email_reporting' );
317 update_option( 'betterdocs_weekly_reporting_day', $reporting_day );
318 }
319 $day = $reporting_day;
320 }
321
322 $frequency = $this->reporting_frequency();
323 if( $frequency === 'betterdocs_weekly' ) {
324 $datetime = strtotime( "next $day 9AM", current_time('timestamp') );
325 $this->mail_report_deactivation( 'betterdocs_daily_email_reporting' );
326 $this->mail_report_deactivation( 'betterdocs_monthly_email_reporting' );
327 if ( ! wp_next_scheduled ( 'betterdocs_weekly_email_reporting' ) ) {
328 wp_schedule_event( $datetime, $frequency, 'betterdocs_weekly_email_reporting' );
329 }
330 }
331
332 }
333
334 /**
335 * Execute Cron Function
336 * Hook: admin_init
337 */
338 public function send_email( $frequency = 'betterdocs_weekly', $test = false, $email = null ) {
339 $data = $this->get_data( $frequency );
340
341 if( empty( $data ) ) {
342 return new \WP_Error('betterdocs_no_reporting_data', __('No data found.', 'betterdocs'));
343 }
344 if( isset( $this->settings['enable_analytics'] ) && ! $this->settings['enable_analytics'] ) {
345 return new \WP_Error('betterdocs_disabled_analytics', __('Analytics disabled. No data found.', 'betterdocs'));
346 }
347 $to = is_null( $email ) ? $this->receiver_email_address() : $email;
348 if( empty( $to ) ) {
349 return new \WP_Error('betterdocs_reporting_email', __('No email found.', 'betterdocs'));
350 }
351
352 $subject = $this->email_subject();
353 $message = $this->template_body( $data, $frequency );
354
355 $headers = array( 'Content-Type: text/html; charset=UTF-8', "From: BetterDocs <support@wpdeveloper.com>" );
356
357 return wp_mail( $to, $subject, $message, $headers );
358 }
359
360 /**
361 * Disable Cron Function
362 * Hook: plugin_deactivation
363 */
364 public function mail_report_deactivation( $clear_hook = 'betterdocs_weekly_email_reporting' ) {
365 wp_clear_scheduled_hook( $clear_hook );
366 }
367 }
368
369 new BetterDocs_Report_Email;