PluginProbe
Property Hive / 1.4.59
Property Hive v1.4.59
2.3.1 2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 All 261 releases
propertyhive / includes / class-ph-rest-api.php

class-ph-rest-api.php in Property Hive 1.4.59, at includes/class-ph-rest-api.php

173 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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&#8217; 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&#8217; 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 $PH_Query = new PH_Query();
72
73 // Meta query
74 $args['meta_query'] = $PH_Query->get_meta_query();
75
76 // Tax query
77 $args['tax_query'] = $PH_Query->get_tax_query();
78
79 // Date query
80 $args['date_query'] = $PH_Query->get_date_query();
81
82 $args = apply_filters( 'propertyhive_rest_api_query_args', $args );
83
84 return $args;
85 }
86
87 public function register_rest_api_property_fields()
88 {
89 $field_array = array(
90 'department',
91 'latitude',
92 'longitude',
93 'price',
94 'price_formatted',
95 'currency',
96 'price_qualifier',
97 'sale_by',
98 'tenure',
99 'deposit',
100 'furnished',
101 'available_date',
102 'bedrooms',
103 'bathrooms',
104 'reception_rooms',
105 'property_type',
106 'parking',
107 'outside_space',
108 'on_market',
109 'featured',
110 'availability',
111 'marketing_flags',
112 'features',
113 'description',
114 'office',
115 'virtual_tours',
116 );
117
118 $field_array = apply_filters( 'propertyhive_rest_api_property_fields', $field_array );
119
120 foreach ( $field_array as $field )
121 {
122 register_rest_field( 'property',
123 $field,
124 array(
125 'get_callback' => function( $object, $field_name, $request )
126 {
127 $property = new PH_Property($object[ 'id' ]);
128
129 switch ($field_name)
130 {
131 case "price":
132 {
133 if ( $property->_poa != 'yes' )
134 {
135 if ( $property->_department == 'residential-lettings' ) { return $property->_rent; }else{ return $property->_price; }
136 }
137
138 return '';
139 }
140 case "price_formatted": { return $property->get_formatted_price(); break; }
141 case "features": { return $property->get_features(); break; }
142 case "description": { return $property->get_formatted_description(); break; }
143 case "office":
144 {
145 return array(
146 'name' => $property->office_name,
147 'address' => $property->office_address,
148 'telephone_number' => $property->office_telephone_number,
149 'email_address' => $property->office_email_address,
150 );
151 break;
152 }
153 case "virtual_tours":
154 {
155 return $property->get_virtual_tours();
156 break;
157 }
158 default:
159 {
160 return $property->{$field_name};
161 }
162 }
163 },
164 'update_callback' => null,
165 'schema' => null,
166 )
167 );
168 }
169 }
170
171 }
172
173