Update API

This commit is contained in:
Hoang Huu
2019-10-04 13:26:00 +07:00
parent 51417949b3
commit 218c914e0d
9 changed files with 832 additions and 171 deletions

View File

@@ -20,7 +20,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @package Opal_Job
* @subpackage Opal_Job/controllers
*/
class Agency_Api extends Base_Api {
class Opalestate_Agency_Api extends Opalestate_Base_API {
/**
* The unique identifier of the route resource.
@@ -87,6 +87,24 @@ class Agency_Api extends Base_Api {
],
]
);
register_rest_route(
$this->namespace,
'/' . $this->base . '/listings/(?P<id>[\d]+)',
[
'args' => [
'id' => [
'description' => __( 'Unique identifier for the resource.', 'opalestate-pro' ),
'type' => 'integer',
],
],
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_listings' ],
// 'permission_callback' => [ $this, 'get_item_permissions_check' ],
],
]
);
}
@@ -164,27 +182,113 @@ class Agency_Api extends Base_Api {
*
*/
public function get_agency_data( $agency_info ) {
$ouput = [];
$ouput['info']['id'] = $agency_info->ID;
$ouput['info']['slug'] = $agency_info->post_name;
$ouput['info']['title'] = $agency_info->post_title;
$ouput['info']['create_date'] = $agency_info->post_date;
$ouput['info']['modified_date'] = $agency_info->post_modified;
$ouput['info']['status'] = $agency_info->post_status;
$ouput['info']['link'] = html_entity_decode( $agency_info->guid );
$ouput['info']['content'] = $agency_info->post_content;
$ouput['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agency_info->ID ) );
$agency = new OpalEstate_Agency( $agency_info->ID );
$ouput['info']['featured'] = (int) $agency->is_featured();
$ouput['info']['email'] = get_post_meta( $agency_info->ID, OPALESTATE_AGENCY_PREFIX . 'email', true );
$ouput['info']['address'] = get_post_meta( $agency_info->ID, OPALESTATE_AGENCY_PREFIX . 'address', true );
$terms = wp_get_post_terms( $agency_info->ID, 'opalestate_agency_location' );
$ouput['info']['location'] = $terms && ! is_wp_error( $terms ) ? $terms : [];
$ouput['socials'] = $agency->get_socials();
$agency = new OpalEstate_Agency( $agency_info->ID );
$ouput['id'] = $agency_info->ID;
$ouput['slug'] = $agency_info->post_name;
$ouput['name'] = $agency_info->post_title;
$ouput['create_date'] = $agency_info->post_date;
$ouput['modified_date'] = $agency_info->post_modified;
$ouput['status'] = $agency_info->post_status;
$ouput['link'] = html_entity_decode( $agency_info->guid );
$ouput['content'] = $agency_info->post_content;
$ouput['avatar'] = $agency->get_meta( 'avatar' );
$ouput['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agency_info->ID ) );
$ouput['featured'] = $agency->is_featured();
$ouput['trusted'] = $agency->get_trusted();
$ouput['email'] = $agency->get_meta( 'email' );
$ouput['address'] = $agency->get_meta( 'address' );
$ouput['map'] = $agency->get_meta( 'map' );
$terms = wp_get_post_terms( $agency_info->ID, 'opalestate_agency_location' );
$ouput['location'] = $terms && ! is_wp_error( $terms ) ? $terms : [];
$ouput['socials'] = $agency->get_socials();
return apply_filters( 'opalestate_api_agencies', $ouput );
}
/**
* Get agent listings.
*
* @param $request
* @return \WP_REST_Response
*/
public function get_listings( $request ) {
if ( $request['id'] > 0 ) {
$post = get_post( $request['id'] );
if ( $post && $this->post_type == get_post_type( $request['id'] ) ) {
$per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5;
$paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1;
$user_id = get_post_meta( $request['id'], OPALESTATE_AGENCY_PREFIX . 'user_id', true );
$agents = get_post_meta( $request['id'], OPALESTATE_AGENCY_PREFIX . 'team', true );
if ( $user_id ) {
$author = [ $user_id ];
$agents = get_post_meta( $request['id'], OPALESTATE_AGENCY_PREFIX . 'team', true );
if ( is_array( $agents ) ) {
$author = array_merge( $author, $agents );
}
$args = [
'post_type' => 'opalestate_property',
'author__in' => $author,
'posts_per_page' => $per_page,
'paged' => $paged,
];
} else {
$args = [
'post_type' => 'opalestate_property',
'posts_per_page' => $per_page,
'paged' => $paged,
];
$args['meta_query'] = [ 'relation' => 'OR' ];
array_push( $args['meta_query'], [
'key' => OPALESTATE_PROPERTY_PREFIX . 'agency',
'value' => $request['id'],
'compare' => '=',
] );
if ( $agents ) {
array_push( $args['meta_query'], [
'key' => OPALESTATE_PROPERTY_PREFIX . 'agent',
'value' => $agents,
] );
}
}
$property_list = get_posts( $args );
if ( $property_list ) {
$i = 0;
foreach ( $property_list as $property_info ) {
$properties[ $i ] = opalestate_api_get_property_data( $property_info );
$i++;
}
}
$response['listings'] = $properties ? $properties : [];
$code = 200;
} else {
$code = 404;
$response['error'] = sprintf( esc_html__( 'Agency ID: %s does not exist!', 'opalestate-pro' ), $request['id'] );
}
} else {
$code = 404;
$response['error'] = sprintf( esc_html__( 'Invalid ID.', 'opalestate-pro' ), $request['id'] );
}
return $this->get_response( $code, $response );
}
/**
* Get the query params for collections of attachments.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
return $params;
}
}

View File

@@ -11,7 +11,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @package Opal_Job
* @subpackage Opal_Job/controllers
*/
class Agent_Api extends Base_Api {
class Opalestate_Agent_Api extends Opalestate_Base_API {
/**
* The unique identifier of the route resource.
@@ -78,6 +78,24 @@ class Agent_Api extends Base_Api {
],
]
);
register_rest_route(
$this->namespace,
'/' . $this->base . '/listings/(?P<id>[\d]+)',
[
'args' => [
'id' => [
'description' => __( 'Unique identifier for the resource.', 'opalestate-pro' ),
'type' => 'integer',
],
],
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_listings' ],
// 'permission_callback' => [ $this, 'get_item_permissions_check' ],
],
]
);
}
/**
@@ -144,6 +162,64 @@ class Agent_Api extends Base_Api {
return $this->get_response( $code, $response );
}
/**
* Get agent listings.
*
* @param $request
* @return \WP_REST_Response
*/
public function get_listings( $request ) {
if ( $request['id'] > 0 ) {
$post = get_post( $request['id'] );
if ( $post && $this->post_type == get_post_type( $request['id'] ) ) {
$per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5;
$paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1;
$user_id = get_post_meta( $request['id'], OPALESTATE_AGENT_PREFIX . 'user_id', true );
$args = [
'post_type' => 'opalestate_property',
'posts_per_page' => $per_page,
'post__not_in' => [ $request['id'] ],
'paged' => $paged,
];
$args['meta_query'] = [ 'relation' => 'AND' ];
if ( $user_id ) {
$args['author'] = $user_id;
} else {
array_push( $args['meta_query'], [
'key' => OPALESTATE_PROPERTY_PREFIX . 'agent',
'value' => $request['id'],
'compare' => '=',
] );
}
$property_list = get_posts( $args );
if ( $property_list ) {
$i = 0;
foreach ( $property_list as $property_info ) {
$properties[ $i ] = opalestate_api_get_property_data( $property_info );
$i++;
}
}
$response['listings'] = $properties ? $properties : [];
$code = 200;
} else {
$code = 404;
$response['error'] = sprintf( esc_html__( 'Agent ID: %s does not exist!', 'opalestate-pro' ), $request['id'] );
}
} else {
$code = 404;
$response['error'] = sprintf( esc_html__( 'Invalid ID.', 'opalestate-pro' ), $request['id'] );
}
return $this->get_response( $code, $response );
}
/**
* The opalestate_agent post object, generate the data for the API output
*
@@ -154,31 +230,39 @@ class Agent_Api extends Base_Api {
*
*/
public function get_agent_data( $agent_info ) {
$ouput = [];
$ouput['info']['id'] = $agent_info->ID;
$ouput['info']['slug'] = $agent_info->post_name;
$ouput['info']['title'] = $agent_info->post_title;
$ouput['info']['create_date'] = $agent_info->post_date;
$ouput['info']['modified_date'] = $agent_info->post_modified;
$ouput['info']['status'] = $agent_info->post_status;
$ouput['info']['link'] = html_entity_decode( $agent_info->guid );
$ouput['info']['content'] = $agent_info->post_content;
$ouput['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agent_info->ID ) );
$agent = new OpalEstate_Agent( $agent_info->ID );
$ouput['id'] = $agent_info->ID;
$ouput['name'] = $agent_info->post_title;
$ouput['slug'] = $agent_info->post_name;
$ouput['created_date'] = $agent_info->post_date;
$ouput['modified_date'] = $agent_info->post_modified;
$ouput['status'] = $agent_info->post_status;
$ouput['permalink'] = html_entity_decode( $agent_info->guid );
$ouput['content'] = $agent_info->post_content;
$ouput['avatar'] = $agent->get_meta( 'avatar' );
$ouput['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agent_info->ID ) );
$ouput['featured'] = $agent->is_featured();
$ouput['trusted'] = $agent->get_trusted();
$ouput['email'] = $agent->get_meta( 'email' );
$ouput['address'] = $agent->get_meta( 'address' );
$ouput['map'] = $agent->get_meta( 'map' );
$agent = new OpalEstate_Agent( $agent_info->ID );
$ouput['info']['avatar'] = $agent->get_meta( 'avatar' );
$ouput['info']['featured'] = $agent->is_featured();
$ouput['info']['trusted'] = $agent->get_trusted();
$ouput['info']['email'] = $agent->get_meta( 'email' );
$ouput['info']['address'] = $agent->get_meta( 'address' );
$ouput['info']['map'] = $agent->get_meta( 'map' );
$terms = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_location' );
$ouput['info']['location'] = $terms && ! is_wp_error( $terms ) ? $terms : [];
$ouput['socials'] = $agent->get_socials();
$ouput['levels'] = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_level' );
$terms = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_location' );
$ouput['location'] = $terms && ! is_wp_error( $terms ) ? $terms : [];
$ouput['socials'] = $agent->get_socials();
$ouput['levels'] = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_level' );
return apply_filters( 'opalestate_api_agents', $ouput );
}
/**
* Get the query params for collections of attachments.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
return $params;
}
}

View File

@@ -10,7 +10,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @since 1.0.0
* @package Property_Api
*/
class Property_Api extends Base_Api {
class Opalestate_Property_Api extends Opalestate_Base_API {
/**
* The unique identifier of the route resource.
@@ -185,65 +185,7 @@ class Property_Api extends Base_Api {
*
*/
private function get_property_data( $property_info ) {
$property = [];
$property['info']['id'] = $property_info->ID;
$property['info']['slug'] = $property_info->post_name;
$property['info']['title'] = $property_info->post_title;
$property['info']['create_date'] = $property_info->post_date;
$property['info']['modified_date'] = $property_info->post_modified;
$property['info']['status'] = $property_info->post_status;
$property['info']['link'] = html_entity_decode( $property_info->guid );
$property['info']['content'] = $property_info->post_content;
$property['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $property_info->ID ) );
$data = opalesetate_property( $property_info->ID );
$gallery = $data->get_gallery();
$gallery_count = $data->get_gallery_count();
$gallery_data = [];
if ( $gallery_count ) {
foreach ( $gallery as $id => $url ) {
$gallery_data[] = [
'id' => $id,
'url' => $url,
];
}
}
$property['info']['gallery'] = $gallery_data;
$property['info']['price'] = opalestate_price_format( $data->get_price() );
$property['info']['saleprice'] = opalestate_price_format( $data->get_sale_price() );
$property['info']['before_price_label'] = $data->get_before_price_label();
$property['info']['price_label'] = $data->get_price_label();
$property['info']['map'] = $data->get_map();
$property['info']['address'] = $data->get_address();
$property['meta'] = $data->get_meta_shortinfo();
$property['fullinfo'] = $data->get_meta_fullinfo();
$property['video'] = $data->get_video_url();
$property['virtual_tour'] = $data->get_virtual_tour();
$property['attachments'] = $data->get_attachments();
$property['floor_plans'] = $data->get_floor_plans();
$property['is_featured'] = $data->is_featured();
$property['status'] = $data->get_status();
$property['labels'] = $data->get_labels();
$property['locations'] = $data->get_locations();
$property['facilities'] = $data->get_facilities();
$property['amenities'] = $data->get_amenities();
$property['types'] = $data->get_types_tax();
$property['author_type'] = $data->get_author_type();
$property['author_data'] = $data->get_author_link_data();
$limit = opalestate_get_option( 'single_views_statistics_limit', 8 );
$stats = new Opalestate_View_Stats( $data->get_id(), $limit );
$array_label = json_encode( $stats->get_traffic_labels() );
$array_values = json_encode( $stats->get_traffic_data_accordion() );
$property['view_stats'] = [
'labels' => $array_label,
'values' => $array_values,
];
return apply_filters( 'opalestate_api_properties_property', $property );
return opalestate_api_get_property_data( $property_info );
}
/**