diff --git a/inc/api/class-opalestate-api.php b/inc/api/class-opalestate-api.php index f9ad1de7..5819d027 100755 --- a/inc/api/class-opalestate-api.php +++ b/inc/api/class-opalestate-api.php @@ -45,6 +45,7 @@ class Opalestate_API { 'v1/property.php', 'v1/agent.php', 'v1/agency.php', + 'v1/search-form.php', 'class-opalestate-api-auth.php', 'functions.php', ] ); @@ -92,6 +93,7 @@ class Opalestate_API { 'Opalestate_Property_Api', 'Opalestate_Agent_Api', 'Opalestate_Agency_Api', + 'Opalestate_Search_Form_Api', ] ); diff --git a/inc/api/v1/property.php b/inc/api/v1/property.php index bf9fc1e1..c54e8415 100644 --- a/inc/api/v1/property.php +++ b/inc/api/v1/property.php @@ -97,18 +97,13 @@ class Opalestate_Property_Api extends Opalestate_Base_API { register_rest_route( $this->namespace, - '/' . $this->base . '/search/', + '/' . $this->base . '/search', [ - 'args' => [ - 'id' => [ - 'description' => __( 'Unique identifier for the resource.', 'opalestate-pro' ), - 'type' => 'integer', - ], - ], [ 'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'get_results' ], - // 'permission_callback' => [ $this, 'get_item_permissions_check' ], + // 'permission_callback' => [ $this, 'get_items_permissions_check' ], + 'args' => $this->get_search_params(), ], ] ); @@ -142,6 +137,8 @@ class Opalestate_Property_Api extends Opalestate_Base_API { $properties[ $i ] = $this->get_property_data( $property_info ); $i++; } + } else { + return $this->get_response( 404, [ 'collection' => esc_html__( 'Not found', 'opalestate-pro' ) ] ); } $response['collection'] = $properties; @@ -179,8 +176,8 @@ class Opalestate_Property_Api extends Opalestate_Base_API { } public function delete_item( $request ) { - $id = (int) $request['id']; - $force = (bool) $request['force']; + $id = (int) $request['id']; + $force = (bool) $request['force']; $property = get_post( absint( $request['id'] ) ); if ( ! $property || $this->post_type != $property->post_type ) { @@ -193,6 +190,25 @@ class Opalestate_Property_Api extends Opalestate_Base_API { return $response; } + public function get_results( $request ) { + $properties = []; + $property_list = $this->get_search_results_query( $request ); + + if ( $property_list ) { + $i = 0; + foreach ( $property_list as $property_info ) { + $properties[ $i ] = $this->get_property_data( $property_info ); + $i++; + } + } else { + return $this->get_response( 404, [ 'collection' => esc_html__( 'Not found', 'opalestate-pro' ) ] ); + } + + $response['collection'] = $properties; + + return $this->get_response( 200, $response ); + } + /** * The opalestate_property post object, generate the data for the API output * @@ -228,13 +244,233 @@ class Opalestate_Property_Api extends Opalestate_Base_API { $post_id = wp_insert_post( $data, true ); $response['id'] = $post_id; - $response = rest_ensure_response( $response ); + $response = rest_ensure_response( $response ); $response->set_status( 201 ); $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->base, $post_id ) ) ); return $response; } + /** + * Get Query Object to display collection of property with user request which submited via search form. + * + * @param WP_REST_Request $request Full details about the request. + * @return + */ + public function get_search_results_query( $request ) { + $search_min_price = isset( $request['min_price'] ) ? sanitize_text_field( $request['min_price'] ) : ''; + $search_max_price = isset( $request['max_price'] ) ? sanitize_text_field( $request['max_price'] ) : ''; + $search_min_area = isset( $request['min_area'] ) ? sanitize_text_field( $request['min_area'] ) : ''; + $search_max_area = isset( $request['max_area'] ) ? sanitize_text_field( $request['max_area'] ) : ''; + $s = isset( $request['search_text'] ) ? sanitize_text_field( $request['search_text'] ) : null; + $per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5; + $paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1; + + if ( isset( $request['paged'] ) && intval( $request['paged'] ) > 0 ) { + $paged = intval( $request['paged'] ); + } + + $args = [ + 'posts_per_page' => $per_page, + 'paged' => $paged, + 'post_type' => $this->post_type, + 'post_status' => 'publish', + 's' => $s, + ]; + + $tax_query = []; + + if ( isset( $request['location'] ) && $request['location'] != -1 ) { + $tax_query[] = + [ + 'taxonomy' => 'opalestate_location', + 'field' => 'slug', + 'terms' => sanitize_text_field( $request['location'] ), + ]; + } + + if ( isset( $request['state'] ) && $request['state'] != -1 ) { + $tax_query[] = + [ + 'taxonomy' => 'opalestate_state', + 'field' => 'slug', + 'terms' => sanitize_text_field( $request['state'] ), + ]; + } + + if ( isset( $request['city'] ) && $request['city'] != -1 ) { + $tax_query[] = + [ + 'taxonomy' => 'opalestate_city', + 'field' => 'slug', + 'terms' => sanitize_text_field( $request['city'] ), + ]; + } + + if ( isset( $request['types'] ) && $request['types'] != -1 ) { + $tax_query[] = + [ + 'taxonomy' => 'opalestate_types', + 'field' => 'slug', + 'terms' => sanitize_text_field( $request['types'] ), + ]; + } + + if ( isset( $request['cat'] ) && $request['cat'] != -1 ) { + $tax_query[] = + [ + 'taxonomy' => 'property_category', + 'field' => 'slug', + 'terms' => sanitize_text_field( $request['cat'] ), + ]; + } + + if ( isset( $request['status'] ) && $request['status'] != -1 ) { + $tax_query[] = + [ + 'taxonomy' => 'opalestate_status', + 'field' => 'slug', + 'terms' => sanitize_text_field( $request['status'] ), + ]; + } + + if ( isset( $request['amenities'] ) && is_array( $request['amenities'] ) ) { + $tax_query[] = + [ + 'taxonomy' => 'opalestate_amenities', + 'field' => 'slug', + 'terms' => sanitize_text_field( $request['amenities'] ), + ]; + } + + if ( $tax_query ) { + $args['tax_query'] = [ 'relation' => 'AND' ]; + $args['tax_query'] = array_merge( $args['tax_query'], $tax_query ); + } + + $args['meta_query'] = [ 'relation' => 'AND' ]; + if ( isset( $request['info'] ) && is_array( $request['info'] ) ) { + $metaquery = []; + foreach ( $request['info'] as $key => $value ) { + if ( trim( $value ) ) { + if ( is_numeric( trim( $value ) ) ) { + $fieldquery = [ + 'key' => OPALESTATE_PROPERTY_PREFIX . $key, + 'value' => sanitize_text_field( trim( $value ) ), + 'compare' => '>=', + 'type' => 'NUMERIC', + ]; + } else { + $fieldquery = [ + 'key' => OPALESTATE_PROPERTY_PREFIX . $key, + 'value' => sanitize_text_field( trim( $value ) ), + 'compare' => 'LIKE', + ]; + } + $sarg = apply_filters( 'opalestate_search_field_query_' . $key, $fieldquery ); + $metaquery[] = $sarg; + } + } + $args['meta_query'] = array_merge( $args['meta_query'], $metaquery ); + } + + if ( $search_min_price != '' && $search_min_price != '' && is_numeric( $search_min_price ) && is_numeric( $search_max_price ) ) { + if ( $search_min_price ) { + array_push( $args['meta_query'], [ + 'key' => OPALESTATE_PROPERTY_PREFIX . 'price', + 'value' => [ $search_min_price, $search_max_price ], + 'compare' => 'BETWEEN', + 'type' => 'NUMERIC', + ] ); + } else { + array_push( $args['meta_query'], [ + [ + [ + 'key' => OPALESTATE_PROPERTY_PREFIX . 'price', + 'compare' => 'NOT EXISTS', + ], + 'relation' => 'OR', + [ + 'key' => OPALESTATE_PROPERTY_PREFIX . 'price', + 'value' => $search_max_price, + 'compare' => '<=', + 'type' => 'NUMERIC', + ], + ], + ] ); + } + + } elseif ( $search_min_price != '' && is_numeric( $search_min_price ) ) { + array_push( $args['meta_query'], [ + 'key' => OPALESTATE_PROPERTY_PREFIX . 'price', + 'value' => $search_min_price, + 'compare' => '>=', + 'type' => 'NUMERIC', + ] ); + } elseif ( $search_max_price != '' && is_numeric( $search_max_price ) ) { + array_push( $args['meta_query'], [ + 'key' => OPALESTATE_PROPERTY_PREFIX . 'price', + 'value' => $search_max_price, + 'compare' => '<=', + 'type' => 'NUMERIC', + ] ); + } + + if ( $search_min_area != '' && $search_min_area != '' && is_numeric( $search_min_area ) && is_numeric( $search_max_area ) ) { + array_push( $args['meta_query'], [ + 'key' => OPALESTATE_PROPERTY_PREFIX . 'areasize', + 'value' => [ $search_min_area, $search_max_area ], + 'compare' => 'BETWEEN', + 'type' => 'NUMERIC', + ] ); + } elseif ( $search_min_area != '' && is_numeric( $search_min_area ) ) { + array_push( $args['meta_query'], [ + 'key' => OPALESTATE_PROPERTY_PREFIX . 'areasize', + 'value' => $search_min_area, + 'compare' => '>=', + 'type' => 'NUMERIC', + ] ); + } elseif ( $search_max_area != '' && is_numeric( $search_max_area ) ) { + array_push( $args['meta_query'], [ + 'key' => OPALESTATE_PROPERTY_PREFIX . 'areasize', + 'value' => $search_max_area, + 'compare' => '<=', + 'type' => 'NUMERIC', + ] ); + } + + if ( isset( $request['geo_long'] ) && isset( $request['geo_lat'] ) ) { + if ( $request['location_text'] && ( empty( $request['geo_long'] ) || empty( $request['geo_lat'] ) ) ) { + array_push( $args['meta_query'], [ + 'key' => OPALESTATE_PROPERTY_PREFIX . 'map_address', + 'value' => sanitize_text_field( trim( $request['location_text'] ) ), + 'compare' => 'LIKE', + 'operator' => 'OR', + ] ); + } elseif ( $request['geo_lat'] && $request['geo_long'] ) { + $radius = isset( $request['geo_radius'] ) ? $request['geo_radius'] : 5; + $post_ids = Opalestate_Query::filter_by_location( $request['geo_lat'], $request['geo_long'], $radius ); + $args['post__in'] = $post_ids; + } + } + + $ksearchs = []; + + if ( isset( $request['opalsortable'] ) && ! empty( $request['opalsortable'] ) ) { + $ksearchs = explode( '_', $request['opalsortable'] ); + } + + if ( ! empty( $ksearchs ) && count( $ksearchs ) == 2 ) { + $args['meta_key'] = OPALESTATE_PROPERTY_PREFIX . $ksearchs[0]; + $args['orderby'] = 'meta_value_num'; + $args['order'] = $ksearchs[1]; + } + + $args = apply_filters( 'opalestate_api_get_search_results_query_args', $args ); + + return get_posts( $args ); + } + /** * Get the query params for collections of attachments. * @@ -245,4 +481,143 @@ class Opalestate_Property_Api extends Opalestate_Base_API { return $params; } + + /** + * Get the query params for collections of attachments. + * + * @return array + */ + public function get_search_params() { + $params = parent::get_collection_params(); + + $params['min_price'] = [ + 'description' => __( 'Min price', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['max_price'] = [ + 'description' => __( 'Min price', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['min_area'] = [ + 'description' => __( 'Min area', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['max_area'] = [ + 'description' => __( 'Max area', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['search_text'] = [ + 'description' => __( 'Search text', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['location_text'] = [ + 'description' => __( 'Location text', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['geo_long'] = [ + 'description' => __( 'Geo long', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['geo_lat'] = [ + 'description' => __( 'Geo lat', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['location'] = [ + 'description' => __( 'Location', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['state'] = [ + 'description' => __( 'State', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['city'] = [ + 'description' => __( 'City', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['types'] = [ + 'description' => __( 'Types', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['status'] = [ + 'description' => __( 'Status', 'opalestate-pro' ), + 'type' => 'string', + // 'default' => '', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['amenities'] = [ + 'description' => __( 'Amenities', 'opalestate-pro' ), + 'type' => 'array', + // 'default' => '', + // 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['amenities'] = [ + 'description' => __( 'Amenities', 'opalestate-pro' ), + 'type' => 'array', + // 'default' => '', + // 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + $params['info'] = [ + 'description' => __( 'Info', 'opalestate-pro' ), + 'type' => 'array', + // 'default' => '', + // 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + ]; + + return $params; + } } diff --git a/inc/api/v1/search-form.php b/inc/api/v1/search-form.php new file mode 100644 index 00000000..18150cbc --- /dev/null +++ b/inc/api/v1/search-form.php @@ -0,0 +1,69 @@ +namespace, + '/' . $this->base, + [ + [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'get_fields' ], + // 'permission_callback' => [ $this, 'get_items_permissions_check' ], + // 'args' => $this->get_search_params(), + ], + ] + ); + } + + /** + * Get List Of Properties + * + * Based on request to get collection + * + * @return WP_REST_Response is json data + * @since 1.0 + * + */ + public function get_fields( $request ) { + $response = []; + + $fields = []; + + $fields['types'] = Opalestate_Taxonomy_Type::get_list(); + $fields['status'] = Opalestate_Taxonomy_Status::get_list(); + $fields['cat'] = Opalestate_Taxonomy_Categories::get_list(); + $fields['amenities'] = Opalestate_Taxonomy_Amenities::get_list(); + $response['fields'] = $fields; + + return $this->get_response( 200, $response ); + } +} diff --git a/inc/property/class-opalestate-search.php b/inc/property/class-opalestate-search.php index f4e1ae24..bbc4fd25 100755 --- a/inc/property/class-opalestate-search.php +++ b/inc/property/class-opalestate-search.php @@ -104,6 +104,15 @@ class OpalEstate_Search { ]; } + if ( isset( $_GET['cat'] ) && $_GET['cat'] != -1 ) { + $tax_query[] = + [ + 'taxonomy' => 'property_category', + 'field' => 'slug', + 'terms' => sanitize_text_field( $_GET['cat'] ), + ]; + } + if ( isset( $_GET['status'] ) && $_GET['status'] != -1 ) { $tax_query[] = [ diff --git a/inc/taxonomies/class-taxomony-amenities.php b/inc/taxonomies/class-taxomony-amenities.php index 394e123a..10169d62 100755 --- a/inc/taxonomies/class-taxomony-amenities.php +++ b/inc/taxonomies/class-taxomony-amenities.php @@ -17,6 +17,11 @@ if ( ! defined( 'ABSPATH' ) ) { class Opalestate_Taxonomy_Amenities { + /** + * Constant. + */ + const OPALESTATE_AMENITY = 'opalestate_amenities'; + /** * Opalestate_Taxonomy_Amenities constructor. */ @@ -26,7 +31,7 @@ class Opalestate_Taxonomy_Amenities { } /** - * + * Definition. */ public function definition() { @@ -44,7 +49,7 @@ class Opalestate_Taxonomy_Amenities { 'menu_name' => esc_html__( 'Amenities', 'opalestate-pro' ), ]; - register_taxonomy( 'opalestate_amenities', 'opalestate_property', [ + register_taxonomy( static::OPALESTATE_AMENITY, 'opalestate_property', [ 'labels' => apply_filters( 'opalestate_taxomony_amenities_labels', $labels ), 'hierarchical' => true, 'query_var' => 'amenity', @@ -54,8 +59,23 @@ class Opalestate_Taxonomy_Amenities { ] ); } - public static function get_list() { - return get_terms( 'opalestate_amenities', [ 'hide_empty' => false ] ); + /** + * Gets list. + * + * @param array $args + * @return array|int|\WP_Error + */ + public static function get_list( $args = [] ) { + $default = apply_filters( 'opalestate_amenity_args', [ + 'taxonomy' => static::OPALESTATE_AMENITY, + 'hide_empty' => false, + ] ); + + if ( $args ) { + $default = array_merge( $default, $args ); + } + + return get_terms( $default ); } public function taxonomy_metaboxes() { @@ -65,7 +85,7 @@ class Opalestate_Taxonomy_Amenities { 'id' => $prefix . 'edit', 'title' => esc_html__( 'Type Metabox', 'opalestate-pro' ), 'object_types' => [ 'term' ], - 'taxonomies' => [ 'opalestate_amenities' ], + 'taxonomies' => [ static::OPALESTATE_AMENITY ], ] ); $cmb_term->add_field( [ diff --git a/inc/taxonomies/class-taxonomy-categories.php b/inc/taxonomies/class-taxonomy-categories.php index 376dd6c9..2a69f9be 100755 --- a/inc/taxonomies/class-taxonomy-categories.php +++ b/inc/taxonomies/class-taxonomy-categories.php @@ -17,9 +17,13 @@ if ( ! defined( 'ABSPATH' ) ) { } class Opalestate_Taxonomy_Categories { + /** + * Constant. + */ + const OPALESTATE_CATEGORY = 'property_category '; /** - * + * Init */ public static function init() { @@ -40,7 +44,7 @@ class Opalestate_Taxonomy_Categories { */ public static function definition() { - register_taxonomy( 'property_category', 'opalestate_property', apply_filters( 'opalestate_taxonomy_args_property_category', [ + register_taxonomy( static::OPALESTATE_CATEGORY, 'opalestate_property', apply_filters( 'opalestate_taxonomy_args_property_category', [ 'labels' => [ 'name' => esc_html__( 'Categories', 'opalestate-pro' ), 'add_new_item' => esc_html__( 'Add New Category', 'opalestate-pro' ), @@ -61,15 +65,15 @@ class Opalestate_Taxonomy_Categories { public static function taxonomy_metaboxes() { $prefix = 'opalestate_category_'; + /** * Metabox to add fields to categories and tags */ $cmb_term = new_cmb2_box( [ 'id' => $prefix . 'edit', - 'title' => esc_html__( 'Category Metabox', 'opalestate-pro' ), // Doesn't output for term boxes - 'object_types' => [ 'term' ], // Tells CMB2 to use term_meta vs post_meta - 'taxonomies' => [ 'property_category' ], // Tells CMB2 which taxonomies should have these fields - // 'new_term_section' => true, // Will display in the "Add New Category" section + 'title' => esc_html__( 'Category Metabox', 'opalestate-pro' ), + 'object_types' => [ 'term' ], + 'taxonomies' => [ static::OPALESTATE_CATEGORY ], ] ); $cmb_term->add_field( [ @@ -80,9 +84,15 @@ class Opalestate_Taxonomy_Categories { ] ); } + /** + * Gets list. + * + * @param array $args + * @return array|int|\WP_Error + */ public static function get_list( $args = [] ) { $default = [ - 'taxonomy' => 'property_category', + 'taxonomy' => static::OPALESTATE_CATEGORY, 'hide_empty' => true, ]; @@ -95,7 +105,7 @@ class Opalestate_Taxonomy_Categories { public static function dropdown_list( $selected = 0 ) { - $id = 'property_category' . rand(); + $id = static::OPALESTATE_CATEGORY . rand(); $args = [ 'show_option_none' => esc_html__( 'Select Category', 'opalestate-pro' ), @@ -103,10 +113,10 @@ class Opalestate_Taxonomy_Categories { 'class' => 'form-control', 'show_count' => 0, 'hierarchical' => '', - 'name' => 'types', + 'name' => 'cat', 'selected' => $selected, 'value_field' => 'slug', - 'taxonomy' => 'property_category', + 'taxonomy' => static::OPALESTATE_CATEGORY, 'echo' => 0, ]; @@ -115,10 +125,10 @@ class Opalestate_Taxonomy_Categories { echo $label . wp_dropdown_categories( $args ); } - public static function get_multi_check_list( $scategory ) { + public static function get_multi_check_list( $scategory = '' ) { $list = self::get_list(); - echo opalestate_terms_multi_check( $list, $scategory ); + echo opalestate_categories_multi_check( $list ); } } diff --git a/inc/taxonomies/class-taxonomy-status.php b/inc/taxonomies/class-taxonomy-status.php index 78feedc7..fab5f875 100755 --- a/inc/taxonomies/class-taxonomy-status.php +++ b/inc/taxonomies/class-taxonomy-status.php @@ -18,6 +18,11 @@ if ( ! defined( 'ABSPATH' ) ) { class Opalestate_Taxonomy_Status { + /** + * Constant. + */ + const OPALESTATE_STATUS = 'opalestate_status'; + /** * Opalestate_Taxonomy_Status constructor. */ @@ -28,7 +33,7 @@ class Opalestate_Taxonomy_Status { } /** - * + * Definition. */ public function definition() { $labels = [ @@ -44,7 +49,7 @@ class Opalestate_Taxonomy_Status { 'new_item_name' => esc_html__( 'New Status', 'opalestate-pro' ), 'menu_name' => esc_html__( 'Status', 'opalestate-pro' ), ]; - register_taxonomy( 'opalestate_status', 'opalestate_property', [ + register_taxonomy( static::OPALESTATE_STATUS , 'opalestate_property', [ 'labels' => apply_filters( 'opalestate_status_labels', $labels ), 'hierarchical' => true, 'query_var' => 'status', @@ -66,23 +71,25 @@ class Opalestate_Taxonomy_Status { */ $cmb_term = new_cmb2_box( [ 'id' => $prefix . 'edit', - 'title' => esc_html__( 'Category Metabox', 'opalestate-pro' ), // Doesn't output for term boxes - 'object_types' => [ 'term' ], // Tells CMB2 to use term_meta vs post_meta - 'taxonomies' => [ 'opalestate_status' ], // Tells CMB2 which taxonomies should have these fields - // 'new_term_section' => true, // Will display in the "Add New Category" section + 'title' => esc_html__( 'Category Metabox', 'opalestate-pro' ), + 'object_types' => [ 'term' ], + 'taxonomies' => [ static::OPALESTATE_STATUS ], ] ); + $cmb_term->add_field( [ 'name' => esc_html__( 'Background', 'opalestate-pro' ), 'desc' => esc_html__( 'Set background of label', 'opalestate-pro' ), 'id' => $prefix . 'lb_bg', 'type' => 'colorpicker', ] ); + $cmb_term->add_field( [ 'name' => esc_html__( 'Color', 'opalestate-pro' ), 'desc' => esc_html__( 'Set background of text', 'opalestate-pro' ), 'id' => $prefix . 'lb_color', 'type' => 'colorpicker', ] ); + $cmb_term->add_field( [ 'name' => esc_html__( 'Order', 'opalestate-pro' ), 'desc' => esc_html__( 'Set a priority to display', 'opalestate-pro' ), @@ -107,7 +114,7 @@ class Opalestate_Taxonomy_Status { */ public static function get_list( $args = [] ) { $default = apply_filters( 'opalestate_status_args', [ - 'taxonomy' => 'opalestate_status', + 'taxonomy' => static::OPALESTATE_STATUS , 'hide_empty' => false, 'hierarchical' => false, 'parent' => 0, @@ -135,7 +142,7 @@ class Opalestate_Taxonomy_Status { * @return string */ public static function dropdown_list( $selected = 0 ) { - $id = 'palestate_status' . rand(); + $id = static::OPALESTATE_STATUS . rand(); $args = [ 'show_option_none' => esc_html__( 'Select Status', 'opalestate-pro' ), @@ -146,7 +153,7 @@ class Opalestate_Taxonomy_Status { 'name' => 'status', 'value_field' => 'slug', 'selected' => $selected, - 'taxonomy' => 'opalestate_status', + 'taxonomy' => static::OPALESTATE_STATUS , 'echo' => 0, ]; diff --git a/inc/taxonomies/class-taxonomy-types.php b/inc/taxonomies/class-taxonomy-types.php index 7bb8c90d..95025f3b 100755 --- a/inc/taxonomies/class-taxonomy-types.php +++ b/inc/taxonomies/class-taxonomy-types.php @@ -17,9 +17,13 @@ if ( ! defined( 'ABSPATH' ) ) { } class Opalestate_Taxonomy_Type { + /** + * Constant. + */ + const OPALESTATE_TYPES = 'opalestate_types'; /** - * + * Opalestate_Taxonomy_Type constructor. */ public function __construct() { add_action( 'init', [ $this, 'definition' ] ); @@ -30,17 +34,16 @@ class Opalestate_Taxonomy_Type { * Hook in and add a metabox to add fields to taxonomy terms */ public function taxonomy_metaboxes() { - $prefix = 'opalestate_type_'; + /** * Metabox to add fields to categories and tags */ $cmb_term = new_cmb2_box( [ 'id' => $prefix . 'edit', - 'title' => esc_html__( 'Type Metabox', 'opalestate-pro' ), // Doesn't output for term boxes - 'object_types' => [ 'term' ], // Tells CMB2 to use term_meta vs post_meta - 'taxonomies' => [ 'opalestate_types' ], // Tells CMB2 which taxonomies should have these fields - // 'new_term_section' => true, // Will display in the "Add New Category" section + 'title' => esc_html__( 'Type Metabox', 'opalestate-pro' ), + 'object_types' => [ 'term' ], + 'taxonomies' => [ static::OPALESTATE_TYPES ], ] ); $cmb_term->add_field( [ @@ -67,10 +70,9 @@ class Opalestate_Taxonomy_Type { } /** - * + * Definition. */ public function definition() { - $labels = [ 'name' => esc_html__( 'Types', 'opalestate-pro' ), 'singular_name' => esc_html__( 'Properties By Type', 'opalestate-pro' ), @@ -85,7 +87,7 @@ class Opalestate_Taxonomy_Type { 'menu_name' => esc_html__( 'Types', 'opalestate-pro' ), ]; - register_taxonomy( 'opalestate_types', [ 'opalestate_property' ], [ + register_taxonomy( static::OPALESTATE_TYPES, [ 'opalestate_property' ], [ 'labels' => apply_filters( 'opalestate_taxomony_types_labels', $labels ), 'hierarchical' => true, 'query_var' => 'type', @@ -99,13 +101,27 @@ class Opalestate_Taxonomy_Type { } - public static function get_list() { - return get_terms( 'opalestate_types', [ 'hide_empty' => false ] ); + /** + * Gets list. + * + * @param array $args + * @return array|int|\WP_Error + */ + public static function get_list( $args = [] ) { + $default = apply_filters( 'opalestate_types_args', [ + 'taxonomy' => static::OPALESTATE_TYPES, + 'hide_empty' => false, + ] ); + + if ( $args ) { + $default = array_merge( $default, $args ); + } + + return get_terms( $default ); } public static function dropdown_list( $selected = 0 ) { - - $id = 'opalestate_types' . rand(); + $id = static::OPALESTATE_TYPES . rand(); $args = [ 'show_option_none' => esc_html__( 'Select Type', 'opalestate-pro' ), @@ -116,7 +132,7 @@ class Opalestate_Taxonomy_Type { 'name' => 'types', 'selected' => $selected, 'value_field' => 'slug', - 'taxonomy' => 'opalestate_types', + 'taxonomy' => static::OPALESTATE_TYPES, 'echo' => 0, ]; @@ -129,7 +145,6 @@ class Opalestate_Taxonomy_Type { $list = self::get_list(); echo opalestate_terms_multi_check( $list, $stypes ); - } } diff --git a/inc/template-functions.php b/inc/template-functions.php index 2f62f47b..0fe58b8a 100755 --- a/inc/template-functions.php +++ b/inc/template-functions.php @@ -100,13 +100,27 @@ function opalestate_terms_multi_check( $terms ) { $html = '
'; foreach ( $terms as $term ) { - $id = time() . '-' . $term->slug; $html .= '
'; $html .= ''; $html .= ' '; $html .= '
'; + } + $html .= '
'; + + return $html; +} + +function opalestate_categories_multi_check( $terms ) { + $html = '
'; + + foreach ( $terms as $term ) { + $id = time() . '-' . $term->slug; + $html .= '
'; + $html .= ''; + $html .= ' '; + $html .= '
'; } $html .= '
'; diff --git a/templates/search-box/fields/categories.php b/templates/search-box/fields/categories.php index b499afcb..0cf0ca75 100755 --- a/templates/search-box/fields/categories.php +++ b/templates/search-box/fields/categories.php @@ -4,5 +4,5 @@ $scategories = isset( $_GET['cat'] ) ? $_GET['cat'] : -1; if ( isset( $ismultiple ) ) { Opalestate_Taxonomy_Categories::get_multi_check_list( $scategories ); } else { - Opalestate_Taxonomy_Categories::dropdown_list(); + Opalestate_Taxonomy_Categories::dropdown_list( $stypes ); } diff --git a/templates/search-box/fields/status.php b/templates/search-box/fields/status.php index fb64c6ee..6f5e8dda 100755 --- a/templates/search-box/fields/status.php +++ b/templates/search-box/fields/status.php @@ -1,4 +1,3 @@ diff --git a/templates/search-box/fields/types.php b/templates/search-box/fields/types.php index 11afac9a..0496b36f 100755 --- a/templates/search-box/fields/types.php +++ b/templates/search-box/fields/types.php @@ -4,5 +4,5 @@ $stypes = isset( $_GET['types'] ) ? $_GET['types'] : -1; if ( isset( $ismultiple ) ) { Opalestate_Taxonomy_Type::get_multi_check_list( $stypes ); } else { - Opalestate_Taxonomy_Type::dropdown_list(); + Opalestate_Taxonomy_Type::dropdown_list( $stypes ); }