PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.10
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.10
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / Admin / Reports / ReportEmail.php

ReportEmail.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.10, at includes/Admin/Reports/ReportEmail.php

340 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace NotificationX\Admin\Reports;
3
4 use NotificationX\Admin\Settings;
5 use NotificationX\Core\Helper as NotificationX_Helper;
6 use NotificationX\GetInstance;
7
8 /**
9 * This class is responsible for sending weekly email with reports
10 * @method static ReportEmail get_instance($args = null)
11 *
12 * @since 1.4.4
13 */
14 class ReportEmail {
15 /**
16 * Instance of Admin
17 *
18 * @var Admin
19 */
20 use GetInstance;
21
22 /**
23 * Get a single Instance of Analytics
24 * @var Report_Email
25 */
26 public $settings = null;
27
28 private static $current_timestamp = null;
29 private static $current_date = null;
30
31 /**
32 * Initially Invoked by Default.
33 */
34 public function __construct() {
35 $this->settings = Settings::get_instance()->get('settings');
36 if( !Settings::get_instance()->get('settings.enable_analytics', true) || Settings::get_instance()->get('settings.disable_reporting')) {
37 $this->mail_report_deactivation( 'daily_email_reporting' );
38 $this->mail_report_deactivation( 'weekly_email_reporting' );
39 $this->mail_report_deactivation( 'monthly_email_reporting' );
40 return;
41 }
42
43 add_filter( 'cron_schedules', array( $this, 'schedules_cron' ) );
44 add_action('admin_init', array( $this, 'mail_report_activation' ));
45 add_action('weekly_email_reporting', array( $this, 'send_email_weekly' ));
46 }
47
48 public function reporting( $request ){
49 if( ! boolval($request->get_param('disable_reporting')) ) {
50 if( $request->has_param('reporting_email') ) {
51 $email = $request->get_param( 'reporting_email' );
52 }
53 $email = $this->receiver_email_address( $email );
54
55 if( ! empty( $email ) ) {
56 $is_send = $this->send_email_weekly( $request->get_param('reporting_frequency'), true, $email );
57 if( $is_send && ! is_wp_error( $is_send ) ) {
58 return [ 'message' => __( 'Successfully Sent an Email', 'notificationx' ) ];
59 } else if ( is_wp_error( $is_send ) ) {
60 return $is_send;
61 } else {
62 new \WP_Error('nx_unknown_reason', __( 'Email cannot be sent for some reason.', 'notificationx' ) );
63 }
64 }
65 new \WP_Error('nx_unknown_reason', __( 'Invalid email address.', 'notificationx' ) );
66 } else {
67 return new \WP_Error('nx_disabled_reporting', __( 'You have to enable Reporting first.', 'notificationx' ) );
68 }
69 }
70
71 private static function timestamps( $date = false ){
72 if( is_null( self::$current_timestamp ) ) {
73 self::$current_timestamp = current_time('timestamp');
74 }
75 if( $date ) {
76 if( is_null( self::$current_date ) ) {
77 self::$current_date = current_time('Y-m-d');
78 }
79 return self::$current_date;
80 }
81 return self::$current_timestamp;
82 }
83
84 public function create_date($count = '-7days'){
85 return date('Y-m-d', strtotime($count, self::timestamps()));
86 }
87
88 public function get_stats( $start_date, $end_date = null ){
89 global $wpdb;
90
91 $extra_query = $wpdb->prepare( 'BETWEEN %s AND %s', $start_date, $end_date );
92
93 if ( is_null( $end_date ) ) {
94 $extra_query = $wpdb->prepare( ' = %s', $start_date );
95 }
96
97 $query = "SELECT MAIN.`nx_id`, MAIN.`title`, MAIN.`type`, STATS.`views`, STATS.`clicks`, STATS.`CTR` FROM ( SELECT P.`nx_id`, title, type FROM {$wpdb->prefix}nx_posts as P LEFT JOIN {$wpdb->prefix}nx_stats as S ON P.`nx_id` = S.`nx_id` GROUP BY P.nx_id ) AS MAIN INNER JOIN ( SELECT *, (clicks/views)*100 as ctr FROM ( SELECT SUM(views) as views, SUM(clicks) as clicks, nx_id FROM {$wpdb->prefix}nx_stats WHERE created_at $extra_query GROUP BY nx_id ) as VCID ) as STATS
98 ON MAIN.`nx_id` = STATS.`nx_id`";
99
100 return $wpdb->get_results( $query );
101 }
102 /**
103 * Calculate Total NotificationX Views
104 * @return int
105 */
106 public function get_data( $frequency = 'nx_weekly'){
107 if( $frequency == 'nx_daily' ) {
108 $start_date = $this->create_date('last day');
109 $end_date = null;
110 $previous_start_date = $this->create_date('last day last day');
111 $previous_end_date = null;
112 }
113
114 if( $frequency == 'nx_weekly' ) {
115 $start_date = $this->create_date('-7days');
116 $end_date = $this->create_date('last day');
117 $previous_start_date = $this->create_date('-14days');
118 $previous_end_date = $this->create_date('-7days');
119 }
120 if( $frequency == 'nx_monthly' ) {
121 $previous_start_date = $this->create_date('first day of last month last month');
122 $previous_end_date = $this->create_date('last day of last month last month');
123 $start_date = $this->create_date('first day of last month');
124 $end_date = $this->create_date('last day of last month');
125 }
126
127 $current_data = $this->get_stats( $start_date, $end_date );
128 $previous_data = $this->get_stats( $previous_start_date, $previous_end_date );
129
130 $from_date = $previous_start_date;
131 $to_date = $frequency == 'nx_daily' ? $start_date : $end_date;
132
133 $data = [
134 'from_date' => $from_date,
135 'to_date' => $to_date,
136 'current_data' => $current_data,
137 'previous_data' => $previous_data,
138 ];
139
140 return $this->generate_data( $data );
141 }
142
143 private function percentage( $_last, $_current ){
144 return $_last > 0 ? number_format( ( ( $_current - $_last ) / $_last ) * 100, 2 ) : 0;
145 }
146
147 private function previous_data( &$_single_data, &$_data = null ){
148 if( empty( $_single_data ) ) {
149 return [
150 'last_views' => 0,
151 'percentage_views' => 0,
152 'last_clicks' => 0,
153 'percentage_clicks' => 0,
154 'last_ctr' => 0,
155 'percentage_ctr' => 0,
156 ];
157 }
158 $_new_data = [];
159 array_walk( $_single_data, function( $item , $key ) use( &$_data, &$_new_data ) {
160 switch( $key ) {
161 case 'views':
162 $_new_data['last_views'] = $item;
163 $_new_data['percentage_views'] = $this->percentage( $item, $_data['views'] );
164 break;
165 case 'clicks':
166 $_new_data['last_clicks'] = $item;
167 $_new_data['percentage_clicks'] = $this->percentage( $item, $_data['clicks'] );
168 break;
169 case 'ctr':
170 $_new_data['last_ctr'] = $item;
171 $_new_data['percentage_ctr'] = $this->percentage( $item, $_data['ctr'] );
172 break;
173 }
174 });
175
176 return $_new_data;
177 }
178
179 public function generate_data( &$data ){
180 if( empty( $data ) ) {
181 return [];
182 }
183
184 // previous -> || (isset( $data['previous_data'] ) && empty( $data['previous_data'] )
185 if( (isset( $data['current_data'] ) && empty( $data['current_data'] ) ) ) {
186 return [];
187 }
188
189 $current_data = [];
190 $previous_data = [];
191 array_walk( $data['previous_data'], function( $single ) use ( &$previous_data ){
192 $previous_data[ $single->nx_id ] = ( array ) $single;
193 });
194 array_walk( $data['current_data'], function( $single ) use ( &$current_data, &$previous_data, $data ){
195 $_single_data = ( array ) $single;
196 $_single_data['source'] = NotificationX_Helper::get_type_title( $single->type );
197 $_single_data['type'] = NotificationX_Helper::get_type_title( $single->type );
198 $_single_data['from_date'] = $data['from_date'];
199 $_single_data['to_date'] = $data['to_date'];
200 $_previous_data = [];
201 if( isset( $previous_data[ $single->nx_id ] ) ) {
202 $_previous_data = $this->previous_data( $previous_data[ $single->nx_id ], $_single_data );
203 } else {
204 $_previous_data = $this->previous_data( $_previous_data, $_single_data );
205 }
206 $current_data[ $single->nx_id ] = array_merge( $_single_data, $_previous_data );
207 });
208
209 return $current_data;
210 }
211 /**
212 * Adds a custom cron schedule for Weekly.
213 *
214 * @param array $schedules An array of non-default cron schedules.
215 * @return array Filtered array of non-default cron schedules.
216 */
217 function schedules_cron( $schedules = array() ) {
218 $schedules['nx_weekly'] = array(
219 'interval' => 604800,
220 'display' => __( 'Once Weekly', 'notificationx' )
221 );
222 $schedules['nx_daily'] = array(
223 'interval' => 86400,
224 'display' => __( 'Once Daily', 'notificationx' )
225 );
226 $schedules['nx_monthly'] = array(
227 'interval' => strtotime( 'first day of next month 9AM' ),
228 'display' => __( 'Once Monthly', 'notificationx' )
229 );
230 return $schedules;
231 }
232
233 /**
234 * Set Email Receiver mail address
235 * By Default, Admin Email Address
236 * Admin can set Custom email from NotificationX Advanced Settings Panel
237 * @return email||String
238 */
239 public function receiver_email_address( $email = '' ) {
240 if( empty( $email ) ) {
241 $email = Settings::get_instance()->get('settings.reporting_email' );
242 if( empty( $email ) ) {
243 $email = get_option( 'admin_email' );
244 }
245 }
246 if( strpos( $email, ',' ) !== false ) {
247 $email = str_replace( ' ', '', $email );
248 $email = explode(',', $email );
249 } else {
250 $email = trim( $email );
251 }
252 return $email;
253 }
254
255 /**
256 * Set Email Subject
257 * By Default, subject will be "Weekly Reporting for NotificationX"
258 * Admin can set Custom Subject from NotificationX Advanced Settings Panel
259 * @return subject||String
260 */
261 public function email_subject() {
262 $site_name = get_bloginfo( 'name' );
263 $subject = sprintf( __( 'Weekly Engagement Summary of “%s”', 'notificationx' ), $site_name );
264 if( isset( $this->settings['reporting_subject'] ) && ! empty( $this->settings['reporting_subject'] ) ) {
265 $subject = stripcslashes( $this->settings['reporting_subject'] );
266 }
267 return $subject;
268 }
269
270 public function reporting_frequency(){
271 $frequency = Settings::get_instance()->get('settings.reporting_frequency', 'nx_weekly');
272 return $frequency;
273 }
274
275 /**
276 * Enable Cron Function
277 * Hook: admin_init
278 */
279 function mail_report_activation() {
280 $day = "monday";
281 if( isset( $this->settings['reporting_day'] ) ) {
282 $day = $this->settings['reporting_day'];
283 }
284
285 $frequency = $this->reporting_frequency();
286 if( $frequency === 'nx_weekly' ) {
287 $datetime = strtotime( "next $day 9AM", current_time('timestamp') );
288 $triggered = Settings::get_instance()->get("reporting.mail_sent.$frequency", false);
289 if ( $triggered == 1 ) {
290 $this->mail_report_deactivation( 'weekly_email_reporting' );
291 }
292 if( ! $triggered ) {
293 $datetime = strtotime( "+1hour", current_time('timestamp') );
294 }
295 $this->mail_report_deactivation( 'daily_email_reporting' );
296 $this->mail_report_deactivation( 'monthly_email_reporting' );
297 if ( ! wp_next_scheduled ( 'weekly_email_reporting' ) ) {
298 wp_schedule_event( $datetime, $frequency, 'weekly_email_reporting' );
299 }
300 }
301
302 }
303
304 /**
305 * Execute Cron Function
306 * Hook: admin_init
307 */
308 public function send_email_weekly( $frequency = 'nx_weekly', $test = false, $email = null ) {
309 $data = $this->get_data( $frequency );
310 if( empty( $data ) ) {
311 return new \WP_Error('nx_no_reporting_data', __('No data found.', 'notificationx'));
312 }
313 if( isset( $this->settings['enable_analytics'] ) && ! $this->settings['enable_analytics'] ) {
314 return new \WP_Error('nx_disabled_analytics', __('Analytics disabled. No data found.', 'notificationx'));
315 }
316 $to = is_null( $email ) ? $this->receiver_email_address() : $email;
317 if( empty( $to ) ) {
318 return new \WP_Error('nx_reporting_email', __('No email found.', 'notificationx'));
319 }
320
321 $subject = $this->email_subject();
322 $template = new EmailTemplate();
323 $message = $template->template_body( $data, $frequency );
324 $headers = array( 'Content-Type: text/html; charset=UTF-8', "From: NotificationX <support@wpdeveloper.com>" );
325 if( ! $test ) {
326 $triggered = Settings::get_instance()->get("reporting.mail_sent.$frequency");
327 $triggered = ! $triggered ? 0 : $triggered++;
328 Settings::get_instance()->set("reporting.mail_sent.$frequency", $triggered);
329 }
330 return wp_mail( $to, $subject, $message, $headers );
331 }
332
333 /**
334 * Disable Cron Function
335 * Hook: plugin_deactivation
336 */
337 public function mail_report_deactivation( $clear_hook = 'weekly_email_reporting' ) {
338 wp_clear_scheduled_hook( $clear_hook );
339 }
340 }