1
|
|
|
<?php if ( !defined( 'ABSPATH' ) ) exit; |
2
|
|
|
|
3
|
|
|
class wps_statistics_ctr { |
4
|
|
|
|
5
|
|
|
private $wps_stats_mdl; |
6
|
|
|
|
7
|
|
|
function __construct() { |
|
|
|
|
8
|
|
|
// WP Main Actions |
9
|
|
|
add_action( 'admin_menu', array(&$this, 'register_stats_menu'), 250); |
10
|
|
|
add_action( 'save_post', array( &$this, 'wps_statistics_save_customer_infos') ); |
11
|
|
|
add_action( 'add_meta_boxes', array( &$this, 'add_customer_meta_box'), 1 ); |
12
|
|
|
|
13
|
|
|
// Add Javascript Files & CSS File in admin |
14
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ) ); |
15
|
|
|
|
16
|
|
|
add_action( 'wp_ajax_wps_statistics_custom_date_view', array( $this, 'wps_statistics_custom_date_view' ) ); |
17
|
|
|
|
18
|
|
|
$this->wps_stats_mdl = new wps_statistics_mdl(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Add Javascript & CSS files |
23
|
|
|
*/ |
24
|
|
|
function add_scripts( $hook ) { |
|
|
|
|
25
|
|
|
global $current_screen; |
26
|
|
|
if( ! in_array( $current_screen->post_type, array( WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS ), true ) && $hook != 'wpshop_shop_order_page_wpshop_statistics' ) |
27
|
|
|
return; |
28
|
|
|
|
29
|
|
|
wp_enqueue_script( 'wps_statistics_js_chart', WPSHOP_JS_URL . 'Chart.js' ); |
30
|
|
|
wp_enqueue_script( 'jquery' ); |
31
|
|
|
wp_enqueue_script( 'jquery-ui-datepicker' ); |
32
|
|
|
wp_enqueue_script( 'jquery-form' ); |
33
|
|
|
|
34
|
|
|
wp_register_style( 'jquery-ui-wpsstats', '//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css', '', WPSHOP_VERSION ); |
35
|
|
|
wp_enqueue_style( 'jquery-ui-wpsstats' ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add Meta Boxes to exclude customers of WPShop Statistics |
40
|
|
|
*/ |
41
|
|
|
function add_customer_meta_box() { |
|
|
|
|
42
|
|
|
global $post; |
43
|
|
|
add_meta_box( 'wps_statistics_customer', __( 'Statistics', 'wps_price' ), array( &$this, 'wps_statistics_meta_box_content' ), WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS, 'side', 'low' ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Meta box content to exclude customers of statistics |
48
|
|
|
*/ |
49
|
|
|
function wps_statistics_meta_box_content() { |
|
|
|
|
50
|
|
|
global $post; |
51
|
|
|
$user_meta = ''; |
52
|
|
|
if ( !empty($post) && !empty($post->post_author) ) { |
53
|
|
|
$user_meta = get_user_meta( $post->post_author, 'wps_statistics_exclude_customer', true ); |
54
|
|
|
} |
55
|
|
|
$output = '<input type="checkbox" name="wps_statistics_exclude_customer" id="wps_statistics_exclude_customer" ' .( (!empty($user_meta) ) ? 'checked="checked"' : '' ). '/> <label for="wps_statistics_exclude_customer">' .__('Exclude this customer from WPShop Statistics', 'wpshop'). '</label>'; |
56
|
|
|
echo $output; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Save action to exclude customer of statistics |
61
|
|
|
*/ |
62
|
|
|
function wps_statistics_save_customer_infos() { |
|
|
|
|
63
|
|
|
$action = !empty( $_POST['action'] ) ? sanitize_text_field( $_POST['action'] ) : ''; |
64
|
|
|
$post_type = !empty( $_POST['post_type'] ) ? sanitize_text_field( $_POST['post_type'] ) : ''; |
65
|
|
|
$post_id = !empty( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0; |
66
|
|
|
$wps_statistics_exclude_customer = isset( $_POST['wps_statistics_exclude_customer'] ) ? (int) $_POST['wps_statistics_exclude_customer'] : 0; |
67
|
|
|
|
68
|
|
|
if ( !empty($action) && $action != 'autosave' && !empty($post_type) && $post_type == WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS ) { |
69
|
|
|
$customer_def = get_post( $post_id ); |
70
|
|
|
if( isset( $wps_statistics_exclude_customer ) ) { |
71
|
|
|
update_user_meta( $customer_def->post_author, 'wps_statistics_exclude_customer', $wps_statistics_exclude_customer ); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Register statistics Menu |
78
|
|
|
*/ |
79
|
|
|
function register_stats_menu() { |
|
|
|
|
80
|
|
|
add_submenu_page( 'edit.php?post_type=' . WPSHOP_NEWTYPE_IDENTIFIER_ORDER, __('Statistics', 'wpshop' ), __('Statistics', 'wpshop'), 'wpshop_view_statistics', 'wpshop_statistics', array($this, 'wps_display_statistics')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Display Statistics Interface |
85
|
|
|
*/ |
86
|
|
|
function wps_display_statistics() { |
|
|
|
|
87
|
|
|
$shop_orders = $this->wps_stats_mdl->wps_orders_all( ); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$main_stats_count = 5; |
90
|
|
|
|
91
|
|
|
$ordered_customers = array(); |
92
|
|
|
foreach ( $shop_orders as $order ) { |
|
|
|
|
93
|
|
|
$user_id = $order['order_postmeta']['customer_id']; |
94
|
|
|
$customer_id = null; |
95
|
|
|
$args = array( |
96
|
|
|
'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS, |
97
|
|
|
'author' => $user_id, |
98
|
|
|
'orderby' => 'post_date', |
99
|
|
|
'order' => 'ASC', |
100
|
|
|
'post_status' => 'all', |
101
|
|
|
'posts_per_page' => 1, |
102
|
|
|
); |
103
|
|
|
$customer = new WP_Query( $args ); |
104
|
|
|
|
105
|
|
|
if ( ! isset( $ordered_customers[ $user_id ]['count'] ) ) { |
106
|
|
|
$ordered_customers[ $user_id ]['count'] = 0; |
107
|
|
|
$ordered_customers[ $user_id ]['total_amount'] = 0; |
108
|
|
|
} |
109
|
|
|
$ordered_customers[ $user_id ]['count']++; |
110
|
|
|
$ordered_customers[ $user_id ]['id'] = $user_id; |
111
|
|
|
$ordered_customers[ $user_id ]['post_id'] = $customer->post->ID; |
112
|
|
|
$ordered_customers[ $user_id ]['name'] = ( !empty($order['order_info']) && !empty($order['order_info']['billing']) && !empty($order['order_info']['billing']['address']) && !empty($order['order_info']['billing']['address']['address_last_name']) && !empty($order['order_info']['billing']['address']['address_first_name']) ) ? $order['order_info']['billing']['address']['address_first_name'].' '.$order['order_info']['billing']['address']['address_last_name'] : '';; |
113
|
|
|
$ordered_customers[ $user_id ]['total_amount'] += $order['order_postmeta']['order_grand_total']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
require( wpshop_tools::get_template_part( WPS_STATISTICS_DIR, WPS_STATISTICS_TEMPLATES_MAIN_DIR, "backend", "wps-statistics") ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the average duration between |
121
|
|
|
* |
122
|
|
|
* @param [type] $order_list [description] |
|
|
|
|
123
|
|
|
* |
124
|
|
|
* @return [type] [description] |
|
|
|
|
125
|
|
|
*/ |
126
|
|
|
function get_average_time_between_orders( $order_list ) { |
|
|
|
|
127
|
|
|
$time_between_orders = 0; |
128
|
|
|
$last_date = null; |
129
|
|
|
foreach ( $order_list as $order ) { |
130
|
|
|
if ( null !== $last_date ) { |
131
|
|
|
$last_order = new DateTime( $last_date ); |
132
|
|
|
$current_order = new DateTime( $order['order_postmeta']['order_date'] ); |
133
|
|
|
$time_between_orders += $last_order->getTimestamp() - $current_order->getTimestamp(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$last_date = $order['order_postmeta']['order_date']; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return round( $time_between_orders / count( $order_list ) ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check if the shop is out of bounds for time since last order |
144
|
|
|
* |
145
|
|
|
* @return boolean The boolean state allowing to know if the shop is out of bounds for time since last order |
146
|
|
|
*/ |
147
|
|
|
function check_current_time_since_last_order() { |
|
|
|
|
148
|
|
|
$average_check = array( |
149
|
|
|
'status' => false, |
150
|
|
|
'last_date' => null, |
151
|
|
|
'average' => null, |
152
|
|
|
'duration' => null, |
153
|
|
|
); |
154
|
|
|
$current_date = new DateTime( 'now' ); |
155
|
|
|
|
156
|
|
|
$list_orders = $this->wps_stats_mdl->wps_orders_all(); |
|
|
|
|
157
|
|
|
if ( ! empty( $list_orders ) ) { |
158
|
|
|
|
159
|
|
|
$last_order = array_slice( $list_orders, 0, 1 ); |
160
|
|
|
foreach ( $last_order as $order ) { |
161
|
|
|
$average_check[ 'last_date' ] = $order['order_postmeta']['order_date']; |
162
|
|
|
} |
163
|
|
|
$last_order_dateTime = new DateTime( $average_check[ 'last_date' ] ); |
164
|
|
|
|
165
|
|
|
$duration_since_last_order = $current_date->getTimestamp() - $last_order_dateTime->getTimestamp(); |
166
|
|
|
$average_check['duration'] = $duration_since_last_order; |
167
|
|
|
$average_check['average'] = $this->get_average_time_between_orders( $list_orders ); |
168
|
|
|
|
169
|
|
|
$average_check['status'] = ( $average_check['duration'] > $average_check['average'] ? true : false ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $average_check; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Main Statistics output |
177
|
|
|
*/ |
178
|
|
|
function wps_display_main_statistics() { |
|
|
|
|
179
|
|
|
global $wpdb, $current_month_offset; |
180
|
|
|
|
181
|
|
|
$current_month_offset = (int) current_time( 'm' ); |
182
|
|
|
$current_month_offset = isset( $_GET['month'] ) ? (int) $_GET['month'] : $current_month_offset; |
183
|
|
|
|
184
|
|
|
$current_month_start = date( 'Y-m-d 00:00:00', strtotime( 'first day of this month', time() ) ); |
185
|
|
|
$current_month_end = date( 'Y-m-d 23:59:59', strtotime( 'last day of this month', time() ) ); |
186
|
|
|
|
187
|
|
|
$last_month_start = date( 'Y-m-d 00:00:00', strtotime( 'first day of last month', time() ) ); |
188
|
|
|
$last_month_end = date( 'Y-m-d 23:59:59', strtotime( 'last day of last month', time() ) ); |
189
|
|
|
$one_month_ago = date( 'Y-m-d 23:59:59', strtotime( '-1 month', time() ) ); |
190
|
|
|
|
191
|
|
|
$dates = array( |
|
|
|
|
192
|
|
|
__( 'Current month', 'wpshop' ) => array( |
193
|
|
|
'after' => $current_month_start, |
194
|
|
|
'before' => $current_month_end, |
195
|
|
|
'inclusive' => true, |
196
|
|
|
), |
197
|
|
|
sprintf( __( 'One month ago (%s)', 'wpshop' ), mysql2date( get_option( 'date_format' ), $one_month_ago, true ) ) => array( |
198
|
|
|
'after' => $last_month_start, |
199
|
|
|
'before' => $one_month_ago, |
200
|
|
|
'inclusive' => true, |
201
|
|
|
), |
202
|
|
|
__( 'Last month', 'wpshop' ) => array( |
203
|
|
|
'after' => $last_month_start, |
204
|
|
|
'before' => $last_month_end, |
205
|
|
|
'inclusive' => true, |
206
|
|
|
), |
207
|
|
|
__( 'From the beginning', 'wpshop' ) => null, |
208
|
|
|
); |
209
|
|
|
|
210
|
|
|
require_once( wpshop_tools::get_template_part( WPS_STATISTICS_DIR, WPS_STATISTICS_TEMPLATES_MAIN_DIR, 'backend', 'wps_statistics_main' ) ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Display custom Statistics area. Allows to choose date for stats displaying |
215
|
|
|
* |
216
|
|
|
* @param string $start Optionnal. The start date to get stats for. |
217
|
|
|
* @param string $end Optionnal. The end date to get stats for. |
218
|
|
|
*/ |
219
|
|
|
function wps_display_custom_statistics( $start = null, $end = null ) { |
|
|
|
|
220
|
|
|
$current_month_start = date( 'Y-m-d', strtotime( 'first day of this month', time() ) ); |
221
|
|
|
$current_month_end = date( 'Y-m-d', strtotime( 'last day of this month', time() ) ); |
222
|
|
|
|
223
|
|
|
$last_month_start = date( 'Y-m-d', strtotime( 'first day of last month', time() ) ); |
224
|
|
|
$last_month_end = date( 'Y-m-d', strtotime( 'last day of last month', time() ) ); |
225
|
|
|
|
226
|
|
|
$date_start = null !== $start ? $start : date( 'Y-m-d', strtotime( 'first day of this month', time() ) ); |
227
|
|
|
$date_end = null !== $end ? $end : date( 'Y-m-d', strtotime( 'last day of this month', time() ) ); |
228
|
|
|
|
229
|
|
|
$stats_translations = array( |
230
|
|
|
'numberOfSales' => __('Number of sales','wpshop'), |
231
|
|
|
'sales' => __('sales','wpshop'), |
232
|
|
|
'salesAmount' => __('Sales amount','wpshop'), |
233
|
|
|
'wpshopCurrency' => wpshop_tools::wpshop_get_currency(), |
234
|
|
|
); |
235
|
|
|
|
236
|
|
|
$order_list = $this->wps_stats_mdl->wps_orders_all( array( |
|
|
|
|
237
|
|
|
'date_query' => array( |
238
|
|
|
array( |
239
|
|
|
'after' => $date_start, |
240
|
|
|
'before' => $date_end, |
241
|
|
|
'inclusive' => true, |
242
|
|
|
) |
243
|
|
|
), |
244
|
|
|
) ); |
245
|
|
|
|
246
|
|
|
$orders_total_amount = $orders_total_shipping_cost = 0; |
247
|
|
|
$orders_number_stats = $orders_amount_stats = array(); |
248
|
|
|
if ( ! empty( $order_list ) ) { |
249
|
|
|
foreach ( $order_list as $order ) { |
250
|
|
|
$order_data = $order['order_postmeta']; |
251
|
|
|
$orders_total_amount += $order_data['order_grand_total']; |
252
|
|
|
|
253
|
|
|
$time = strtotime( date( 'Ymd', strtotime( $order_data['order_date'] ) ) ) . '000'; |
254
|
|
|
if ( ! isset( $orders_number_stats[ $time ] ) ) { |
255
|
|
|
$orders_number_stats[ $time ] = 0; |
256
|
|
|
} |
257
|
|
|
$orders_number_stats[ $time ]++; |
258
|
|
|
|
259
|
|
|
if ( ! isset( $orders_amount_stats[ $time ] ) ) { |
260
|
|
|
$orders_amount_stats[ $time ] = 0; |
261
|
|
|
} |
262
|
|
|
$orders_amount_stats[ $time ] += $order_data['order_grand_total']; |
263
|
|
|
|
264
|
|
|
$orders_total_shipping_cost += $order_data['order_shipping_cost']; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$orders_number_for_stats = null; |
269
|
|
|
if ( ! empty( $orders_number_stats ) ) { |
270
|
|
|
$orders_numbers= array(); |
271
|
|
|
foreach ( $orders_number_stats as $time => $number ) { |
272
|
|
|
$orders_numbers[] = "[$time, $number]"; |
273
|
|
|
} |
274
|
|
|
$orders_number_for_stats = implode( ',', $orders_numbers ); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$orders_amount_for_stats = null; |
278
|
|
|
if ( ! empty( $orders_amount_stats ) ) { |
279
|
|
|
$orders_amounts = array(); |
280
|
|
|
foreach ( $orders_amount_stats as $time => $amount ) { |
281
|
|
|
$orders_amounts[] = "[$time, $amount]"; |
282
|
|
|
} |
283
|
|
|
$orders_amount_for_stats = implode( ',', $orders_amounts ); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
$user_subscription_number = new WP_User_Query( array( |
287
|
|
|
'date_query' => array( |
288
|
|
|
array( |
289
|
|
|
'after' => $date_start, |
290
|
|
|
'before' => $date_end, |
291
|
|
|
'inclusive' => true, |
292
|
|
|
) |
293
|
|
|
), |
294
|
|
|
'count_total' => true, |
295
|
|
|
) ); |
296
|
|
|
|
297
|
|
|
require( wpshop_tools::get_template_part( WPS_STATISTICS_DIR, WPS_STATISTICS_TEMPLATES_MAIN_DIR, 'backend', 'wps_statistics_custom') ); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Ajax callback - Display custom statistics for given date |
302
|
|
|
*/ |
303
|
|
|
function wps_statistics_custom_date_view() { |
|
|
|
|
304
|
|
|
check_ajax_referer( 'wps_statistics_custom_date_view' ); |
305
|
|
|
|
306
|
|
|
$start_date = ! empty( $_POST ) && ! empty( $_POST['wps_statistics_start_date'] ) ? sanitize_text_field( $_POST['wps_statistics_start_date'] ) : date( 'Y-m-d', strtotime( 'first day of this month', time() ) ); |
307
|
|
|
$end_date = ! empty( $_POST ) && ! empty( $_POST['wps_statistics_end_date'] ) ? sanitize_text_field( $_POST['wps_statistics_end_date'] ) : date( 'Y-m-d', strtotime( 'last day of this month', time() ) ); |
308
|
|
|
|
309
|
|
|
ob_start(); |
310
|
|
|
$this->wps_display_custom_statistics( $start_date, $end_date ); |
311
|
|
|
$output = ob_get_clean(); |
312
|
|
|
|
313
|
|
|
wp_die( $output ); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
} |
317
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.