2019-09-28 11:39:55 +02:00
|
|
|
<?php
|
|
|
|
// Exit if accessed directly.
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
2019-10-02 09:14:31 +02:00
|
|
|
|
2019-09-28 11:39:55 +02:00
|
|
|
/**
|
2019-10-02 09:14:31 +02:00
|
|
|
* @class Agent_Api
|
2019-09-28 11:39:55 +02:00
|
|
|
*
|
|
|
|
* @since 1.0.0
|
|
|
|
* @package Opal_Job
|
|
|
|
* @subpackage Opal_Job/controllers
|
|
|
|
*/
|
2019-10-04 08:26:00 +02:00
|
|
|
class Opalestate_Agent_Api extends Opalestate_Base_API {
|
2019-09-28 11:39:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The unique identifier of the route resource.
|
|
|
|
*
|
|
|
|
* @since 1.0.0
|
|
|
|
* @access public
|
2019-10-02 09:14:31 +02:00
|
|
|
* @var string $base .
|
2019-09-28 11:39:55 +02:00
|
|
|
*/
|
2019-10-03 12:24:12 +02:00
|
|
|
public $base = '/agents';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post type.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $post_type = 'opalestate_agent';
|
2019-10-02 09:14:31 +02:00
|
|
|
|
|
|
|
/**
|
2019-09-28 11:39:55 +02:00
|
|
|
* Register Routes
|
|
|
|
*
|
|
|
|
* Register all CURD actions with POST/GET/PUT and calling function for each
|
|
|
|
*
|
|
|
|
* @since 1.0
|
|
|
|
*
|
|
|
|
*/
|
2019-10-02 09:14:31 +02:00
|
|
|
public function register_routes() {
|
|
|
|
/**
|
|
|
|
* Get list of agents.
|
|
|
|
*
|
2019-10-03 12:24:12 +02:00
|
|
|
* Call http://domain.com/wp-json/estate-api/v1/agents
|
2019-10-02 09:14:31 +02:00
|
|
|
*/
|
2019-10-03 12:24:12 +02:00
|
|
|
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(),
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
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' ],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
2019-10-04 08:26:00 +02:00
|
|
|
|
|
|
|
register_rest_route(
|
|
|
|
$this->namespace,
|
|
|
|
'/' . $this->base . '/listings/(?P<id>[\d]+)',
|
|
|
|
[
|
|
|
|
'args' => [
|
|
|
|
'id' => [
|
|
|
|
'description' => __( 'Unique identifier for the resource.', 'opalestate-pro' ),
|
|
|
|
'type' => 'integer',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => [ $this, 'get_listings' ],
|
|
|
|
// 'permission_callback' => [ $this, 'get_item_permissions_check' ],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
2019-10-02 09:14:31 +02:00
|
|
|
}
|
2019-09-28 11:39:55 +02:00
|
|
|
|
|
|
|
/**
|
2019-10-02 09:14:31 +02:00
|
|
|
* Get List Of agents.
|
2019-09-28 11:39:55 +02:00
|
|
|
*
|
|
|
|
* Based on request to get collection
|
|
|
|
*
|
2019-10-02 09:14:31 +02:00
|
|
|
* @return WP_REST_Response is json data
|
2019-09-28 11:39:55 +02:00
|
|
|
* @since 1.0
|
|
|
|
*
|
|
|
|
*/
|
2019-10-03 12:24:12 +02:00
|
|
|
public function get_items( $request ) {
|
2019-10-03 10:45:46 +02:00
|
|
|
$agents['agents'] = [];
|
2019-09-28 11:39:55 +02:00
|
|
|
|
2019-10-03 12:24:12 +02:00
|
|
|
$per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5;
|
|
|
|
$paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1;
|
2019-09-28 11:39:55 +02:00
|
|
|
|
2019-10-03 10:45:46 +02:00
|
|
|
$agent_list = get_posts( [
|
2019-10-03 12:24:12 +02:00
|
|
|
'post_type' => $this->post_type,
|
|
|
|
'posts_per_page' => $per_page,
|
|
|
|
'paged' => $paged,
|
2019-10-03 10:45:46 +02:00
|
|
|
'suppress_filters' => true,
|
|
|
|
] );
|
|
|
|
|
|
|
|
if ( $agent_list ) {
|
|
|
|
$i = 0;
|
|
|
|
foreach ( $agent_list as $agent_info ) {
|
|
|
|
$agents['agents'][ $i ] = $this->get_agent_data( $agent_info );
|
|
|
|
$i++;
|
2019-09-28 11:39:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 09:14:31 +02:00
|
|
|
$response['collection'] = $agents['agents'];
|
|
|
|
|
2019-09-28 11:39:55 +02:00
|
|
|
return $this->get_response( 200, $response );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-02 09:14:31 +02:00
|
|
|
* Get Agent
|
2019-09-28 11:39:55 +02:00
|
|
|
*
|
2019-10-02 09:14:31 +02:00
|
|
|
* Based on request to get a agent.
|
2019-09-28 11:39:55 +02:00
|
|
|
*
|
2019-10-02 09:14:31 +02:00
|
|
|
* @return WP_REST_Response is json data
|
|
|
|
* @since 1.0
|
2019-09-28 11:39:55 +02:00
|
|
|
*
|
|
|
|
*/
|
2019-10-03 12:24:12 +02:00
|
|
|
public function get_item( $request ) {
|
2019-10-02 09:14:31 +02:00
|
|
|
$response = [];
|
|
|
|
if ( $request['id'] > 0 ) {
|
|
|
|
$post = get_post( $request['id'] );
|
2019-10-03 12:24:12 +02:00
|
|
|
if ( $post && $this->post_type == get_post_type( $request['id'] ) ) {
|
2019-10-02 09:14:31 +02:00
|
|
|
$agent = $this->get_agent_data( $post );
|
|
|
|
$response['agent'] = $agent ? $agent : [];
|
2019-10-03 10:45:46 +02:00
|
|
|
$code = 200;
|
2019-10-02 09:14:31 +02:00
|
|
|
} 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'] );
|
|
|
|
}
|
2019-09-28 11:39:55 +02:00
|
|
|
|
2019-10-02 09:14:31 +02:00
|
|
|
return $this->get_response( $code, $response );
|
|
|
|
}
|
2019-09-28 11:39:55 +02:00
|
|
|
|
2019-10-04 08:26:00 +02:00
|
|
|
/**
|
|
|
|
* Get agent listings.
|
|
|
|
*
|
|
|
|
* @param $request
|
|
|
|
* @return \WP_REST_Response
|
|
|
|
*/
|
|
|
|
public function get_listings( $request ) {
|
|
|
|
if ( $request['id'] > 0 ) {
|
|
|
|
$post = get_post( $request['id'] );
|
|
|
|
if ( $post && $this->post_type == get_post_type( $request['id'] ) ) {
|
|
|
|
$per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5;
|
|
|
|
$paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1;
|
|
|
|
|
|
|
|
$user_id = get_post_meta( $request['id'], OPALESTATE_AGENT_PREFIX . 'user_id', true );
|
|
|
|
|
|
|
|
$args = [
|
|
|
|
'post_type' => 'opalestate_property',
|
|
|
|
'posts_per_page' => $per_page,
|
|
|
|
'post__not_in' => [ $request['id'] ],
|
|
|
|
'paged' => $paged,
|
|
|
|
];
|
|
|
|
|
|
|
|
$args['meta_query'] = [ 'relation' => 'AND' ];
|
|
|
|
|
|
|
|
if ( $user_id ) {
|
|
|
|
$args['author'] = $user_id;
|
|
|
|
} else {
|
|
|
|
array_push( $args['meta_query'], [
|
|
|
|
'key' => OPALESTATE_PROPERTY_PREFIX . 'agent',
|
|
|
|
'value' => $request['id'],
|
|
|
|
'compare' => '=',
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
$property_list = get_posts( $args );
|
|
|
|
|
|
|
|
if ( $property_list ) {
|
|
|
|
$i = 0;
|
|
|
|
foreach ( $property_list as $property_info ) {
|
|
|
|
$properties[ $i ] = opalestate_api_get_property_data( $property_info );
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$response['listings'] = $properties ? $properties : [];
|
|
|
|
$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 );
|
|
|
|
}
|
|
|
|
|
2019-10-02 09:14:31 +02:00
|
|
|
/**
|
|
|
|
* The opalestate_agent post object, generate the data for the API output
|
|
|
|
*
|
|
|
|
* @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 ) {
|
2019-10-04 08:26:00 +02:00
|
|
|
$agent = new OpalEstate_Agent( $agent_info->ID );
|
|
|
|
$ouput['id'] = $agent_info->ID;
|
|
|
|
$ouput['name'] = $agent_info->post_title;
|
|
|
|
$ouput['slug'] = $agent_info->post_name;
|
|
|
|
$ouput['created_date'] = $agent_info->post_date;
|
|
|
|
$ouput['modified_date'] = $agent_info->post_modified;
|
|
|
|
$ouput['status'] = $agent_info->post_status;
|
|
|
|
$ouput['permalink'] = html_entity_decode( $agent_info->guid );
|
|
|
|
$ouput['content'] = $agent_info->post_content;
|
|
|
|
$ouput['avatar'] = $agent->get_meta( 'avatar' );
|
|
|
|
$ouput['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agent_info->ID ) );
|
|
|
|
$ouput['featured'] = $agent->is_featured();
|
|
|
|
$ouput['trusted'] = $agent->get_trusted();
|
|
|
|
$ouput['email'] = $agent->get_meta( 'email' );
|
|
|
|
$ouput['address'] = $agent->get_meta( 'address' );
|
|
|
|
$ouput['map'] = $agent->get_meta( 'map' );
|
|
|
|
|
|
|
|
$terms = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_location' );
|
|
|
|
$ouput['location'] = $terms && ! is_wp_error( $terms ) ? $terms : [];
|
|
|
|
$ouput['socials'] = $agent->get_socials();
|
|
|
|
$ouput['levels'] = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_level' );
|
2019-09-28 11:39:55 +02:00
|
|
|
|
|
|
|
return apply_filters( 'opalestate_api_agents', $ouput );
|
|
|
|
}
|
2019-10-04 08:26:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the query params for collections of attachments.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_collection_params() {
|
|
|
|
$params = parent::get_collection_params();
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
2019-10-02 09:14:31 +02:00
|
|
|
}
|