Opal-Estate-Pro/inc/api/v1/agency.php

191 lines
5.1 KiB
PHP
Raw Normal View History

2019-09-28 11:39:55 +02:00
<?php
/**
2019-10-02 09:14:31 +02:00
* Define
2019-09-28 11:39:55 +02:00
* Note: only use for internal purpose.
*
* @package OpalJob
* @copyright Copyright (c) 2019, WpOpal <https://www.wpopal.com>
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 1.0
*/
// 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 Job_Api
2019-09-28 11:39:55 +02:00
*
* @since 1.0.0
* @package Opal_Job
* @subpackage Opal_Job/controllers
*/
2019-10-02 09:14:31 +02:00
class Agency_Api extends 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 = '/agencies';
/**
* Post type.
*
* @var string
*/
protected $post_type = 'opalestate_agency';
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() {
2019-10-02 09:52:28 +02:00
/**
2019-10-03 12:24:12 +02:00
* Get list of agencies.
2019-10-02 09:52:28 +02:00
*
2019-10-03 12:24:12 +02:00
* Call http://domain.com/wp-json/estate-api/v1/agencies
2019-10-02 09:52:28 +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-09-28 11:39:55 +02:00
}
/**
2019-10-02 09:14:31 +02:00
* Get List Of agencies.
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 ) {
$agencies['agencies'] = [];
2019-10-02 09:14:31 +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-10-02 09:14:31 +02:00
2019-10-03 12:24:12 +02:00
$agency_list = get_posts( [
'post_type' => $this->post_type,
'posts_per_page' => $per_page,
'paged' => $paged,
'suppress_filters' => true,
] );
2019-10-02 09:14:31 +02:00
2019-10-03 12:24:12 +02:00
if ( $agency_list ) {
$i = 0;
foreach ( $agency_list as $agency_info ) {
$agencies['agencies'][ $i ] = $this->get_agency_data( $agency_info );
$i++;
2019-10-02 09:14:31 +02:00
}
}
$response['collection'] = $agencies['agencies'];
2019-09-28 11:39:55 +02:00
return $this->get_response( 200, $response );
}
2019-10-02 09:14:31 +02:00
/**
* Get Agency
*
* Based on request to get a agency.
*
* @return WP_REST_Response is json data
* @since 1.0
*
*/
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
$agency = $this->get_agency_data( $post );
$response['agency'] = $agency ? $agency : [];
2019-10-03 12:24:12 +02:00
$code = 200;
2019-10-02 09:14:31 +02:00
} else {
2019-10-03 12:24:12 +02:00
$code = 404;
2019-10-02 09:14:31 +02:00
$response['error'] = sprintf( esc_html__( 'Agency ID: %s does not exist!', 'opalestate-pro' ), $request['id'] );
}
} else {
2019-10-03 12:24:12 +02:00
$code = 404;
2019-10-02 09:14:31 +02:00
$response['error'] = sprintf( esc_html__( 'Invalid ID.', 'opalestate-pro' ), $request['id'] );
}
return $this->get_response( $code, $response );
}
/**
* The opalestate_agency post object, generate the data for the API output
*
* @param object $agency_info The Download Post Object
*
* @return array Array of post data to return back in the API
* @since 1.0
*
*/
public function get_agency_data( $agency_info ) {
$ouput = [];
$ouput['info']['id'] = $agency_info->ID;
$ouput['info']['slug'] = $agency_info->post_name;
$ouput['info']['title'] = $agency_info->post_title;
$ouput['info']['create_date'] = $agency_info->post_date;
$ouput['info']['modified_date'] = $agency_info->post_modified;
$ouput['info']['status'] = $agency_info->post_status;
$ouput['info']['link'] = html_entity_decode( $agency_info->guid );
$ouput['info']['content'] = $agency_info->post_content;
$ouput['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agency_info->ID ) );
$agency = new OpalEstate_Agency( $agency_info->ID );
$ouput['info']['featured'] = (int) $agency->is_featured();
$ouput['info']['email'] = get_post_meta( $agency_info->ID, OPALESTATE_AGENCY_PREFIX . 'email', true );
$ouput['info']['address'] = get_post_meta( $agency_info->ID, OPALESTATE_AGENCY_PREFIX . 'address', true );
$terms = wp_get_post_terms( $agency_info->ID, 'opalestate_agency_location' );
$ouput['info']['location'] = $terms && ! is_wp_error( $terms ) ? $terms : [];
$ouput['socials'] = $agency->get_socials();
return apply_filters( 'opalestate_api_agencies', $ouput );
}
}