PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.1
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.1
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / modules / email-report / class-cartflows-admin-report-emails.php

class-cartflows-admin-report-emails.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.1, at modules/email-report/class-cartflows-admin-report-emails.php

223 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Email.
4 *
5 * @package CartFlows.
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 use CartflowsAdmin\AdminCore\Inc\AdminHelper;
13 /**
14 * Class Cartflows_Admin_Report_Emails.
15 */
16 class Cartflows_Admin_Report_Emails {
17
18
19 /**
20 * Class instance.
21 *
22 * @access private
23 * @var $instance Class instance.
24 */
25 private static $instance;
26
27 /**
28 * Initiator
29 */
30 public static function get_instance() {
31 if ( ! isset( self::$instance ) ) {
32 self::$instance = new self();
33 }
34 return self::$instance;
35 }
36
37
38 /**
39 * Constructor.
40 */
41 public function __construct() {
42
43 // It will run once.
44 add_action( 'admin_init', array( $this, 'schedule_weekly_report_email' ) );
45
46 add_action( 'cartflows_send_report_summary_email', array( $this, 'send_weekly_report_email' ) );
47
48 add_filter( 'admin_init', array( $this, 'unsubscribe_cartflows_weekly_emails' ), 10 );
49 }
50
51 /**
52 * Schedule the weekly email.
53 */
54 public function schedule_weekly_report_email() {
55
56 $is_report_emails = get_option( 'cartflows_stats_report_emails', 'enable' );
57
58 if ( 'enable' === $is_report_emails && function_exists( 'as_next_scheduled_action' ) && false === as_next_scheduled_action( 'cartflows_send_report_summary_email' ) ) {
59
60 wcf()->logger->log( 'Is weekly report enabled: ' . $is_report_emails );
61
62 $date = new DateTime( 'next monday 2pm' );
63
64 // It will automatically reschedule the action once initiated.
65 as_schedule_recurring_action( $date, WEEK_IN_SECONDS, 'cartflows_send_report_summary_email' );
66
67 wcf()->logger->log( 'Weekly report action scheduled. Action: cartflows_send_report_summary_email ' );
68 } elseif ( 'enable' !== $is_report_emails && as_next_scheduled_action( 'cartflows_send_report_summary_email' ) ) {
69 as_unschedule_all_actions( 'cartflows_send_report_summary_email' );
70
71 wcf()->logger->log( 'Weekly report action unscheduled. Action: cartflows_send_report_summary_email ' );
72 }
73 }
74
75 /**
76 * Send weekly report email.
77 */
78 public function send_weekly_report_email() {
79
80 $is_report_emails = get_option( 'cartflows_stats_report_emails', 'enable' );
81
82 $emails = get_option( 'cartflows_stats_report_email_ids', get_option( 'admin_email' ) );
83
84 wcf()->logger->log( 'Start-' . __CLASS__ . '::' . __FUNCTION__ );
85
86 if ( 'enable' === $is_report_emails && ! empty( $emails ) && apply_filters( 'cartflows_send_weekly_report_email', true ) ) {
87
88 $stats = $this->get_last_week_stats();
89
90 wcf()->logger->log( 'Send weekly emails to ' . $emails );
91 wcf()->logger->log( 'Total Revenue: ' . $stats['total_revenue'] );
92
93 if ( isset( $stats['total_revenue_raw'] ) && $stats['total_revenue_raw'] > 0 ) {
94
95 $subject = $this->get_email_subject();
96 $headers = 'From: ' . get_bloginfo( 'name' ) . ' <' . get_option( 'admin_email' ) . '>' . "\r\n";
97 $headers .= "Content-Type: text/html;\r\n";
98
99 $emails = preg_split( "/[\f\r\n]+/", $emails );
100
101 foreach ( $emails as $email_id ) {
102 $user_info = get_user_by( 'email', $email_id );
103 $name = $user_info ? $user_info->display_name : __( 'There', 'cartflows' );
104 $email_body = $this->get_email_content( $stats, $name, $email_id );
105 $status = wp_mail( $email_id, $subject, stripslashes( $email_body ), $headers ); //phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
106 wcf()->logger->log( 'Email send status for email ' . $email_id . ' is ' . $status );
107 }
108 }
109 }
110 }
111
112 /**
113 * Unsubscribe the user from the mailing list.
114 */
115 public function unsubscribe_cartflows_weekly_emails() {
116
117 $unsubscribe = filter_input( INPUT_GET, 'unsubscribe_weekly_email', FILTER_VALIDATE_BOOLEAN );
118 $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
119 $email = filter_input( INPUT_GET, 'email', FILTER_SANITIZE_EMAIL );
120 $token = isset( $_GET['token'] ) ? sanitize_text_field( wp_unslash( $_GET['token'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
121
122 if ( $unsubscribe && 'cartflows' === $page && ! empty( $email ) && is_user_logged_in() && current_user_can( 'cartflows_manage_settings' ) ) {
123
124 // Verify unsubscribe token to prevent CSRF.
125 $expected_token = wp_hash( 'cartflows_unsubscribe_' . $email );
126 if ( ! hash_equals( $expected_token, $token ) ) {
127 wp_die( esc_html__( 'Invalid unsubscribe link.', 'cartflows' ), esc_html__( 'Error', 'cartflows' ), array( 'response' => 403 ) );
128 }
129
130 $email_list = get_option( 'cartflows_stats_report_email_ids', false );
131
132 if ( ! empty( $email_list ) ) {
133 $email_list = preg_split( "/[\f\r\n]+/", $email_list );
134
135 $email_list = array_filter(
136 $email_list,
137 function ( $e ) use ( $email ) {
138 return ( $e !== $email );
139 }
140 );
141
142 $email_list = implode( "\n", $email_list );
143
144 update_option( 'cartflows_stats_report_email_ids', $email_list );
145 wcf()->logger->log( 'Email unsubscribed: ' . $email );
146 }
147
148 wp_die( esc_html__( 'You have successfully unsubscribed from our weekly emails list.', 'cartflows' ), esc_html__( 'Unsubscribed', 'cartflows' ) );
149 }
150 }
151
152 /**
153 * Get the stats mention in to email.
154 */
155 public function get_last_week_stats() {
156
157 $start_date = gmdate( 'Y-m-d', strtotime( '-7 days' ) );
158 $end_date = gmdate( 'Y-m-d' );
159
160 return AdminHelper::get_earnings( $start_date, $end_date );
161 }
162
163 /**
164 * Get the stats mention in to email.
165 */
166 public function get_last_month_stats() {
167
168 $start_date = gmdate( 'Y-m-d', strtotime( '-30 days' ) );
169 $end_date = gmdate( 'Y-m-d' );
170
171 return AdminHelper::get_earnings( $start_date, $end_date );
172 }
173
174 /**
175 * Get admin report email subject.
176 */
177 public function get_email_subject() {
178
179 return esc_html__( 'Here’s how your store performed last week!', 'cartflows' );
180 }
181
182 /**
183 * Get admin report email content.
184 *
185 * @param array $stats reports details.
186 * @param string $user_name user name.
187 * @param string $email_id email id.
188 */
189 public function get_email_content( $stats, $user_name, $email_id ) {
190
191 $cf_logo = CARTFLOWS_URL . 'assets/images/cartflows-email-logo.png';
192 $unsubscribe_token = wp_hash( 'cartflows_unsubscribe_' . $email_id );
193 $unsubscribe_link = add_query_arg(
194 array(
195 'page' => 'cartflows',
196 'unsubscribe_weekly_email' => true,
197 'email' => $email_id,
198 'token' => $unsubscribe_token,
199 ),
200 admin_url( 'admin.php' )
201 );
202 $facebook_icon = CARTFLOWS_URL . 'assets/images/facebook2x.png';
203 $twitter_icon = CARTFLOWS_URL . 'assets/images/twitter2x.png';
204 $youtube_icon = CARTFLOWS_URL . 'assets/images/youtube2x.png';
205 $from_date = gmdate( 'M j', strtotime( '-7 days' ) );
206 $to_date = gmdate( 'M j' );
207 $total_orders = $stats['total_orders'];
208 $total_visits = $stats['total_visits'];
209 $order_bump_revenue = $stats['total_bump_revenue'];
210 $offers_revenue = $stats['total_offers_revenue'];
211 $lock_icon = CARTFLOWS_URL . 'assets/images/lock.png';
212
213 $total_revenue = $stats['total_revenue'];
214 $last_month_stats = $this->get_last_month_stats();
215 $last_month_revenue = $last_month_stats['total_revenue'];
216 $store_name = get_bloginfo( 'name' );
217
218 return include CARTFLOWS_DIR . 'modules/email-report/templates/email-body.php';
219 }
220 }
221
222 Cartflows_Admin_Report_Emails::get_instance();
223