Update API.

This commit is contained in:
Hoang Huu 2019-10-03 15:45:46 +07:00
parent 517d9b2110
commit bbc403b54e
8 changed files with 235 additions and 234 deletions

@ -45,13 +45,19 @@ class OpalEstate_Agent {
global $post; global $post;
if ( $post ) { if ( $post ) {
$this->post = $post;
$this->post_id = $post_id ? $post_id : get_the_ID(); $this->post_id = $post_id ? $post_id : get_the_ID();
$this->post = $post;
$this->author = get_userdata( $post->post_author ); $this->author = get_userdata( $post->post_author );
$this->author_name = ! empty( $this->author ) ? sprintf( '%s %s', $this->author->first_name, $this->author->last_name ) : null; $this->author_name = ! empty( $this->author ) ? sprintf( '%s %s', $this->author->first_name, $this->author->last_name ) : null;
$this->is_featured = $this->get_meta( 'featured' ); } else {
$this->is_trusted = $this->get_meta( 'trusted' ); $this->post_id = $post_id;
$this->post = get_post( $post_id );
$this->author = get_userdata( $this->post->post_author );
$this->author_name = ! empty( $this->author ) ? sprintf( '%s %s', $this->author->first_name, $this->author->last_name ) : null;
} }
$this->is_featured = $this->get_meta( 'featured' );
$this->is_trusted = $this->get_meta( 'trusted' );
} }
public function get_id() { public function get_id() {
@ -74,7 +80,6 @@ class OpalEstate_Agent {
$output = []; $output = [];
foreach ( $socials as $social => $k ) { foreach ( $socials as $social => $k ) {
$data = $this->get_meta( $social ); $data = $this->get_meta( $social );
if ( $data && $data != "#" && ! empty( $data ) ) { if ( $data && $data != "#" && ! empty( $data ) ) {
$output[ $social ] = $data; $output[ $social ] = $data;
@ -87,11 +92,11 @@ class OpalEstate_Agent {
/** /**
* Get url of user avatar by agent id * Get url of user avatar by agent id
*/ */
public static function get_avatar_url( $userID, $size='thumbnail' ) { public static function get_avatar_url( $userID, $size = 'thumbnail' ) {
$id = get_post_meta( $userID, OPALESTATE_AGENT_PREFIX . 'avatar_id', true );; $id = get_post_meta( $userID, OPALESTATE_AGENT_PREFIX . 'avatar_id', true );;
$url = wp_get_attachment_image_url( $id, $size ); $url = wp_get_attachment_image_url( $id, $size );
if( $url ) { if ( $url ) {
return $url; return $url;
} }
} }
@ -127,7 +132,7 @@ class OpalEstate_Agent {
* return true if this agent is featured * return true if this agent is featured
*/ */
public function is_featured() { public function is_featured() {
return $this->is_featured; return 'on' === $this->is_featured;
} }
/** /**
@ -148,7 +153,7 @@ class OpalEstate_Agent {
]; ];
} }
$url = self::get_avatar_url( $agent_id ); $url = self::get_avatar_url( $agent_id );
return [ return [
'name' => $agent->post_title, 'name' => $agent->post_title,
@ -168,7 +173,7 @@ class OpalEstate_Agent {
* @return mixed * @return mixed
*/ */
public function get_trusted() { public function get_trusted() {
return $this->is_trusted; return 'on' === $this->is_trusted;
} }
/** /**

@ -27,6 +27,13 @@ abstract class Base_API {
*/ */
public $base ; public $base ;
/**
* Post type.
*
* @var string
*/
protected $post_type = '';
/** /**
* The unique identifier of this plugin. * The unique identifier of this plugin.
* *
@ -191,4 +198,32 @@ abstract class Base_API {
private function invalid_key() { private function invalid_key() {
return new WP_Error( 'rest_forbidden', esc_html__( 'Invalid API key!' ), array( 'status' => rest_authorization_required_code() ) ); return new WP_Error( 'rest_forbidden', esc_html__( 'Invalid API key!' ), array( 'status' => rest_authorization_required_code() ) );
} }
/**
* Check if a given request has access to read items.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! opalestate_rest_check_post_permissions( $this->post_type, 'read' ) ) {
return new WP_Error( 'opalestate_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'opalestate-pro' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check if a given request has access to create an item.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function create_item_permissions_check( $request ) {
if ( ! opalestate_rest_check_post_permissions( $this->post_type, 'create' ) ) {
return new WP_Error( 'opalestate_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'opalestate-pro' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
} }

@ -54,7 +54,8 @@ class Opalestate_API {
'v1/property.php', 'v1/property.php',
'v1/agent.php', 'v1/agent.php',
'v1/agency.php', 'v1/agency.php',
'class-api-auth.php' 'class-api-auth.php',
'functions.php'
] ); ] );
add_action( 'rest_api_init', [$this,'register_resources'] ); add_action( 'rest_api_init', [$this,'register_resources'] );

29
inc/api/functions.php Normal file

@ -0,0 +1,29 @@
<?php
/**
* Check permissions of posts on REST API.
*
* @param string $post_type Post type.
* @param string $context Request context.
* @param int $object_id Post ID.
* @return bool
*/
function opalestate_rest_check_post_permissions( $post_type, $context = 'read', $object_id = 0 ) {
$contexts = [
'read' => 'read_private_posts',
'create' => 'publish_posts',
'edit' => 'edit_post',
'delete' => 'delete_post',
'batch' => 'edit_others_posts',
];
if ( 'revision' === $post_type ) {
$permission = false;
} else {
$cap = $contexts[ $context ];
$post_type_object = get_post_type_object( $post_type );
$permission = current_user_can( $post_type_object->cap->$cap, $object_id );
}
var_dump($post_type_object->cap->$cap);
return apply_filters( 'opalestate_rest_check_permissions', $permission, $context, $object_id, $post_type );
}

@ -98,39 +98,22 @@ class Agent_Api extends Base_Api {
*/ */
public function get_list( $request ) { public function get_list( $request ) {
$agents = []; $agents = [];
$error = [];
$agent = null;
if ( $agent == null ) {
$agents['agents'] = [];
$agent_list = get_posts( [ $agents['agents'] = [];
'post_type' => 'opalestate_agent',
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
'paged' => $this->get_paged(),
] );
if ( $agent_list ) {
$i = 0;
foreach ( $agent_list as $agent_info ) {
$agents['agents'][ $i ] = $this->get_agent_data( $agent_info );
$i++;
}
}
} else {
if ( get_post_type( $agent ) == 'opalestate_agent' ) {
$agent_info = get_post( $agent );
$agents['agents'][0] = $this->get_agent_data( $agent_info ); $agent_list = get_posts( [
'post_type' => 'opalestate_agent',
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
'paged' => $this->get_paged(),
] );
} else { if ( $agent_list ) {
$error['error'] = sprintf( $i = 0;
/* translators: %s: agent */ foreach ( $agent_list as $agent_info ) {
esc_html__( 'Form %s not found!', 'opalestate-pro' ), $agents['agents'][ $i ] = $this->get_agent_data( $agent_info );
$agent $i++;
);
return $this->get_response( 404, $error );
} }
} }
@ -157,7 +140,7 @@ class Agent_Api extends Base_Api {
if ( $post && 'opalestate_agent' == get_post_type( $request['id'] ) ) { if ( $post && 'opalestate_agent' == 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;
} else { } else {
$code = 404; $code = 404;
$response['error'] = sprintf( esc_html__( 'Agent ID: %s does not exist!', 'opalestate-pro' ), $request['id'] ); $response['error'] = sprintf( esc_html__( 'Agent ID: %s does not exist!', 'opalestate-pro' ), $request['id'] );
@ -193,9 +176,12 @@ 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']['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']['trusted'] = $agent->get_trusted();
$ouput['info']['address'] = get_post_meta( $agent_info->ID, OPALESTATE_AGENT_PREFIX . 'address', true ); $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' ); $terms = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_location' );
$ouput['info']['location'] = $terms && ! is_wp_error( $terms ) ? $terms : []; $ouput['info']['location'] = $terms && ! is_wp_error( $terms ) ? $terms : [];

@ -19,14 +19,20 @@ class Property_Api extends Base_Api {
* @access public * @access public
* @var string $base . * @var string $base .
*/ */
public $base = '/property'; public $base = '/properties';
/**
* Post type.
*
* @var string
*/
protected $post_type = 'opalestate_property';
/** /**
* 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
* *
*/ */
@ -34,132 +40,60 @@ class Property_Api extends Base_Api {
/** /**
* Get list of properties. * Get list of properties.
* *
* Call http://domain.com/wp-json/estate-api/v1/property/list * Call http://domain.com/wp-json/estate-api/v1/properties
*/ */
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(),
],
// [
// 'methods' => WP_REST_Server::CREATABLE,
// 'callback' => [ $this, 'create_item' ],
// // 'permission_callback' => [ $this, 'create_item_permissions_check' ],
// ],
]
);
/** register_rest_route(
* Get list of featured properties. $this->namespace,
* '/' . $this->base . '/(?P<id>[\d]+)',
* Call http://domain.com/wp-json/estate-api/v1/property/featured [
*/ 'args' => [
register_rest_route( $this->namespace, $this->base . '/featured', [ 'id' => [
'methods' => WP_REST_Server::READABLE, 'description' => __( 'Unique identifier for the resource.', 'opalestate-pro' ),
'callback' => [ $this, 'get_featured_list' ], 'type' => 'integer',
'permission_callback' => [ $this, 'validate_request' ], ],
] ); ],
[
/** 'methods' => WP_REST_Server::READABLE,
* Get property detail. 'callback' => [ $this, 'get_item' ],
* // 'permission_callback' => [ $this, 'get_item_permissions_check' ],
* Call http://domain.com/wp-json/estate-api/v1/property/1 ],
*/ [
register_rest_route( $this->namespace, $this->base . '/(?P<id>\d+)', [ 'methods' => WP_REST_Server::EDITABLE,
'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'update_item' ],
'callback' => [ $this, 'get_detail' ], // 'permission_callback' => [ $this, 'update_item_permissions_check' ],
'permission_callback' => [ $this, 'validate_request' ], ],
] ); [
'methods' => WP_REST_Server::DELETABLE,
/** 'callback' => [ $this, 'delete_item' ],
* Create a property. // 'permission_callback' => [ $this, 'delete_item_permissions_check' ],
* 'args' => [
* Call http://domain.com/wp-json/estate-api/v1/property/create 'force' => [
*/ 'default' => false,
register_rest_route( $this->namespace, $this->base . '/create', [ 'description' => __( 'Whether to bypass trash and force deletion.', 'opalestate-pro' ),
'methods' => 'GET', 'type' => 'boolean',
'callback' => [ $this, 'create' ], ],
'permission_callback' => [ $this, 'validate_request' ], ],
] ); ],
]
/** );
* 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' ],
] );
/**
* 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' ],
'permission_callback' => [ $this, 'validate_request' ],
] );
/**
* List property tags.
*
* Call http://domain.com/wp-json/estate-api/v1/property/tags
*/
register_rest_route( $this->namespace, $this->base . '/tags', [
'methods' => 'GET',
'callback' => [ $this, 'delete' ],
'permission_callback' => [ $this, 'validate_request' ],
] );
}
/**
* Get list of featured properties.
*
* @return array|\WP_REST_Response
*/
public function get_featured_list() {
$properties = [];
$error = [];
$property = null;
if ( $property == null ) {
$properties = [];
$property_list = get_posts( [
'post_type' => 'opalestate_property',
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
'meta_key' => OPALESTATE_PROPERTY_PREFIX . 'featured',
'meta_value' => 'on',
'paged' => $this->get_paged(),
] );
if ( $property_list ) {
$i = 0;
foreach ( $property_list as $property_info ) {
$properties[ $i ] = $this->get_property_data( $property_info );
$i++;
}
}
} else {
if ( get_post_type( $property ) == 'opalestate_property' ) {
$property_info = get_post( $property );
$properties[0] = $this->get_property_data( $property_info );
} else {
$error['error'] = sprintf(
/* translators: %s: property */
esc_html__( 'Form %s not found!', 'opalestate-pro' ),
$property
);
return $error;
}
}
$response['collection'] = $properties;
$response['pages'] = 4;
$response['current'] = 1;
return $this->get_response( 200, $response );
} }
/** /**
@ -171,42 +105,24 @@ class Property_Api extends Base_Api {
* @since 1.0 * @since 1.0
* *
*/ */
public function get_list( $request ) { public function get_items( $request ) {
$properties = []; $properties = [];
$error = [];
$property = null;
if ( $property == null ) { $per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5;
$properties = []; $paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1;
$property_list = get_posts( [ $property_list = get_posts( [
'post_type' => 'opalestate_property', 'post_type' => $this->post_type,
'posts_per_page' => $this->per_page(), 'posts_per_page' => $per_page,
'suppress_filters' => true, 'paged' => $paged,
'paged' => $this->get_paged(), 'suppress_filters' => true,
] ); ] );
if ( $property_list ) { if ( $property_list ) {
$i = 0; $i = 0;
foreach ( $property_list as $property_info ) { foreach ( $property_list as $property_info ) {
$properties[ $i ] = $this->get_property_data( $property_info ); $properties[ $i ] = $this->get_property_data( $property_info );
$i++; $i++;
}
}
} else {
if ( get_post_type( $property ) == 'opalestate_property' ) {
$property_info = get_post( $property );
$properties[0] = $this->get_property_data( $property_info );
} else {
$error['error'] = sprintf(
/* translators: %s: property */
esc_html__( 'Form %s not found!', 'opalestate-pro' ),
$property
);
return $this->get_response( 404, $error );
} }
} }
@ -226,11 +142,11 @@ class Property_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_property' == get_post_type( $request['id'] ) ) { if ( $post && $this->post_type == get_post_type( $request['id'] ) ) {
$property = $this->get_property_data( $post ); $property = $this->get_property_data( $post );
$response['property'] = $property ? $property : []; $response['property'] = $property ? $property : [];
$code = 200; $code = 200;
@ -318,28 +234,58 @@ class Property_Api extends Base_Api {
} }
/** /**
* Delete job * Create a single item.
*
* Based on request to get collection
*
* @return WP_REST_Response is json data
* @since 1.0
* *
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/ */
public function delete() { public function create_item( $request ) {
if ( ! empty( $request['id'] ) ) {
/* translators: %s: post type */
return new WP_Error( "opalestate_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'opalestate-pro' ), $this->post_type ), [ 'status' => 400 ] );
}
$data = [
'post_title' => $request['post_title'],
'post_type' => $this->post_type,
'post_content' => $request['post_content'],
];
$data['post_status'] = 'pending';
$post_id = wp_insert_post( $data, true );
$response['id'] = $post_id;
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->base, $post_id ) ) );
return $response;
} }
/**
* 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',
];
public function reviews() { return $params;
}
public function categories() {
}
public function tags() {
} }
} }

@ -70,7 +70,7 @@ class Opalestate_User_MetaBox {
public function get_avatar_fields( $prefix ) { public function get_avatar_fields( $prefix ) {
return [ return [
[ [
'name' => esc_html__( 'Avatar Pictures', 'opalestate-pro' ), 'name' => esc_html__( 'Avatar Picture', 'opalestate-pro' ),
'desc' => esc_html__( 'This image will display in user detail and profile box information', 'opalestate-pro' ), 'desc' => esc_html__( 'This image will display in user detail and profile box information', 'opalestate-pro' ),
'id' => $prefix . 'avatar', 'id' => $prefix . 'avatar',
'type' => is_admin() ? 'file' : 'opal_upload', 'type' => is_admin() ? 'file' : 'opal_upload',
@ -207,7 +207,7 @@ class Opalestate_User_MetaBox {
], ],
[ [
'name' => esc_html__( 'Avatar Pictures', 'opalestate-pro' ), 'name' => esc_html__( 'Avatar Picture', 'opalestate-pro' ),
'desc' => esc_html__( 'This image will display in user detail and profile box information', 'opalestate-pro' ), 'desc' => esc_html__( 'This image will display in user detail and profile box information', 'opalestate-pro' ),
'id' => $prefix . 'avatar', 'id' => $prefix . 'avatar',
'type' => is_admin() ? 'file' : 'uploader', 'type' => is_admin() ? 'file' : 'uploader',

@ -103,9 +103,8 @@ class Opalestate_Property {
/** /**
* Constructor * Constructor
*/ */
public function __construct( $post_id ) { public function __construct( $post_id = null ) {
$this->post_id = $post_id; $this->post_id = $post_id;
$this->map = $this->get_metabox_value( 'map' ); $this->map = $this->get_metabox_value( 'map' );
$this->address = $this->get_metabox_value( 'address' ); $this->address = $this->get_metabox_value( 'address' );
$this->price = $this->get_metabox_value( 'price' ); $this->price = $this->get_metabox_value( 'price' );
@ -207,7 +206,7 @@ class Opalestate_Property {
* *
*/ */
public function is_featured() { public function is_featured() {
return $this->featured; return 'on' === $this->featured;
} }
/** /**
@ -463,7 +462,7 @@ class Opalestate_Property {
} }
public function get_author_link() { public function get_author_link() {
$user_id = get_post_field( 'post_author', $this->get_id() ); $user_id = get_post_field( 'post_author', $this->get_id() );
$image_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'avatar_id', true ); $image_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'avatar_id', true );
$related_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true ); $related_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true );