PluginProbe
Property Hive / 1.4.6
Property Hive v1.4.6
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.6, at includes/class-ph-rest-api.php

159 lines 3.6 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 $args['meta_query'][] = array(
72 'key' => '_on_market',
73 'value' => 'yes'
74 );
75
76 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 foreach ( $field_array as $field )
112 {
113 register_rest_field( 'property',
114 $field,
115 array(
116 'get_callback' => function( $object, $field_name, $request )
117 {
118 $property = new PH_Property($object[ 'id' ]);
119
120 switch ($field_name)
121 {
122 case "price":
123 {
124 if ( $property->poa != 'yes' )
125 {
126 if ( $property->department == 'residential-lettings' ) { return $property->rent; }else{ return $property->price; }
127 }
128
129 return '';
130 }
131 case "price_formatted": { return $property->get_formatted_price(); break; }
132 case "features": { return $property->get_features(); break; }
133 case "description": { return $property->get_formatted_description(); break; }
134 case "office":
135 {
136 return array(
137 'name' => $property->office_name,
138 'address' => $property->office_address,
139 'telephone_number' => $property->office_telephone_number,
140 'email_address' => $property->office_email_address,
141 );
142 break;
143 }
144 default:
145 {
146 return $property->{$field_name};
147 }
148 }
149 },
150 'update_callback' => null,
151 'schema' => null,
152 )
153 );
154 }
155 }
156
157 }
158
159