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

View File

@ -45,13 +45,19 @@ class OpalEstate_Agent {
global $post;
if ( $post ) {
$this->post = $post;
$this->post_id = $post_id ? $post_id : get_the_ID();
$this->post = $post;
$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->is_featured = $this->get_meta( 'featured' );
$this->is_trusted = $this->get_meta( 'trusted' );
} else {
$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() {
@ -74,7 +80,6 @@ class OpalEstate_Agent {
$output = [];
foreach ( $socials as $social => $k ) {
$data = $this->get_meta( $social );
if ( $data && $data != "#" && ! empty( $data ) ) {
$output[ $social ] = $data;
@ -87,11 +92,11 @@ class OpalEstate_Agent {
/**
* Get url of user avatar by agent id
*/
public static function get_avatar_url( $userID, $size='thumbnail' ) {
$id = get_post_meta( $userID, OPALESTATE_AGENT_PREFIX . 'avatar_id', true );;
public static function get_avatar_url( $userID, $size = 'thumbnail' ) {
$id = get_post_meta( $userID, OPALESTATE_AGENT_PREFIX . 'avatar_id', true );;
$url = wp_get_attachment_image_url( $id, $size );
if( $url ) {
if ( $url ) {
return $url;
}
}
@ -127,7 +132,7 @@ class OpalEstate_Agent {
* return true if this agent 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 [
'name' => $agent->post_title,
@ -168,7 +173,7 @@ class OpalEstate_Agent {
* @return mixed
*/
public function get_trusted() {
return $this->is_trusted;
return 'on' === $this->is_trusted;
}
/**

View File

@ -27,6 +27,13 @@ abstract class Base_API {
*/
public $base ;
/**
* Post type.
*
* @var string
*/
protected $post_type = '';
/**
* The unique identifier of this plugin.
*
@ -191,4 +198,32 @@ abstract class Base_API {
private function invalid_key() {
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;
}
}

View File

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

29
inc/api/functions.php Normal file
View 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 );
}

View File

@ -98,39 +98,22 @@ class Agent_Api extends Base_Api {
*/
public function get_list( $request ) {
$agents = [];
$error = [];
$agent = null;
if ( $agent == null ) {
$agents['agents'] = [];
$agent_list = get_posts( [
'post_type' => 'opalestate_agent',
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
'paged' => $this->get_paged(),
] );
$agents['agents'] = [];
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 {
$error['error'] = sprintf(
/* translators: %s: agent */
esc_html__( 'Form %s not found!', 'opalestate-pro' ),
$agent
);
return $this->get_response( 404, $error );
if ( $agent_list ) {
$i = 0;
foreach ( $agent_list as $agent_info ) {
$agents['agents'][ $i ] = $this->get_agent_data( $agent_info );
$i++;
}
}
@ -157,7 +140,7 @@ class Agent_Api extends Base_Api {
if ( $post && 'opalestate_agent' == get_post_type( $request['id'] ) ) {
$agent = $this->get_agent_data( $post );
$response['agent'] = $agent ? $agent : [];
$code = 200;
$code = 200;
} else {
$code = 404;
$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 );
$ouput['info']['avatar'] = $agent->get_meta( 'avatar' );
$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 );
$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 : [];

View File

@ -19,14 +19,20 @@ class Property_Api extends Base_Api {
* @access public
* @var string $base .
*/
public $base = '/property';
public $base = '/properties';
/**
* Post type.
*
* @var string
*/
protected $post_type = 'opalestate_property';
/**
* Register Routes
*
* Register all CURD actions with POST/GET/PUT and calling function for each
*
* @return avoid
* @since 1.0
*
*/
@ -34,132 +40,60 @@ class Property_Api extends Base_Api {
/**
* 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', [
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_list' ],
'permission_callback' => [ $this, 'validate_request' ],
] );
register_rest_route(
$this->namespace,
'/' . $this->base,
[
[
'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' ],
// ],
]
);
/**
* 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' ],
] );
/**
* Get property detail.
*
* 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::READABLE,
'callback' => [ $this, 'get_detail' ],
'permission_callback' => [ $this, 'validate_request' ],
] );
/**
* 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' ],
] );
/**
* 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 );
register_rest_route(
$this->namespace,
'/' . $this->base . '/(?P<id>[\d]+)',
[
'args' => [
'id' => [
'description' => __( 'Unique identifier for the resource.', 'opalestate-pro' ),
'type' => 'integer',
],
],
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_item' ],
// 'permission_callback' => [ $this, 'get_item_permissions_check' ],
],
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'update_item' ],
// 'permission_callback' => [ $this, 'update_item_permissions_check' ],
],
[
'methods' => WP_REST_Server::DELETABLE,
'callback' => [ $this, 'delete_item' ],
// 'permission_callback' => [ $this, 'delete_item_permissions_check' ],
'args' => [
'force' => [
'default' => false,
'description' => __( 'Whether to bypass trash and force deletion.', 'opalestate-pro' ),
'type' => 'boolean',
],
],
],
]
);
}
/**
@ -171,42 +105,24 @@ class Property_Api extends Base_Api {
* @since 1.0
*
*/
public function get_list( $request ) {
public function get_items( $request ) {
$properties = [];
$error = [];
$property = null;
if ( $property == null ) {
$properties = [];
$per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5;
$paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1;
$property_list = get_posts( [
'post_type' => 'opalestate_property',
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
'paged' => $this->get_paged(),
] );
$property_list = get_posts( [
'post_type' => $this->post_type,
'posts_per_page' => $per_page,
'paged' => $paged,
'suppress_filters' => true,
] );
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 $this->get_response( 404, $error );
if ( $property_list ) {
$i = 0;
foreach ( $property_list as $property_info ) {
$properties[ $i ] = $this->get_property_data( $property_info );
$i++;
}
}
@ -226,11 +142,11 @@ class Property_Api extends Base_Api {
* @since 1.0
*
*/
public function get_detail( $request ) {
public function get_item( $request ) {
$response = [];
if ( $request['id'] > 0 ) {
$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 );
$response['property'] = $property ? $property : [];
$code = 200;
@ -318,28 +234,58 @@ class Property_Api extends Base_Api {
}
/**
* Delete job
*
* Based on request to get collection
*
* @return WP_REST_Response is json data
* @since 1.0
* Create a single item.
*
* @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() {
}
public function categories() {
}
public function tags() {
return $params;
}
}

View File

@ -70,7 +70,7 @@ class Opalestate_User_MetaBox {
public function get_avatar_fields( $prefix ) {
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' ),
'id' => $prefix . 'avatar',
'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' ),
'id' => $prefix . 'avatar',
'type' => is_admin() ? 'file' : 'uploader',

View File

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