Update form && API

This commit is contained in:
Hoang Huu 2019-10-09 17:23:19 +07:00
parent 7b67843a52
commit a7f217d7a4
12 changed files with 576 additions and 56 deletions

View File

@ -45,6 +45,7 @@ class Opalestate_API {
'v1/property.php', 'v1/property.php',
'v1/agent.php', 'v1/agent.php',
'v1/agency.php', 'v1/agency.php',
'v1/search-form.php',
'class-opalestate-api-auth.php', 'class-opalestate-api-auth.php',
'functions.php', 'functions.php',
] ); ] );
@ -92,6 +93,7 @@ class Opalestate_API {
'Opalestate_Property_Api', 'Opalestate_Property_Api',
'Opalestate_Agent_Api', 'Opalestate_Agent_Api',
'Opalestate_Agency_Api', 'Opalestate_Agency_Api',
'Opalestate_Search_Form_Api',
] ]
); );

View File

@ -97,18 +97,13 @@ class Opalestate_Property_Api extends Opalestate_Base_API {
register_rest_route( register_rest_route(
$this->namespace, $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, 'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_results' ], '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 ); $properties[ $i ] = $this->get_property_data( $property_info );
$i++; $i++;
} }
} else {
return $this->get_response( 404, [ 'collection' => esc_html__( 'Not found', 'opalestate-pro' ) ] );
} }
$response['collection'] = $properties; $response['collection'] = $properties;
@ -179,8 +176,8 @@ class Opalestate_Property_Api extends Opalestate_Base_API {
} }
public function delete_item( $request ) { public function delete_item( $request ) {
$id = (int) $request['id']; $id = (int) $request['id'];
$force = (bool) $request['force']; $force = (bool) $request['force'];
$property = get_post( absint( $request['id'] ) ); $property = get_post( absint( $request['id'] ) );
if ( ! $property || $this->post_type != $property->post_type ) { if ( ! $property || $this->post_type != $property->post_type ) {
@ -193,6 +190,25 @@ class Opalestate_Property_Api extends Opalestate_Base_API {
return $response; 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 * 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 ); $post_id = wp_insert_post( $data, true );
$response['id'] = $post_id; $response['id'] = $post_id;
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );
$response->set_status( 201 ); $response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->base, $post_id ) ) ); $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->base, $post_id ) ) );
return $response; 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. * Get the query params for collections of attachments.
* *
@ -245,4 +481,143 @@ class Opalestate_Property_Api extends Opalestate_Base_API {
return $params; 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;
}
} }

View File

@ -0,0 +1,69 @@
<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Property_Api
*
* @since 1.0.0
* @package Opalestate_Search_Form_Api
*/
class Opalestate_Search_Form_Api extends Opalestate_Base_API {
/**
* The unique identifier of the route resource.
*
* @since 1.0.0
* @access public
* @var string $base .
*/
public $base = '/search-form';
/**
* Register Routes
*
* Register all CURD actions with POST/GET/PUT and calling function for each
*
* @since 1.0
*
*/
public function register_routes() {
register_rest_route(
$this->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 );
}
}

View File

@ -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 ) { if ( isset( $_GET['status'] ) && $_GET['status'] != -1 ) {
$tax_query[] = $tax_query[] =
[ [

View File

@ -17,6 +17,11 @@ if ( ! defined( 'ABSPATH' ) ) {
class Opalestate_Taxonomy_Amenities { class Opalestate_Taxonomy_Amenities {
/**
* Constant.
*/
const OPALESTATE_AMENITY = 'opalestate_amenities';
/** /**
* Opalestate_Taxonomy_Amenities constructor. * Opalestate_Taxonomy_Amenities constructor.
*/ */
@ -26,7 +31,7 @@ class Opalestate_Taxonomy_Amenities {
} }
/** /**
* * Definition.
*/ */
public function definition() { public function definition() {
@ -44,7 +49,7 @@ class Opalestate_Taxonomy_Amenities {
'menu_name' => esc_html__( 'Amenities', 'opalestate-pro' ), '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 ), 'labels' => apply_filters( 'opalestate_taxomony_amenities_labels', $labels ),
'hierarchical' => true, 'hierarchical' => true,
'query_var' => 'amenity', '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() { public function taxonomy_metaboxes() {
@ -65,7 +85,7 @@ class Opalestate_Taxonomy_Amenities {
'id' => $prefix . 'edit', 'id' => $prefix . 'edit',
'title' => esc_html__( 'Type Metabox', 'opalestate-pro' ), 'title' => esc_html__( 'Type Metabox', 'opalestate-pro' ),
'object_types' => [ 'term' ], 'object_types' => [ 'term' ],
'taxonomies' => [ 'opalestate_amenities' ], 'taxonomies' => [ static::OPALESTATE_AMENITY ],
] ); ] );
$cmb_term->add_field( [ $cmb_term->add_field( [

View File

@ -17,9 +17,13 @@ if ( ! defined( 'ABSPATH' ) ) {
} }
class Opalestate_Taxonomy_Categories { class Opalestate_Taxonomy_Categories {
/**
* Constant.
*/
const OPALESTATE_CATEGORY = 'property_category ';
/** /**
* * Init
*/ */
public static function init() { public static function init() {
@ -40,7 +44,7 @@ class Opalestate_Taxonomy_Categories {
*/ */
public static function definition() { 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' => [ 'labels' => [
'name' => esc_html__( 'Categories', 'opalestate-pro' ), 'name' => esc_html__( 'Categories', 'opalestate-pro' ),
'add_new_item' => esc_html__( 'Add New Category', '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() { public static function taxonomy_metaboxes() {
$prefix = 'opalestate_category_'; $prefix = 'opalestate_category_';
/** /**
* Metabox to add fields to categories and tags * Metabox to add fields to categories and tags
*/ */
$cmb_term = new_cmb2_box( [ $cmb_term = new_cmb2_box( [
'id' => $prefix . 'edit', 'id' => $prefix . 'edit',
'title' => esc_html__( 'Category Metabox', 'opalestate-pro' ), // Doesn't output for term boxes 'title' => esc_html__( 'Category Metabox', 'opalestate-pro' ),
'object_types' => [ 'term' ], // Tells CMB2 to use term_meta vs post_meta 'object_types' => [ 'term' ],
'taxonomies' => [ 'property_category' ], // Tells CMB2 which taxonomies should have these fields 'taxonomies' => [ static::OPALESTATE_CATEGORY ],
// 'new_term_section' => true, // Will display in the "Add New Category" section
] ); ] );
$cmb_term->add_field( [ $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 = [] ) { public static function get_list( $args = [] ) {
$default = [ $default = [
'taxonomy' => 'property_category', 'taxonomy' => static::OPALESTATE_CATEGORY,
'hide_empty' => true, 'hide_empty' => true,
]; ];
@ -95,7 +105,7 @@ class Opalestate_Taxonomy_Categories {
public static function dropdown_list( $selected = 0 ) { public static function dropdown_list( $selected = 0 ) {
$id = 'property_category' . rand(); $id = static::OPALESTATE_CATEGORY . rand();
$args = [ $args = [
'show_option_none' => esc_html__( 'Select Category', 'opalestate-pro' ), 'show_option_none' => esc_html__( 'Select Category', 'opalestate-pro' ),
@ -103,10 +113,10 @@ class Opalestate_Taxonomy_Categories {
'class' => 'form-control', 'class' => 'form-control',
'show_count' => 0, 'show_count' => 0,
'hierarchical' => '', 'hierarchical' => '',
'name' => 'types', 'name' => 'cat',
'selected' => $selected, 'selected' => $selected,
'value_field' => 'slug', 'value_field' => 'slug',
'taxonomy' => 'property_category', 'taxonomy' => static::OPALESTATE_CATEGORY,
'echo' => 0, 'echo' => 0,
]; ];
@ -115,10 +125,10 @@ class Opalestate_Taxonomy_Categories {
echo $label . wp_dropdown_categories( $args ); 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(); $list = self::get_list();
echo opalestate_terms_multi_check( $list, $scategory ); echo opalestate_categories_multi_check( $list );
} }
} }

View File

@ -18,6 +18,11 @@ if ( ! defined( 'ABSPATH' ) ) {
class Opalestate_Taxonomy_Status { class Opalestate_Taxonomy_Status {
/**
* Constant.
*/
const OPALESTATE_STATUS = 'opalestate_status';
/** /**
* Opalestate_Taxonomy_Status constructor. * Opalestate_Taxonomy_Status constructor.
*/ */
@ -28,7 +33,7 @@ class Opalestate_Taxonomy_Status {
} }
/** /**
* * Definition.
*/ */
public function definition() { public function definition() {
$labels = [ $labels = [
@ -44,7 +49,7 @@ class Opalestate_Taxonomy_Status {
'new_item_name' => esc_html__( 'New Status', 'opalestate-pro' ), 'new_item_name' => esc_html__( 'New Status', 'opalestate-pro' ),
'menu_name' => esc_html__( '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 ), 'labels' => apply_filters( 'opalestate_status_labels', $labels ),
'hierarchical' => true, 'hierarchical' => true,
'query_var' => 'status', 'query_var' => 'status',
@ -66,23 +71,25 @@ class Opalestate_Taxonomy_Status {
*/ */
$cmb_term = new_cmb2_box( [ $cmb_term = new_cmb2_box( [
'id' => $prefix . 'edit', 'id' => $prefix . 'edit',
'title' => esc_html__( 'Category Metabox', 'opalestate-pro' ), // Doesn't output for term boxes 'title' => esc_html__( 'Category Metabox', 'opalestate-pro' ),
'object_types' => [ 'term' ], // Tells CMB2 to use term_meta vs post_meta 'object_types' => [ 'term' ],
'taxonomies' => [ 'opalestate_status' ], // Tells CMB2 which taxonomies should have these fields 'taxonomies' => [ static::OPALESTATE_STATUS ],
// 'new_term_section' => true, // Will display in the "Add New Category" section
] ); ] );
$cmb_term->add_field( [ $cmb_term->add_field( [
'name' => esc_html__( 'Background', 'opalestate-pro' ), 'name' => esc_html__( 'Background', 'opalestate-pro' ),
'desc' => esc_html__( 'Set background of label', 'opalestate-pro' ), 'desc' => esc_html__( 'Set background of label', 'opalestate-pro' ),
'id' => $prefix . 'lb_bg', 'id' => $prefix . 'lb_bg',
'type' => 'colorpicker', 'type' => 'colorpicker',
] ); ] );
$cmb_term->add_field( [ $cmb_term->add_field( [
'name' => esc_html__( 'Color', 'opalestate-pro' ), 'name' => esc_html__( 'Color', 'opalestate-pro' ),
'desc' => esc_html__( 'Set background of text', 'opalestate-pro' ), 'desc' => esc_html__( 'Set background of text', 'opalestate-pro' ),
'id' => $prefix . 'lb_color', 'id' => $prefix . 'lb_color',
'type' => 'colorpicker', 'type' => 'colorpicker',
] ); ] );
$cmb_term->add_field( [ $cmb_term->add_field( [
'name' => esc_html__( 'Order', 'opalestate-pro' ), 'name' => esc_html__( 'Order', 'opalestate-pro' ),
'desc' => esc_html__( 'Set a priority to display', '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 = [] ) { public static function get_list( $args = [] ) {
$default = apply_filters( 'opalestate_status_args', [ $default = apply_filters( 'opalestate_status_args', [
'taxonomy' => 'opalestate_status', 'taxonomy' => static::OPALESTATE_STATUS ,
'hide_empty' => false, 'hide_empty' => false,
'hierarchical' => false, 'hierarchical' => false,
'parent' => 0, 'parent' => 0,
@ -135,7 +142,7 @@ class Opalestate_Taxonomy_Status {
* @return string * @return string
*/ */
public static function dropdown_list( $selected = 0 ) { public static function dropdown_list( $selected = 0 ) {
$id = 'palestate_status' . rand(); $id = static::OPALESTATE_STATUS . rand();
$args = [ $args = [
'show_option_none' => esc_html__( 'Select Status', 'opalestate-pro' ), 'show_option_none' => esc_html__( 'Select Status', 'opalestate-pro' ),
@ -146,7 +153,7 @@ class Opalestate_Taxonomy_Status {
'name' => 'status', 'name' => 'status',
'value_field' => 'slug', 'value_field' => 'slug',
'selected' => $selected, 'selected' => $selected,
'taxonomy' => 'opalestate_status', 'taxonomy' => static::OPALESTATE_STATUS ,
'echo' => 0, 'echo' => 0,
]; ];

View File

@ -17,9 +17,13 @@ if ( ! defined( 'ABSPATH' ) ) {
} }
class Opalestate_Taxonomy_Type { class Opalestate_Taxonomy_Type {
/**
* Constant.
*/
const OPALESTATE_TYPES = 'opalestate_types';
/** /**
* * Opalestate_Taxonomy_Type constructor.
*/ */
public function __construct() { public function __construct() {
add_action( 'init', [ $this, 'definition' ] ); 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 * Hook in and add a metabox to add fields to taxonomy terms
*/ */
public function taxonomy_metaboxes() { public function taxonomy_metaboxes() {
$prefix = 'opalestate_type_'; $prefix = 'opalestate_type_';
/** /**
* Metabox to add fields to categories and tags * Metabox to add fields to categories and tags
*/ */
$cmb_term = new_cmb2_box( [ $cmb_term = new_cmb2_box( [
'id' => $prefix . 'edit', 'id' => $prefix . 'edit',
'title' => esc_html__( 'Type Metabox', 'opalestate-pro' ), // Doesn't output for term boxes 'title' => esc_html__( 'Type Metabox', 'opalestate-pro' ),
'object_types' => [ 'term' ], // Tells CMB2 to use term_meta vs post_meta 'object_types' => [ 'term' ],
'taxonomies' => [ 'opalestate_types' ], // Tells CMB2 which taxonomies should have these fields 'taxonomies' => [ static::OPALESTATE_TYPES ],
// 'new_term_section' => true, // Will display in the "Add New Category" section
] ); ] );
$cmb_term->add_field( [ $cmb_term->add_field( [
@ -67,10 +70,9 @@ class Opalestate_Taxonomy_Type {
} }
/** /**
* * Definition.
*/ */
public function definition() { public function definition() {
$labels = [ $labels = [
'name' => esc_html__( 'Types', 'opalestate-pro' ), 'name' => esc_html__( 'Types', 'opalestate-pro' ),
'singular_name' => esc_html__( 'Properties By Type', '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' ), '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 ), 'labels' => apply_filters( 'opalestate_taxomony_types_labels', $labels ),
'hierarchical' => true, 'hierarchical' => true,
'query_var' => 'type', '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 ) { public static function dropdown_list( $selected = 0 ) {
$id = static::OPALESTATE_TYPES . rand();
$id = 'opalestate_types' . rand();
$args = [ $args = [
'show_option_none' => esc_html__( 'Select Type', 'opalestate-pro' ), 'show_option_none' => esc_html__( 'Select Type', 'opalestate-pro' ),
@ -116,7 +132,7 @@ class Opalestate_Taxonomy_Type {
'name' => 'types', 'name' => 'types',
'selected' => $selected, 'selected' => $selected,
'value_field' => 'slug', 'value_field' => 'slug',
'taxonomy' => 'opalestate_types', 'taxonomy' => static::OPALESTATE_TYPES,
'echo' => 0, 'echo' => 0,
]; ];
@ -129,7 +145,6 @@ class Opalestate_Taxonomy_Type {
$list = self::get_list(); $list = self::get_list();
echo opalestate_terms_multi_check( $list, $stypes ); echo opalestate_terms_multi_check( $list, $stypes );
} }
} }

View File

@ -100,13 +100,27 @@ function opalestate_terms_multi_check( $terms ) {
$html = '<div class="opal-form-group">'; $html = '<div class="opal-form-group">';
foreach ( $terms as $term ) { foreach ( $terms as $term ) {
$id = time() . '-' . $term->slug; $id = time() . '-' . $term->slug;
$html .= '<div class="group-item">'; $html .= '<div class="group-item">';
$html .= '<input type="checkbox" class="form-control-checkbox" id="' . $id . '" name="types[' . $term->slug . ']" id="' . $id . '" value="' . $term->slug . '">'; $html .= '<input type="checkbox" class="form-control-checkbox" id="' . $id . '" name="types[' . $term->slug . ']" id="' . $id . '" value="' . $term->slug . '">';
$html .= ' <label for="' . $id . '">' . $term->name . '</label>'; $html .= ' <label for="' . $id . '">' . $term->name . '</label>';
$html .= '</div>'; $html .= '</div>';
}
$html .= '</div>';
return $html;
}
function opalestate_categories_multi_check( $terms ) {
$html = '<div class="opal-form-group">';
foreach ( $terms as $term ) {
$id = time() . '-' . $term->slug;
$html .= '<div class="group-item">';
$html .= '<input type="checkbox" class="form-control-checkbox" id="' . $id . '" name="cat[' . $term->slug . ']" id="' . $id . '" value="' . $term->slug . '">';
$html .= ' <label for="' . $id . '">' . $term->name . '</label>';
$html .= '</div>';
} }
$html .= '</div>'; $html .= '</div>';

View File

@ -4,5 +4,5 @@ $scategories = isset( $_GET['cat'] ) ? $_GET['cat'] : -1;
if ( isset( $ismultiple ) ) { if ( isset( $ismultiple ) ) {
Opalestate_Taxonomy_Categories::get_multi_check_list( $scategories ); Opalestate_Taxonomy_Categories::get_multi_check_list( $scategories );
} else { } else {
Opalestate_Taxonomy_Categories::dropdown_list(); Opalestate_Taxonomy_Categories::dropdown_list( $stypes );
} }

View File

@ -1,4 +1,3 @@
<?php <?php
$status = isset( $_GET['status'] ) ? sanitize_text_field( $_GET['status'] ): -1; $status = isset( $_GET['status'] ) ? sanitize_text_field( $_GET['status'] ): -1;
Opalestate_Taxonomy_Status::dropdown_list( $status ); Opalestate_Taxonomy_Status::dropdown_list( $status );
?>

View File

@ -4,5 +4,5 @@ $stypes = isset( $_GET['types'] ) ? $_GET['types'] : -1;
if ( isset( $ismultiple ) ) { if ( isset( $ismultiple ) ) {
Opalestate_Taxonomy_Type::get_multi_check_list( $stypes ); Opalestate_Taxonomy_Type::get_multi_check_list( $stypes );
} else { } else {
Opalestate_Taxonomy_Type::dropdown_list(); Opalestate_Taxonomy_Type::dropdown_list( $stypes );
} }