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

328 lines
8.4 KiB
PHP
Raw Normal View History

2019-09-28 11:39:55 +02:00
<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
2019-10-01 09:00:06 +02:00
2019-09-28 11:39:55 +02:00
/**
2019-10-01 09:00:06 +02:00
* Property_Api
2019-09-28 11:39:55 +02:00
*
* @since 1.0.0
2019-10-01 09:00:06 +02:00
* @package Property_Api
2019-09-28 11:39:55 +02:00
*/
2019-10-01 09:00:06 +02:00
class Property_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-01 09:00:06 +02:00
* @var string $base .
2019-09-28 11:39:55 +02:00
*/
public $base = '/property';
2019-10-01 09:00:06 +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
*
2019-10-01 09:00:06 +02:00
* @return avoid
2019-09-28 11:39:55 +02:00
* @since 1.0
*
*/
2019-10-01 09:00:06 +02:00
public function register_routes() {
2019-10-02 09:14:31 +02:00
/**
* Get list of properties.
*
* Call http://domain.com/wp-json/estate-api/v1/property/list
*/
2019-10-01 09:00:06 +02:00
register_rest_route( $this->namespace, $this->base . '/list', [
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_list' ],
'permission_callback' => [ $this, 'validate_request' ],
] );
2019-10-02 09:14:31 +02:00
/**
* Get list of featured properties.
*
* Call http://domain.com/wp-json/estate-api/v1/property/featured
*/
2019-10-01 09:00:06 +02:00
register_rest_route( $this->namespace, $this->base . '/featured', [
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_featured_list' ],
'permission_callback' => [ $this, 'validate_request' ],
] );
2019-09-28 11:39:55 +02:00
2019-10-02 09:14:31 +02:00
/**
* Get property detail.
*
* Call http://domain.com/wp-json/estate-api/v1/property/1
*/
2019-10-01 09:00:06 +02:00
register_rest_route( $this->namespace, $this->base . '/(?P<id>\d+)', [
'methods' => WP_REST_Server::READABLE,
2019-10-02 09:14:31 +02:00
'callback' => [ $this, 'get_detail' ],
2019-10-01 09:00:06 +02:00
'permission_callback' => [ $this, 'validate_request' ],
] );
2019-09-28 11:39:55 +02:00
2019-10-02 09:14:31 +02:00
/**
* Create a property.
*
* Call http://domain.com/wp-json/estate-api/v1/property/create
*/
2019-10-01 09:00:06 +02:00
register_rest_route( $this->namespace, $this->base . '/create', [
'methods' => 'GET',
'callback' => [ $this, 'create' ],
'permission_callback' => [ $this, 'validate_request' ],
] );
2019-10-02 09:14:31 +02:00
/**
* Edit a property.
*
* Call http://domain.com/wp-json/estate-api/v1/property/edit
*/
2019-10-01 09:00:06 +02:00
register_rest_route( $this->namespace, $this->base . '/edit', [
'methods' => 'GET',
'callback' => [ $this, 'edit' ],
] );
2019-10-02 09:14:31 +02:00
/**
* Delete a property.
*
* Call http://domain.com/wp-json/estate-api/v1/property/delete
*/
2019-10-01 09:00:06 +02:00
register_rest_route( $this->namespace, $this->base . '/delete', [
'methods' => 'GET',
'callback' => [ $this, 'delete' ],
'permission_callback' => [ $this, 'validate_request' ],
] );
2019-09-28 11:39:55 +02:00
/**
2019-10-02 09:14:31 +02:00
* List property tags.
*
* Call http://domain.com/wp-json/estate-api/v1/property/tags
2019-09-28 11:39:55 +02:00
*/
2019-10-01 09:00:06 +02:00
register_rest_route( $this->namespace, $this->base . '/tags', [
'methods' => 'GET',
'callback' => [ $this, 'delete' ],
'permission_callback' => [ $this, 'validate_request' ],
] );
2019-09-28 11:39:55 +02:00
}
2019-10-01 09:00:06 +02:00
2019-10-02 09:14:31 +02:00
/**
* Get list of featured properties.
*
* @return array|\WP_REST_Response
*/
2019-10-01 09:00:06 +02:00
public function get_featured_list() {
$properties = [];
$error = [];
$property = null;
2019-09-28 11:39:55 +02:00
if ( $property == null ) {
2019-10-01 09:00:06 +02:00
$properties = [];
2019-09-28 11:39:55 +02:00
2019-10-01 09:00:06 +02:00
$property_list = get_posts( [
2019-09-28 11:39:55 +02:00
'post_type' => 'opalestate_property',
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
2019-10-01 09:00:06 +02:00
'meta_key' => OPALESTATE_PROPERTY_PREFIX . 'featured',
'meta_value' => 'on',
'paged' => $this->get_paged(),
] );
2019-09-28 11:39:55 +02:00
if ( $property_list ) {
$i = 0;
foreach ( $property_list as $property_info ) {
$properties[ $i ] = $this->get_property_data( $property_info );
2019-10-01 09:00:06 +02:00
$i++;
2019-09-28 11:39:55 +02:00
}
}
} 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(
2019-10-01 09:00:06 +02:00
/* translators: %s: property */
2019-09-28 11:39:55 +02:00
esc_html__( 'Form %s not found!', 'opalestate-pro' ),
$property
);
return $error;
}
}
2019-10-01 09:00:06 +02:00
$response['collection'] = $properties;
$response['pages'] = 4;
$response['current'] = 1;
2019-09-28 11:39:55 +02:00
return $this->get_response( 200, $response );
2019-10-01 09:00:06 +02:00
}
2019-09-28 11:39:55 +02:00
/**
2019-10-02 09:14:31 +02:00
* Get List Of Properties
2019-09-28 11:39:55 +02:00
*
* Based on request to get collection
*
2019-10-01 09:00:06 +02:00
* @return WP_REST_Response is json data
2019-09-28 11:39:55 +02:00
* @since 1.0
*
*/
2019-10-01 09:00:06 +02:00
public function get_list( $request ) {
$properties = [];
$error = [];
2019-10-02 09:14:31 +02:00
$property = null;
2019-09-28 11:39:55 +02:00
if ( $property == null ) {
2019-10-01 09:00:06 +02:00
$properties = [];
2019-09-28 11:39:55 +02:00
2019-10-01 09:00:06 +02:00
$property_list = get_posts( [
2019-09-28 11:39:55 +02:00
'post_type' => 'opalestate_property',
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
2019-10-01 09:00:06 +02:00
'paged' => $this->get_paged(),
] );
2019-09-28 11:39:55 +02:00
if ( $property_list ) {
$i = 0;
foreach ( $property_list as $property_info ) {
$properties[ $i ] = $this->get_property_data( $property_info );
2019-10-01 09:00:06 +02:00
$i++;
2019-09-28 11:39:55 +02:00
}
}
} 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(
2019-10-01 09:00:06 +02:00
/* translators: %s: property */
2019-09-28 11:39:55 +02:00
esc_html__( 'Form %s not found!', 'opalestate-pro' ),
$property
);
2019-10-02 09:14:31 +02:00
return $this->get_response( 404, $error );
2019-09-28 11:39:55 +02:00
}
}
2019-10-01 09:00:06 +02:00
$response['collection'] = $properties;
$response['pages'] = 4;
$response['current'] = 1;
2019-09-28 11:39:55 +02:00
return $this->get_response( 200, $response );
}
2019-10-02 08:20:44 +02:00
/**
* Get Property
*
2019-10-02 09:14:31 +02:00
* Based on request to get a property.
2019-10-02 08:20:44 +02:00
*
* @return WP_REST_Response is json data
* @since 1.0
*
*/
2019-10-02 09:14:31 +02:00
public function get_detail( $request ) {
2019-10-02 08:20:44 +02:00
$response = [];
if ( $request['id'] > 0 ) {
$post = get_post( $request['id'] );
if ( $post && 'opalestate_property' == get_post_type( $request['id'] ) ) {
2019-10-02 09:14:31 +02:00
$property = $this->get_property_data( $post );
$response['property'] = $property ? $property : [];
$code = 200;
} else {
$code = 404;
$response['error'] = sprintf( esc_html__( 'Property ID: %s does not exist!', 'opalestate-pro' ), $request['id'] );
2019-10-02 08:20:44 +02:00
}
2019-10-02 09:14:31 +02:00
} else {
$code = 404;
$response['error'] = sprintf( esc_html__( 'Invalid ID.', 'opalestate-pro' ), $request['id'] );
2019-10-02 08:20:44 +02:00
}
2019-10-02 09:14:31 +02:00
return $this->get_response( $code, $response );
2019-10-02 08:20:44 +02:00
}
2019-09-28 11:39:55 +02:00
/**
2019-10-02 09:14:31 +02:00
* The opalestate_property post object, generate the data for the API output
2019-09-28 11:39:55 +02:00
*
2019-10-01 09:00:06 +02:00
* @param object $property_info The Download Post Object
2019-09-28 11:39:55 +02:00
*
* @return array Array of post data to return back in the API
2019-10-02 09:14:31 +02:00
* @since 1.0
2019-10-01 09:00:06 +02:00
*
2019-09-28 11:39:55 +02:00
*/
private function get_property_data( $property_info ) {
2019-10-01 09:00:06 +02:00
$property = [];
2019-09-28 11:39:55 +02:00
$property['info']['id'] = $property_info->ID;
$property['info']['slug'] = $property_info->post_name;
$property['info']['title'] = $property_info->post_title;
$property['info']['create_date'] = $property_info->post_date;
$property['info']['modified_date'] = $property_info->post_modified;
$property['info']['status'] = $property_info->post_status;
$property['info']['link'] = html_entity_decode( $property_info->guid );
$property['info']['content'] = $property_info->post_content;
$property['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $property_info->ID ) );
2019-10-02 09:36:24 +02:00
$data = opalesetate_property( $property_info->ID );
$gallery = $data->get_gallery();
$gallery_count = $data->get_gallery_count();
$gallery_data = [];
if ( $gallery_count ) {
foreach ( $gallery as $id => $url ) {
$gallery_data[] = [
'id' => $id,
'url' => $url,
];
}
}
$property['info']['gallery'] = $gallery_data;
2019-10-01 09:00:06 +02:00
$property['info']['price'] = opalestate_price_format( $data->get_price() );
$property['info']['map'] = $data->get_map();
2019-09-28 11:39:55 +02:00
$property['info']['address'] = $data->get_address();
2019-10-01 09:00:06 +02:00
$property['meta'] = $data->get_meta_shortinfo();
$property['is_featured'] = $data->is_featured();
$property['status'] = $data->get_status();
$property['labels'] = $data->get_labels();
$property['locations'] = $data->get_locations();
$property['amenities'] = $data->get_amenities();
$property['types'] = $data->get_types_tax();
$property['author_type'] = $data->get_author_type();
2019-10-01 10:57:58 +02:00
$property['author_data'] = $data->get_author_link_data();
2019-10-01 09:03:48 +02:00
2019-09-28 11:39:55 +02:00
return apply_filters( 'opalestate_api_properties_property', $property );
}
/**
* Delete job
*
* Based on request to get collection
*
2019-10-01 09:00:06 +02:00
* @return WP_REST_Response is json data
2019-09-28 11:39:55 +02:00
* @since 1.0
*
*/
2019-10-01 09:00:06 +02:00
public function delete() {
2019-09-28 11:39:55 +02:00
}
2019-10-01 09:00:06 +02:00
public function reviews() {
2019-09-28 11:39:55 +02:00
}
2019-10-01 09:00:06 +02:00
public function categories() {
2019-09-28 11:39:55 +02:00
}
2019-10-01 09:00:06 +02:00
public function tags() {
2019-09-28 11:39:55 +02:00
}
2019-10-01 04:08:40 +02:00
}