edd_ajax_customer_search

Search the customers database via AJAX.

Description

edd_ajax_customer_search(); 

Usage

  1. if ( !function_exists( 'edd_ajax_customer_search' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'easy-digital-downloads/includes/ajax-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = edd_ajax_customer_search(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/ajax-functions.php  
  1. function edd_ajax_customer_search() { 
  2. global $wpdb; 
  3.  
  4. $search = esc_sql( sanitize_text_field( $_GET['s'] ) ); 
  5. $results = array(); 
  6. $customer_view_role = apply_filters( 'edd_view_customers_role', 'view_shop_reports' ); 
  7. if ( ! current_user_can( $customer_view_role ) ) { 
  8. $customers = array(); 
  9. } else { 
  10. $select = "SELECT id, name, email FROM {$wpdb->prefix}edd_customers "; 
  11. if ( is_numeric( $search ) ) { 
  12. $where = "WHERE `id` LIKE '%$search%' OR `user_id` LIKE '%$search%' "; 
  13. } else { 
  14. $where = "WHERE `name` LIKE '%$search%' OR `email` LIKE '%$search%' "; 
  15. $limit = "LIMIT 50"; 
  16.  
  17. $customers = $wpdb->get_results( $select . $where . $limit ); 
  18.  
  19. if( $customers ) { 
  20.  
  21. foreach( $customers as $customer ) { 
  22.  
  23. $results[] = array( 
  24. 'id' => $customer->id,  
  25. 'name' => $customer->name . '(' . $customer->email . ')' 
  26. ); 
  27.  
  28. } else { 
  29.  
  30. $customers[] = array( 
  31. 'id' => 0,  
  32. 'name' => __( 'No results found', 'easy-digital-downloads' ) 
  33. ); 
  34.  
  35.  
  36. echo json_encode( $results ); 
  37.  
  38. edd_die();