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