1 <?php
  2 if ( ! defined( 'ABSPATH' ) ) {
  3     exit; // Exit if accessed directly
  4 }
  5 
  6 if ( ! class_exists( 'WP_List_Table' ) ) {
  7     require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  8 }
  9 
 10 /**
 11  * WC_Report_Customer_List
 12  *
 13  * @author      WooThemes
 14  * @category    Admin
 15  * @package     WooCommerce/Admin/Reports
 16  * @version     2.1.0
 17  */
 18 class WC_Report_Customer_List extends WP_List_Table {
 19 
 20     /**
 21      * __construct function.
 22      *
 23      * @access public
 24      */
 25     function __construct(){
 26         global $status, $page;
 27 
 28         parent::__construct( array(
 29             'singular'  => __( 'Customer', 'woocommerce' ),
 30             'plural'    => __( 'Customers', 'woocommerce' ),
 31             'ajax'      => false
 32         ) );
 33     }
 34 
 35     /**
 36      * No items found text
 37      */
 38     public function no_items() {
 39         _e( 'No customers found.', 'woocommerce' );
 40     }
 41 
 42     /**
 43      * Output the report
 44      */
 45     public function output_report() {
 46         $this->prepare_items();
 47 
 48         echo '<div id="poststuff" class="woocommerce-reports-wide">';
 49 
 50         if ( ! empty( $_GET['link_orders'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'link_orders' ) ) {
 51             $linked = wc_update_new_customer_past_orders( absint( $_GET['link_orders'] ) );
 52 
 53             echo '<div class="updated"><p>' . sprintf( _n( '%s previous order linked', '%s previous orders linked', $linked, 'woocommerce' ), $linked ) . '</p></div>';
 54         }
 55 
 56         echo '<form method="post" id="woocommerce_customers">';
 57 
 58         $this->search_box( __( 'Search customers', 'woocommerce' ), 'customer_search' );
 59         $this->display();
 60 
 61         echo '</form>';
 62         echo '</div>';
 63     }
 64 
 65     /**
 66      * column_default function.
 67      * @access public
 68      * @param mixed  $user
 69      * @param string $column_name
 70      * @return int|string
 71      * @todo Inconsistent return types, and void return at the end. Needs a rewrite.
 72      */
 73     function column_default( $user, $column_name ) {
 74         global $wpdb;
 75 
 76         switch( $column_name ) {
 77             case 'customer_name' :
 78                 if ( $user->last_name && $user->first_name ) {
 79                     return $user->last_name . ', ' . $user->first_name;
 80                 } else {
 81                     return '-';
 82                 }
 83             case 'username' :
 84                 return $user->user_login;
 85             break;
 86             case 'location' :
 87 
 88                 $state_code   = get_user_meta( $user->ID, 'billing_state', true );
 89                 $country_code = get_user_meta( $user->ID, 'billing_country', true );
 90 
 91                 $state = isset( WC()->countries->states[ $country_code ][ $state_code ] ) ? WC()->countries->states[ $country_code ][ $state_code ] : $state_code;
 92                 $country = isset( WC()->countries->countries[ $country_code ] ) ? WC()->countries->countries[ $country_code ] : $country_code;
 93 
 94                 $value = '';
 95 
 96                 if ( $state ) {
 97                     $value .= $state . ', ';
 98                 }
 99 
100                 $value .= $country;
101 
102                 if ( $value ) {
103                     return $value;
104                 } else {
105                     return '-';
106                 }
107             break;
108             case 'email' :
109                 return '<a href="mailto:' . $user->user_email . '">' . $user->user_email . '</a>';
110             case 'spent' :
111                 if ( ! $spent = get_user_meta( $user->ID, '_money_spent', true ) ) {
112 
113                     $spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value)
114                         FROM $wpdb->posts as posts
115 
116                         LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
117                         LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
118                         LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
119                         LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
120                         LEFT JOIN {$wpdb->terms} AS term USING( term_id )
121 
122                         WHERE   meta.meta_key       = '_customer_user'
123                         AND     meta.meta_value     = $user->ID
124                         AND     posts.post_type     = 'shop_order'
125                         AND     posts.post_status   = 'publish'
126                         AND     tax.taxonomy        = 'shop_order_status'
127                         AND     term.slug           IN ( 'completed' )
128                         AND     meta2.meta_key      = '_order_total'
129                     " );
130 
131                     update_user_meta( $user->ID, '_money_spent', $spent );
132                 }
133 
134                 return wc_price( $spent );
135             break;
136             case 'orders' :
137                 if ( ! $count = get_user_meta( $user->ID, '_order_count', true ) ) {
138 
139                     $count = $wpdb->get_var( "SELECT COUNT(*)
140                         FROM $wpdb->posts as posts
141 
142                         LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
143                         LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
144                         LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
145                         LEFT JOIN {$wpdb->terms} AS term USING( term_id )
146 
147                         WHERE   meta.meta_key       = '_customer_user'
148                         AND     posts.post_type     = 'shop_order'
149                         AND     posts.post_status   = 'publish'
150                         AND     tax.taxonomy        = 'shop_order_status'
151                         AND     term.slug           IN ( 'completed' )
152                         AND     meta_value          = $user->ID
153                     " );
154 
155                     update_user_meta( $user->ID, '_order_count', $count );
156                 }
157 
158                 return absint( $count );
159             break;
160             case 'last_order' :
161 
162                 $order_ids = get_posts( array(
163                     'posts_per_page' => 1,
164                     'post_type'      => 'shop_order',
165                     'orderby'        => 'date',
166                     'order'          => 'desc',
167                     'meta_query' => array(
168                         array(
169                             'key'     => '_customer_user',
170                             'value'   => $user->ID
171                         )
172                     ),
173                     'fields' => 'ids'
174                 ) );
175 
176                 if ( $order_ids ) {
177                     $order = new WC_Order( $order_ids[0] );
178 
179                     echo '<a href="' . admin_url( 'post.php?post=' . $order->id . '&action=edit' ) . '">' . $order->get_order_number() . '</a> &ndash; ' . date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) );
180                 } else echo '-';
181 
182             break;
183             case 'user_actions' :
184                 ?><p>
185                     <?php
186                         do_action( 'woocommerce_admin_user_actions_start', $user );
187 
188                         $actions = array();
189 
190                         $actions['edit'] = array(
191                             'url'       => admin_url( 'user-edit.php?user_id=' . $user->ID ),
192                             'name'      => __( 'Edit', 'woocommerce' ),
193                             'action'    => "edit"
194                         );
195 
196                         $actions['view'] = array(
197                             'url'       => admin_url( 'edit.php?post_type=shop_order&_customer_user=' . $user->ID ),
198                             'name'      => __( 'View orders', 'woocommerce' ),
199                             'action'    => "view"
200                         );
201 
202                         $order_ids = get_posts( array(
203                             'posts_per_page' => 1,
204                             'post_type'      => 'shop_order',
205                             'meta_query' => array(
206                                 array(
207                                     'key'     => '_customer_user',
208                                     'value'   => array( 0, '' ),
209                                     'compare' => 'IN'
210                                 ),
211                                 array(
212                                     'key'     => '_billing_email',
213                                     'value'   => $user->user_email
214                                 )
215                             ),
216                             'fields' => 'ids'
217                         ) );
218 
219                         if ( $order_ids ) {
220                             $actions['link'] = array(
221                                 'url'       => wp_nonce_url( add_query_arg( 'link_orders', $user->ID ), 'link_orders' ),
222                                 'name'      => __( 'Link previous orders', 'woocommerce' ),
223                                 'action'    => "link"
224                             );
225                         }
226 
227                         $actions = apply_filters( 'woocommerce_admin_user_actions', $actions, $user );
228 
229                         foreach ( $actions as $action ) {
230                             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'] ) );
231                         }
232 
233                         do_action( 'woocommerce_admin_user_actions_end', $user );
234                     ?>
235                 </p><?php
236             break;
237         }
238     }
239 
240     /**
241      * get_columns function.
242      *
243      * @access public
244      */
245     function get_columns(){
246         $columns = array(
247             'customer_name'   => __( 'Name (Last, First)', 'woocommerce' ),
248             'username'        => __( 'Username', 'woocommerce' ),
249             'email'           => __( 'Email', 'woocommerce' ),
250             'location'        => __( 'Location', 'woocommerce' ),
251             'orders'          => __( 'Orders', 'woocommerce' ),
252             'spent'           => __( 'Spent', 'woocommerce' ),
253             'last_order'      => __( 'Last order', 'woocommerce' ),
254             'user_actions'    => __( 'Actions', 'woocommerce' )
255         );
256 
257         return $columns;
258     }
259 
260     /**
261      * Order users by name
262      */
263     public function order_by_last_name( $query ) {
264         global $wpdb;
265 
266         $s = ! empty( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : '';
267 
268         $query->query_from    .= " LEFT JOIN {$wpdb->usermeta} as meta2 ON ({$wpdb->users}.ID = meta2.user_id) ";
269         $query->query_where   .= " AND meta2.meta_key = 'last_name' ";
270         $query->query_orderby  = " ORDER BY meta2.meta_value, user_login ASC ";
271 
272         if ( $s ) {
273             $query->query_from    .= " LEFT JOIN {$wpdb->usermeta} as meta3 ON ({$wpdb->users}.ID = meta3.user_id)";
274             $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 ) ) . "%' ) ";
275             $query->query_orderby  = " GROUP BY ID " . $query->query_orderby;
276         }
277 
278         return $query;
279     }
280 
281     /**
282      * prepare_items function.
283      *
284      * @access public
285      */
286     public function prepare_items() {
287         global $wpdb;
288 
289         $current_page = absint( $this->get_pagenum() );
290         $per_page     = 20;
291 
292         /**
293          * Init column headers
294          */
295         $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() );
296 
297         add_action( 'pre_user_query', array( $this, 'order_by_last_name' ) );
298 
299         /**
300          * Get users
301          */
302         $admin_users = new WP_User_Query(
303             array(
304                 'role'   => 'administrator',
305                 'fields' => 'ID'
306             )
307         );
308 
309         $manager_users = new WP_User_Query(
310             array(
311                 'role'   => 'shop_manager',
312                 'fields' => 'ID'
313             )
314         );
315 
316         $query = new WP_User_Query( array(
317             'exclude' => array_merge( $admin_users->get_results(), $manager_users->get_results() ),
318             'number'  => $per_page,
319             'offset'  => ( $current_page - 1 ) * $per_page
320         ) );
321 
322         $this->items = $query->get_results();
323 
324         remove_action( 'pre_user_query', array( $this, 'order_by_last_name' ) );
325 
326         /**
327          * Pagination
328          */
329         $this->set_pagination_args( array(
330             'total_items' => $query->total_users,
331             'per_page'    => $per_page,
332             'total_pages' => ceil( $query->total_users / $per_page )
333         ) );
334     }
335 }
336 
WooCommerce API documentation generated by ApiGen 2.8.0