1: <?php
2: /**
3: * EPL Rest API Class.
4: *
5: * @package EPL
6: * @subpackage Classes/RestAPI
7: * @copyright Copyright (c) 2024, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.5.8
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * EPL_Rest_API Class
19: *
20: * @since 3.5.8
21: */
22: if ( ! class_exists( 'EPL_Rest_API' ) ) :
23:
24: /**
25: * EPL_Rest_API Class
26: *
27: * @since 3.5.8
28: */
29: class EPL_Rest_API {
30:
31: /**
32: * Instance
33: *
34: * @var EPL_Rest_API instance
35: */
36: private static $instance;
37:
38: /**
39: * Insures that only one instance of EPL_Rest_API exists in memory.
40: *
41: * @return EPL_Rest_API instance
42: * @static
43: * @staticvar array $instance
44: *
45: * @since 3.5.8
46: */
47: public static function instance() {
48: if ( ! isset( self::$instance ) && ! ( self::$instance instanceof EPL_Rest_API ) ) {
49: self::$instance = new EPL_Rest_API();
50: self::$instance->hooks();
51: }
52: return self::$instance;
53: }
54:
55: /**
56: * Add hooks to Rest API.
57: *
58: * @since 3.5.8
59: */
60: public function hooks() {
61: foreach ( epl_get_core_post_types() as $post_type ) {
62: add_filter( 'rest_prepare_' . $post_type, array( $this, 'meta_to_rest_api' ), 10, 3 );
63: }
64: }
65:
66: /**
67: * Add EPL Meta data to Rest API.
68: *
69: * @param array $response The response.
70: * @param array $post The post.
71: * @param array $request The request.
72: *
73: * @return WP_REST_Response $response
74: *
75: * @since 3.5.8
76: * @since 3.5.10 Fix error when group fields are empty.
77: * @since 3.5.27 Hidden and internal fields are only returned to users who can edit the listing.
78: * @since 3.5.28 Users who cannot edit the listing only receive public fields the listing displays.
79: */
80: public function meta_to_rest_api( $response, $post, $request ) {
81:
82: if ( ! is_epl_post( $post->post_type ) ) {
83: return $response;
84: }
85:
86: $meta_field_sections = epl_get_meta_boxes();
87:
88: if ( ! empty( $meta_field_sections ) ) {
89:
90: foreach ( $meta_field_sections as $epl_meta_box ) {
91:
92: $section_post_types = (array) $epl_meta_box['post_type'];
93:
94: if ( ! in_array( $post->post_type, $section_post_types, true ) ) {
95: continue;
96: }
97:
98: if ( ! empty( $epl_meta_box['groups'] ) ) {
99:
100: foreach ( $epl_meta_box['groups'] as $group ) {
101:
102: if ( isset( $group['fields'] ) && is_array( $group['fields'] ) ) {
103: $fields = $group['fields'];
104: $fields = array_filter( $fields );
105:
106: } else {
107: $fields = array();
108: }
109:
110: if ( ! empty( $fields ) ) {
111:
112: foreach ( $fields as $field ) {
113: if ( isset( $field['exclude'] ) && ! empty( $field['exclude'] ) ) {
114: if ( in_array( $post->post_type, $field['exclude'], true ) ) {
115: continue;
116: }
117: }
118:
119: if ( isset( $field['include'] ) && ! empty( $field['include'] ) ) {
120: if ( ! in_array( $post->post_type, $field['include'], true ) ) {
121: continue;
122: }
123: }
124: $response->data['meta'][ $field['name'] ] = get_post_meta( $post->ID, $field['name'], true );
125: }
126: }
127: }
128: }
129: }
130: }
131:
132: if ( ! empty( $response->data['meta'] ) && is_array( $response->data['meta'] ) && ! current_user_can( 'edit_post', $post->ID ) ) {
133: $response->data['meta'] = array_intersect_key(
134: $response->data['meta'],
135: array_flip( $this->get_public_meta_keys( $post ) )
136: );
137: }
138:
139: return $response;
140: }
141:
142: /**
143: * Meta keys returned to users who cannot edit the listing.
144: *
145: * Only fields the listing displays on the front end are allowed. Any other field, including
146: * fields added by extensions, is withheld unless added with the epl_rest_api_public_meta_keys
147: * filter. Fields are then removed when the listing or the site settings hide them.
148: *
149: * @param WP_Post $post The post.
150: *
151: * @return array Meta keys.
152: *
153: * @since 3.5.28
154: */
155: public function get_public_meta_keys( $post ) {
156:
157: $keys = array(
158: // Listing details.
159: 'property_heading',
160: 'property_status',
161: 'property_authority',
162: 'property_unique_id',
163: 'property_category',
164: 'property_rural_category',
165: 'property_land_category',
166: 'property_com_authority',
167: 'property_com_listing_type',
168: 'property_commercial_category',
169: 'property_inspection_times',
170: 'property_featured',
171: 'property_under_offer',
172: 'property_auction',
173: 'property_sold_date',
174:
175: // Agents.
176: 'property_agent',
177: 'property_second_agent',
178: 'property_third_agent',
179: 'property_fourth_agent',
180: 'property_agent_hide_author_box',
181:
182: // Features.
183: 'property_bedrooms',
184: 'property_bathrooms',
185: 'property_toilet',
186: 'property_ensuite',
187: 'property_garage',
188: 'property_carport',
189: 'property_open_spaces',
190: 'property_rooms',
191: 'property_year_built',
192: 'property_new_construction',
193: 'property_pool',
194: 'property_air_conditioning',
195: 'property_security_system',
196: 'property_pet_friendly',
197: 'property_broadband',
198: 'property_built_in_robes',
199: 'property_dishwasher',
200: 'property_floor_boards',
201: 'property_gym',
202: 'property_intercom',
203: 'property_pay_tv',
204: 'property_remote_garage',
205: 'property_rumpus_room',
206: 'property_secure_parking',
207: 'property_spa',
208: 'property_study',
209: 'property_vacuum_system',
210: 'property_workshop',
211: 'property_balcony',
212: 'property_courtyard',
213: 'property_deck',
214: 'property_outdoor_entertaining',
215: 'property_shed',
216: 'property_tennis_court',
217: 'property_ducted_cooling',
218: 'property_ducted_heating',
219: 'property_evaporative_cooling',
220: 'property_gas_heating',
221: 'property_hydronic_heating',
222: 'property_open_fire_place',
223: 'property_reverse_cycle_aircon',
224: 'property_split_system_aircon',
225: 'property_split_system_heating',
226:
227: // Land.
228: 'property_land_area',
229: 'property_land_area_unit',
230: 'property_building_area',
231: 'property_building_area_unit',
232: 'property_energy_rating',
233: 'property_land_fully_fenced',
234:
235: // Address.
236: 'property_address_display',
237: 'property_address_hide_map',
238: 'property_com_display_suburb',
239: 'property_address_lot_number',
240: 'property_address_sub_number',
241: 'property_address_street_number',
242: 'property_address_street',
243: 'property_address_suburb',
244: 'property_address_city',
245: 'property_address_state',
246: 'property_address_postal_code',
247: 'property_address_country',
248: 'property_address_coordinates',
249:
250: // Pricing.
251: 'property_price_display',
252: 'property_price',
253: 'property_price_view',
254: 'property_sold_price_display',
255: 'property_sold_price',
256: 'property_rent_display',
257: 'property_rent',
258: 'property_rent_period',
259: 'property_rent_view',
260: 'property_bond',
261: 'property_date_available',
262: 'property_furnished',
263: 'property_holiday_rental',
264:
265: // Rural.
266: 'property_rural_fencing',
267: 'property_rural_annual_rainfall',
268: 'property_rural_soil_types',
269: 'property_rural_improvements',
270: 'property_rural_council_rates',
271: 'property_rural_irrigation',
272: 'property_rural_carrying_capacity',
273:
274: // Commercial.
275: 'property_com_rent',
276: 'property_com_rent_period',
277: 'property_com_plus_outgoings',
278: 'property_com_further_options',
279: 'property_com_zone',
280: 'property_com_car_spaces',
281: 'property_com_highlight_1',
282: 'property_com_highlight_2',
283: 'property_com_highlight_3',
284: 'property_com_parking_comments',
285:
286: // Files and links.
287: 'property_video_url',
288: 'property_external_link',
289: 'property_external_link_label',
290: 'property_external_link_2',
291: 'property_external_link_2_label',
292: 'property_external_link_3',
293: 'property_external_link_3_label',
294: 'property_com_mini_web',
295: 'property_com_mini_web_2',
296: 'property_com_mini_web_3',
297: 'property_floorplan',
298: 'property_floorplan_label',
299: 'property_floorplan_2',
300: 'property_floorplan_2_label',
301: 'property_energy_certificate',
302: 'property_energy_certificate_label',
303: );
304:
305: $keys = (array) apply_filters( 'epl_rest_api_public_meta_keys', $keys, $post );
306: $hidden = array();
307: $status = get_post_meta( $post->ID, 'property_status', true );
308:
309: $show_price = 'yes' === get_post_meta( $post->ID, 'property_price_display', true ) && ! in_array( $status, array( 'sold', 'leased' ), true );
310: $show_sold = 'yes' === get_post_meta( $post->ID, 'property_sold_price_display', true ) && 'sold' === $status;
311: $show_rent = 'yes' === get_post_meta( $post->ID, 'property_rent_display', true ) && 'leased' !== $status;
312: $show_bond = $show_rent && in_array( epl_get_option( 'display_bond' ), array( 1, '1', 'yes' ), true );
313: $show_addr = 'yes' === get_post_meta( $post->ID, 'property_address_display', true );
314:
315: if ( ! $show_price ) {
316: $hidden = array_merge( $hidden, array( 'property_price', 'property_price_view', 'property_com_rent', 'property_com_rent_period' ) );
317: }
318:
319: if ( ! $show_sold ) {
320: $hidden[] = 'property_sold_price';
321: }
322:
323: if ( ! $show_rent ) {
324: $hidden = array_merge( $hidden, array( 'property_rent', 'property_rent_view', 'property_rent_period' ) );
325: }
326:
327: if ( ! $show_bond ) {
328: $hidden[] = 'property_bond';
329: }
330:
331: if ( ! $show_addr ) {
332: $hidden = array_merge(
333: $hidden,
334: array(
335: 'property_address_lot_number',
336: 'property_address_sub_number',
337: 'property_address_street_number',
338: 'property_address_street',
339: 'property_address_coordinates',
340: )
341: );
342:
343: if ( in_array( $post->post_type, array( 'commercial', 'commercial_land', 'business' ), true ) && 'yes' !== get_post_meta( $post->ID, 'property_com_display_suburb', true ) ) {
344: $hidden[] = 'property_address_suburb';
345: }
346: }
347:
348: if ( 'yes' === get_post_meta( $post->ID, 'property_address_hide_map', true ) ) {
349: $hidden[] = 'property_address_coordinates';
350: }
351:
352: if ( 'yes' !== epl_get_option( 'epl_enable_city_field' ) ) {
353: $hidden[] = 'property_address_city';
354: }
355:
356: if ( 'yes' !== epl_get_option( 'epl_enable_country_field' ) ) {
357: $hidden[] = 'property_address_country';
358: }
359:
360: $hidden = (array) apply_filters( 'epl_rest_api_private_meta_keys', $hidden, $post );
361:
362: return array_values( array_diff( array_unique( $keys ), $hidden ) );
363: }
364: }
365: endif;
366:
367: EPL_Rest_API::instance();
368: