edd_ajax_download_search

Retrieve a states drop down.

Description

edd_ajax_download_search(); 

Usage

  1. if ( !function_exists( 'edd_ajax_download_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_download_search(); 
  7.  

Defined (1)

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

/includes/ajax-functions.php  
  1. function edd_ajax_download_search() { 
  2. global $wpdb; 
  3.  
  4. $search = esc_sql( sanitize_text_field( $_GET['s'] ) ); 
  5. $excludes = ( isset( $_GET['current_id'] ) ? (array) $_GET['current_id'] : array() ); 
  6.  
  7. $no_bundles = isset( $_GET['no_bundles'] ) ? filter_var( $_GET['no_bundles'], FILTER_VALIDATE_BOOLEAN ) : false; 
  8. if( true === $no_bundles ) { 
  9. $bundles = $wpdb->get_results( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_edd_product_type' AND meta_value = 'bundle';", ARRAY_A ); 
  10. $bundles = wp_list_pluck( $bundles, 'post_id' ); 
  11. $excludes = array_merge( $excludes, $bundles ); 
  12.  
  13. $variations = isset( $_GET['variations'] ) ? filter_var( $_GET['variations'], FILTER_VALIDATE_BOOLEAN ) : false; 
  14.  
  15. $excludes = array_unique( array_map( 'absint', $excludes ) ); 
  16. $exclude = implode( ", ", $excludes ); 
  17.  
  18. $results = array(); 
  19.  
  20. // Setup the SELECT statement 
  21. $select = "SELECT ID, post_title FROM $wpdb->posts "; 
  22.  
  23. // Setup the WHERE clause 
  24. $where = "WHERE `post_type` = 'download' and `post_title` LIKE '%s' "; 
  25.  
  26. // If we have items to exclude, exclude them 
  27. if( ! empty( $exclude ) ) { 
  28. $where .= "AND `ID` NOT IN (" . $exclude . ") "; 
  29.  
  30. // If the user can't edit products, limit to just published items 
  31. if( ! current_user_can( 'edit_products' ) ) { 
  32. $where .= "AND `post_status` = 'publish' "; 
  33.  
  34. // Limit the result sets 
  35. $limit = "LIMIT 50"; 
  36.  
  37. $sql = $select . $where . $limit; 
  38.  
  39. $prepared_statement = $wpdb->prepare( $sql, '%' . $search . '%' ); 
  40.  
  41. $items = $wpdb->get_results( $prepared_statement ); 
  42.  
  43. if( $items ) { 
  44.  
  45. foreach( $items as $item ) { 
  46.  
  47. $results[] = array( 
  48. 'id' => $item->ID,  
  49. 'name' => $item->post_title 
  50. ); 
  51.  
  52. if ( $variations && edd_has_variable_prices( $item->ID ) ) { 
  53. $prices = edd_get_variable_prices( $item->ID ); 
  54.  
  55. foreach ( $prices as $key => $value ) { 
  56. $name = ! empty( $value['name'] ) ? $value['name'] : ''; 
  57. $amount = ! empty( $value['amount'] ) ? $value['amount'] : ''; 
  58. $index = ! empty( $value['index'] ) ? $value['index'] : $key; 
  59.  
  60. if ( $name && $index ) { 
  61. $results[] = array( 
  62. 'id' => $item->ID . '_' . $key,  
  63. 'name' => esc_html( $item->post_title . ': ' . $name ),  
  64. ); 
  65.  
  66. } else { 
  67.  
  68. $results[] = array( 
  69. 'id' => 0,  
  70. 'name' => __( 'No results found', 'easy-digital-downloads' ) 
  71. ); 
  72.  
  73.  
  74. echo json_encode( $results ); 
  75.  
  76. edd_die();