Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Jalin_Search {
- public $add_filter = 0;
- public function __construct(){
- add_action( 'wp_ajax_query-projects', array( $this, 'jalin_query_projects' ) );
- add_action( 'wp_ajax_nopriv_query-projects', array( $this, 'jalin_query_projects' ) );
- }
- private function build_meta_query_array( $search ){
- if(!empty($search)){
- $meta_query = array('relation' => 'AND');
- $rental = $search->for_buy_or_rent == 'rent' ? true : false;
- foreach( (array) $search as $key => $value){
- if($value == 'Any' || $value == '') continue;
- switch($key){
- case 'for_buy_or_rent' :
- case 'type' :
- $meta_query[] = $this->build_meta_query($key, $value, '=');
- break;
- case 'max_beds' :
- if($value > 0 || $value != ''){
- $this->add_filter = 1;
- _log('do add filter');
- $meta_query[] = $this->build_meta_query('stock_item_main_%_bedrooms', $value, '<=', 'NUMERIC');
- }
- break;
- case 'max_price' :
- if($value > 0 || $value != ''){
- $this->add_filter = 1;
- $meta_query[] = $this->build_meta_query('stock_item_main_%_starting_price', $value, '<=', 'NUMERIC');
- }
- break;
- // attempt #2
- case 'keyword' :
- trim($value);
- $keyword_query = array('relation' => 'OR');
- $keyword_query[] = $this->build_meta_query('project_id', $value, 'LIKE', 'NUMERIC');
- $keyword_query[] = $this->build_meta_query('postcode', $value, 'LIKE', 'NUMERIC');
- $keyword_query[] = $this->build_meta_query('suburb', $value, 'LIKE');
- $meta_query[] = $keyword_query;
- break;
- }
- }
- }
- return $meta_query;
- }
- private function build_meta_query($key, $value, $compare, $type = 'CHAR'){
- return array(
- 'key' => $key,
- 'value' => $value,
- 'compare' => $compare,
- 'type' => $type
- );
- }
- public function jalin_where_filter( $pieces ){
- $pieces['where'] = str_replace("meta_key = 'stock_item_main_%", "meta_key LIKE 'stock_item_main_%", $pieces['where']);
- return $pieces;
- }
- public function jalin_query_projects() {
- $search = json_decode(stripslashes($_GET['search']));
- $projects = array();
- $project_args = array(
- 'post_type' => 'project',
- 'paged' => isset($search->page) ? $search->page : 1,
- 'posts_per_page' => 24,
- );
- if(gettype($search) === 'object'){
- $meta_query = $this->build_meta_query_array($search);
- $project_args['meta_query'] = $meta_query;
- }
- if($this->add_filter)
- add_filter('get_meta_sql', array($this, 'jalin_where_filter'));
- $posts = get_posts($project_args);
- if($this->add_filter)
- remove_filter( 'get_meta_sql', array($this, 'jalin_where_filter') );
- foreach($posts as $post){
- $fields = get_fields($post->ID);
- $stock = array();
- foreach($fields['stock_item_main'] as $s){
- $stock[] = array(
- 'bedrooms' => $s['bedrooms'],
- 'bathrooms' => $s['bathrooms'],
- 'cars' => $s['cars'],
- 'price' => $s['starting_price']
- );
- }
- _log($fields['project_image']['sizes']);
- $projects[] = array(
- 'name' => $post->post_title,
- 'slug' => $post->post_name,
- 'image' => $fields['project_image']['sizes']['thumb_crop'],
- 'type' => $fields['type'],
- 'contract' => $fields['for_buy_or_rent'],
- 'stock' => $stock,
- 'address' => array(
- 'street' => $fields['street'],
- 'suburb' => $fields['suburb'],
- 'postcode' => $fields['postcode'],
- 'state' => $fields['state'],
- )
- );
- }
- wp_send_json( $projects );
- }
- }
- $jalin_search = new Jalin_Search();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement