| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Serve properties using the REST API |
| 9 |
* |
| 10 |
* @class PH_Rest_Api |
| 11 |
* @version 1.0.0 |
| 12 |
* @package PropertyHive/Classes/ |
| 13 |
* @category Class |
| 14 |
* @author PropertyHive |
| 15 |
*/ |
| 16 |
class PH_Rest_Api { |
| 17 |
|
| 18 |
/** @var PH_Rest_Api The single instance of the class */ |
| 19 |
protected static $_instance = null; |
| 20 |
|
| 21 |
/** |
| 22 |
* Main PH_Rest_Api Instance. |
| 23 |
* |
| 24 |
* Ensures only one instance of PH_Rest_Api is loaded or can be loaded. |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
* @static |
| 28 |
* @return PH_Licenses Main instance |
| 29 |
*/ |
| 30 |
public static function instance() { |
| 31 |
if ( is_null( self::$_instance ) ) { |
| 32 |
self::$_instance = new self(); |
| 33 |
} |
| 34 |
return self::$_instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Cloning is forbidden. |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
*/ |
| 42 |
public function __clone() { |
| 43 |
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'propertyhive' ), '1.0.0' ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Unserializing instances of this class is forbidden. |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
*/ |
| 51 |
public function __wakeup() { |
| 52 |
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'propertyhive' ), '1.0.0' ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Constructor for the licenses class |
| 57 |
* |
| 58 |
*/ |
| 59 |
public function __construct() { |
| 60 |
add_filter( 'rest_property_query', array( $this, 'modify_rest_property_query' ), 10, 2 ); |
| 61 |
add_action( 'rest_api_init', array( $this, 'register_rest_api_property_fields' ), 99 ); |
| 62 |
} |
| 63 |
|
| 64 |
public function modify_rest_property_query($args, $request) |
| 65 |
{ |
| 66 |
if ( !isset( $args['meta_query'] ) ) |
| 67 |
{ |
| 68 |
$args['meta_query'] = array(); |
| 69 |
} |
| 70 |
|
| 71 |
$args['meta_query'][] = array( |
| 72 |
'key' => '_on_market', |
| 73 |
'value' => 'yes' |
| 74 |
); |
| 75 |
|
| 76 |
$args = apply_filters( 'propertyhive_rest_api_query_args', $args ); |
| 77 |
|
| 78 |
return $args; |
| 79 |
} |
| 80 |
|
| 81 |
public function register_rest_api_property_fields() |
| 82 |
{ |
| 83 |
$field_array = array( |
| 84 |
'department', |
| 85 |
'latitude', |
| 86 |
'longitude', |
| 87 |
'price', |
| 88 |
'price_formatted', |
| 89 |
'currency', |
| 90 |
'price_qualifier', |
| 91 |
'sale_by', |
| 92 |
'tenure', |
| 93 |
'deposit', |
| 94 |
'furnished', |
| 95 |
'available_date', |
| 96 |
'bedrooms', |
| 97 |
'bathrooms', |
| 98 |
'reception_rooms', |
| 99 |
'property_type', |
| 100 |
'parking', |
| 101 |
'outside_space', |
| 102 |
'on_market', |
| 103 |
'featured', |
| 104 |
'availability', |
| 105 |
'marketing_flags', |
| 106 |
'features', |
| 107 |
'description', |
| 108 |
'office', |
| 109 |
); |
| 110 |
|
| 111 |
$field_array = apply_filters( 'propertyhive_rest_api_property_fields', $field_array ); |
| 112 |
|
| 113 |
foreach ( $field_array as $field ) |
| 114 |
{ |
| 115 |
register_rest_field( 'property', |
| 116 |
$field, |
| 117 |
array( |
| 118 |
'get_callback' => function( $object, $field_name, $request ) |
| 119 |
{ |
| 120 |
$property = new PH_Property($object[ 'id' ]); |
| 121 |
|
| 122 |
switch ($field_name) |
| 123 |
{ |
| 124 |
case "price": |
| 125 |
{ |
| 126 |
if ( $property->_poa != 'yes' ) |
| 127 |
{ |
| 128 |
if ( $property->_department == 'residential-lettings' ) { return $property->_rent; }else{ return $property->_price; } |
| 129 |
} |
| 130 |
|
| 131 |
return ''; |
| 132 |
} |
| 133 |
case "price_formatted": { return $property->get_formatted_price(); break; } |
| 134 |
case "features": { return $property->get_features(); break; } |
| 135 |
case "description": { return $property->get_formatted_description(); break; } |
| 136 |
case "office": |
| 137 |
{ |
| 138 |
return array( |
| 139 |
'name' => $property->office_name, |
| 140 |
'address' => $property->office_address, |
| 141 |
'telephone_number' => $property->office_telephone_number, |
| 142 |
'email_address' => $property->office_email_address, |
| 143 |
); |
| 144 |
break; |
| 145 |
} |
| 146 |
default: |
| 147 |
{ |
| 148 |
return $property->{$field_name}; |
| 149 |
} |
| 150 |
} |
| 151 |
}, |
| 152 |
'update_callback' => null, |
| 153 |
'schema' => null, |
| 154 |
) |
| 155 |
); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
|