From ca848a3844f70ba8f65871429429dcc0f5acccdb Mon Sep 17 00:00:00 2001 From: Hoang Huu Date: Wed, 2 Oct 2019 14:14:31 +0700 Subject: [PATCH] Update API --- inc/api/v1/agency.php | 224 ++++++++++++++++++++++++++------------ inc/api/v1/agent.php | 233 ++++++++++++++++++++++------------------ inc/api/v1/property.php | 81 +++++++++----- 3 files changed, 342 insertions(+), 196 deletions(-) diff --git a/inc/api/v1/agency.php b/inc/api/v1/agency.php index 04e7b140..271024d4 100755 --- a/inc/api/v1/agency.php +++ b/inc/api/v1/agency.php @@ -1,6 +1,6 @@ namespace, $this->base.'/list', array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_list' ), - 'permission_callback' => array( $this, 'validate_request' ), - )); - /// call http://domain.com/wp-json/job-api/v1/job/1 //// - register_rest_route( $this->namespace, $this->base.'/(?P\d+)', array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_job' ), - 'permission_callback' => array( $this, 'validate_request' ), - )); + public function register_routes() { + /// call http://domain.com/wp-json/estate-api/v1/job/list //// + register_rest_route( $this->namespace, $this->base . '/list', [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'get_list' ], + 'permission_callback' => [ $this, 'validate_request' ], + ] ); - /// call http://domain.com/wp-json/job-api/v1/job/create //// - register_rest_route( $this->namespace, $this->base.'/create', array( - 'methods' => 'GET', - 'callback' => array( $this, 'create' ), - 'permission_callback' => array( $this, 'validate_request' ), - )); - /// call http://domain.com/wp-json/job-api/v1/job/edit //// - register_rest_route( $this->namespace, $this->base.'/edit', array( - 'methods' => 'GET', - 'callback' => array( $this, 'edit' ), - )); - /// call http://domain.com/wp-json/job-api/v1/job/delete //// - register_rest_route( $this->namespace, $this->base.'/delete', array( - 'methods' => 'GET', - 'callback' => array( $this, 'delete' ), - 'permission_callback' => array( $this, 'validate_request' ), - )); + /// call http://domain.com/wp-json/estate-api/v1/job/1 //// + register_rest_route( $this->namespace, $this->base . '/(?P\d+)', [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'get_detail' ], + 'permission_callback' => [ $this, 'validate_request' ], + ] ); + + /// call http://domain.com/wp-json/estate-api/v1/job/create //// + register_rest_route( $this->namespace, $this->base . '/create', [ + 'methods' => 'GET', + 'callback' => [ $this, 'create' ], + 'permission_callback' => [ $this, 'validate_request' ], + ] ); + + /// call http://domain.com/wp-json/estate-api/v1/job/edit //// + register_rest_route( $this->namespace, $this->base . '/edit', [ + 'methods' => 'GET', + 'callback' => [ $this, 'edit' ], + ] ); + + /// call http://domain.com/wp-json/estate-api/v1/job/delete //// + register_rest_route( $this->namespace, $this->base . '/delete', [ + 'methods' => 'GET', + 'callback' => [ $this, 'delete' ], + 'permission_callback' => [ $this, 'validate_request' ], + ] ); /** - * List job by tags and taxonmies + * List job by tags and taxonmies */ - /// call http://domain.com/wp-json/job-api/v1/jobs //// - register_rest_route( $this->namespace, $this->base.'/tags', array( - 'methods' => 'GET', - 'callback' => array( $this, 'delete' ), - 'permission_callback' => array( $this, 'validate_request' ), - )); + /// call http://domain.com/wp-json/estate-api/v1/jobs //// + register_rest_route( $this->namespace, $this->base . '/tags', [ + 'methods' => 'GET', + 'callback' => [ $this, 'delete' ], + 'permission_callback' => [ $this, 'validate_request' ], + ] ); } - /** - * Get List Of Job + * Get List Of agencies. * * Based on request to get collection * + * @return WP_REST_Response is json data * @since 1.0 * - * @return WP_REST_Response is json data */ - public function get_list ( $request ) { + public function get_list( $request ) { + $agencies = []; + $error = []; + $agency = null; + if ( $agency == null ) { + $agencies['agencies'] = []; - $query = new Opalestate_Agency_Query( - array( + $agency_list = get_posts( [ 'post_type' => 'opalestate_agency', 'posts_per_page' => $this->per_page(), 'suppress_filters' => true, - 'paged' => $this->get_paged() - ) - ); + 'paged' => $this->get_paged(), + ] ); - $data = $query->get_api_list(); - $response['collection'] = $data['collection']; - $response['found'] = $data['found']; - $response['current'] = 1; + if ( $agency_list ) { + $i = 0; + foreach ( $agency_list as $agency_info ) { + $agencies['agencies'][ $i ] = $this->get_agency_data( $agency_info ); + $i++; + } + } + } else { + if ( get_post_type( $agency ) == 'opalestate_agency' ) { + $agency_info = get_post( $agency ); + + $agencies['agencies'][0] = $this->get_agency_data( $agency_info ); + + } else { + $error['error'] = sprintf( + /* translators: %s: agency */ + esc_html__( 'Form %s not found!', 'opalestate-pro' ), + $agency + ); + + return $this->get_response( 404, $error ); + } + } + + $response['collection'] = $agencies['agencies']; + $response['pages'] = 4; + $response['current'] = 1; return $this->get_response( 200, $response ); } - + + /** + * Get Agency + * + * Based on request to get a agency. + * + * @return WP_REST_Response is json data + * @since 1.0 + * + */ + public function get_detail( $request ) { + $response = []; + if ( $request['id'] > 0 ) { + $post = get_post( $request['id'] ); + if ( $post && 'opalestate_agency' == get_post_type( $request['id'] ) ) { + $agency = $this->get_agency_data( $post ); + $response['agency'] = $agency ? $agency : []; + $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 ); + } + + /** + * The opalestate_agency post object, generate the data for the API output + * + * @param object $agency_info The Download Post Object + * + * @return array Array of post data to return back in the API + * @since 1.0 + * + */ + 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(); + + return apply_filters( 'opalestate_api_agencies', $ouput ); + } + /** * Delete job * * Based on request to get collection * + * @return WP_REST_Response is json data * @since 1.0 * - * @return WP_REST_Response is json data */ - public function delete( ) { + public function delete() { } - public function reviews () { + public function reviews() { } - public function categories () { + public function categories() { } - - public function tags () { - + + public function tags() { + } -} \ No newline at end of file +} diff --git a/inc/api/v1/agent.php b/inc/api/v1/agent.php index ea4c99f9..384b5f34 100755 --- a/inc/api/v1/agent.php +++ b/inc/api/v1/agent.php @@ -1,157 +1,186 @@ - * @license https://opensource.org/licenses/gpl-license GNU Public License - * @since 1.0 - */ // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { exit; } + /** - * @class Job_Api + * @class Agent_Api * * @since 1.0.0 * @package Opal_Job * @subpackage Opal_Job/controllers */ -class Agent_Api extends Base_Api { +class Agent_Api extends Base_Api { /** * The unique identifier of the route resource. * * @since 1.0.0 * @access public - * @var string $base. + * @var string $base . */ public $base = '/agent'; - - /** + + /** * Register Routes * * Register all CURD actions with POST/GET/PUT and calling function for each * + * @return avoid * @since 1.0 * - * @return avoid */ - public function register_routes ( ) { - /// call http://domain.com/wp-json/job-api/v1/job/list //// - register_rest_route( $this->namespace, $this->base.'/list', array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_list' ), - 'permission_callback' => array( $this, 'validate_request' ), - )); - /// call http://domain.com/wp-json/job-api/v1/job/1 //// - register_rest_route( $this->namespace, $this->base.'/(?P\d+)', array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_job' ), - 'permission_callback' => array( $this, 'validate_request' ), - )); - - /// call http://domain.com/wp-json/job-api/v1/job/create //// - register_rest_route( $this->namespace, $this->base.'/create', array( - 'methods' => 'GET', - 'callback' => array( $this, 'create' ), - 'permission_callback' => array( $this, 'validate_request' ), - )); - /// call http://domain.com/wp-json/job-api/v1/job/edit //// - register_rest_route( $this->namespace, $this->base.'/edit', array( - 'methods' => 'GET', - 'callback' => array( $this, 'edit' ), - )); - /// call http://domain.com/wp-json/job-api/v1/job/delete //// - register_rest_route( $this->namespace, $this->base.'/delete', array( - 'methods' => 'GET', - 'callback' => array( $this, 'delete' ), - 'permission_callback' => array( $this, 'validate_request' ), - )); + public function register_routes() { + /** + * Get list of agents. + * + * Call http://domain.com/wp-json/estate-api/v1/agent/list + */ + register_rest_route( $this->namespace, $this->base . '/list', [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'get_list' ], + 'permission_callback' => [ $this, 'validate_request' ], + ] ); /** - * List job by tags and taxonmies + * Get agent detail. + * + * Call http://domain.com/wp-json/estate-api/v1/agent/1 */ - /// call http://domain.com/wp-json/job-api/v1/jobs //// - register_rest_route( $this->namespace, $this->base.'/tags', array( - 'methods' => 'GET', - 'callback' => array( $this, 'delete' ), - 'permission_callback' => array( $this, 'validate_request' ), - )); + register_rest_route( $this->namespace, $this->base . '/(?P\d+)', [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'get_detail' ], + 'permission_callback' => [ $this, 'validate_request' ], + ] ); + + /** + * Create a agent. + * + * Call http://domain.com/wp-json/estate-api/v1/agent/create + */ + register_rest_route( $this->namespace, $this->base . '/create', [ + 'methods' => 'GET', + 'callback' => [ $this, 'create' ], + 'permission_callback' => [ $this, 'validate_request' ], + ] ); + + /** + * Edit a agent. + * + * Call http://domain.com/wp-json/estate-api/v1/agent/edit + */ + register_rest_route( $this->namespace, $this->base . '/edit', [ + 'methods' => 'GET', + 'callback' => [ $this, 'edit' ], + ] ); + + /** + * Delete a agent. + * + * Call http://domain.com/wp-json/estate-api/v1/agent/delete + */ + register_rest_route( $this->namespace, $this->base . '/delete', [ + 'methods' => 'GET', + 'callback' => [ $this, 'delete' ], + 'permission_callback' => [ $this, 'validate_request' ], + ] ); } - - /** - * Get List Of Job + * Get List Of agents. * * Based on request to get collection * + * @return WP_REST_Response is json data * @since 1.0 * - * @return WP_REST_Response is json data */ - public function get_list ( $request ) { - - $agents = array(); - $error = array(); - $agent = null; + public function get_list( $request ) { + $agents = []; + $error = []; + $agent = null; if ( $agent == null ) { - $agents['agents'] = array(); + $agents['agents'] = []; - $property_list = get_posts( array( + $agent_list = get_posts( [ 'post_type' => 'opalestate_agent', 'posts_per_page' => $this->per_page(), 'suppress_filters' => true, - 'paged' => $this->get_paged() - ) ); + 'paged' => $this->get_paged(), + ] ); - if ( $property_list ) { + if ( $agent_list ) { $i = 0; - foreach ( $property_list as $agent_info ) { + foreach ( $agent_list as $agent_info ) { $agents['agents'][ $i ] = $this->get_agent_data( $agent_info ); - $i ++; + $i++; } } } else { - if ( get_post_type( $agent ) == 'opalestate_property' ) { + if ( get_post_type( $agent ) == 'opalestate_agent' ) { $agent_info = get_post( $agent ); $agents['agents'][0] = $this->get_agent_data( $agent_info ); } else { $error['error'] = sprintf( - /* translators: %s: property */ + /* translators: %s: agent */ esc_html__( 'Form %s not found!', 'opalestate-pro' ), $agent ); - return $error; + return $this->get_response( 404, $error ); } } - $response['collection'] = $agents['agents']; - $response['pages'] = 4; - $response['current'] = 1; + $response['collection'] = $agents['agents']; + $response['pages'] = 4; + $response['current'] = 1; + return $this->get_response( 200, $response ); } + /** + * Get Agent + * + * Based on request to get a agent. + * + * @return WP_REST_Response is json data + * @since 1.0 + * + */ + public function get_detail( $request ) { + $response = []; + if ( $request['id'] > 0 ) { + $post = get_post( $request['id'] ); + if ( $post && 'opalestate_agent' == get_post_type( $request['id'] ) ) { + $agent = $this->get_agent_data( $post ); + $response['agent'] = $agent ? $agent : []; + $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 ); + } /** - * Opalestaten a opalestate_property post object, generate the data for the API output + * The opalestate_agent post object, generate the data for the API output * - * @since 1.1 - * - * @param object $property_info The Download Post Object + * @param object $agent_info The Download Post Object * * @return array Array of post data to return back in the API + * @since 1.0 + * */ - - public function get_agent_data( $agent_info ){ - $ouput = array(); - + 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; @@ -162,48 +191,42 @@ class Agent_Api extends Base_Api { $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['info']['featured'] = (int)$agent->is_featured(); - $ouput['info']['email'] = get_post_meta( $agent_info->ID, OPALESTATE_AGENT_PREFIX . 'email', true ); - $ouput['info']['address'] = get_post_meta( $agent_info->ID, OPALESTATE_AGENT_PREFIX . 'address', true ); - - $terms = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_location' ); - $ouput['info']['location'] = $terms && !is_wp_error($terms) ? $terms : array(); - - $ouput['socials'] = $agent->get_socials(); - - $ouput['levels'] = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_level' ); + $ouput['info']['featured'] = (int) $agent->is_featured(); + $ouput['info']['email'] = get_post_meta( $agent_info->ID, OPALESTATE_AGENT_PREFIX . 'email', true ); + $ouput['info']['address'] = get_post_meta( $agent_info->ID, OPALESTATE_AGENT_PREFIX . 'address', true ); + $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' ); return apply_filters( 'opalestate_api_agents', $ouput ); } - + /** * Delete job * * Based on request to get collection * + * @return WP_REST_Response is json data * @since 1.0 * - * @return WP_REST_Response is json data */ - public function delete( ) { + public function delete() { } - - public function reviews () { + public function reviews() { } - public function categories () { + public function categories() { } - - public function tags () { - + + public function tags() { + } -} \ No newline at end of file +} diff --git a/inc/api/v1/property.php b/inc/api/v1/property.php index 46382d54..f49a03f5 100644 --- a/inc/api/v1/property.php +++ b/inc/api/v1/property.php @@ -31,41 +31,65 @@ class Property_Api extends Base_Api { * */ public function register_routes() { - /// call http://domain.com/wp-json/estate-api/v1/estate/list //// + /** + * Get list of properties. + * + * Call http://domain.com/wp-json/estate-api/v1/property/list + */ register_rest_route( $this->namespace, $this->base . '/list', [ 'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'get_list' ], 'permission_callback' => [ $this, 'validate_request' ], ] ); - /// call http://domain.com/wp-json/estate-api/v1/estate/featured //// + /** + * Get list of featured properties. + * + * Call http://domain.com/wp-json/estate-api/v1/property/featured + */ register_rest_route( $this->namespace, $this->base . '/featured', [ 'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'get_featured_list' ], 'permission_callback' => [ $this, 'validate_request' ], ] ); - /// call http://domain.com/wp-json/job-api/v1/estate/1 //// + /** + * Get property detail. + * + * Call http://domain.com/wp-json/estate-api/v1/property/1 + */ register_rest_route( $this->namespace, $this->base . '/(?P\d+)', [ 'methods' => WP_REST_Server::READABLE, - 'callback' => [ $this, 'get_property' ], + 'callback' => [ $this, 'get_detail' ], 'permission_callback' => [ $this, 'validate_request' ], ] ); - /// call http://domain.com/wp-json/job-api/v1/job/create //// + /** + * Create a property. + * + * Call http://domain.com/wp-json/estate-api/v1/property/create + */ register_rest_route( $this->namespace, $this->base . '/create', [ 'methods' => 'GET', 'callback' => [ $this, 'create' ], 'permission_callback' => [ $this, 'validate_request' ], ] ); - /// call http://domain.com/wp-json/job-api/v1/job/edit //// + /** + * Edit a property. + * + * Call http://domain.com/wp-json/estate-api/v1/property/edit + */ register_rest_route( $this->namespace, $this->base . '/edit', [ 'methods' => 'GET', 'callback' => [ $this, 'edit' ], ] ); - /// call http://domain.com/wp-json/job-api/v1/job/delete //// + /** + * Delete a property. + * + * Call http://domain.com/wp-json/estate-api/v1/property/delete + */ register_rest_route( $this->namespace, $this->base . '/delete', [ 'methods' => 'GET', 'callback' => [ $this, 'delete' ], @@ -73,9 +97,10 @@ class Property_Api extends Base_Api { ] ); /** - * List job by tags and taxonmies + * List property tags. + * + * Call http://domain.com/wp-json/estate-api/v1/property/tags */ - /// call http://domain.com/wp-json/job-api/v1/jobs //// register_rest_route( $this->namespace, $this->base . '/tags', [ 'methods' => 'GET', 'callback' => [ $this, 'delete' ], @@ -83,6 +108,11 @@ class Property_Api extends Base_Api { ] ); } + /** + * Get list of featured properties. + * + * @return array|\WP_REST_Response + */ public function get_featured_list() { $properties = []; $error = []; @@ -133,7 +163,7 @@ class Property_Api extends Base_Api { } /** - * Get List Of Job + * Get List Of Properties * * Based on request to get collection * @@ -142,11 +172,9 @@ class Property_Api extends Base_Api { * */ public function get_list( $request ) { - $properties = []; $error = []; - - $property = null; + $property = null; if ( $property == null ) { $properties = []; @@ -178,7 +206,7 @@ class Property_Api extends Base_Api { $property ); - return $error; + return $this->get_response( 404, $error ); } } @@ -192,39 +220,42 @@ class Property_Api extends Base_Api { /** * Get Property * - * Based on request to get collection + * Based on request to get a property. * * @return WP_REST_Response is json data * @since 1.0 * */ - public function get_property( $request ) { + public function get_detail( $request ) { $response = []; if ( $request['id'] > 0 ) { $post = get_post( $request['id'] ); - if ( $post && 'opalestate_property' == get_post_type( $request['id'] ) ) { - $this->get_response( 200, $response ); + $property = $this->get_property_data( $post ); + $response['property'] = $property ? $property : []; + $code = 200; + } else { + $code = 404; + $response['error'] = sprintf( esc_html__( 'Property ID: %s does not exist!', 'opalestate-pro' ), $request['id'] ); } - - $property = $this->get_property_data( $post ); - $response['property'] = $property ? $property : []; + } else { + $code = 404; + $response['error'] = sprintf( esc_html__( 'Invalid ID.', 'opalestate-pro' ), $request['id'] ); } - return $this->get_response( 200, $response ); + return $this->get_response( $code, $response ); } /** - * Opalestaten a opalestate_property post object, generate the data for the API output + * The opalestate_property post object, generate the data for the API output * * @param object $property_info The Download Post Object * * @return array Array of post data to return back in the API - * @since 1.1 + * @since 1.0 * */ private function get_property_data( $property_info ) { - $property = []; $property['info']['id'] = $property_info->ID;