1: <?php
2: /**
3: * Conditional Tags
4: *
5: * @package EPL
6: * @subpackage Functions/ConditionalTags
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 2.2
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * Get list of core epl posts
19: *
20: * @since 2.3
21: */
22: function epl_get_core_post_types() {
23: return apply_filters( 'epl_core_post_types', array( 'rural', 'property', 'rental', 'land', 'commercial', 'commercial_land', 'business' ) );
24: }
25:
26: /**
27: * Get list of sales epl posts
28: *
29: * @since 2.3
30: */
31: function epl_get_core_sales_post_types() {
32: return apply_filters( 'epl_core_sales_post_types', array( 'rural', 'property', 'land', 'commercial', 'commercial_land', 'business' ) );
33: }
34:
35: /**
36: * Get list of rental epl posts
37: *
38: * @since 2.3
39: */
40: function epl_get_core_rental_post_types() {
41: return apply_filters( 'epl_core_rental_post_types', array( 'rental' ) );
42: }
43:
44: /**
45: * List of all epl custom post types
46: *
47: * @since 2.3
48: */
49: function epl_all_post_types() {
50: $epl_posts = epl_get_active_post_types();
51: $epl_posts = array_keys( $epl_posts );
52: return apply_filters( 'epl_additional_post_types', $epl_posts );
53: }
54:
55: /**
56: * Check if post is core epl post
57: *
58: * @param string $type EPL post type.
59: *
60: * @return bool
61: * @since 2.3
62: */
63: function is_epl_core_post( $type = null ) {
64: $type = is_null( $type ) ? get_post_type() : $type;
65: return in_array( $type, epl_get_core_post_types(), true );
66: }
67:
68: /**
69: * Check if post is sales post
70: *
71: * @param string $type EPL post type.
72: *
73: * @return bool
74: * @since 2.3
75: */
76: function is_epl_sales_post( $type = null ) {
77: $type = is_null( $type ) ? get_post_type() : $type;
78: return in_array( $type, epl_get_core_sales_post_types(), true );
79: }
80:
81: /**
82: * Check if post is rental post
83: *
84: * @param string $type EPL post type.
85: *
86: * @return bool
87: * @since 2.3
88: */
89: function is_epl_rental_post( $type = null ) {
90: $type = is_null( $type ) ? get_post_type() : $type;
91: return in_array( $type, epl_get_core_rental_post_types(), true );
92: }
93:
94: /**
95: * Check if current post is of epl
96: *
97: * @param string $type EPL post type.
98: *
99: * @return bool
100: * @since 2.2
101: */
102: function is_epl_post( $type = null ) {
103: $all_types = epl_all_post_types();
104: if ( empty( $all_types ) ) {
105: return false;
106: }
107:
108: $type = null === $type ? get_post_type() : $type;
109: return in_array( $type, epl_all_post_types(), true );
110: }
111:
112: /**
113: * Check if viewing a single post of epl
114: *
115: * @since 2.2
116: */
117: function is_epl_post_single() {
118:
119: $all_types = epl_all_post_types();
120: if ( empty( $all_types ) ) {
121: return false;
122: }
123:
124: return is_singular( epl_all_post_types() );
125: }
126:
127: /**
128: * Check if cpt is from epl
129: *
130: * @param string $type EPL post type.
131: *
132: * @return bool
133: * @since 2.2
134: */
135: function is_epl_post_type( $type ) {
136:
137: $all_types = epl_all_post_types();
138: if ( empty( $all_types ) ) {
139: return false;
140: }
141:
142: return ( in_array( $type, epl_all_post_types(), true ) && get_post_type() === $type );
143: }
144:
145: /**
146: * Check if current post is of epl
147: *
148: * @since 2.2
149: */
150: function is_epl_post_archive() {
151:
152: $all_types = epl_all_post_types();
153: if ( empty( $all_types ) ) {
154: return false;
155: }
156:
157: return is_post_type_archive( epl_all_post_types() );
158: }
159:
160: /**
161: * Same as epl_listing_has_secondary_agent, kept for backward compatibility
162: *
163: * @return bool|int [type] [description]
164: */
165: function epl_listing_has_secondary_author() {
166: return epl_listing_has_secondary_agent();
167: }
168:
169: /**
170: * Check if listing has secondary agent
171: *
172: * @since 3.2
173: */
174: function epl_listing_has_secondary_agent() {
175: return epl_listing_is_agent_available( 'property_second_agent' );
176: }
177:
178: /**
179: * Check if listing has third agent
180: *
181: * @since 3.4.38
182: */
183: function epl_listing_has_third_agent() {
184: return epl_listing_is_agent_available( 'property_third_agent' );
185: }
186:
187: /**
188: * Check if listing has fourth agent
189: *
190: * @since 3.4.38
191: */
192: function epl_listing_has_fourth_agent() {
193: return epl_listing_is_agent_available( 'property_fourth_agent' );
194: }
195:
196: /**
197: * Check if agent exists
198: *
199: * @param string $agent_key Agent Key.
200: *
201: * @return false|int $agent_author Return an array.
202: *
203: * @since 3.4.38
204: */
205: function epl_listing_is_agent_available( $agent_key = 'property_second_agent' ) {
206: $exists = false;
207: $agent = get_property_meta( $agent_key );
208: if ( ! empty( $agent ) ) {
209: $agent_author = get_user_by( 'login', sanitize_user( $agent ) );
210: if ( false !== $agent_author ) {
211: $exists = $agent_author->ID;
212: }
213: }
214: return $exists;
215: }
216:
217: /**
218: * Check if listing has primary agent
219: *
220: * @since 3.2
221: */
222: function epl_listing_has_primary_agent() {
223: $exists = false;
224: $property_agent = get_property_meta( 'property_agent' );
225: if ( ! empty( $property_agent ) ) {
226: $author = get_user_by( 'login', sanitize_user( $property_agent ) );
227: if ( false !== $author ) {
228: $exists = $author->ID;
229: }
230: }
231: return $exists;
232: }
233:
234: /**
235: * Check if the current listing has a value in any of the meta keys
236: *
237: * A file field saved as an array counts when its image_url_or_path is set. With $respect_show,
238: * a key whose epl_show_{$key} filter returns false is skipped, as the listing buttons do.
239: *
240: * @param array $keys Meta keys.
241: * @param bool $respect_show Skip keys hidden with the epl_show_{$key} filter.
242: *
243: * @return bool
244: *
245: * @since 3.6
246: */
247: function epl_listing_has_any_meta( $keys = array(), $respect_show = false ) {
248: global $property;
249:
250: if ( ! $property instanceof EPL_Property_Meta ) {
251: return false;
252: }
253:
254: foreach ( (array) $keys as $key ) {
255: if ( $respect_show && ! apply_filters( 'epl_show_' . $key, true ) ) {
256: continue;
257: }
258:
259: $value = $property->get_property_meta( $key );
260:
261: if ( is_array( $value ) ) {
262: $value = isset( $value['image_url_or_path'] ) ? $value['image_url_or_path'] : '';
263: }
264:
265: if ( is_string( $value ) ) {
266: $value = trim( $value );
267: }
268:
269: if ( ! empty( $value ) ) {
270: return true;
271: }
272: }
273:
274: return false;
275: }
276:
277: /**
278: * Check if the listing has a floor plan
279: *
280: * Uses the same keys as the Floor Plan button (epl_floorplan_keys filter).
281: *
282: * @return bool
283: *
284: * @since 3.6
285: */
286: function epl_listing_has_floor_plans() {
287: $keys = apply_filters( 'epl_floorplan_keys', array( 'property_floorplan', 'property_floorplan_2' ) );
288:
289: return (bool) apply_filters( 'epl_listing_has_floor_plans', epl_listing_has_any_meta( $keys ) );
290: }
291:
292: /**
293: * Check if the listing has a video URL
294: *
295: * @return bool
296: *
297: * @since 3.6
298: */
299: function epl_listing_has_video() {
300: return (bool) apply_filters( 'epl_listing_has_video', epl_listing_has_any_meta( array( 'property_video_url' ) ) );
301: }
302:
303: /**
304: * Check if the listing has upcoming inspection times
305: *
306: * Past times and sold or leased listings don't count, as in get_property_inspection_times().
307: *
308: * @return bool
309: *
310: * @since 3.6
311: */
312: function epl_listing_has_inspection_times() {
313: global $property;
314:
315: $has = false;
316:
317: if ( $property instanceof EPL_Property_Meta ) {
318: $times = $property->get_property_inspection_times();
319: $has = is_string( $times ) && '' !== trim( wp_strip_all_tags( $times ) );
320: }
321:
322: return (bool) apply_filters( 'epl_listing_has_inspection_times', $has );
323: }
324:
325: /**
326: * Check if the listing has an auction date
327: *
328: * @return bool
329: *
330: * @since 3.6
331: */
332: function epl_listing_has_auction_date() {
333: global $property;
334:
335: $has = false;
336:
337: if ( $property instanceof EPL_Property_Meta ) {
338: $auction = $property->get_property_auction();
339: $has = is_string( $auction ) && '' !== trim( $auction );
340: }
341:
342: return (bool) apply_filters( 'epl_listing_has_auction_date', $has );
343: }
344:
345: /**
346: * Check if the listing has any button link
347: *
348: * External links, floor plans, mini web, energy certificate (the keys and epl_show_{$key}
349: * filters the single listing buttons use) and the video URL. Use it to skip an empty
350: * buttons wrapper in a template.
351: *
352: * @return bool
353: *
354: * @since 3.6
355: */
356: function epl_listing_has_links() {
357: $keys = array_merge(
358: (array) apply_filters( 'epl_external_link_keys', array( 'property_external_link', 'property_external_link_2', 'property_external_link_3' ) ),
359: (array) apply_filters( 'epl_floorplan_keys', array( 'property_floorplan', 'property_floorplan_2' ) ),
360: (array) apply_filters( 'epl_mini_web_keys', array( 'property_com_mini_web', 'property_com_mini_web_2', 'property_com_mini_web_3' ) ),
361: (array) apply_filters( 'epl_energy_certificate_keys', array( 'property_energy_certificate' ) ),
362: array( 'property_video_url' )
363: );
364:
365: return (bool) apply_filters( 'epl_listing_has_links', epl_listing_has_any_meta( $keys, true ) );
366: }
367: