2013-06-05 15:04:09 +00:00
< ? php
2019-01-30 09:33:51 +00:00
/**
* Class WC_Report_Customer_List file .
*
* @ package WooCommerce\Reports
*/
2014-09-20 19:58:32 +00:00
2013-12-30 16:28:39 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2019-01-30 09:33:51 +00:00
exit ; // Exit if accessed directly.
2013-12-30 16:28:39 +00:00
}
2013-06-05 15:04:09 +00:00
2013-12-30 16:28:39 +00:00
if ( ! class_exists ( 'WP_List_Table' ) ) {
2018-03-05 18:59:17 +00:00
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ;
2013-12-30 16:28:39 +00:00
}
2013-06-05 15:04:09 +00:00
/**
2015-11-03 12:28:01 +00:00
* WC_Report_Customer_List .
2014-02-14 13:02:37 +00:00
*
2020-08-05 16:36:24 +00:00
* @ package WooCommerce\Admin\Reports
2014-02-14 13:02:37 +00:00
* @ version 2.1 . 0
2013-06-05 15:04:09 +00:00
*/
2013-07-09 14:45:42 +00:00
class WC_Report_Customer_List extends WP_List_Table {
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
/**
2016-01-05 09:00:52 +00:00
* Constructor .
2013-12-30 16:25:35 +00:00
*/
2014-08-31 07:19:13 +00:00
public function __construct () {
2018-03-05 18:59:17 +00:00
parent :: __construct (
array (
'singular' => 'customer' ,
'plural' => 'customers' ,
'ajax' => false ,
)
);
2013-12-30 16:25:35 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* No items found text .
2013-12-30 16:25:35 +00:00
*/
public function no_items () {
2019-01-30 09:33:51 +00:00
esc_html_e ( 'No customers found.' , 'woocommerce' );
2013-12-30 16:25:35 +00:00
}
2013-07-09 14:45:42 +00:00
/**
2015-11-03 12:28:01 +00:00
* Output the report .
2013-07-09 14:45:42 +00:00
*/
public function output_report () {
$this -> prepare_items ();
echo '<div id="poststuff" class="woocommerce-reports-wide">' ;
2019-01-30 09:33:51 +00:00
if ( ! empty ( $_GET [ 'link_orders' ] ) && wp_verify_nonce ( $_REQUEST [ '_wpnonce' ], 'link_orders' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
2013-11-25 12:52:53 +00:00
$linked = wc_update_new_customer_past_orders ( absint ( $_GET [ 'link_orders' ] ) );
2019-01-30 09:33:51 +00:00
/* translators: single or plural number of orders */
echo '<div class="updated"><p>' . sprintf ( esc_html ( _n ( '%s previous order linked' , '%s previous orders linked' , $linked , 'woocommerce' ), $linked ) ) . '</p></div>' ;
2013-07-09 14:45:42 +00:00
}
2019-01-30 09:33:51 +00:00
if ( ! empty ( $_GET [ 'refresh' ] ) && wp_verify_nonce ( $_REQUEST [ '_wpnonce' ], 'refresh' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
2014-10-02 15:44:36 +00:00
$user_id = absint ( $_GET [ 'refresh' ] );
$user = get_user_by ( 'id' , $user_id );
delete_user_meta ( $user_id , '_money_spent' );
delete_user_meta ( $user_id , '_order_count' );
2020-08-17 08:57:09 +00:00
delete_user_meta ( $user_id , '_last_order' );
2019-01-30 09:33:51 +00:00
/* translators: User display name */
echo '<div class="updated"><p>' . sprintf ( esc_html__ ( 'Refreshed stats for %s' , 'woocommerce' ), esc_html ( $user -> display_name ) ) . '</p></div>' ;
2014-10-02 15:44:36 +00:00
}
2013-12-30 16:25:35 +00:00
echo '<form method="post" id="woocommerce_customers">' ;
2013-07-09 14:45:42 +00:00
$this -> search_box ( __ ( 'Search customers' , 'woocommerce' ), 'customer_search' );
2013-12-30 16:25:35 +00:00
$this -> display ();
2013-07-09 14:45:42 +00:00
echo '</form>' ;
echo '</div>' ;
}
2013-11-28 16:49:30 +00:00
/**
2016-01-05 09:00:52 +00:00
* Get column value .
2014-08-31 07:19:13 +00:00
*
2019-01-30 09:33:51 +00:00
* @ param WP_User $user WP User object .
* @ param string $column_name Column name .
2015-01-23 18:34:43 +00:00
* @ return string
2013-11-28 16:49:30 +00:00
*/
2016-01-05 09:00:52 +00:00
public function column_default ( $user , $column_name ) {
2015-01-23 18:34:43 +00:00
switch ( $column_name ) {
2014-08-31 07:19:13 +00:00
2018-03-05 18:59:17 +00:00
case 'customer_name' :
2013-12-30 16:28:39 +00:00
if ( $user -> last_name && $user -> first_name ) {
2013-12-30 16:25:35 +00:00
return $user -> last_name . ', ' . $user -> first_name ;
2013-12-30 16:28:39 +00:00
} else {
2013-12-30 16:25:35 +00:00
return '-' ;
2013-12-30 16:28:39 +00:00
}
2014-08-31 07:19:13 +00:00
2018-03-05 18:59:17 +00:00
case 'username' :
2013-12-30 16:25:35 +00:00
return $user -> user_login ;
2014-08-31 07:19:13 +00:00
2018-03-05 18:59:17 +00:00
case 'location' :
2013-06-05 15:04:09 +00:00
$state_code = get_user_meta ( $user -> ID , 'billing_state' , true );
$country_code = get_user_meta ( $user -> ID , 'billing_country' , true );
2014-08-31 07:19:13 +00:00
$state = isset ( WC () -> countries -> states [ $country_code ][ $state_code ] ) ? WC () -> countries -> states [ $country_code ][ $state_code ] : $state_code ;
2013-11-25 14:01:32 +00:00
$country = isset ( WC () -> countries -> countries [ $country_code ] ) ? WC () -> countries -> countries [ $country_code ] : $country_code ;
2013-06-05 15:04:09 +00:00
$value = '' ;
2013-12-30 16:28:39 +00:00
if ( $state ) {
2013-06-05 15:04:09 +00:00
$value .= $state . ', ' ;
2013-12-30 16:28:39 +00:00
}
2013-06-05 15:04:09 +00:00
$value .= $country ;
2013-12-30 16:28:39 +00:00
if ( $value ) {
2013-07-09 14:45:42 +00:00
return $value ;
2013-12-30 16:28:39 +00:00
} else {
2013-12-30 16:25:35 +00:00
return '-' ;
2013-12-30 16:28:39 +00:00
}
2014-08-31 07:19:13 +00:00
2018-03-05 18:59:17 +00:00
case 'email' :
2013-12-30 16:25:35 +00:00
return '<a href="mailto:' . $user -> user_email . '">' . $user -> user_email . '</a>' ;
2014-08-31 07:19:13 +00:00
2018-03-05 18:59:17 +00:00
case 'spent' :
2015-01-12 17:09:42 +00:00
return wc_price ( wc_get_customer_total_spent ( $user -> ID ) );
2014-08-31 07:19:13 +00:00
2018-03-05 18:59:17 +00:00
case 'orders' :
2015-01-12 17:09:42 +00:00
return wc_get_customer_order_count ( $user -> ID );
2014-08-31 07:19:13 +00:00
2018-03-05 18:59:17 +00:00
case 'last_order' :
$orders = wc_get_orders (
array (
'limit' => 1 ,
'status' => array_map ( 'wc_get_order_status_name' , wc_get_is_paid_statuses () ),
'customer' => $user -> ID ,
)
);
2013-06-05 15:04:09 +00:00
2016-03-09 16:11:05 +00:00
if ( ! empty ( $orders ) ) {
$order = $orders [ 0 ];
2017-03-10 16:43:05 +00:00
return '<a href="' . admin_url ( 'post.php?post=' . $order -> get_id () . '&action=edit' ) . '">' . _x ( '#' , 'hash before order number' , 'woocommerce' ) . $order -> get_order_number () . '</a> – ' . wc_format_datetime ( $order -> get_date_created () );
2015-01-23 18:34:43 +00:00
} else {
return '-' ;
}
2013-06-05 15:04:09 +00:00
2018-03-05 18:59:17 +00:00
break ;
2014-08-31 07:19:13 +00:00
2018-03-05 18:59:17 +00:00
case 'wc_actions' :
2015-01-23 18:34:43 +00:00
ob_start ();
2013-06-05 15:04:09 +00:00
?> <p>
< ? php
2018-03-05 18:59:17 +00:00
do_action ( 'woocommerce_admin_user_actions_start' , $user );
$actions = array ();
$actions [ 'refresh' ] = array (
'url' => wp_nonce_url ( add_query_arg ( 'refresh' , $user -> ID ), 'refresh' ),
'name' => __ ( 'Refresh stats' , 'woocommerce' ),
'action' => 'refresh' ,
);
$actions [ 'edit' ] = array (
'url' => admin_url ( 'user-edit.php?user_id=' . $user -> ID ),
'name' => __ ( 'Edit' , 'woocommerce' ),
'action' => 'edit' ,
);
$actions [ 'view' ] = array (
'url' => admin_url ( 'edit.php?post_type=shop_order&_customer_user=' . $user -> ID ),
'name' => __ ( 'View orders' , 'woocommerce' ),
'action' => 'view' ,
);
$orders = wc_get_orders (
array (
'limit' => 1 ,
'status' => array_map ( 'wc_get_order_status_name' , wc_get_is_paid_statuses () ),
'customer' => array ( array ( 0 , $user -> user_email ) ),
)
);
if ( $orders ) {
$actions [ 'link' ] = array (
'url' => wp_nonce_url ( add_query_arg ( 'link_orders' , $user -> ID ), 'link_orders' ),
'name' => __ ( 'Link previous orders' , 'woocommerce' ),
'action' => 'link' ,
2014-10-02 15:44:36 +00:00
);
2018-03-05 18:59:17 +00:00
}
2014-10-02 15:44:36 +00:00
2018-03-05 18:59:17 +00:00
$actions = apply_filters ( 'woocommerce_admin_user_actions' , $actions , $user );
2013-06-05 15:04:09 +00:00
2018-03-05 18:59:17 +00:00
foreach ( $actions as $action ) {
printf ( '<a class="button tips %s" href="%s" data-tip="%s">%s</a>' , esc_attr ( $action [ 'action' ] ), esc_url ( $action [ 'url' ] ), esc_attr ( $action [ 'name' ] ), esc_attr ( $action [ 'name' ] ) );
}
2013-06-05 15:04:09 +00:00
2018-03-05 18:59:17 +00:00
do_action ( 'woocommerce_admin_user_actions_end' , $user );
2013-06-05 15:04:09 +00:00
?>
2018-03-05 18:59:17 +00:00
</ p >
< ? php
2015-01-23 18:34:43 +00:00
$user_actions = ob_get_contents ();
ob_end_clean ();
return $user_actions ;
2013-12-30 16:25:35 +00:00
}
2015-01-23 18:34:43 +00:00
return '' ;
2013-06-05 15:04:09 +00:00
}
2013-12-30 16:25:35 +00:00
/**
2016-01-05 09:00:52 +00:00
* Get columns .
2016-01-05 15:49:41 +00:00
*
* @ return array
2013-12-30 16:25:35 +00:00
*/
2016-01-05 09:00:52 +00:00
public function get_columns () {
2013-12-30 16:25:35 +00:00
$columns = array (
2018-03-05 18:59:17 +00:00
'customer_name' => __ ( 'Name (Last, First)' , 'woocommerce' ),
'username' => __ ( 'Username' , 'woocommerce' ),
'email' => __ ( 'Email' , 'woocommerce' ),
'location' => __ ( 'Location' , 'woocommerce' ),
'orders' => __ ( 'Orders' , 'woocommerce' ),
'spent' => __ ( 'Money spent' , 'woocommerce' ),
'last_order' => __ ( 'Last order' , 'woocommerce' ),
'wc_actions' => __ ( 'Actions' , 'woocommerce' ),
2013-12-30 16:25:35 +00:00
);
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
return $columns ;
}
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
/**
2015-11-03 12:28:01 +00:00
* Order users by name .
2016-01-05 15:49:41 +00:00
*
2019-01-30 09:33:51 +00:00
* @ param WP_User_Query $query Query that gets passed through .
2017-05-12 08:48:46 +00:00
* @ return WP_User_Query
2013-12-30 16:25:35 +00:00
*/
2013-06-05 15:04:09 +00:00
public function order_by_last_name ( $query ) {
2013-12-30 16:25:35 +00:00
global $wpdb ;
2013-06-05 15:04:09 +00:00
2019-01-30 09:33:51 +00:00
$s = ! empty ( $_REQUEST [ 's' ] ) ? wp_unslash ( $_REQUEST [ 's' ] ) : '' ; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2013-06-05 15:04:09 +00:00
2018-03-05 18:59:17 +00:00
$query -> query_from .= " LEFT JOIN { $wpdb -> usermeta } as meta2 ON ( { $wpdb -> users } .ID = meta2.user_id) " ;
$query -> query_where .= " AND meta2.meta_key = 'last_name' " ;
$query -> query_orderby = ' ORDER BY meta2.meta_value, user_login ASC ' ;
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
if ( $s ) {
2018-03-05 18:59:17 +00:00
$query -> query_from .= " LEFT JOIN { $wpdb -> usermeta } as meta3 ON ( { $wpdb -> users } .ID = meta3.user_id) " ;
$query -> query_where .= " AND ( user_login LIKE '% " . esc_sql ( str_replace ( '*' , '' , $s ) ) . " %' OR user_nicename LIKE '% " . esc_sql ( str_replace ( '*' , '' , $s ) ) . " %' OR meta3.meta_value LIKE '% " . esc_sql ( str_replace ( '*' , '' , $s ) ) . " %' ) " ;
$query -> query_orderby = ' GROUP BY ID ' . $query -> query_orderby ;
2013-12-30 16:25:35 +00:00
}
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
return $query ;
2013-06-05 15:04:09 +00:00
}
2013-12-30 16:25:35 +00:00
/**
2016-01-05 09:00:52 +00:00
* Prepare customer list items .
2013-12-30 16:25:35 +00:00
*/
public function prepare_items () {
2013-06-05 15:04:09 +00:00
$current_page = absint ( $this -> get_pagenum () );
$per_page = 20 ;
2013-12-30 16:25:35 +00:00
/**
2015-11-03 12:28:01 +00:00
* Init column headers .
2013-12-30 16:25:35 +00:00
*/
$this -> _column_headers = array ( $this -> get_columns (), array (), $this -> get_sortable_columns () );
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
add_action ( 'pre_user_query' , array ( $this , 'order_by_last_name' ) );
2013-06-05 15:04:09 +00:00
2013-12-30 16:25:35 +00:00
/**
2015-11-03 12:28:01 +00:00
* Get users .
2013-12-30 16:25:35 +00:00
*/
$admin_users = new WP_User_Query (
2013-07-09 14:45:42 +00:00
array (
2018-09-09 00:21:00 +00:00
'role' => 'administrator' ,
2016-08-27 01:46:45 +00:00
'fields' => 'ID' ,
2013-07-09 14:45:42 +00:00
)
);
$manager_users = new WP_User_Query (
array (
'role' => 'shop_manager' ,
2016-08-27 01:46:45 +00:00
'fields' => 'ID' ,
2013-07-09 14:45:42 +00:00
)
);
2018-03-05 18:59:17 +00:00
$query = new WP_User_Query (
2019-01-30 09:19:34 +00:00
apply_filters (
'woocommerce_admin_report_customer_list_user_query_args' ,
array (
'exclude' => array_merge ( $admin_users -> get_results (), $manager_users -> get_results () ),
'number' => $per_page ,
'offset' => ( $current_page - 1 ) * $per_page ,
)
2018-03-05 18:59:17 +00:00
)
);
2013-06-05 15:04:09 +00:00
$this -> items = $query -> get_results ();
remove_action ( 'pre_user_query' , array ( $this , 'order_by_last_name' ) );
2013-12-30 16:25:35 +00:00
/**
2015-11-03 12:28:01 +00:00
* Pagination .
2013-12-30 16:25:35 +00:00
*/
2018-03-05 18:59:17 +00:00
$this -> set_pagination_args (
array (
'total_items' => $query -> total_users ,
'per_page' => $per_page ,
'total_pages' => ceil ( $query -> total_users / $per_page ),
)
);
2013-12-30 16:25:35 +00:00
}
2013-12-02 11:34:27 +00:00
}