Update API

This commit is contained in:
Hoang Huu 2019-10-02 14:14:31 +07:00
parent a99fe44f10
commit ca848a3844
3 changed files with 342 additions and 196 deletions

@ -1,6 +1,6 @@
<?php <?php
/** /**
* Define * Define
* Note: only use for internal purpose. * Note: only use for internal purpose.
* *
* @package OpalJob * @package OpalJob
@ -12,129 +12,221 @@
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; exit;
} }
/** /**
* @class Job_Api * @class Job_Api
* *
* @since 1.0.0 * @since 1.0.0
* @package Opal_Job * @package Opal_Job
* @subpackage Opal_Job/controllers * @subpackage Opal_Job/controllers
*/ */
class Agency_Api extends Base_Api { class Agency_Api extends Base_Api {
/** /**
* The unique identifier of the route resource. * The unique identifier of the route resource.
* *
* @since 1.0.0 * @since 1.0.0
* @access public * @access public
* @var string $base. * @var string $base .
*/ */
public $base = '/agency'; public $base = '/agency';
/** /**
* Register Routes * Register Routes
* *
* Register all CURD actions with POST/GET/PUT and calling function for each * Register all CURD actions with POST/GET/PUT and calling function for each
* *
* @return avoid
* @since 1.0 * @since 1.0
* *
* @return avoid
*/ */
public function register_routes ( ) { public function register_routes() {
/// call http://domain.com/wp-json/job-api/v1/job/list //// /// call http://domain.com/wp-json/estate-api/v1/job/list ////
register_rest_route( $this->namespace, $this->base.'/list', array( register_rest_route( $this->namespace, $this->base . '/list', [
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_list' ), 'callback' => [ $this, 'get_list' ],
'permission_callback' => array( $this, 'validate_request' ), 'permission_callback' => [ $this, 'validate_request' ],
)); ] );
/// call http://domain.com/wp-json/job-api/v1/job/1 ////
register_rest_route( $this->namespace, $this->base.'/(?P<id>\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 //// /// call http://domain.com/wp-json/estate-api/v1/job/1 ////
register_rest_route( $this->namespace, $this->base.'/create', array( register_rest_route( $this->namespace, $this->base . '/(?P<id>\d+)', [
'methods' => 'GET', 'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'create' ), 'callback' => [ $this, 'get_detail' ],
'permission_callback' => array( $this, 'validate_request' ), 'permission_callback' => [ $this, 'validate_request' ],
)); ] );
/// call http://domain.com/wp-json/job-api/v1/job/edit ////
register_rest_route( $this->namespace, $this->base.'/edit', array( /// call http://domain.com/wp-json/estate-api/v1/job/create ////
'methods' => 'GET', register_rest_route( $this->namespace, $this->base . '/create', [
'callback' => array( $this, 'edit' ), 'methods' => 'GET',
)); 'callback' => [ $this, 'create' ],
/// call http://domain.com/wp-json/job-api/v1/job/delete //// 'permission_callback' => [ $this, 'validate_request' ],
register_rest_route( $this->namespace, $this->base.'/delete', array( ] );
'methods' => 'GET',
'callback' => array( $this, 'delete' ), /// call http://domain.com/wp-json/estate-api/v1/job/edit ////
'permission_callback' => array( $this, 'validate_request' ), 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 //// /// call http://domain.com/wp-json/estate-api/v1/jobs ////
register_rest_route( $this->namespace, $this->base.'/tags', array( register_rest_route( $this->namespace, $this->base . '/tags', [
'methods' => 'GET', 'methods' => 'GET',
'callback' => array( $this, 'delete' ), 'callback' => [ $this, 'delete' ],
'permission_callback' => array( $this, 'validate_request' ), 'permission_callback' => [ $this, 'validate_request' ],
)); ] );
} }
/** /**
* Get List Of Job * Get List Of agencies.
* *
* Based on request to get collection * Based on request to get collection
* *
* @return WP_REST_Response is json data
* @since 1.0 * @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( $agency_list = get_posts( [
array(
'post_type' => 'opalestate_agency', 'post_type' => 'opalestate_agency',
'posts_per_page' => $this->per_page(), 'posts_per_page' => $this->per_page(),
'suppress_filters' => true, 'suppress_filters' => true,
'paged' => $this->get_paged() 'paged' => $this->get_paged(),
) ] );
);
$data = $query->get_api_list(); if ( $agency_list ) {
$response['collection'] = $data['collection']; $i = 0;
$response['found'] = $data['found']; foreach ( $agency_list as $agency_info ) {
$response['current'] = 1; $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 ); 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 * Delete job
* *
* Based on request to get collection * Based on request to get collection
* *
* @return WP_REST_Response is json data
* @since 1.0 * @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() {
} }
} }

@ -1,157 +1,186 @@
<?php <?php
/**
* Define
* Note: only use for internal purpose.
*
* @package OpalJob
* @copyright Copyright (c) 2019, WpOpal <https://www.wpopal.com>
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 1.0
*/
// Exit if accessed directly. // Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; exit;
} }
/** /**
* @class Job_Api * @class Agent_Api
* *
* @since 1.0.0 * @since 1.0.0
* @package Opal_Job * @package Opal_Job
* @subpackage Opal_Job/controllers * @subpackage Opal_Job/controllers
*/ */
class Agent_Api extends Base_Api { class Agent_Api extends Base_Api {
/** /**
* The unique identifier of the route resource. * The unique identifier of the route resource.
* *
* @since 1.0.0 * @since 1.0.0
* @access public * @access public
* @var string $base. * @var string $base .
*/ */
public $base = '/agent'; public $base = '/agent';
/** /**
* Register Routes * Register Routes
* *
* Register all CURD actions with POST/GET/PUT and calling function for each * Register all CURD actions with POST/GET/PUT and calling function for each
* *
* @return avoid
* @since 1.0 * @since 1.0
* *
* @return avoid
*/ */
public function register_routes ( ) { public function register_routes() {
/// call http://domain.com/wp-json/job-api/v1/job/list //// /**
register_rest_route( $this->namespace, $this->base.'/list', array( * Get list of agents.
'methods' => WP_REST_Server::READABLE, *
'callback' => array( $this, 'get_list' ), * Call http://domain.com/wp-json/estate-api/v1/agent/list
'permission_callback' => array( $this, 'validate_request' ), */
)); register_rest_route( $this->namespace, $this->base . '/list', [
/// call http://domain.com/wp-json/job-api/v1/job/1 //// 'methods' => WP_REST_Server::READABLE,
register_rest_route( $this->namespace, $this->base.'/(?P<id>\d+)', array( 'callback' => [ $this, 'get_list' ],
'methods' => WP_REST_Server::READABLE, 'permission_callback' => [ $this, 'validate_request' ],
'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' ),
));
/** /**
* 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 . '/(?P<id>\d+)', [
register_rest_route( $this->namespace, $this->base.'/tags', array( 'methods' => WP_REST_Server::READABLE,
'methods' => 'GET', 'callback' => [ $this, 'get_detail' ],
'callback' => array( $this, 'delete' ), 'permission_callback' => [ $this, 'validate_request' ],
'permission_callback' => array( $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 * Based on request to get collection
* *
* @return WP_REST_Response is json data
* @since 1.0 * @since 1.0
* *
* @return WP_REST_Response is json data
*/ */
public function get_list ( $request ) { public function get_list( $request ) {
$agents = [];
$agents = array(); $error = [];
$error = array(); $agent = null;
$agent = null;
if ( $agent == null ) { if ( $agent == null ) {
$agents['agents'] = array(); $agents['agents'] = [];
$property_list = get_posts( array( $agent_list = get_posts( [
'post_type' => 'opalestate_agent', 'post_type' => 'opalestate_agent',
'posts_per_page' => $this->per_page(), 'posts_per_page' => $this->per_page(),
'suppress_filters' => true, 'suppress_filters' => true,
'paged' => $this->get_paged() 'paged' => $this->get_paged(),
) ); ] );
if ( $property_list ) { if ( $agent_list ) {
$i = 0; $i = 0;
foreach ( $property_list as $agent_info ) { foreach ( $agent_list as $agent_info ) {
$agents['agents'][ $i ] = $this->get_agent_data( $agent_info ); $agents['agents'][ $i ] = $this->get_agent_data( $agent_info );
$i ++; $i++;
} }
} }
} else { } else {
if ( get_post_type( $agent ) == 'opalestate_property' ) { if ( get_post_type( $agent ) == 'opalestate_agent' ) {
$agent_info = get_post( $agent ); $agent_info = get_post( $agent );
$agents['agents'][0] = $this->get_agent_data( $agent_info ); $agents['agents'][0] = $this->get_agent_data( $agent_info );
} else { } else {
$error['error'] = sprintf( $error['error'] = sprintf(
/* translators: %s: property */ /* translators: %s: agent */
esc_html__( 'Form %s not found!', 'opalestate-pro' ), esc_html__( 'Form %s not found!', 'opalestate-pro' ),
$agent $agent
); );
return $error; return $this->get_response( 404, $error );
} }
} }
$response['collection'] = $agents['agents']; $response['collection'] = $agents['agents'];
$response['pages'] = 4; $response['pages'] = 4;
$response['current'] = 1; $response['current'] = 1;
return $this->get_response( 200, $response ); 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 $agent_info The Download Post Object
*
* @param object $property_info The Download Post Object
* *
* @return array Array of post data to return back in the API * @return array Array of post data to return back in the API
* @since 1.0
*
*/ */
public function get_agent_data( $agent_info ) {
public function get_agent_data( $agent_info ){ $ouput = [];
$ouput = array();
$ouput['info']['id'] = $agent_info->ID; $ouput['info']['id'] = $agent_info->ID;
$ouput['info']['slug'] = $agent_info->post_name; $ouput['info']['slug'] = $agent_info->post_name;
$ouput['info']['title'] = $agent_info->post_title; $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']['content'] = $agent_info->post_content;
$ouput['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agent_info->ID ) ); $ouput['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agent_info->ID ) );
$agent = new OpalEstate_Agent( $agent_info->ID ); $agent = new OpalEstate_Agent( $agent_info->ID );
$ouput['info']['featured'] = (int)$agent->is_featured(); $ouput['info']['featured'] = (int) $agent->is_featured();
$ouput['info']['email'] = get_post_meta( $agent_info->ID, OPALESTATE_AGENT_PREFIX . 'email', true ); $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 ); $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' );
$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 ); return apply_filters( 'opalestate_api_agents', $ouput );
} }
/** /**
* Delete job * Delete job
* *
* Based on request to get collection * Based on request to get collection
* *
* @return WP_REST_Response is json data
* @since 1.0 * @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() {
} }
} }

@ -31,41 +31,65 @@ class Property_Api extends Base_Api {
* *
*/ */
public function register_routes() { 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', [ register_rest_route( $this->namespace, $this->base . '/list', [
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_list' ], 'callback' => [ $this, 'get_list' ],
'permission_callback' => [ $this, 'validate_request' ], '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', [ register_rest_route( $this->namespace, $this->base . '/featured', [
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_featured_list' ], 'callback' => [ $this, 'get_featured_list' ],
'permission_callback' => [ $this, 'validate_request' ], '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<id>\d+)', [ register_rest_route( $this->namespace, $this->base . '/(?P<id>\d+)', [
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_property' ], 'callback' => [ $this, 'get_detail' ],
'permission_callback' => [ $this, 'validate_request' ], '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', [ register_rest_route( $this->namespace, $this->base . '/create', [
'methods' => 'GET', 'methods' => 'GET',
'callback' => [ $this, 'create' ], 'callback' => [ $this, 'create' ],
'permission_callback' => [ $this, 'validate_request' ], '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', [ register_rest_route( $this->namespace, $this->base . '/edit', [
'methods' => 'GET', 'methods' => 'GET',
'callback' => [ $this, 'edit' ], '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', [ register_rest_route( $this->namespace, $this->base . '/delete', [
'methods' => 'GET', 'methods' => 'GET',
'callback' => [ $this, 'delete' ], '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', [ register_rest_route( $this->namespace, $this->base . '/tags', [
'methods' => 'GET', 'methods' => 'GET',
'callback' => [ $this, 'delete' ], '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() { public function get_featured_list() {
$properties = []; $properties = [];
$error = []; $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 * Based on request to get collection
* *
@ -142,11 +172,9 @@ class Property_Api extends Base_Api {
* *
*/ */
public function get_list( $request ) { public function get_list( $request ) {
$properties = []; $properties = [];
$error = []; $error = [];
$property = null;
$property = null;
if ( $property == null ) { if ( $property == null ) {
$properties = []; $properties = [];
@ -178,7 +206,7 @@ class Property_Api extends Base_Api {
$property $property
); );
return $error; return $this->get_response( 404, $error );
} }
} }
@ -192,39 +220,42 @@ class Property_Api extends Base_Api {
/** /**
* Get Property * Get Property
* *
* Based on request to get collection * Based on request to get a property.
* *
* @return WP_REST_Response is json data * @return WP_REST_Response is json data
* @since 1.0 * @since 1.0
* *
*/ */
public function get_property( $request ) { public function get_detail( $request ) {
$response = []; $response = [];
if ( $request['id'] > 0 ) { if ( $request['id'] > 0 ) {
$post = get_post( $request['id'] ); $post = get_post( $request['id'] );
if ( $post && 'opalestate_property' == get_post_type( $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'] );
} }
} else {
$property = $this->get_property_data( $post ); $code = 404;
$response['property'] = $property ? $property : []; $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 * @param object $property_info The Download Post Object
* *
* @return array Array of post data to return back in the API * @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 ) { private function get_property_data( $property_info ) {
$property = []; $property = [];
$property['info']['id'] = $property_info->ID; $property['info']['id'] = $property_info->ID;