PluginProbe
WANotifier for Forms and Actions / 2.6.1
WANotifier for Forms and Actions v2.6.1
3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 1.0.3 All 66 releases
notifier / includes / classes / class-notifier-tools.php

class-notifier-tools.php in WANotifier for Forms and Actions 2.6.1, at includes/classes/class-notifier-tools.php

225 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 * Tools page class
4 *
5 * @package Wa_Notifier
6 */
7 class Notifier_Tools {
8
9 /**
10 * Init
11 */
12 public static function init() {
13 add_action( 'admin_menu', array( __CLASS__ , 'setup_admin_page') );
14 add_action( 'admin_init', array( __CLASS__ , 'export_customers') );
15 add_action( 'wp_ajax_fetch_activity_logs_by_date', array(__CLASS__, 'fetch_activity_logs_by_date'));
16 }
17
18 /**
19 * Add settings page to men
20 */
21 public static function setup_admin_page () {
22 if (!Notifier::is_api_active()){
23 return;
24 }
25
26 add_submenu_page( NOTIFIER_NAME, 'Tools', 'Tools', 'manage_options', NOTIFIER_NAME . '-tools', array( __CLASS__, 'output' ) );
27 }
28
29 /**
30 * Check if on tools page
31 */
32 public static function is_tools_page() {
33 $current_page = isset($_GET['page']) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
34 return strpos($current_page, NOTIFIER_NAME . '-tools') !== false;
35 }
36
37 /**
38 * Output
39 */
40 public static function output() {
41 include_once NOTIFIER_PATH . '/views/admin-tools.php';
42 }
43
44 /**
45 * Export WooCommerce Customers
46 */
47 public static function export_customers() {
48 if ( ! self::is_tools_page() ) {
49 return;
50 }
51
52 if ( ! class_exists('WooCommerce') || ! in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins'))) ) {
53 return '<div class="notice notice-error is-dismissible"><p>WooCommerce plugin is not installed or is not active. Please install or activate WooCommerce to export customers.</p></div>';
54 }
55
56 if ( ! isset( $_POST['export_customer'] ) ) {
57 return;
58 }
59
60 //phpcs:ignore
61 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-tools-export-customers' ) ) {
62 return;
63 }
64
65 // Get the current domain name
66 $domain_name = $_SERVER['HTTP_HOST'];
67
68 // Generate a unique filename with a timestamp
69 $filename = $domain_name . '-woocommerce-customers-' . current_time( 'timestamp' ) . '.csv';
70
71 // Set the appropriate headers for a CSV file download
72 header( 'Content-Type: text/csv' );
73 header( 'Content-Disposition: attachment; filename=' . $filename );
74
75 // Open a file handle for writing
76 $file_handle = fopen( 'php://output', 'w' );
77
78 global $wpdb;
79
80 // Query WooCommerce customers
81 $customers = $wpdb->get_results("
82 SELECT u.ID, u.user_email
83 FROM {$wpdb->users} u
84 WHERE EXISTS (
85 SELECT 1
86 FROM {$wpdb->usermeta} um
87 WHERE u.ID = um.user_id
88 AND um.meta_key = 'wp_capabilities'
89 )
90 ORDER BY u.ID ASC
91 ");
92
93 // Prepare CSV data
94 $csv_data = array();
95 $csv_data[] = array(
96 'First Name',
97 'Last Name',
98 'WhatsApp Number',
99 'Status',
100 'List Name',
101 'Tags',
102 'billing_address_1',
103 'billing_address_2',
104 'billing_city',
105 'billing_state',
106 'billing_country',
107 );
108
109 foreach ( $customers as $customer ) {
110 $billing_phone = get_user_meta( $customer->ID, 'billing_phone', true );
111 if(!$billing_phone || trim($billing_phone) == ''){
112 continue;
113 }
114
115 $country_code = get_user_meta( $customer->ID, 'billing_country', true );
116 $state_code = get_user_meta( $customer->ID, 'billing_state', true );
117
118 $country = '';
119 $state = '';
120
121 if($country_code && $country_code != ''){
122 $country = WC()->countries->get_countries()[$country_code];
123 if($state_code && $state_code != ''){
124 $state = WC()->countries->get_states($country_code)[$state_code];
125 }
126 }
127
128 $csv_data[] = array(
129 get_user_meta( $customer->ID, 'billing_first_name', true ),
130 get_user_meta( $customer->ID, 'billing_last_name', true ),
131 str_replace('+','',Notifier_Woocommerce::get_formatted_phone_number($billing_phone, $country_code)),
132 'subscribed',
133 'WooCommerce Customers',
134 '',
135 get_user_meta( $customer->ID, 'billing_address_1', true ) ?: '',
136 get_user_meta( $customer->ID, 'billing_address_2', true ) ?: '',
137 get_user_meta( $customer->ID, 'billing_city', true ) ?: '',
138 $state,
139 $country,
140 );
141 }
142
143 // Generate CSV file
144 foreach ( $csv_data as $row ) {
145 fputcsv( $file_handle, $row );
146 }
147 fclose( $file_handle );
148 exit();
149 }
150
151 /**
152 * Insert New log into table wanotifier_activity_log
153 */
154 public static function insert_activity_log( $type = 'debug', $message = '' ) {
155 if ('yes' === get_option('notifier_enable_activity_log')) {
156 global $wpdb;
157 $table_name = $wpdb->prefix . NOTIFIER_ACTIVITY_TABLE_NAME;
158 $timestamp = current_time('mysql');
159
160 $data = array(
161 'timestamp' => $timestamp,
162 'message' => $message,
163 'type' => $type,
164 );
165
166 $format = array('%s', '%s', '%s');
167 $wpdb->insert($table_name, $data, $format);
168 }
169 }
170
171 /**
172 * Fetch all dates info for current user
173 */
174 public static function get_logs_date_lists() {
175 global $wpdb;
176 $table_name = $wpdb->prefix . NOTIFIER_ACTIVITY_TABLE_NAME;
177 $dates = $wpdb->get_results("SELECT DISTINCT DATE(timestamp) as log_date FROM `$table_name` ORDER BY timestamp DESC");
178
179 $final_dates = [];
180 foreach ($dates as $date) {
181 $final_dates[] = notifier_convert_date_from_utc($date->log_date,'Y-m-d');
182 }
183
184 return $final_dates;
185 }
186
187 /**
188 * Fetch all activity info for current user by date
189 */
190 public static function fetch_activity_logs_by_date() {
191 $activity_date = isset($_POST['notifier_activity_date']) ? sanitize_text_field($_POST['notifier_activity_date']) : '';
192
193 global $wpdb;
194 $table_name = $wpdb->prefix . NOTIFIER_ACTIVITY_TABLE_NAME;
195
196 $logs = $wpdb->get_results( $wpdb->prepare(
197 "SELECT * FROM `$table_name` WHERE DATE(timestamp) = %s ORDER BY timestamp DESC",
198 $activity_date
199 ));
200
201 $logs_preview_htm = '<div class="activity-logs">';
202 $logs_preview_htm .= '<table>';
203 $logs_preview_htm .= '<tbody>';
204 if (!empty($logs)){
205 foreach ($logs as $log){
206 $logs_preview_htm .= '<tr class="activity-record">';
207 $logs_preview_htm .= '<td style="width:10%; vertical-align: top;"><strong>'.esc_html($log->timestamp).'</strong> </td>';
208 $logs_preview_htm .= '<td style="width:90%; vertical-align: top;">'.esc_html($log->message).'</td>';
209 $logs_preview_htm .= '</tr>';
210 }
211 } else {
212 $logs_preview_htm .= '<tr class="no-records-found"> <td colspan="2">No logs found...</td></tr>';
213 }
214 $logs_preview_htm .= '</tbody>';
215 $logs_preview_htm .= '</table>';
216 $logs_preview_htm .= '</div>';
217
218 wp_send_json( array(
219 'preview' => $logs_preview_htm
220 ) );
221
222 }
223
224 }
225