PluginProbe
Property Hive / trunk
Property Hive vtrunk
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 1.4.62 1.4.63 All 259 releases
propertyhive / includes / class-ph-additional-fields.php

class-ph-additional-fields.php in Property Hive trunk, at includes/class-ph-additional-fields.php

1,761 lines 87.8 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 class PH_Additional_Fields {
8
9 public function __construct() {
10
11 $current_settings = get_option( 'propertyhive_template_assistant', array() );
12
13 if ( isset($current_settings['custom_fields']) && is_array($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
14 {
15 add_action( 'propertyhive_property_meta_list_end', array( $this, 'display_custom_fields_on_website' ) );
16 add_filter( 'propertyhive_user_details_form_fields', array( $this, 'display_custom_fields_on_user_details' ), 10, 1 );
17 add_action( 'propertyhive_applicant_registered', array( $this, 'save_custom_fields_on_user_details' ), 10, 2 );
18 add_action( 'propertyhive_account_details_updated', array( $this, 'save_custom_fields_on_user_details' ), 10, 2 );
19
20 add_filter( 'propertyhive_property_query_meta_query', array( $this, 'custom_fields_in_meta_query' ) );
21
22 $post_types = array(
23 'property',
24 'contact',
25 'enquiry',
26 'appraisal',
27 'viewing',
28 'offer',
29 'sale',
30 'tenancy',
31 );
32
33 foreach ( $post_types as $post_type )
34 {
35 add_filter( 'manage_edit-' . $post_type . '_columns', function($existing_columns) use ($post_type)
36 {
37 if ( !is_array( $existing_columns ) )
38 {
39 $existing_columns = array();
40 }
41
42 $current_settings = get_option( 'propertyhive_template_assistant', array() );
43
44 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
45 {
46 foreach ( $current_settings['custom_fields'] as $custom_field )
47 {
48 if (
49 isset($custom_field['admin_list']) &&
50 $custom_field['admin_list'] == '1' &&
51 substr($custom_field['meta_box'], 0, (strlen($post_type)+1)) == $post_type .'_'
52 )
53 {
54 $existing_columns[$custom_field['field_name']] = __( $custom_field['field_label'], 'propertyhive' );
55 }
56 }
57 }
58
59 return $existing_columns;
60 } );
61
62 add_action( 'manage_' . $post_type . '_posts_custom_column', function($column, $post_id) use ($post_type)
63 {
64 $current_settings = get_option( 'propertyhive_template_assistant', array() );
65
66 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
67 {
68 foreach ( $current_settings['custom_fields'] as $custom_field )
69 {
70 if (
71 isset($custom_field['admin_list']) &&
72 $custom_field['admin_list'] == '1' &&
73 substr($custom_field['meta_box'], 0, (strlen($post_type)+1)) == $post_type . '_' &&
74 $custom_field['field_name'] == $column
75 )
76 {
77 if ( get_post_meta( $post_id, $custom_field['field_name'], true ) != '' )
78 {
79 if ( $custom_field['field_type'] == 'multiselect' )
80 {
81 $values = get_post_meta( $post_id, $custom_field['field_name'], TRUE );
82 if ( !empty($values) )
83 {
84 echo esc_html(is_array($values) ? implode(", ", $values) : $values);
85 }
86 }
87 elseif ( $custom_field['field_type'] == 'date' )
88 {
89 echo date(get_option( 'date_format' ), strtotime(get_post_meta( $post_id, $custom_field['field_name'], true )));
90 }
91 elseif ( $custom_field['field_type'] == 'image' )
92 {
93 $image_id = get_post_meta( $post_id, $custom_field['field_name'], true );
94 if ( $image_id != '' )
95 {
96 $image = wp_get_attachment_image_src( $image_id, 'thumbnail' );
97 if ($image !== FALSE)
98 {
99 echo '<img src="' . esc_url($image[0]) . '" width="150" alt="">';
100 }
101 else
102 {
103 echo 'Image doesn\'t exist';
104 }
105 }
106 }
107 elseif ( $custom_field['field_type'] == 'file' )
108 {
109 $file = get_attached_file( get_post_meta( $post_id, $custom_field['field_name'], true ) );
110 if ( $file !== FALSE )
111 {
112 $filename = basename( $file );
113 echo '<a href="' . esc_url(wp_get_attachment_url(get_post_meta( $post_id, $custom_field['field_name'], true ))) . '" rel="noopener noreferrer" target="_blank">' . esc_html($filename) . '</a>';
114 }
115 else
116 {
117 echo 'File doesn\'t exist';
118 }
119 }
120 else
121 {
122 echo nl2br(esc_html(get_post_meta( $post_id, $custom_field['field_name'], true )));
123 }
124 }
125 break;
126 }
127 }
128 }
129 }, 2, 2 );
130
131 add_filter( 'manage_edit-' . $post_type . '_sortable_columns', function($columns) use ($post_type)
132 {
133 $custom = array();
134
135 $current_settings = get_option( 'propertyhive_template_assistant', array() );
136
137 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
138 {
139 foreach ( $current_settings['custom_fields'] as $custom_field )
140 {
141 if (
142 isset($custom_field['admin_list']) &&
143 $custom_field['admin_list'] == '1' &&
144 isset($custom_field['admin_list_sortable']) &&
145 $custom_field['admin_list_sortable'] == '1' &&
146 substr($custom_field['meta_box'], 0, (strlen($post_type)+1)) == $post_type . '_'
147 )
148 {
149 $custom[$custom_field['field_name']] = $custom_field['field_name'];
150 }
151 }
152 }
153
154 return wp_parse_args( $custom, $columns );
155 } );
156
157 add_filter( 'request', function($vars) use ($post_type)
158 {
159 if (
160 !is_admin() ||
161 !isset($vars['post_type']) ||
162 $vars['post_type'] !== $post_type ||
163 !isset($vars['orderby'])
164 ) {
165 return $vars;
166 }
167
168 $current_settings = get_option( 'propertyhive_template_assistant', array() );
169
170 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
171 {
172 foreach ( $current_settings['custom_fields'] as $custom_field )
173 {
174 if (
175 isset($custom_field['admin_list']) &&
176 $custom_field['admin_list'] == '1' &&
177 isset($custom_field['admin_list_sortable']) &&
178 $custom_field['admin_list_sortable'] == '1' &&
179 substr($custom_field['meta_box'], 0, (strlen($post_type)+1)) == $post_type . '_'
180 )
181 {
182 if ( $custom_field['field_name'] == $vars['orderby'] )
183 {
184 $vars = array_merge( $vars, array(
185 'meta_key' => $custom_field['field_name'],
186 'orderby' => 'meta_value'
187 ) );
188 }
189 }
190 }
191 }
192
193 return $vars;
194 } );
195 }
196
197 add_action( 'propertyhive_office_table_header_columns', array( $this, 'add_office_additional_field_table_header_column' ), 10 );
198 add_action( 'propertyhive_office_table_row_columns', array( $this, 'add_office_additional_field_table_row_column' ), 10 );
199
200 add_action( 'propertyhive_contact_applicant_requirements_details_fields', array( $this, 'add_applicant_requirements_fields' ), 10, 2 );
201 add_action( 'propertyhive_contact_applicant_requirements_residential_details_fields', array( $this, 'add_applicant_requirements_residential_fields' ), 10, 2 );
202 add_action( 'propertyhive_contact_applicant_requirements_residential_sales_details_fields', array( $this, 'add_applicant_requirements_residential_sales_fields' ), 10, 2 );
203 add_action( 'propertyhive_contact_applicant_requirements_residential_lettings_details_fields', array( $this, 'add_applicant_requirements_residential_lettings_fields' ), 10, 2 );
204 add_action( 'propertyhive_contact_applicant_requirements_commercial_details_fields', array( $this, 'add_applicant_requirements_commercial_fields' ), 10, 2 );
205
206 add_action( 'propertyhive_save_contact_applicant_requirements', array( $this, 'save_applicant_requirements_fields' ), 10, 2 );
207
208 add_filter( 'propertyhive_applicant_requirements_display', array( $this, 'applicant_requirements_display' ), 10, 3 );
209 add_filter( 'propertyhive_matching_properties_args', array( $this, 'matching_properties_args' ), 10, 3 );
210 add_filter( 'propertyhive_matching_applicants_check', array( $this, 'matching_applicants_check' ), 10, 4 );
211 add_filter( 'propertyhive_applicant_requirements_form_fields', array( $this, 'applicant_requirements_form_fields' ), 10, 2 );
212 add_action( 'propertyhive_applicant_registered', array( $this, 'applicant_registered' ), 10, 2 );
213 add_action( 'propertyhive_account_requirements_updated', array( $this, 'applicant_registered' ), 10, 2 );
214
215 add_filter( 'propertyhive_applicant_list_check', array( $this, 'applicant_list_check' ), 10, 3 );
216
217 add_filter( 'propertyhive_room_breakdown_data', array( $this, 'add_custom_fields_to_room_breakdown' ), 10, 3 ); // Applicable when Rooms / Student Accommodation add on active
218
219 $meta_boxes_done = array();
220 $office_details_fields_exist = false;
221 $offices_opening_section_done = false;
222 foreach ( $current_settings['custom_fields'] as $custom_field )
223 {
224 if ( !in_array( $custom_field['meta_box'], $meta_boxes_done ) )
225 {
226 if ( substr( $custom_field['meta_box'], 0, 6 ) == 'office' )
227 {
228 add_filter( 'propertyhive_' . $custom_field['meta_box'] . '_settings', function( $settings )
229 {
230 global $offices_opening_section_done;
231
232 $current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id'];
233
234 $meta_box_being_done = str_replace( "propertyhive_", "", current_filter() );
235 $meta_box_being_done = str_replace( "_settings", "", $meta_box_being_done );
236
237 $current_settings = get_option( 'propertyhive_template_assistant', array() );
238
239 foreach ( $current_settings['custom_fields'] as $custom_field )
240 {
241 if ( $custom_field['meta_box'] == $meta_box_being_done )
242 {
243 if ( !$offices_opening_section_done )
244 {
245 $settings[] = array( 'title' => __( 'Additional Fields', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_template_assistant_additional_field' );
246 $offices_opening_section_done = true;
247 }
248
249 switch ( $custom_field['field_type'] )
250 {
251 case "image":
252 {
253 if ( !did_action( 'wp_enqueue_media' ) )
254 wp_enqueue_media();
255
256 $settings[] = array(
257 'title' => $custom_field['field_label'],
258 'id' => $custom_field['field_name'],
259 'default' => get_post_meta($current_id, $custom_field['field_name'], TRUE),
260 'type' => $custom_field['field_type'],
261 'desc_tip' => false,
262 );
263 break;
264 }
265 case "select":
266 {
267 $options = array('' => '');
268 if ( isset($custom_field['dropdown_options']) && is_array($custom_field['dropdown_options']) && !empty($custom_field['dropdown_options']) )
269 {
270 foreach ( $custom_field['dropdown_options'] as $dropdown_option )
271 {
272 $options[$dropdown_option] = $dropdown_option;
273 }
274 }
275
276 $settings[] = array(
277 'title' => $custom_field['field_label'],
278 'id' => $custom_field['field_name'],
279 'default' => get_post_meta($current_id, $custom_field['field_name'], TRUE),
280 'type' => $custom_field['field_type'],
281 'desc_tip' => false,
282 'options' => $options,
283 );
284 break;
285 }
286 default:
287 {
288 $settings[] = array(
289 'title' => $custom_field['field_label'],
290 'id' => $custom_field['field_name'],
291 'default' => get_post_meta($current_id, $custom_field['field_name'], TRUE),
292 'type' => $custom_field['field_type'],
293 'desc_tip' => false,
294 );
295 }
296 }
297 }
298 }
299
300 if ( $offices_opening_section_done )
301 {
302 $settings[] = array( 'type' => 'sectionend', 'id' => 'office_template_assistant_additional_field');
303 }
304
305 return $settings;
306 });
307
308 $office_details_fields_exist = true;
309
310 add_action( 'propertyhive_save_office', function( $post_id )
311 {
312 $meta_box_being_done = 'office_details';
313
314 $current_settings = get_option( 'propertyhive_template_assistant', array() );
315
316 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
317 {
318 $current_settings['custom_fields'] = apply_filters( 'propertyhive_template_assistant_custom_fields_to_save', $current_settings['custom_fields'] );
319
320 foreach ( $current_settings['custom_fields'] as $custom_field )
321 {
322 if ( $custom_field['meta_box'] == $meta_box_being_done )
323 {
324 update_post_meta( $post_id, $custom_field['field_name'], (isset($_POST[$custom_field['field_name']]) ? $_POST[$custom_field['field_name']] : '') );
325 }
326 }
327 }
328 });
329 }
330 else
331 {
332 add_action( 'propertyhive_' . $custom_field['meta_box'] . '_fields', function()
333 {
334 global $thepostid;
335
336 $meta_box_being_done = str_replace( "propertyhive_", "", current_filter() );
337 $meta_box_being_done = str_replace( "_fields", "", $meta_box_being_done );
338
339 $current_settings = get_option( 'propertyhive_template_assistant', array() );
340
341 foreach ( $current_settings['custom_fields'] as $custom_field )
342 {
343 if ( $custom_field['meta_box'] == $meta_box_being_done )
344 {
345 if ( isset($custom_field['field_type']) && $custom_field['field_type'] == 'select' )
346 {
347 $options = array('' => '');
348 if ( isset($custom_field['dropdown_options']) && is_array($custom_field['dropdown_options']) && !empty($custom_field['dropdown_options']) )
349 {
350 foreach ( $custom_field['dropdown_options'] as $dropdown_option )
351 {
352 $options[$dropdown_option] = $dropdown_option;
353 }
354 }
355 propertyhive_wp_select( apply_filters( 'propertyhive_template_assistant_custom_field_args_' . ltrim($custom_field['field_name'], '_'), array(
356 'id' => $custom_field['field_name'],
357 'label' => $custom_field['field_label'],
358 'desc_tip' => false,
359 'options' => $options
360 ), $thepostid ) );
361 }
362 elseif ( isset($custom_field['field_type']) && $custom_field['field_type'] == 'multiselect' )
363 {
364 ?>
365 <p class="form-field <?php echo esc_attr($custom_field['field_name']); ?>_field"><label for="<?php echo esc_attr($custom_field['field_name']); ?>"><?php echo esc_html(__( $custom_field['field_label'], 'propertyhive' )); ?></label>
366 <select id="<?php echo esc_attr($custom_field['field_name']); ?>" name="<?php echo esc_attr($custom_field['field_name']); ?>[]" multiple="multiple" data-placeholder="<?php echo esc_attr(__( 'Select ' . $custom_field['field_label'], 'propertyhive' )); ?>" class="multiselect attribute_values">
367 <?php
368 $selected_values = get_post_meta( $thepostid, $custom_field['field_name'], true );
369 if ( !is_array($selected_values) && $selected_values == '' )
370 {
371 $selected_values = array();
372 }
373 elseif ( !is_array($selected_values) && $selected_values != '' )
374 {
375 $selected_values = array($selected_values);
376 }
377
378 if ( isset($custom_field['dropdown_options']) && is_array($custom_field['dropdown_options']) && !empty($custom_field['dropdown_options']) )
379 {
380 foreach ( $custom_field['dropdown_options'] as $dropdown_option )
381 {
382 echo '<option value="' . esc_attr( $dropdown_option ) . '"';
383 if ( in_array( $dropdown_option, $selected_values ) )
384 {
385 echo ' selected';
386 }
387 echo '>' . esc_html( $dropdown_option ) . '</option>';
388 }
389 }
390 ?>
391 </select>
392 <?php
393 }
394 elseif ( isset($custom_field['field_type']) && $custom_field['field_type'] == 'textarea' )
395 {
396 propertyhive_wp_textarea_input( apply_filters( 'propertyhive_template_assistant_custom_field_args_' . ltrim($custom_field['field_name'], '_'), array(
397 'id' => $custom_field['field_name'],
398 'label' => $custom_field['field_label'],
399 'desc_tip' => false,
400 'type' => 'text'
401 ), $thepostid ) );
402 }
403 elseif ( isset($custom_field['field_type']) && $custom_field['field_type'] == 'date' )
404 {
405 propertyhive_wp_text_input( apply_filters( 'propertyhive_template_assistant_custom_field_args_' . ltrim($custom_field['field_name'], '_'), array(
406 'id' => $custom_field['field_name'],
407 'label' => $custom_field['field_label'],
408 'desc_tip' => false,
409 'type' => 'date',
410 'class' => 'small',
411 ), $thepostid ) );
412 }
413 elseif ( isset($custom_field['field_type']) && $custom_field['field_type'] == 'checkbox' )
414 {
415 propertyhive_wp_checkbox( apply_filters( 'propertyhive_template_assistant_custom_field_args_' . ltrim($custom_field['field_name'], '_'), array(
416 'id' => $custom_field['field_name'],
417 'label' => $custom_field['field_label'],
418 'desc_tip' => false,
419 ), $thepostid ) );
420 }
421 elseif ( isset($custom_field['field_type']) && $custom_field['field_type'] == 'image' )
422 {
423 propertyhive_wp_photo_upload( apply_filters( 'propertyhive_template_assistant_custom_field_args_' . ltrim($custom_field['field_name'], '_'), array(
424 'id' => $custom_field['field_name'],
425 'label' => $custom_field['field_label'],
426 'desc_tip' => false,
427 'button_label' => __( 'Select Image', 'propertyhive' )
428 ), $thepostid ) );
429 }
430 elseif ( isset($custom_field['field_type']) && $custom_field['field_type'] == 'file' )
431 {
432 propertyhive_wp_file_upload( apply_filters( 'propertyhive_template_assistant_custom_field_args_' . ltrim($custom_field['field_name'], '_'), array(
433 'id' => $custom_field['field_name'],
434 'label' => $custom_field['field_label'],
435 'desc_tip' => false,
436 'button_label' => __( 'Select File', 'propertyhive' )
437 ), $thepostid ) );
438 }
439 else
440 {
441 propertyhive_wp_text_input( apply_filters( 'propertyhive_template_assistant_custom_field_args_' . ltrim($custom_field['field_name'], '_'), array(
442 'id' => $custom_field['field_name'],
443 'label' => $custom_field['field_label'],
444 'desc_tip' => false,
445 'type' => 'text'
446 ), $thepostid ) );
447 }
448 }
449 }
450 });
451
452 add_action( 'propertyhive_save_' . $custom_field['meta_box'], function( $post_id )
453 {
454 $meta_box_being_done = str_replace( "propertyhive_save_", "", current_filter() );
455
456 $current_settings = get_option( 'propertyhive_template_assistant', array() );
457
458 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
459 {
460 $current_settings['custom_fields'] = apply_filters( 'propertyhive_template_assistant_custom_fields_to_save', $current_settings['custom_fields'] );
461
462 foreach ( $current_settings['custom_fields'] as $custom_field )
463 {
464 if ( $custom_field['meta_box'] == $meta_box_being_done )
465 {
466 if ( isset($custom_field['field_type']) && $custom_field['field_type'] == 'textarea' )
467 {
468 update_post_meta( $post_id, $custom_field['field_name'], (isset($_POST[$custom_field['field_name']]) ? sanitize_textarea_field($_POST[$custom_field['field_name']]) : '') );
469 }
470 else
471 {
472 update_post_meta( $post_id, $custom_field['field_name'], (isset($_POST[$custom_field['field_name']]) ? ph_clean($_POST[$custom_field['field_name']]) : '') );
473 }
474 }
475 }
476 }
477 });
478 }
479
480 $meta_boxes_done[] = $custom_field['meta_box'];
481 }
482 }
483
484 if ( $office_details_fields_exist )
485 {
486 add_filter( 'propertyhive_' . $custom_field['meta_box'] . '_settings', function( $settings )
487 {
488 $settings[] = array( 'type' => 'sectionend', 'id' => 'office_location_options' );
489
490 return $settings;
491 });
492 }
493
494 $shortcodes = array(
495 'properties',
496 'recent_properties',
497 'featured_properties',
498 'similar_properties',
499 );
500
501 foreach ( $shortcodes as $shortcode )
502 {
503 add_filter( 'shortcode_atts_' . $shortcode, function ($out, $pairs, $atts, $shortcode)
504 {
505 $current_settings = get_option( 'propertyhive_template_assistant', array() );
506
507 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
508 {
509 foreach ( $current_settings['custom_fields'] as $custom_field )
510 {
511 if ( strpos($custom_field['meta_box'], 'property') !== FALSE )
512 {
513 $out[trim($custom_field['field_name'], '_')] = ( isset($atts[trim($custom_field['field_name'], '_')]) ? $atts[trim($custom_field['field_name'], '_')] : '' );
514 }
515 }
516 }
517
518 return $out;
519 }, 10, 4 );
520
521 add_filter( 'propertyhive_shortcode_' . $shortcode . '_query', function ($args, $atts)
522 {
523 $current_settings = get_option( 'propertyhive_template_assistant', array() );
524
525 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
526 {
527 foreach ( $current_settings['custom_fields'] as $custom_field )
528 {
529 if ( strpos($custom_field['meta_box'], 'property') !== FALSE )
530 {
531 if (
532 isset($atts[trim($custom_field['field_name'], '_')]) &&
533 $atts[trim($custom_field['field_name'], '_')] != ''
534 )
535 {
536 if ( !isset($args['meta_query']) )
537 {
538 $args['meta_query'] = array();
539 }
540
541 // Format meta query as "= value" or "LIKE value"
542 $value = $atts[trim($custom_field['field_name'], '_')];
543 $compare = $custom_field['field_type'] == 'multiselect' ? 'LIKE' : '=';
544
545 // A comma-delimited list of values has been specified
546 if ( strpos($value, ',') !== false )
547 {
548 if ( $custom_field['field_type'] == 'multiselect' )
549 {
550 // Format meta query as "REGEXP value1|value2"
551 $value = '"' . str_replace(',', '"|"', $value) . '"';
552 $compare = 'REGEXP';
553 }
554 else
555 {
556 // Format meta query as "IN array(value1, value2)"
557 $value = explode(',', $value);
558 $compare = 'IN';
559 }
560 }
561
562 $args['meta_query'][] = array(
563 'key' => $custom_field['field_name'],
564 'value' => $value,
565 'compare' => $compare,
566 );
567 }
568 }
569 }
570 }
571 return $args;
572 }, 99, 2 );
573 }
574 }
575 }
576
577 public function display_custom_fields_on_website()
578 {
579 global $property;
580
581 $current_settings = get_option( 'propertyhive_template_assistant', array() );
582
583 $custom_fields = ( (isset($current_settings['custom_fields'])) ? $current_settings['custom_fields'] : array() );
584
585 foreach ( $custom_fields as $custom_field )
586 {
587 if ( isset($custom_field['display_on_website']) && $custom_field['display_on_website'] == '1' && substr($custom_field['meta_box'], 0, 9) == 'property_' )
588 {
589 $label = '<span class="' . esc_attr(trim($custom_field['field_name'], '_')) . '_label">' . esc_html($custom_field['field_label']) . ': </span>';
590
591 if ( $custom_field['field_type'] == 'multiselect' )
592 {
593 $values = get_post_meta( $property->id, $custom_field['field_name'], TRUE );
594
595 if ( !empty($values) )
596 {
597 echo '<li class="' . esc_attr(trim($custom_field['field_name'], '_')) . '">' . $label;
598 echo esc_html(is_array($values) ? implode(", ", $values) : $values);
599 echo '</li>';
600 }
601 }
602 elseif ( $custom_field['field_type'] == 'date' )
603 {
604 if ( $property->{$custom_field['field_name']} != '' )
605 {
606 ?>
607 <li class="<?php echo esc_attr(trim($custom_field['field_name'], '_')); ?>">
608 <?php echo $label . date(get_option( 'date_format' ), strtotime($property->{$custom_field['field_name']})); ?>
609 </li>
610 <?php
611 }
612 }
613 elseif ( $custom_field['field_type'] == 'image' )
614 {
615 if ( $property->{$custom_field['field_name']} != '' )
616 {
617 ?>
618 <li class="<?php echo esc_attr(trim($custom_field['field_name'], '_')); ?>">
619 <?php echo $label . wp_get_attachment_image($property->{$custom_field['field_name']}); ?>
620 </li>
621 <?php
622 }
623 }
624 elseif ( $custom_field['field_type'] == 'file' )
625 {
626 if ( $property->{$custom_field['field_name']} != '' )
627 {
628 ?>
629 <li class="<?php echo esc_attr(trim($custom_field['field_name'], '_')); ?>">
630 <?php echo $label . '<a href="' . esc_url(wp_get_attachment_url($property->{$custom_field['field_name']})) . '" rel="noopener noreferrer" target="_blank">' . esc_html(__( 'View', 'propertyhive' )) . '</a>'; ?>
631 </li>
632 <?php
633 }
634 }
635 else
636 {
637 if ( $property->{$custom_field['field_name']} != '' )
638 {
639 $value = trim( $property->{$custom_field['field_name']} );
640
641 // If the custom field value is an email address or a URL, automatically make it a link
642 if ( apply_filters( 'propertyhive_auto_hyperlink_custom_fields', true ) )
643 {
644 if ( filter_var($value, FILTER_VALIDATE_URL) )
645 {
646 $value = '<a href="' . esc_url($value) . '" rel="noopener noreferrer" target="_blank">' . esc_html($value) . '</a>';
647 }
648 elseif ( filter_var($value, FILTER_VALIDATE_EMAIL) )
649 {
650 $value = '<a href="mailto:' . antispambot( sanitize_email( $value ) ) . '">' . esc_html($value) . '</a>';
651 }
652 }
653 ?>
654 <li class="<?php echo esc_attr(trim($custom_field['field_name'], '_')); ?>">
655 <?php echo $label . $value; ?>
656 </li>
657 <?php
658 }
659 }
660 }
661 }
662 }
663
664 public function display_custom_fields_on_user_details( $form_controls )
665 {
666 if ( is_user_logged_in() )
667 {
668 $current_user = wp_get_current_user();
669
670 if ( $current_user instanceof WP_User )
671 {
672 $contact = new PH_Contact( '', $current_user->ID );
673 }
674 }
675
676 $current_settings = get_option( 'propertyhive_template_assistant', array() );
677
678 $custom_fields = ( (isset($current_settings['custom_fields'])) ? $current_settings['custom_fields'] : array() );
679
680 foreach ( $custom_fields as $custom_field )
681 {
682 if ( isset($custom_field['display_on_user_details']) && $custom_field['display_on_user_details'] == '1' && substr($custom_field['meta_box'], 0, 8) == 'contact_' )
683 {
684 $form_controls[$custom_field['field_name']] = array(
685 'type' => $custom_field['field_type'],
686 'label' => $custom_field['field_label'],
687 );
688
689 if ( is_user_logged_in() && $current_user instanceof WP_User )
690 {
691 $form_controls[$custom_field['field_name']]['value'] = $contact->{$custom_field['field_name']};
692 }
693
694 switch ( $custom_field['field_type'] )
695 {
696 case 'select':
697 case 'multiselect':
698 {
699 $options = array('' => '');
700 if ( isset($custom_field['dropdown_options']) && is_array($custom_field['dropdown_options']) && !empty($custom_field['dropdown_options']) )
701 {
702 foreach ( $custom_field['dropdown_options'] as $dropdown_option )
703 {
704 $options[$dropdown_option] = $dropdown_option;
705 }
706 }
707 $form_controls[$custom_field['field_name']]['options'] = $options;
708 break;
709 }
710 }
711 }
712 }
713 return $form_controls;
714 }
715
716 public function save_custom_fields_on_user_details( $contact_post_id, $user_id )
717 {
718 $current_settings = get_option( 'propertyhive_template_assistant', array() );
719
720 $custom_fields = ( (isset($current_settings['custom_fields'])) ? $current_settings['custom_fields'] : array() );
721
722 foreach ( $custom_fields as $custom_field )
723 {
724 if ( isset($custom_field['display_on_user_details']) && $custom_field['display_on_user_details'] == '1' && substr($custom_field['meta_box'], 0, 8) == 'contact_' )
725 {
726 update_post_meta( $contact_post_id, $custom_field['field_name'], (isset($_POST[$custom_field['field_name']]) ? ph_clean($_POST[$custom_field['field_name']]) : '') );
727 }
728 }
729 }
730
731 public function custom_fields_in_meta_query( $meta_query )
732 {
733 $current_settings = get_option( 'propertyhive_template_assistant', array() );
734
735 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
736 {
737 foreach ( $current_settings['custom_fields'] as $custom_field )
738 {
739 if (
740 $custom_field['meta_box'] == 'property_residential_sales_details'
741 ||
742 $custom_field['meta_box'] == 'property_residential_lettings_details'
743 ||
744 $custom_field['meta_box'] == 'property_commercial_details'
745 )
746 {
747 // this is a department specific field. Make sure department in question in being searched
748 $meta_box_department = str_replace("property_", "", $custom_field['meta_box']);
749 $meta_box_department = str_replace("_details", "", $meta_box_department);
750 $meta_box_department = str_replace("_", "-", $meta_box_department);
751
752 if (
753 isset( $_REQUEST['department'] ) &&
754 ( $_REQUEST['department'] == $meta_box_department || ph_get_custom_department_based_on($_REQUEST['department']) == $meta_box_department )
755 )
756 {
757
758 }
759 else
760 {
761 continue;
762 }
763 }
764
765 if ( $custom_field['field_type'] == 'checkbox' )
766 {
767 if ( $custom_field['exact_match'] == '' )
768 {
769 // not exact match (i.e. pets allowed)
770 if ( isset($_REQUEST[$custom_field['field_name']]) && ph_clean( $_REQUEST[$custom_field['field_name']] ) == 'yes' )
771 {
772 $meta_query[] = array(
773 'key' => $custom_field['field_name'],
774 'value' => ph_clean( $_REQUEST[$custom_field['field_name']] ),
775 );
776 }
777 }
778 else
779 {
780 // should match exactly only (i.e. something only)
781 if ( isset($_REQUEST[$custom_field['field_name']]) && ph_clean( $_REQUEST[$custom_field['field_name']] ) == 'yes' )
782 {
783 $meta_query[] = array(
784 'key' => $custom_field['field_name'],
785 'value' => 'yes',
786 );
787 }
788 else
789 {
790 $meta_query[] = array(
791 'relation' => 'OR',
792 array(
793 'key' => $custom_field['field_name'],
794 'value' => '',
795 ),
796 array(
797 'key' => $custom_field['field_name'],
798 'compare' => 'NOT EXISTS',
799 )
800 );
801
802 }
803 }
804 }
805 else
806 {
807 if (
808 isset( $_REQUEST[$custom_field['field_name']] ) && $_REQUEST[$custom_field['field_name']] != ''
809 )
810 {
811 if (
812 ( $custom_field['field_type'] == 'select' || $custom_field['field_type'] == 'multiselect' ) &&
813 is_array($_REQUEST[$custom_field['field_name']])
814 )
815 {
816 $sub_meta_query = array('relation' => 'OR');
817 foreach ( $_REQUEST[$custom_field['field_name']] as $value )
818 {
819 $sub_meta_query[] = array(
820 'key' => $custom_field['field_name'],
821 'value' => ph_clean( $value ),
822 'compare' => '=',
823 );
824 $sub_meta_query[] = array(
825 'key' => $custom_field['field_name'],
826 'value' => '"' . ph_clean( $value ) . '"',
827 'compare' => 'LIKE',
828 );
829 }
830 $meta_query[] = $sub_meta_query;
831 }
832 elseif ( $custom_field['field_type'] == 'select' )
833 {
834 $meta_query[] = array(
835 'key' => $custom_field['field_name'],
836 'value' => ph_clean( $_REQUEST[$custom_field['field_name']] ),
837 'compare' => '=',
838 );
839 }
840 else
841 {
842 $meta_query[] = array(
843 'key' => $custom_field['field_name'],
844 'value' => ph_clean( $_REQUEST[$custom_field['field_name']] ),
845 'compare' => 'LIKE',
846 );
847 }
848 }
849 }
850 }
851 }
852
853 return $meta_query;
854 }
855
856 public function add_office_additional_field_table_header_column()
857 {
858 $current_settings = get_option( 'propertyhive_template_assistant', array() );
859
860 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
861 {
862 foreach ( $current_settings['custom_fields'] as $custom_field )
863 {
864 if (
865 isset($custom_field['admin_list']) &&
866 $custom_field['admin_list'] == '1' &&
867 substr($custom_field['meta_box'], 0, 6) == 'office'
868 )
869 {
870 echo '<th>' . esc_html($custom_field['field_label']) . '</th>';
871 }
872 }
873 }
874 }
875
876 public function add_office_additional_field_table_row_column( $office_id )
877 {
878 $current_settings = get_option( 'propertyhive_template_assistant', array() );
879
880 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
881 {
882 foreach ( $current_settings['custom_fields'] as $custom_field )
883 {
884 if ( isset($custom_field['admin_list']) && $custom_field['admin_list'] == '1' && substr($custom_field['meta_box'], 0, 6) == 'office' )
885 {
886 echo '<td>';
887 switch ( $custom_field['field_type'] )
888 {
889 case "image":
890 {
891 $image_id = get_post_meta( $office_id, $custom_field['field_name'], TRUE );
892 if ( $image_id != '' )
893 {
894 $image = wp_get_attachment_image_src( $image_id, 'thumbnail' );
895 if ($image !== FALSE)
896 {
897 echo '<img src="' . esc_url($image[0]) . '" width="150" alt="">';
898 }
899 else
900 {
901 echo 'Image doesn\'t exist';
902 }
903 }
904 break;
905 }
906 default:
907 {
908 echo esc_html(get_post_meta( $office_id, $custom_field['field_name'], TRUE ));
909 }
910 }
911 echo '</td>';
912 }
913 }
914 }
915 }
916
917 public function add_applicant_requirements_fields( $contact_post_id, $applicant_profile_id )
918 {
919 $applicant_profile = get_post_meta( $contact_post_id, '_applicant_profile_' . $applicant_profile_id, TRUE );
920
921 $current_settings = get_option( 'propertyhive_template_assistant', array() );
922
923 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
924 {
925 foreach ( $current_settings['custom_fields'] as $custom_field )
926 {
927 if (
928 isset($custom_field['display_on_applicant_requirements']) &&
929 $custom_field['display_on_applicant_requirements'] == '1' &&
930 substr($custom_field['meta_box'], 0, 9) == 'property_' &&
931 !in_array( $custom_field['meta_box'], array('property_residential_details', 'property_residential_sales_details', 'property_residential_lettings_details', 'property_commercial_details') )
932 )
933 {
934 $this->add_applicant_requirements_field( $custom_field, $applicant_profile, $applicant_profile_id );
935 }
936 }
937 }
938 }
939
940 private function add_applicant_requirements_field( $custom_field, $applicant_profile, $applicant_profile_id )
941 {
942 switch ( $custom_field['field_type'] )
943 {
944 case "select":
945 {
946 $options = array('' => '');
947 foreach ($custom_field['dropdown_options'] as $dropdown_option)
948 {
949 $options[$dropdown_option] = ph_clean($dropdown_option);
950 }
951
952 propertyhive_wp_select( array(
953 'id' => '_applicant' . $custom_field['field_name'] . '_' . $applicant_profile_id,
954 'label' => $custom_field['field_label'],
955 'desc_tip' => false,
956 'custom_attributes' => array(
957 'style' => 'width:100%; max-width:150px;'
958 ),
959 'value' => ( ( isset($applicant_profile[$custom_field['field_name']]) ) ? $applicant_profile[$custom_field['field_name']] : '' ),
960 'options' => $options,
961 ) );
962
963 break;
964 }
965 case "multiselect":
966 {
967 $options = array('' => '');
968 foreach ($custom_field['dropdown_options'] as $dropdown_option)
969 {
970 $options[$dropdown_option] = ph_clean($dropdown_option);
971 }
972 ?>
973 <p class="form-field">
974 <label for="_applicant<?php echo esc_attr($custom_field['field_name']); ?>_<?php echo $applicant_profile_id; ?>"><?php echo esc_html($custom_field['field_label']); ?></label>
975 <select id="_applicant<?php echo esc_attr($custom_field['field_name']); ?>_<?php echo $applicant_profile_id; ?>" name="_applicant<?php echo esc_attr($custom_field['field_name']); ?>_<?php echo $applicant_profile_id; ?>[]" multiple="multiple" data-placeholder="Start typing to add <?php echo esc_attr($custom_field['field_label']); ?>..." class="multiselect attribute_values">
976 <?php
977 foreach ( $options as $option )
978 {
979 echo '<option value="' . esc_attr( $option ) . '"';
980 if (
981 isset($applicant_profile[$custom_field['field_name']])
982 )
983 {
984 if ( !is_array($applicant_profile[$custom_field['field_name']]) && $applicant_profile[$custom_field['field_name']] != '' )
985 {
986 $applicant_profile[$custom_field['field_name']] = array($applicant_profile[$custom_field['field_name']]);
987 }
988
989 if ( in_array( $option, $applicant_profile[$custom_field['field_name']] ) )
990 {
991 echo ' selected';
992 }
993 }
994 echo '>' . esc_html( $option ) . '</option>';
995 }
996 ?>
997 </select>
998 </p>
999 <?php
1000 break;
1001 }
1002 case "checkbox":
1003 {
1004 propertyhive_wp_checkbox( array(
1005 'id' => '_applicant' . $custom_field['field_name'] . '_' . $applicant_profile_id,
1006 'label' => $custom_field['field_label'],
1007 'desc_tip' => false,
1008 'value' => ( ( isset($applicant_profile[$custom_field['field_name']]) && $applicant_profile[$custom_field['field_name']] == 'yes' ) ? 'yes' : '' ),
1009 ) );
1010
1011 break;
1012 }
1013 }
1014 }
1015
1016 public function add_applicant_requirements_residential_fields( $contact_post_id, $applicant_profile_id )
1017 {
1018 $applicant_profile = get_post_meta( $contact_post_id, '_applicant_profile_' . $applicant_profile_id, TRUE );
1019
1020 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1021
1022 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1023 {
1024 foreach ( $current_settings['custom_fields'] as $custom_field )
1025 {
1026 if (
1027 isset($custom_field['display_on_applicant_requirements']) &&
1028 $custom_field['display_on_applicant_requirements'] == '1' &&
1029 substr($custom_field['meta_box'], 0, 9) == 'property_' &&
1030 in_array( $custom_field['meta_box'], array('property_residential_details') )
1031 )
1032 {
1033 $this->add_applicant_requirements_field( $custom_field, $applicant_profile, $applicant_profile_id );
1034 }
1035 }
1036 }
1037 }
1038
1039 public function add_applicant_requirements_residential_sales_fields( $contact_post_id, $applicant_profile_id )
1040 {
1041 $applicant_profile = get_post_meta( $contact_post_id, '_applicant_profile_' . $applicant_profile_id, TRUE );
1042
1043 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1044
1045 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1046 {
1047 foreach ( $current_settings['custom_fields'] as $custom_field )
1048 {
1049 if (
1050 isset($custom_field['display_on_applicant_requirements']) &&
1051 $custom_field['display_on_applicant_requirements'] == '1' &&
1052 substr($custom_field['meta_box'], 0, 9) == 'property_' &&
1053 in_array( $custom_field['meta_box'], array('property_residential_sales_details') )
1054 )
1055 {
1056 $this->add_applicant_requirements_field( $custom_field, $applicant_profile, $applicant_profile_id );
1057 }
1058 }
1059 }
1060 }
1061
1062 public function add_applicant_requirements_residential_lettings_fields( $contact_post_id, $applicant_profile_id )
1063 {
1064 $applicant_profile = get_post_meta( $contact_post_id, '_applicant_profile_' . $applicant_profile_id, TRUE );
1065
1066 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1067
1068 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1069 {
1070 foreach ( $current_settings['custom_fields'] as $custom_field )
1071 {
1072 if (
1073 isset($custom_field['display_on_applicant_requirements']) &&
1074 $custom_field['display_on_applicant_requirements'] == '1' &&
1075 substr($custom_field['meta_box'], 0, 9) == 'property_' &&
1076 in_array( $custom_field['meta_box'], array('property_residential_lettings_details') )
1077 )
1078 {
1079 $this->add_applicant_requirements_field( $custom_field, $applicant_profile, $applicant_profile_id );
1080 }
1081 }
1082 }
1083 }
1084
1085 public function add_applicant_requirements_commercial_fields( $contact_post_id, $applicant_profile_id )
1086 {
1087 $applicant_profile = get_post_meta( $contact_post_id, '_applicant_profile_' . $applicant_profile_id, TRUE );
1088
1089 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1090
1091 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1092 {
1093 foreach ( $current_settings['custom_fields'] as $custom_field )
1094 {
1095 if (
1096 isset($custom_field['display_on_applicant_requirements']) &&
1097 $custom_field['display_on_applicant_requirements'] == '1' &&
1098 substr($custom_field['meta_box'], 0, 9) == 'property_' &&
1099 in_array( $custom_field['meta_box'], array('property_commercial_details') )
1100 )
1101 {
1102 $this->add_applicant_requirements_field( $custom_field, $applicant_profile, $applicant_profile_id );
1103 }
1104 }
1105 }
1106 }
1107
1108 public function save_applicant_requirements_fields( $contact_post_id, $applicant_profile_id )
1109 {
1110 $applicant_profile = get_post_meta( $contact_post_id, '_applicant_profile_' . $applicant_profile_id, TRUE );
1111
1112 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1113
1114 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1115 {
1116 foreach ( $current_settings['custom_fields'] as $custom_field )
1117 {
1118 if ( isset($custom_field['display_on_applicant_requirements']) && $custom_field['display_on_applicant_requirements'] == '1' && substr($custom_field['meta_box'], 0, 9) == 'property_' )
1119 {
1120 switch ( $custom_field['field_type'] )
1121 {
1122 case "select":
1123 case "multiselect":
1124 {
1125 if ( isset($_POST['_applicant' . $custom_field['field_name'] . '_' . $applicant_profile_id]) )
1126 {
1127 $applicant_profile[$custom_field['field_name']] = ph_clean($_POST['_applicant' . $custom_field['field_name'] . '_' . $applicant_profile_id]);
1128 }
1129 break;
1130 }
1131 case "checkbox":
1132 {
1133 if ( isset($_POST['_applicant' . $custom_field['field_name'] . '_' . $applicant_profile_id]) )
1134 {
1135 $applicant_profile[$custom_field['field_name']] = ph_clean($_POST['_applicant' . $custom_field['field_name'] . '_' . $applicant_profile_id]);
1136 }
1137 else
1138 {
1139 $applicant_profile[$custom_field['field_name']] = '';
1140 }
1141 break;
1142 }
1143 }
1144 }
1145 }
1146 }
1147
1148 update_post_meta( $contact_post_id, '_applicant_profile_' . $applicant_profile_id, $applicant_profile );
1149 }
1150
1151 public function applicant_requirements_display( $requirements, $contact_post_id, $applicant_profile )
1152 {
1153 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1154
1155 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1156 {
1157 foreach ( $current_settings['custom_fields'] as $custom_field )
1158 {
1159 if ( isset($custom_field['display_on_applicant_requirements']) && $custom_field['display_on_applicant_requirements'] == '1' && substr($custom_field['meta_box'], 0, 9) == 'property_' )
1160 {
1161 if ( isset($applicant_profile[$custom_field['field_name']]) )
1162 {
1163 switch ( $custom_field['field_type'] )
1164 {
1165 case "select":
1166 case "checkbox":
1167 {
1168 if ( $applicant_profile[$custom_field['field_name']] != '' )
1169 {
1170 $requirements[] = array(
1171 'label' => $custom_field['field_label'],
1172 'value' => ph_clean($applicant_profile[$custom_field['field_name']]),
1173 );
1174 }
1175 break;
1176 }
1177 case "multiselect":
1178 {
1179 if ( !is_array($applicant_profile[$custom_field['field_name']]) && $applicant_profile[$custom_field['field_name']] != '' )
1180 {
1181 $applicant_profile[$custom_field['field_name']] = array($applicant_profile[$custom_field['field_name']]);
1182 }
1183
1184 if ( !empty($applicant_profile[$custom_field['field_name']]) )
1185 {
1186 $sliced_terms = array_slice( ph_clean($applicant_profile[$custom_field['field_name']]), 0, 2 );
1187 $requirements[] = array(
1188 'label' => $custom_field['field_label'],
1189 'value' => implode(", ", $sliced_terms) . ( (count($applicant_profile[$custom_field['field_name']]) > 2) ? '<span title="' . esc_attr( implode(", ", $applicant_profile[$custom_field['field_name']]) ) .'"> + ' . (count($applicant_profile[$custom_field['field_name']]) - 2) . ' more</span>' : '' )
1190 );
1191 }
1192 break;
1193 }
1194 }
1195 }
1196 }
1197 }
1198 }
1199
1200 return $requirements;
1201 }
1202
1203 public function matching_properties_args( $args, $contact_post_id, $applicant_profile )
1204 {
1205 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1206
1207 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1208 {
1209 foreach ( $current_settings['custom_fields'] as $custom_field )
1210 {
1211 if ( isset($custom_field['display_on_applicant_requirements']) && $custom_field['display_on_applicant_requirements'] == '1' && substr($custom_field['meta_box'], 0, 9) == 'property_' )
1212 {
1213 if ( isset($applicant_profile[$custom_field['field_name']]) )
1214 {
1215 // ensure if field is specific to department it's taken into account, else ignored
1216 if (
1217 $custom_field['meta_box'] == 'property_residential_sales_details'
1218 ||
1219 $custom_field['meta_box'] == 'property_residential_lettings_details'
1220 ||
1221 $custom_field['meta_box'] == 'property_commercial_details'
1222 )
1223 {
1224 $meta_box_department = str_replace("property_", "", $custom_field['meta_box']);
1225 $meta_box_department = str_replace("_details", "", $meta_box_department);
1226 $meta_box_department = str_replace("_", "-", $meta_box_department);
1227
1228 if (
1229 isset( $applicant_profile['department'] ) &&
1230 ( $applicant_profile['department'] == $meta_box_department || ph_get_custom_department_based_on($applicant_profile['department']) == $meta_box_department )
1231 )
1232 {
1233
1234 }
1235 else
1236 {
1237 continue;
1238 }
1239 }
1240
1241 switch ( $custom_field['field_type'] )
1242 {
1243 case "select":
1244 {
1245 if ( $applicant_profile[$custom_field['field_name']] != '' )
1246 {
1247 $args['meta_query'][] = array(
1248 'key' => $custom_field['field_name'],
1249 'value' => $applicant_profile[$custom_field['field_name']],
1250 );
1251 }
1252 break;
1253 }
1254 case "multiselect":
1255 {
1256 if ( !is_array($applicant_profile[$custom_field['field_name']]) && $applicant_profile[$custom_field['field_name']] != '' )
1257 {
1258 $applicant_profile[$custom_field['field_name']] = array($applicant_profile[$custom_field['field_name']]);
1259 }
1260
1261 if ( !empty($applicant_profile[$custom_field['field_name']]) )
1262 {
1263 $sub_meta_query = array(
1264 'relation' => 'OR'
1265 );
1266
1267 foreach ( $applicant_profile[$custom_field['field_name']] as $option )
1268 {
1269 $sub_meta_query[] = array(
1270 'key' => $custom_field['field_name'],
1271 'value' => $option,
1272 'compare' => 'LIKE',
1273 );
1274 }
1275
1276 $args['meta_query'][] = $sub_meta_query;
1277 }
1278 break;
1279 }
1280 case "checkbox":
1281 {
1282 if ( $custom_field['exact_match'] == '' )
1283 {
1284 if ( $applicant_profile[$custom_field['field_name']] != '' )
1285 {
1286 $args['meta_query'][] = array(
1287 'key' => $custom_field['field_name'],
1288 'value' => $applicant_profile[$custom_field['field_name']],
1289 );
1290 }
1291 }
1292 else
1293 {
1294 // should match exactly only
1295 if ( $applicant_profile[$custom_field['field_name']] == 'yes' )
1296 {
1297 $args['meta_query'][] = array(
1298 'key' => $custom_field['field_name'],
1299 'value' => 'yes',
1300 );
1301 }
1302 else
1303 {
1304 $args['meta_query'][] = array(
1305 'relation' => 'OR',
1306 array(
1307 'key' => $custom_field['field_name'],
1308 'value' => '',
1309 ),
1310 array(
1311 'key' => $custom_field['field_name'],
1312 'compare' => 'NOT EXISTS',
1313 )
1314 );
1315
1316 }
1317 }
1318 break;
1319 }
1320 }
1321 }
1322 }
1323 }
1324 }
1325
1326 return $args;
1327 }
1328
1329 public function matching_applicants_check( $check, $property, $contact_post_id, $applicant_profile )
1330 {
1331 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1332
1333 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1334 {
1335 foreach ( $current_settings['custom_fields'] as $custom_field )
1336 {
1337 if ( isset($custom_field['display_on_applicant_requirements']) && $custom_field['display_on_applicant_requirements'] == '1' && substr($custom_field['meta_box'], 0, 9) == 'property_' )
1338 {
1339 // ensure if field is specific to department it's taken into account, else ignored
1340 if (
1341 $custom_field['meta_box'] == 'property_residential_sales_details'
1342 ||
1343 $custom_field['meta_box'] == 'property_residential_lettings_details'
1344 ||
1345 $custom_field['meta_box'] == 'property_commercial_details'
1346 )
1347 {
1348 $meta_box_department = str_replace("property_", "", $custom_field['meta_box']);
1349 $meta_box_department = str_replace("_details", "", $meta_box_department);
1350 $meta_box_department = str_replace("_", "-", $meta_box_department);
1351
1352 if (
1353 isset( $applicant_profile['department'] ) &&
1354 ( $applicant_profile['department'] == $meta_box_department || ph_get_custom_department_based_on($applicant_profile['department']) == $meta_box_department )
1355 )
1356 {
1357
1358 }
1359 else
1360 {
1361 continue;
1362 }
1363 }
1364
1365 if ( isset($applicant_profile[$custom_field['field_name']]) )
1366 {
1367 switch ( $custom_field['field_type'] )
1368 {
1369 case "select":
1370 {
1371 if (
1372 $applicant_profile[$custom_field['field_name']] == '' ||
1373 $property->{$custom_field['field_name']} == $applicant_profile[$custom_field['field_name']]
1374 )
1375 {
1376
1377 }
1378 else
1379 {
1380 return false;
1381 }
1382 break;
1383 }
1384 case "multiselect":
1385 {
1386 if ( !is_array($applicant_profile[$custom_field['field_name']]) && $applicant_profile[$custom_field['field_name']] != '' )
1387 {
1388 $applicant_profile[$custom_field['field_name']] = array($applicant_profile[$custom_field['field_name']]);
1389 }
1390
1391 if ( empty($applicant_profile[$custom_field['field_name']]) )
1392 {
1393
1394 }
1395 else
1396 {
1397 $property_values = !is_array($property->{$custom_field['field_name']}) ? array($property->{$custom_field['field_name']}) : $property->{$custom_field['field_name']};
1398 if ( empty($property_values) )
1399 {
1400 return false;
1401 }
1402
1403 $applicant_values = $applicant_profile[$custom_field['field_name']];
1404
1405 $value_exists = false;
1406
1407 foreach ( $property_values as $property_value )
1408 {
1409 foreach ( $applicant_values as $applicant_value )
1410 {
1411 if ( $property_value == $applicant_value )
1412 {
1413 $value_exists = true;
1414 }
1415 }
1416 }
1417
1418 if ( !$value_exists )
1419 {
1420 return false;
1421 }
1422 }
1423
1424 break;
1425 }
1426 case "checkbox":
1427 {
1428 if ( $custom_field['exact_match'] == '' )
1429 {
1430 // not exact match (i.e. pets allowed)
1431 if (
1432 $applicant_profile[$custom_field['field_name']] == '' ||
1433 $property->{$custom_field['field_name']} == $applicant_profile[$custom_field['field_name']]
1434 )
1435 {
1436
1437 }
1438 else
1439 {
1440 return false;
1441 }
1442 }
1443 else
1444 {
1445 // exact match
1446 if (
1447 $property->{$custom_field['field_name']} == $applicant_profile[$custom_field['field_name']]
1448 )
1449 {
1450
1451 }
1452 else
1453 {
1454 return false;
1455 }
1456 }
1457 break;
1458 }
1459 }
1460 }
1461 }
1462 }
1463 }
1464
1465 return $check;
1466 }
1467
1468 public function applicant_requirements_form_fields( $form_controls, $applicant_profile = false )
1469 {
1470 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1471
1472 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1473 {
1474 foreach ( $current_settings['custom_fields'] as $custom_field )
1475 {
1476 if ( isset($custom_field['display_on_applicant_requirements']) && $custom_field['display_on_applicant_requirements'] == '1' && substr($custom_field['meta_box'], 0, 9) == 'property_' )
1477 {
1478 switch ( $custom_field['field_type'] )
1479 {
1480 case "select":
1481 case "multiselect":
1482 {
1483 $options = array('' => '');
1484 foreach ($custom_field['dropdown_options'] as $dropdown_option)
1485 {
1486 $options[$dropdown_option] = ph_clean($dropdown_option);
1487 }
1488
1489 $value = isset($applicant_profile[$custom_field['field_name']]) ? $applicant_profile[$custom_field['field_name']] : '';
1490 /*if ( is_array($value) && !empty($value) )
1491 {
1492 $value = $value[0];
1493 }*/
1494
1495 $form_controls[$custom_field['field_name']] = array(
1496 'type' => 'select',
1497 'label' => $custom_field['field_label'],
1498 'required' => false,
1499 'show_label' => true,
1500 'value' => $value,
1501 'options' => $options,
1502 'multiselect' => $custom_field['field_type'] == 'multiselect' ? true : false
1503 );
1504
1505 break;
1506 }
1507 case "checkbox":
1508 {
1509 $value = isset($applicant_profile[$custom_field['field_name']]) ? $applicant_profile[$custom_field['field_name']] : '';
1510
1511 $form_controls[$custom_field['field_name']] = array(
1512 'type' => 'checkbox',
1513 'label' => $custom_field['field_label'],
1514 'required' => false,
1515 'show_label' => true,
1516 'value' => $value,
1517 );
1518
1519 break;
1520 }
1521 }
1522 }
1523 }
1524 }
1525
1526 return $form_controls;
1527 }
1528
1529 public function applicant_registered( $contact_post_id, $user_id )
1530 {
1531 $applicant_profile = get_post_meta( $contact_post_id, '_applicant_profile_' . ( isset($_POST['profile_id']) && $_POST['profile_id'] != '' ? (int)$_POST['profile_id'] : '0' ), TRUE );
1532
1533 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1534
1535 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1536 {
1537 foreach ( $current_settings['custom_fields'] as $custom_field )
1538 {
1539 if ( isset($custom_field['display_on_applicant_requirements']) && $custom_field['display_on_applicant_requirements'] == '1' && substr($custom_field['meta_box'], 0, 9) == 'property_' )
1540 {
1541 switch ( $custom_field['field_type'] )
1542 {
1543 case "select":
1544 {
1545 $applicant_profile[$custom_field['field_name']] = isset($_POST[$custom_field['field_name']]) ? ph_clean($_POST[$custom_field['field_name']]) : '';
1546 break;
1547 }
1548 case "multiselect":
1549 {
1550 if ( isset($_POST[$custom_field['field_name']]) )
1551 {
1552 if ( !is_array($_POST[$custom_field['field_name']]) )
1553 {
1554 $_POST[$custom_field['field_name']] = array($_POST[$custom_field['field_name']]);
1555 }
1556 }
1557 $applicant_profile[$custom_field['field_name']] = isset($_POST[$custom_field['field_name']]) ? ph_clean($_POST[$custom_field['field_name']]) : array();
1558 break;
1559 }
1560 case "checkbox":
1561 {
1562 $applicant_profile[$custom_field['field_name']] = isset($_POST[$custom_field['field_name']]) ? ph_clean($_POST[$custom_field['field_name']]) : '';
1563 break;
1564 }
1565 }
1566 }
1567 }
1568 }
1569
1570 update_post_meta( $contact_post_id, '_applicant_profile_' . ( isset($_POST['profile_id']) && $_POST['profile_id'] != '' ? (int)$_POST['profile_id'] : '0' ), $applicant_profile );
1571 }
1572
1573 public function applicant_list_check( $check, $contact_post_id, $applicant_profile )
1574 {
1575 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1576
1577 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1578 {
1579 foreach ( $current_settings['custom_fields'] as $custom_field )
1580 {
1581 if ( isset($custom_field['display_on_applicant_requirements']) && $custom_field['display_on_applicant_requirements'] == '1' && substr($custom_field['meta_box'], 0, 9) == 'property_' )
1582 {
1583 // ensure if field is specific to department it's taken into account, else ignored
1584 if (
1585 $custom_field['meta_box'] == 'property_residential_sales_details'
1586 ||
1587 $custom_field['meta_box'] == 'property_residential_lettings_details'
1588 ||
1589 $custom_field['meta_box'] == 'property_commercial_details'
1590 )
1591 {
1592 $meta_box_department = str_replace("property_", "", $custom_field['meta_box']);
1593 $meta_box_department = str_replace("_details", "", $meta_box_department);
1594 $meta_box_department = str_replace("_", "-", $meta_box_department);
1595
1596 if (
1597 isset( $_POST['department'] ) &&
1598 ( $_POST['department'] == $meta_box_department || ph_get_custom_department_based_on($_POST['department']) == $meta_box_department )
1599 )
1600 {
1601
1602 }
1603 else
1604 {
1605 continue;
1606 }
1607 }
1608
1609
1610 if ( isset($applicant_profile[$custom_field['field_name']]) )
1611 {
1612 switch ( $custom_field['field_type'] )
1613 {
1614 case "select":
1615 {
1616 if ( !empty($_POST[$custom_field['field_name']]) )
1617 {
1618 if (
1619 $applicant_profile[$custom_field['field_name']] == '' ||
1620 $_POST[$custom_field['field_name']] == $applicant_profile[$custom_field['field_name']]
1621 )
1622 {
1623
1624 }
1625 else
1626 {
1627 return false;
1628 }
1629 }
1630 break;
1631 }
1632 case "multiselect":
1633 {
1634 if ( !empty($_POST[$custom_field['field_name']]) )
1635 {
1636 if ( !is_array($applicant_profile[$custom_field['field_name']]) && $applicant_profile[$custom_field['field_name']] != '' )
1637 {
1638 $applicant_profile[$custom_field['field_name']] = array($applicant_profile[$custom_field['field_name']]);
1639 }
1640
1641 if ( empty($applicant_profile[$custom_field['field_name']]) )
1642 {
1643
1644 }
1645 else
1646 {
1647 $property_values = $_POST[$custom_field['field_name']];
1648 if ( empty($property_values) )
1649 {
1650 return false;
1651 }
1652
1653 $applicant_values = $applicant_profile[$custom_field['field_name']];
1654
1655 $value_exists = false;
1656
1657 foreach ( $property_values as $property_value )
1658 {
1659 foreach ( $applicant_values as $applicant_value )
1660 {
1661 if ( $property_value == $applicant_value )
1662 {
1663 $value_exists = true;
1664 }
1665 }
1666 }
1667
1668 if ( !$value_exists )
1669 {
1670 return false;
1671 }
1672 }
1673 }
1674
1675 break;
1676 }
1677 case "checkbox":
1678 {
1679 if ( $custom_field['exact_match'] == '' )
1680 {
1681 // not exact match (i.e. pets allowed)
1682 if (
1683 $applicant_profile[$custom_field['field_name']] == '' ||
1684 $_POST[$custom_field['field_name']] == $applicant_profile[$custom_field['field_name']]
1685 )
1686 {
1687
1688 }
1689 else
1690 {
1691 return false;
1692 }
1693 }
1694 else
1695 {
1696 // exact match
1697 if ( isset($_POST[$custom_field['field_name']]) )
1698 {
1699 if (
1700 $_POST[$custom_field['field_name']] == $applicant_profile[$custom_field['field_name']]
1701 )
1702 {
1703
1704 }
1705 else
1706 {
1707 return false;
1708 }
1709 }
1710 else
1711 {
1712 if (
1713 '' == $applicant_profile[$custom_field['field_name']]
1714 )
1715 {
1716
1717 }
1718 else
1719 {
1720 return false;
1721 }
1722 }
1723 }
1724 break;
1725 }
1726 }
1727 }
1728 }
1729 }
1730 }
1731
1732 return $check;
1733 }
1734
1735 public function add_custom_fields_to_room_breakdown( $room_data, $post_id, $room )
1736 {
1737 $current_settings = get_option( 'propertyhive_template_assistant', array() );
1738
1739 if ( isset($current_settings['custom_fields']) && !empty($current_settings['custom_fields']) )
1740 {
1741 foreach ( $current_settings['custom_fields'] as $custom_field )
1742 {
1743 if ( isset($custom_field['display_on_website']) && $custom_field['display_on_website'] == '1' && $custom_field['meta_box'] == 'property_rooms_breakdown' )
1744 {
1745 if ( $room->{$custom_field['field_name']} != '' )
1746 {
1747 $room_data[] = array(
1748 'class' => sanitize_title($custom_field['field_name']),
1749 'label' => __( $custom_field['field_label'], 'propertyhive' ),
1750 'value' => $room->{$custom_field['field_name']}
1751 );
1752 }
1753 }
1754 }
1755 }
1756
1757 return $room_data;
1758 }
1759 }
1760
1761 new PH_Additional_Fields();