Advertisement
Guest User

Untitled

a guest
Oct 7th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. <?php
  2. add_action( 'wp_ajax_store_search', 'wpsl_store_search' );
  3. add_action( 'wp_ajax_nopriv_store_search', 'wpsl_store_search' );
  4.  
  5. /**
  6. * Handle the ajax store search on the frontend.
  7. *
  8. * Search for stores that fall within the selected distance range.
  9. * This happens by calculating the distance between the latlng of the starting point
  10. * and the latlng from the stores.
  11. *
  12. * @since 1.0
  13. * @return json The list of stores in $store_results that matches with the request or false if the query returns no results
  14. */
  15.  
  16. error_reporting(E_ALL);
  17.  
  18. function wpsl_store_search() {
  19.  
  20. global $wpdb;
  21.  
  22. $options = get_option( 'wpsl_settings' );
  23. $distance_unit = ( $options['distance_unit'] == 'km' ) ? '6371' : '3959';
  24.  
  25. /* Check if we need to include the distance and radius limit in the sql query.
  26. * If autoload is enabled we load all stores, so no limits required.
  27. */
  28. if ( isset( $_GET['autoload'] ) && ( $_GET['autoload'] == 1 ) ) {
  29. $sql_part = ' ORDER BY distance';
  30. $placeholders = array(
  31. $_GET["lat"],
  32. $_GET["lng"],
  33. $_GET["lat"]
  34. );
  35. } else {
  36. $max_results = ( isset( $_GET['max_results'] ) ) ? $_GET['max_results'] : '';
  37.  
  38. if ( ( $max_results == 'NaN' ) || ( !$max_results ) ) {
  39. $max_results = get_default_list_value( $type = 'max_results' );
  40. }
  41.  
  42. $sql_part = ' HAVING distance < %d ORDER BY distance LIMIT 0, %d';
  43. $placeholders = array(
  44. $_GET["lat"],
  45. $_GET["lng"],
  46. $_GET["lat"],
  47. $_GET["radius"],
  48. $max_results
  49. );
  50. }
  51.  
  52. //i want to pull a number from a GET variable to limit the query to a certain store id.
  53. //i will be adding multiple and statements once I figure out how to add this variable string to the query.
  54.  
  55. if (isset($_GET['stores']))
  56. $storeid = " && wpsl_id = " . $_GET['stores'];
  57. else $storeid = "";
  58.  
  59. $result = $wpdb->get_results(
  60. $wpdb->prepare( "
  61. SELECT *, ( $distance_unit * acos( cos( radians( %s ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( lat ) ) ) )
  62. AS distance FROM $wpdb->wpsl_stores WHERE active = 1 $storeid
  63. $sql_part
  64. ",
  65. $placeholders
  66. )
  67. );
  68.  
  69. if ( $result === false ) {
  70. wp_send_json_error();
  71. } else {
  72. $store_results = array();
  73.  
  74. foreach ( $result as $k => $v ) {
  75. /* If we have a valid thumb id, get the src */
  76. if ( absint ( $result[$k]->thumb_id ) ) {
  77. $thumb_src = wp_get_attachment_image_src( $result[$k]->thumb_id );
  78. $result[$k]->thumb_src = $thumb_src[0];
  79. } else {
  80. $result[$k]->thumb_src = '';
  81. }
  82.  
  83. /* Sanitize the results before they are returned */
  84. $store_results[] = array (
  85. 'id' => absint( $result[$k]->wpsl_id ),
  86. 'store' => sanitize_text_field( stripslashes( $result[$k]->store ) ),
  87. 'address' => sanitize_text_field( stripslashes( $result[$k]->address ) ),
  88. 'address2' => sanitize_text_field( stripslashes( $result[$k]->address2 ) ),
  89. 'city' => sanitize_text_field( stripslashes( $result[$k]->city ) ),
  90. 'state' => sanitize_text_field( stripslashes( $result[$k]->state ) ),
  91. 'zip' => sanitize_text_field( stripslashes( $result[$k]->zip ) ),
  92. 'country' => sanitize_text_field( stripslashes( $result[$k]->country ) ),
  93. 'distance' => $result[$k]->distance,
  94. 'lat' => $result[$k]->lat,
  95. 'lng' => $result[$k]->lng,
  96. 'description' => wpautop( strip_tags( stripslashes( $result[$k]->description ) ) ),
  97. 'phone' => sanitize_text_field( stripslashes( $result[$k]->phone ) ),
  98. 'fax' => sanitize_text_field( stripslashes( $result[$k]->fax ) ),
  99. 'email' => sanitize_email( $result[$k]->email ),
  100. 'hours' => wpautop( strip_tags( stripslashes( $result[$k]->hours ) ) ),
  101. 'url' => esc_url( $result[$k]->url ),
  102. 'thumb' => esc_url( $result[$k]->thumb_src )
  103. );
  104. }
  105.  
  106. wp_send_json( $store_results );
  107. }
  108.  
  109. die();
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130. function wpsl_store_product_search() {
  131.  
  132. global $wpdb;
  133.  
  134. $options = get_option( 'wpsl_settings' );
  135. $distance_unit = ( $options['distance_unit'] == 'km' ) ? '6371' : '3959';
  136.  
  137. /* Check if we need to include the distance and radius limit in the sql query.
  138. * If autoload is enabled we load all stores, so no limits required.
  139. */
  140. if ( isset( $_GET['autoload'] ) && ( $_GET['autoload'] == 1 ) ) {
  141. $sql_part = ' ORDER BY distance';
  142. $placeholders = array(
  143. $_GET["lat"],
  144. $_GET["lng"],
  145. $_GET["lat"]
  146. );
  147. } else {
  148. $max_results = ( isset( $_GET['max_results'] ) ) ? $_GET['max_results'] : '';
  149.  
  150. if ( ( $max_results == 'NaN' ) || ( !$max_results ) ) {
  151. $max_results = get_default_list_value( $type = 'max_results' );
  152. }
  153.  
  154. $sql_part = ' HAVING distance < %d ORDER BY distance LIMIT 0, %d';
  155. $placeholders = array(
  156. $_GET["lat"],
  157. $_GET["lng"],
  158. $_GET["lat"],
  159. $_GET["radius"],
  160. $max_results
  161. );
  162. }
  163.  
  164. $result = $wpdb->get_results(
  165. $wpdb->prepare( "
  166. SELECT *, ( $distance_unit * acos( cos( radians( %s ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( lat ) ) ) )
  167. AS distance FROM $wpdb->wpsl_stores WHERE active = 1
  168. $sql_part
  169. ",
  170. $placeholders
  171. )
  172. );
  173.  
  174. if ( $result === false ) {
  175. wp_send_json_error();
  176. } else {
  177. $store_results = array();
  178.  
  179. foreach ( $result as $k => $v ) {
  180. /* If we have a valid thumb id, get the src */
  181. if ( absint ( $result[$k]->thumb_id ) ) {
  182. $thumb_src = wp_get_attachment_image_src( $result[$k]->thumb_id );
  183. $result[$k]->thumb_src = $thumb_src[0];
  184. } else {
  185. $result[$k]->thumb_src = '';
  186. }
  187.  
  188. /* Sanitize the results before they are returned */
  189. $store_results[] = array (
  190. 'id' => absint( $result[$k]->wpsl_id ),
  191. 'store' => sanitize_text_field( stripslashes( $result[$k]->store ) ),
  192. 'address' => sanitize_text_field( stripslashes( $result[$k]->address ) ),
  193. 'address2' => sanitize_text_field( stripslashes( $result[$k]->address2 ) ),
  194. 'city' => sanitize_text_field( stripslashes( $result[$k]->city ) ),
  195. 'state' => sanitize_text_field( stripslashes( $result[$k]->state ) ),
  196. 'zip' => sanitize_text_field( stripslashes( $result[$k]->zip ) ),
  197. 'country' => sanitize_text_field( stripslashes( $result[$k]->country ) ),
  198. 'distance' => $result[$k]->distance,
  199. 'lat' => $result[$k]->lat,
  200. 'lng' => $result[$k]->lng,
  201. 'description' => wpautop( strip_tags( stripslashes( $result[$k]->description ) ) ),
  202. 'phone' => sanitize_text_field( stripslashes( $result[$k]->phone ) ),
  203. 'fax' => sanitize_text_field( stripslashes( $result[$k]->fax ) ),
  204. 'email' => sanitize_email( $result[$k]->email ),
  205. 'hours' => wpautop( strip_tags( stripslashes( $result[$k]->hours ) ) ),
  206. 'url' => esc_url( $result[$k]->url ),
  207. 'thumb' => esc_url( $result[$k]->thumb_src )
  208. );
  209. }
  210.  
  211. wp_send_json( $store_results );
  212. }
  213.  
  214. die();
  215. }
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255. /**
  256. * Get the default selected value for a dropdown
  257. *
  258. * @since 1.0
  259. * @param string $type The request list type
  260. * @return string $response The default list value
  261. */
  262. function get_default_list_value( $type ) {
  263.  
  264. $settings = get_option( 'wpsl_settings' );
  265. $list_values = explode( ',', $settings[$type] );
  266.  
  267. foreach ( $list_values as $k => $list_value ) {
  268.  
  269. /* The default radius has a () wrapped around it, so we check for that and filter out the () */
  270. if ( strpos( $list_value, '(' ) !== false ) {
  271. $response = filter_var( $list_value, FILTER_SANITIZE_NUMBER_INT );
  272. break;
  273. }
  274. }
  275.  
  276. return $response;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement