185 lines
5.0 KiB
PHP
Executable File
185 lines
5.0 KiB
PHP
Executable File
<?php
|
|
// Exit if accessed directly.
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* @class Agent_Api
|
|
*
|
|
* @since 1.0.0
|
|
* @package Opal_Job
|
|
* @subpackage Opal_Job/controllers
|
|
*/
|
|
class Agent_Api extends Base_Api {
|
|
|
|
/**
|
|
* The unique identifier of the route resource.
|
|
*
|
|
* @since 1.0.0
|
|
* @access public
|
|
* @var string $base .
|
|
*/
|
|
public $base = '/agents';
|
|
|
|
/**
|
|
* Post type.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $post_type = 'opalestate_agent';
|
|
|
|
/**
|
|
* Register Routes
|
|
*
|
|
* Register all CURD actions with POST/GET/PUT and calling function for each
|
|
*
|
|
* @since 1.0
|
|
*
|
|
*/
|
|
public function register_routes() {
|
|
/**
|
|
* Get list of agents.
|
|
*
|
|
* Call http://domain.com/wp-json/estate-api/v1/agents
|
|
*/
|
|
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' ],
|
|
],
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get List Of agents.
|
|
*
|
|
* Based on request to get collection
|
|
*
|
|
* @return WP_REST_Response is json data
|
|
* @since 1.0
|
|
*
|
|
*/
|
|
public function get_items( $request ) {
|
|
$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( [
|
|
'post_type' => $this->post_type,
|
|
'posts_per_page' => $per_page,
|
|
'paged' => $paged,
|
|
'suppress_filters' => true,
|
|
] );
|
|
|
|
if ( $agent_list ) {
|
|
$i = 0;
|
|
foreach ( $agent_list as $agent_info ) {
|
|
$agents['agents'][ $i ] = $this->get_agent_data( $agent_info );
|
|
$i++;
|
|
}
|
|
}
|
|
|
|
$response['collection'] = $agents['agents'];
|
|
|
|
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_item( $request ) {
|
|
$response = [];
|
|
if ( $request['id'] > 0 ) {
|
|
$post = get_post( $request['id'] );
|
|
if ( $post && $this->post_type == 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 );
|
|
}
|
|
|
|
/**
|
|
* 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 ) {
|
|
$ouput = [];
|
|
$ouput['info']['id'] = $agent_info->ID;
|
|
$ouput['info']['slug'] = $agent_info->post_name;
|
|
$ouput['info']['title'] = $agent_info->post_title;
|
|
$ouput['info']['create_date'] = $agent_info->post_date;
|
|
$ouput['info']['modified_date'] = $agent_info->post_modified;
|
|
$ouput['info']['status'] = $agent_info->post_status;
|
|
$ouput['info']['link'] = html_entity_decode( $agent_info->guid );
|
|
$ouput['info']['content'] = $agent_info->post_content;
|
|
$ouput['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agent_info->ID ) );
|
|
|
|
$agent = new OpalEstate_Agent( $agent_info->ID );
|
|
|
|
$ouput['info']['avatar'] = $agent->get_meta( 'avatar' );
|
|
$ouput['info']['featured'] = $agent->is_featured();
|
|
$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 : [];
|
|
$ouput['socials'] = $agent->get_socials();
|
|
$ouput['levels'] = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_level' );
|
|
|
|
return apply_filters( 'opalestate_api_agents', $ouput );
|
|
}
|
|
}
|