Update API

This commit is contained in:
Hoang Huu 2019-10-03 17:24:12 +07:00
parent a716e01a4c
commit 51417949b3
4 changed files with 171 additions and 233 deletions

@ -226,4 +226,31 @@ abstract class Base_API {
return true; return true;
} }
/**
* Get the query params for collections of attachments.
*
* @return array
*/
public function get_collection_params() {
$params['page'] = [
'description' => __( 'Current page of the collection.', 'opalestate-pro' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
];
$params['per_page'] = [
'description' => __( 'Maximum number of items to be returned in result set.', 'opalestate-pro' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
];
return $params;
}
} }

@ -29,71 +29,64 @@ class Agency_Api extends Base_Api {
* @access public * @access public
* @var string $base . * @var string $base .
*/ */
public $base = '/agency'; public $base = '/agencies';
/**
* Post type.
*
* @var string
*/
protected $post_type = 'opalestate_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
* *
*/ */
public function register_routes() { public function register_routes() {
/** /**
* Get agency list. * Get list of agencies.
* *
* Call http://domain.com/wp-json/estate-api/v1/agency/list * Call http://domain.com/wp-json/estate-api/v1/agencies
*/ */
register_rest_route( $this->namespace, $this->base . '/list', [ register_rest_route(
'methods' => WP_REST_Server::READABLE, $this->namespace,
'callback' => [ $this, 'get_list' ], '/' . $this->base,
'permission_callback' => [ $this, 'validate_request' ], [
] ); [
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
// 'permission_callback' => [ $this, 'get_items_permissions_check' ],
'args' => $this->get_collection_params(),
],
]
);
/** register_rest_route(
* Get agency detail. $this->namespace,
* '/' . $this->base . '/(?P<id>[\d]+)',
* Call http://domain.com/wp-json/estate-api/v1/agency/1 [
*/ 'args' => [
register_rest_route( $this->namespace, $this->base . '/(?P<id>\d+)', [ 'id' => [
'methods' => WP_REST_Server::READABLE, 'description' => __( 'Unique identifier for the resource.', 'opalestate-pro' ),
'callback' => [ $this, 'get_detail' ], 'type' => 'integer',
'permission_callback' => [ $this, 'validate_request' ], ],
] ); ],
[
/** 'methods' => WP_REST_Server::READABLE,
* Create a agency. 'callback' => [ $this, 'get_item' ],
* // 'permission_callback' => [ $this, 'get_item_permissions_check' ],
* Call http://domain.com/wp-json/estate-api/v1/agency/create ],
*/ [
register_rest_route( $this->namespace, $this->base . '/create', [ 'methods' => WP_REST_Server::EDITABLE,
'methods' => 'GET', 'callback' => [ $this, 'update_item' ],
'callback' => [ $this, 'create' ], // 'permission_callback' => [ $this, 'update_item_permissions_check' ],
'permission_callback' => [ $this, 'validate_request' ], ],
] ); ]
);
/**
* Edit a agency.
*
* Call http://domain.com/wp-json/estate-api/v1/agency/edit
*/
register_rest_route( $this->namespace, $this->base . '/edit', [
'methods' => 'GET',
'callback' => [ $this, 'edit' ],
] );
/**
* Delete a agency.
*
* Call http://domain.com/wp-json/estate-api/v1/agency/delete
*/
register_rest_route( $this->namespace, $this->base . '/delete', [
'methods' => 'GET',
'callback' => [ $this, 'delete' ],
'permission_callback' => [ $this, 'validate_request' ],
] );
} }
@ -106,47 +99,28 @@ class Agency_Api extends Base_Api {
* @since 1.0 * @since 1.0
* *
*/ */
public function get_list( $request ) { public function get_items( $request ) {
$agencies = []; $agencies['agencies'] = [];
$error = [];
$agency = null;
if ( $agency == null ) {
$agencies['agencies'] = [];
$agency_list = get_posts( [ $per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5;
'post_type' => 'opalestate_agency', $paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1;
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
'paged' => $this->get_paged(),
] );
if ( $agency_list ) { $agency_list = get_posts( [
$i = 0; 'post_type' => $this->post_type,
foreach ( $agency_list as $agency_info ) { 'posts_per_page' => $per_page,
$agencies['agencies'][ $i ] = $this->get_agency_data( $agency_info ); 'paged' => $paged,
$i++; 'suppress_filters' => true,
} ] );
}
} else {
if ( get_post_type( $agency ) == 'opalestate_agency' ) {
$agency_info = get_post( $agency );
$agencies['agencies'][0] = $this->get_agency_data( $agency_info ); if ( $agency_list ) {
$i = 0;
} else { foreach ( $agency_list as $agency_info ) {
$error['error'] = sprintf( $agencies['agencies'][ $i ] = $this->get_agency_data( $agency_info );
/* translators: %s: agency */ $i++;
esc_html__( 'Form %s not found!', 'opalestate-pro' ),
$agency
);
return $this->get_response( 404, $error );
} }
} }
$response['collection'] = $agencies['agencies']; $response['collection'] = $agencies['agencies'];
$response['pages'] = 4;
$response['current'] = 1;
return $this->get_response( 200, $response ); return $this->get_response( 200, $response );
} }
@ -160,20 +134,20 @@ class Agency_Api extends Base_Api {
* @since 1.0 * @since 1.0
* *
*/ */
public function get_detail( $request ) { public function get_item( $request ) {
$response = []; $response = [];
if ( $request['id'] > 0 ) { if ( $request['id'] > 0 ) {
$post = get_post( $request['id'] ); $post = get_post( $request['id'] );
if ( $post && 'opalestate_agency' == get_post_type( $request['id'] ) ) { if ( $post && $this->post_type == get_post_type( $request['id'] ) ) {
$agency = $this->get_agency_data( $post ); $agency = $this->get_agency_data( $post );
$response['agency'] = $agency ? $agency : []; $response['agency'] = $agency ? $agency : [];
$code = 200; $code = 200;
} else { } else {
$code = 404; $code = 404;
$response['error'] = sprintf( esc_html__( 'Agency ID: %s does not exist!', 'opalestate-pro' ), $request['id'] ); $response['error'] = sprintf( esc_html__( 'Agency ID: %s does not exist!', 'opalestate-pro' ), $request['id'] );
} }
} else { } else {
$code = 404; $code = 404;
$response['error'] = sprintf( esc_html__( 'Invalid ID.', 'opalestate-pro' ), $request['id'] ); $response['error'] = sprintf( esc_html__( 'Invalid ID.', 'opalestate-pro' ), $request['id'] );
} }
@ -213,30 +187,4 @@ class Agency_Api extends Base_Api {
return apply_filters( 'opalestate_api_agencies', $ouput ); 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
*
*/
public function delete() {
}
public function reviews() {
}
public function categories() {
}
public function tags() {
}
} }

@ -20,14 +20,20 @@ class Agent_Api extends Base_Api {
* @access public * @access public
* @var string $base . * @var string $base .
*/ */
public $base = '/agent'; public $base = '/agents';
/**
* Post type.
*
* @var string
*/
protected $post_type = 'opalestate_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
* *
*/ */
@ -35,56 +41,43 @@ class Agent_Api extends Base_Api {
/** /**
* Get list of agents. * Get list of agents.
* *
* Call http://domain.com/wp-json/estate-api/v1/agent/list * Call http://domain.com/wp-json/estate-api/v1/agents
*/ */
register_rest_route( $this->namespace, $this->base . '/list', [ register_rest_route(
'methods' => WP_REST_Server::READABLE, $this->namespace,
'callback' => [ $this, 'get_list' ], '/' . $this->base,
'permission_callback' => [ $this, 'validate_request' ], [
] ); [
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
// 'permission_callback' => [ $this, 'get_items_permissions_check' ],
'args' => $this->get_collection_params(),
],
]
);
/** register_rest_route(
* Get agent detail. $this->namespace,
* '/' . $this->base . '/(?P<id>[\d]+)',
* Call http://domain.com/wp-json/estate-api/v1/agent/1 [
*/ 'args' => [
register_rest_route( $this->namespace, $this->base . '/(?P<id>\d+)', [ 'id' => [
'methods' => WP_REST_Server::READABLE, 'description' => __( 'Unique identifier for the resource.', 'opalestate-pro' ),
'callback' => [ $this, 'get_detail' ], 'type' => 'integer',
'permission_callback' => [ $this, 'validate_request' ], ],
] ); ],
[
/** 'methods' => WP_REST_Server::READABLE,
* Create a agent. 'callback' => [ $this, 'get_item' ],
* // 'permission_callback' => [ $this, 'get_item_permissions_check' ],
* Call http://domain.com/wp-json/estate-api/v1/agent/create ],
*/ [
register_rest_route( $this->namespace, $this->base . '/create', [ 'methods' => WP_REST_Server::EDITABLE,
'methods' => 'GET', 'callback' => [ $this, 'update_item' ],
'callback' => [ $this, 'create' ], // 'permission_callback' => [ $this, 'update_item_permissions_check' ],
'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' ],
] );
} }
/** /**
@ -96,17 +89,17 @@ class Agent_Api extends Base_Api {
* @since 1.0 * @since 1.0
* *
*/ */
public function get_list( $request ) { public function get_items( $request ) {
$agents = [];
$agents['agents'] = []; $agents['agents'] = [];
$per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5;
$paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1;
$agent_list = get_posts( [ $agent_list = get_posts( [
'post_type' => 'opalestate_agent', 'post_type' => $this->post_type,
'posts_per_page' => $this->per_page(), 'posts_per_page' => $per_page,
'paged' => $paged,
'suppress_filters' => true, 'suppress_filters' => true,
'paged' => $this->get_paged(),
] ); ] );
if ( $agent_list ) { if ( $agent_list ) {
@ -118,8 +111,6 @@ class Agent_Api extends Base_Api {
} }
$response['collection'] = $agents['agents']; $response['collection'] = $agents['agents'];
$response['pages'] = 4;
$response['current'] = 1;
return $this->get_response( 200, $response ); return $this->get_response( 200, $response );
} }
@ -133,11 +124,11 @@ class Agent_Api extends Base_Api {
* @since 1.0 * @since 1.0
* *
*/ */
public function get_detail( $request ) { public function get_item( $request ) {
$response = []; $response = [];
if ( $request['id'] > 0 ) { if ( $request['id'] > 0 ) {
$post = get_post( $request['id'] ); $post = get_post( $request['id'] );
if ( $post && 'opalestate_agent' == get_post_type( $request['id'] ) ) { if ( $post && $this->post_type == get_post_type( $request['id'] ) ) {
$agent = $this->get_agent_data( $post ); $agent = $this->get_agent_data( $post );
$response['agent'] = $agent ? $agent : []; $response['agent'] = $agent ? $agent : [];
$code = 200; $code = 200;
@ -177,7 +168,7 @@ class Agent_Api extends Base_Api {
$agent = new OpalEstate_Agent( $agent_info->ID ); $agent = new OpalEstate_Agent( $agent_info->ID );
$ouput['info']['avatar'] = $agent->get_meta( 'avatar' ); $ouput['info']['avatar'] = $agent->get_meta( 'avatar' );
$ouput['info']['featured'] = (int) $agent->is_featured(); $ouput['info']['featured'] = $agent->is_featured();
$ouput['info']['trusted'] = $agent->get_trusted(); $ouput['info']['trusted'] = $agent->get_trusted();
$ouput['info']['email'] = $agent->get_meta( 'email' ); $ouput['info']['email'] = $agent->get_meta( 'email' );
$ouput['info']['address'] = $agent->get_meta( 'address' ); $ouput['info']['address'] = $agent->get_meta( 'address' );
@ -190,29 +181,4 @@ class Agent_Api extends Base_Api {
return apply_filters( 'opalestate_api_agents', $ouput ); 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
*
*/
public function delete() {
}
public function reviews() {
}
public function categories() {
}
public function tags() {
}
} }

@ -80,18 +80,18 @@ class Property_Api extends Base_Api {
'callback' => [ $this, 'update_item' ], 'callback' => [ $this, 'update_item' ],
// 'permission_callback' => [ $this, 'update_item_permissions_check' ], // 'permission_callback' => [ $this, 'update_item_permissions_check' ],
], ],
[ // [
'methods' => WP_REST_Server::DELETABLE, // 'methods' => WP_REST_Server::DELETABLE,
'callback' => [ $this, 'delete_item' ], // 'callback' => [ $this, 'delete_item' ],
// 'permission_callback' => [ $this, 'delete_item_permissions_check' ], // // 'permission_callback' => [ $this, 'delete_item_permissions_check' ],
'args' => [ // 'args' => [
'force' => [ // 'force' => [
'default' => false, // 'default' => false,
'description' => __( 'Whether to bypass trash and force deletion.', 'opalestate-pro' ), // 'description' => __( 'Whether to bypass trash and force deletion.', 'opalestate-pro' ),
'type' => 'boolean', // 'type' => 'boolean',
], // ],
], // ],
], // ],
] ]
); );
} }
@ -127,8 +127,6 @@ class Property_Api extends Base_Api {
} }
$response['collection'] = $properties; $response['collection'] = $properties;
$response['pages'] = 4;
$response['current'] = 1;
return $this->get_response( 200, $response ); return $this->get_response( 200, $response );
} }
@ -162,6 +160,21 @@ class Property_Api extends Base_Api {
return $this->get_response( $code, $response ); return $this->get_response( $code, $response );
} }
public function delete_item( $request ) {
$id = (int) $request['id'];
$force = (bool) $request['force'];
$property = get_post( absint( $request['id'] ) );
if ( ! $property || $this->post_type != $property->post_type ) {
$response['test'] = 0;
} else {
wp_delete_post( absint( $request['id'] ) );
$response['test'] = 1;
}
return $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
* *
@ -268,23 +281,7 @@ class Property_Api extends Base_Api {
* @return array * @return array
*/ */
public function get_collection_params() { public function get_collection_params() {
$params['page'] = [ $params = parent::get_collection_params();
'description' => __( 'Current page of the collection.', 'opalestate-pro' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
];
$params['per_page'] = [
'description' => __( 'Maximum number of items to be returned in result set.', 'opalestate-pro' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
];
return $params; return $params;
} }