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-post-types.php

class-ph-post-types.php in Property Hive trunk, at includes/class-ph-post-types.php

1,475 lines 61.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly
4 }
5
6 /**
7 * Post types
8 *
9 * Registers post types
10 *
11 * @class PH_Post_types
12 * @version 1.0.0
13 * @package PropertyHive/Classes
14 * @category Class
15 * @author PropertyHive
16 */
17 class PH_Post_types {
18
19 /**
20 * Constructor
21 */
22 public function __construct() {
23 add_action( 'init', array( __CLASS__, 'register_taxonomies' ), 5 );
24 add_action( 'init', array( __CLASS__, 'register_post_types' ), 5 );
25 add_action( 'init', array( __CLASS__, 'register_post_statuses' ), 5 );
26
27 add_action( 'wp_trash_post', array( __CLASS__, 'trash_property_children' ), 5 );
28 add_action( 'wp_trash_post', array( __CLASS__, 'trash_property_enquiries' ), 5 );
29
30 add_action( 'before_delete_post', array( __CLASS__, 'delete_property_media' ), 5 );
31 add_action( 'before_delete_post', array( __CLASS__, 'delete_property_children' ), 5 );
32 add_action( 'before_delete_post', array( __CLASS__, 'delete_property_enquiries' ), 5 );
33 add_action( 'before_delete_post', array( __CLASS__, 'delete_contact_user' ), 5 );
34
35 add_action( 'delete_user', array( $this, 'delete_contact_user_link' ) );
36
37 add_action( 'save_post', array( __CLASS__, 'create_concatenated_indexable_meta' ), 99, 3 );
38
39 add_action( 'save_post', array( __CLASS__, 'ensure_property_floor_area_to_set' ), 99, 3 );
40
41 add_action( 'save_post', array( __CLASS__, 'update_property_indexed_owner_names' ), 99, 3 );
42
43 add_action( 'save_post', array( __CLASS__, 'store_related_viewings' ), 99, 3 );
44 add_action( 'updated_post_meta', array( __CLASS__, 'store_related_viewings_meta_change' ), 10, 4 );
45
46 add_action( 'propertyhive_update_address_concatenated', array( __CLASS__, 'update_address_concatenated' ) );
47
48 add_filter( 'get_terms', array( $this, 'put_terms_in_order' ), 10, 4 );
49
50 add_action( 'propertyhive_after_register_post_types', array( __CLASS__, 'maybe_flush_rewrite_rules' ) );
51 }
52
53 public static function maybe_flush_rewrite_rules()
54 {
55 if ( 'yes' === get_option( 'propertyhive_queue_flush_rewrite_rules' ) ) {
56 update_option( 'propertyhive_queue_flush_rewrite_rules', 'no' );
57 flush_rewrite_rules();
58 }
59 }
60
61 public static function register_post_statuses()
62 {
63 register_post_status(
64 'archive',
65 array(
66 'label' => _x('Archived', 'post status label', 'propertyhive'),
67 'public' => false,
68 'exclude_from_search' => true,
69 'show_in_admin_all_list' => false,
70 'show_in_admin_status_list' => true,
71 'label_count' => _n_noop(
72 /* translators: %s: number of posts in "Archived" status */
73 'Archived <span class="count">(%s)</span>',
74 'Archived <span class="count">(%s)</span>',
75 'propertyhive'
76 ),
77 )
78 );
79 }
80
81 public static function update_property_indexed_owner_names( $post_id, $post, $update )
82 {
83 // $post_id and $post are required
84 if ( empty( $post_id ) || empty( $post ) ) {
85 return;
86 }
87
88 // Dont' save meta boxes for revisions or autosaves
89 if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
90 return;
91 }
92
93 if ( $post->post_type !== 'property' && $post->post_type !== 'contact' ) {
94 return;
95 }
96
97 if ( $post->post_type == 'property' )
98 {
99 // Get the owner contact IDs
100 $contact_ids = get_post_meta($post_id, '_owner_contact_id', true);
101 if ( !is_array($contact_ids) )
102 {
103 $contact_ids = array($contact_ids);
104 }
105
106 // Get the names of the contacts
107 $owner_details = array();
108 if ( !empty($contact_ids) )
109 {
110 foreach ( $contact_ids as $contact_id )
111 {
112 $contact_post = get_post($contact_id);
113 if ( $contact_post && $contact_post->post_type == 'contact' )
114 {
115 $owner_details[] = $contact_post->post_title;
116 if ( get_post_meta( $contact_id, '_telephone_number', TRUE ) != '' )
117 {
118 $owner_details[] = get_post_meta( $contact_id, '_telephone_number', TRUE );
119 }
120 if ( get_post_meta( $contact_id, '_email_address', TRUE ) != '' )
121 {
122 $owner_details[] = get_post_meta( $contact_id, '_email_address', TRUE );
123 }
124 }
125 }
126
127 $owner_details = array_filter($owner_details);
128 $owner_details = array_unique($owner_details);
129 }
130
131 // Update the owner names meta field
132 update_post_meta($post_id, '_owner_details', implode(', ', $owner_details));
133 }
134
135 if ( $post->post_type == 'contact' )
136 {
137 // Find all properties linked to this contact
138 $args = array(
139 'post_type' => 'property',
140 'meta_query' => array(
141 array(
142 'key' => '_owner_contact_id',
143 'value' => '"' . $post_id . '"',
144 'compare' => 'LIKE'
145 )
146 ),
147 'posts_per_page' => -1
148 );
149
150 $properties = get_posts($args);
151 foreach ( $properties as $property )
152 {
153 // Get the owner contact IDs
154 $contact_ids = get_post_meta($property->ID, '_owner_contact_id', true);
155 if ( !is_array($contact_ids) )
156 {
157 $contact_ids = array($contact_ids);
158 }
159
160 // Get the names of the contacts
161 $owner_details = array();
162 if ( !empty($contact_ids) )
163 {
164 foreach ( $contact_ids as $contact_id )
165 {
166 $contact_post = get_post($contact_id);
167 if ( $contact_post && $contact_post->post_type == 'contact' )
168 {
169 $owner_details[] = $contact_post->post_title;
170 if ( get_post_meta( $contact_id, '_telephone_number', TRUE ) != '' )
171 {
172 $owner_details[] = get_post_meta( $contact_id, '_telephone_number', TRUE );
173 }
174 if ( get_post_meta( $contact_id, '_email_address', TRUE ) != '' )
175 {
176 $owner_details[] = get_post_meta( $contact_id, '_email_address', TRUE );
177 }
178 }
179 }
180
181 $owner_details = array_filter($owner_details);
182 $owner_details = array_unique($owner_details);
183 }
184
185 // Update the owner names meta field
186 update_post_meta($property->ID, '_owner_details', implode(', ', $owner_details));
187 }
188 }
189 }
190
191 public static function put_terms_in_order( $terms, $taxonomies, $query_vars, $term_query ) {
192
193 if ( empty($taxonomies) )
194 {
195 return $terms;
196 }
197
198 if ( !isset($query_vars['fields']) || ( isset($query_vars['fields']) && $query_vars['fields'] != 'all' ) )
199 {
200 return $terms;
201 }
202
203 if ( !is_array($taxonomies) ) { $taxonomies = array($taxonomies); }
204
205 foreach ( $taxonomies as $taxonomy_name )
206 {
207 $taxonomy = get_taxonomy( $taxonomy_name );
208
209 if ( is_array($taxonomy->object_type) && in_array('property', $taxonomy->object_type) )
210 {
211
212 }
213 else
214 {
215 return $terms;
216 }
217
218 $order = get_option( 'propertyhive_taxonomy_terms_order_' . $taxonomy_name, '' );
219
220 if ( empty($order) )
221 {
222 return $terms;
223 }
224
225 // Convert $order string to an array of IDs
226 $order_array = explode('|', $order);
227
228 // Initialize a new array to store the sorted objects
229 $sorted_array = array();
230
231 // Loop through each order ID and find matching WP_Term objects
232 foreach ( $order_array as $order_id )
233 {
234 foreach ( $terms as $item )
235 {
236 if ( $item->term_id == $order_id )
237 {
238 $sorted_array[] = $item;
239 break; // Stop the inner loop once a match is found
240 }
241 }
242 }
243
244 // Append WP_Term objects not listed in $order at the end of $sorted_array
245 foreach ( $terms as $item )
246 {
247 // Check if the item is not in $sorted_array
248 $found = false;
249 foreach ( $sorted_array as $sorted_item )
250 {
251 if ( $sorted_item->term_id == $item->term_id )
252 {
253 $found = true;
254 break;
255 }
256 }
257 // If not found, append to $sorted_array
258 if ( !$found )
259 {
260 $sorted_array[] = $item;
261 }
262 }
263
264 // Replace the original array with the sorted array
265 $terms = $sorted_array;
266 }
267
268 return $terms;
269
270 }
271
272 /**
273 * Register PropertyHive taxonomies.
274 */
275 public static function register_taxonomies() {
276
277 if ( taxonomy_exists( 'property_type' ) )
278 return;
279
280 do_action( 'propertyhive_register_taxonomy' );
281
282 //$permalinks = get_option( 'propertyhive_permalinks' );
283
284 register_taxonomy( 'availability',
285 apply_filters( 'propertyhive_taxonomy_objects_availability', array( 'property' ) ),
286 apply_filters(
287 'propertyhive_taxonomy_args_availability',
288 array(
289 'label' => __( 'Availabilities', 'propertyhive' ),
290 'hierarchical' => true,
291 'show_ui' => false,
292 'show_in_nav_menus' => false,
293 'query_var' => is_admin(),
294 'rewrite' => false,
295 'public' => true,
296 'show_in_rest' => true,
297 )
298 )
299 );
300
301 register_taxonomy( 'property_type',
302 apply_filters( 'propertyhive_taxonomy_objects_property_type', array( 'property', 'appraisal' ) ),
303 apply_filters(
304 'propertyhive_taxonomy_args_property_type',
305 array(
306 'label' => __( 'Property Types', 'propertyhive' ),
307 'hierarchical' => true,
308 'show_ui' => false,
309 'show_in_nav_menus' => false,
310 'query_var' => is_admin(),
311 'rewrite' => false,
312 'public' => true,
313 'show_in_rest' => true,
314 )
315 )
316 );
317
318 register_taxonomy( 'commercial_property_type',
319 apply_filters( 'propertyhive_taxonomy_objects_commercial_property_type', array( 'property', 'appraisal' ) ),
320 apply_filters(
321 'propertyhive_taxonomy_args_commercial_property_type',
322 array(
323 'label' => __( 'Commercial Property Types', 'propertyhive' ),
324 'hierarchical' => true,
325 'show_ui' => false,
326 'show_in_nav_menus' => false,
327 'query_var' => is_admin(),
328 'rewrite' => false,
329 'public' => true,
330 'show_in_rest' => true,
331 )
332 )
333 );
334
335 register_taxonomy( 'location',
336 apply_filters( 'propertyhive_taxonomy_objects_location', array( 'property' ) ),
337 apply_filters(
338 'propertyhive_taxonomy_args_location',
339 array(
340 'label' => __( 'Locations', 'propertyhive' ),
341 'hierarchical' => true,
342 'show_ui' => false,
343 'show_in_nav_menus' => false,
344 'query_var' => is_admin(),
345 'rewrite' => false,
346 'public' => true,
347 'show_in_rest' => true,
348 )
349 )
350 );
351
352 register_taxonomy( 'parking',
353 apply_filters( 'propertyhive_taxonomy_objects_parking', array( 'property', 'appraisal' ) ),
354 apply_filters(
355 'propertyhive_taxonomy_args_parking',
356 array(
357 'label' => __( 'Parking', 'propertyhive' ),
358 'hierarchical' => true,
359 'show_ui' => false,
360 'show_in_nav_menus' => false,
361 'query_var' => is_admin(),
362 'rewrite' => false,
363 'public' => true,
364 'show_in_rest' => true,
365 )
366 )
367 );
368
369 register_taxonomy( 'outside_space',
370 apply_filters( 'propertyhive_taxonomy_objects_outside_space', array( 'property', 'appraisal' ) ),
371 apply_filters(
372 'propertyhive_taxonomy_args_outside_space',
373 array(
374 'label' => __( 'Outside Spaces', 'propertyhive' ),
375 'hierarchical' => true,
376 'show_ui' => false,
377 'show_in_nav_menus' => false,
378 'query_var' => is_admin(),
379 'rewrite' => false,
380 'public' => true,
381 'show_in_rest' => true,
382 )
383 )
384 );
385
386 register_taxonomy( 'price_qualifier',
387 apply_filters( 'propertyhive_taxonomy_objects_price_qualifier', array( 'property' ) ),
388 apply_filters(
389 'propertyhive_taxonomy_args_price_qualifier',
390 array(
391 'label' => __( 'Price Qualifiers', 'propertyhive' ),
392 'hierarchical' => true,
393 'show_ui' => false,
394 'show_in_nav_menus' => false,
395 'query_var' => is_admin(),
396 'rewrite' => false,
397 'public' => true,
398 'show_in_rest' => true,
399 )
400 )
401 );
402
403 register_taxonomy( 'tenure',
404 apply_filters( 'propertyhive_taxonomy_objects_tenure', array( 'property' ) ),
405 apply_filters(
406 'propertyhive_taxonomy_args_tenure',
407 array(
408 'label' => __( 'Tenures', 'propertyhive' ),
409 'hierarchical' => true,
410 'show_ui' => false,
411 'show_in_nav_menus' => false,
412 'query_var' => is_admin(),
413 'rewrite' => false,
414 'public' => true,
415 'show_in_rest' => true,
416 )
417 )
418 );
419
420 register_taxonomy( 'commercial_tenure',
421 apply_filters( 'propertyhive_taxonomy_objects_commercial_tenure', array( 'property' ) ),
422 apply_filters(
423 'propertyhive_taxonomy_args_commercial_tenure',
424 array(
425 'label' => __( 'Commercial Tenures', 'propertyhive' ),
426 'hierarchical' => true,
427 'show_ui' => false,
428 'show_in_nav_menus' => false,
429 'query_var' => is_admin(),
430 'rewrite' => false,
431 'public' => true,
432 'show_in_rest' => true,
433 )
434 )
435 );
436
437 register_taxonomy( 'sale_by',
438 apply_filters( 'propertyhive_taxonomy_objects_sale_by', array( 'property' ) ),
439 apply_filters(
440 'propertyhive_taxonomy_args_sale_by',
441 array(
442 'label' => __( 'Sale By', 'propertyhive' ),
443 'hierarchical' => true,
444 'show_ui' => false,
445 'show_in_nav_menus' => false,
446 'query_var' => is_admin(),
447 'rewrite' => false,
448 'public' => true,
449 'show_in_rest' => true,
450 )
451 )
452 );
453
454 register_taxonomy( 'furnished',
455 apply_filters( 'propertyhive_taxonomy_objects_furnished', array( 'property', 'appraisal' ) ),
456 apply_filters(
457 'propertyhive_taxonomy_args_furnished',
458 array(
459 'label' => __( 'Furnished', 'propertyhive' ),
460 'hierarchical' => true,
461 'show_ui' => false,
462 'show_in_nav_menus' => false,
463 'query_var' => is_admin(),
464 'rewrite' => false,
465 'public' => true,
466 'show_in_rest' => true,
467 )
468 )
469 );
470
471 register_taxonomy( 'marketing_flag',
472 apply_filters( 'propertyhive_taxonomy_objects_marketing_flag', array( 'property' ) ),
473 apply_filters(
474 'propertyhive_taxonomy_args_marketing_flag',
475 array(
476 'label' => __( 'Marketing Flags', 'propertyhive' ),
477 'hierarchical' => true,
478 'show_ui' => false,
479 'show_in_nav_menus' => false,
480 'query_var' => is_admin(),
481 'rewrite' => false,
482 'public' => true,
483 'show_in_rest' => true,
484 )
485 )
486 );
487
488 register_taxonomy( 'property_feature',
489 apply_filters( 'propertyhive_taxonomy_objects_property_feature', array( 'property' ) ),
490 apply_filters(
491 'propertyhive_taxonomy_args_property_feature',
492 array(
493 'label' => __( 'Property Features', 'propertyhive' ),
494 'hierarchical' => true,
495 'show_ui' => false,
496 'show_in_nav_menus' => false,
497 'query_var' => is_admin(),
498 'rewrite' => false,
499 'public' => true,
500 'show_in_rest' => true,
501 )
502 )
503 );
504
505 register_taxonomy( 'management_key_date_type',
506 apply_filters( 'propertyhive_taxonomy_objects_management_key_date_type', array( 'key_date' ) ),
507 apply_filters(
508 'propertyhive_taxonomy_args_management_key_date_type',
509 array(
510 'label' => __( 'Management Dates', 'propertyhive' ),
511 'hierarchical' => true,
512 'show_ui' => false,
513 'show_in_nav_menus' => false,
514 'query_var' => is_admin(),
515 'rewrite' => false,
516 'public' => true
517 )
518 )
519 );
520
521 do_action( 'do_action_after_register_taxonomies' );
522 }
523
524 /**
525 * Register core post types
526 */
527 public static function register_post_types() {
528
529 if ( post_type_exists('property') )
530 return;
531
532 do_action( 'propertyhive_register_post_type' );
533
534 //$permalinks = get_option( 'property_permalinks' );
535 //$product_permalink = empty( $permalinks['property_base'] ) ? _x( 'property', 'slug', 'propertyhive' ) : $permalinks['property_base'];
536
537 register_post_type( "property",
538 apply_filters( 'propertyhive_register_post_type_property',
539 array(
540 'labels' => array(
541 'name' => __( 'Properties', 'propertyhive' ),
542 'singular_name' => __( 'Property', 'propertyhive' ),
543 'menu_name' => _x( 'Properties', 'Admin menu name', 'propertyhive' ),
544 'add_new' => __( 'Add Property', 'propertyhive' ),
545 'add_new_item' => __( 'Add New Property', 'propertyhive' ),
546 'edit' => __( 'Edit', 'propertyhive' ),
547 'edit_item' => __( 'Edit Property', 'propertyhive' ),
548 'new_item' => __( 'New Property', 'propertyhive' ),
549 'view' => __( 'View Property', 'propertyhive' ),
550 'view_item' => __( 'View Property', 'propertyhive' ),
551 'search_items' => __( 'Search Properties', 'propertyhive' ),
552 'not_found' => __( 'No properties found', 'propertyhive' ),
553 'not_found_in_trash' => __( 'No properties found in trash', 'propertyhive' ),
554 'parent' => __( 'Parent Property', 'propertyhive' )
555 ),
556 'description' => __( 'This is where you can add new properties to your site.', 'propertyhive' ),
557 'public' => true,
558 'show_ui' => true,
559 'capability_type' => 'post',
560 'map_meta_cap' => true,
561 'publicly_queryable' => true,
562 'exclude_from_search' => false,
563 'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
564 //'rewrite' => $product_permalink ? array( 'slug' => untrailingslashit( $product_permalink ), 'with_front' => false, 'feeds' => true ) : false,
565 'query_var' => true,
566 'supports' => array( 'title', 'excerpt' ),
567 'has_archive' => ( $search_results_page_id = ph_get_page_id( 'search_results' ) ) && get_page( $search_results_page_id ) ? get_page_uri( $search_results_page_id ) : 'search_results',
568 'show_in_nav_menus' => true,
569 'show_in_menu' => false,
570 'show_in_admin_bar' => true,
571 'show_in_rest' => true,
572 )
573 )
574 );
575
576 register_post_type( "contact",
577 apply_filters( 'propertyhive_register_post_type_contact',
578 array(
579 'labels' => array(
580 'name' => __( 'Contacts', 'propertyhive' ),
581 'singular_name' => __( 'Contact', 'propertyhive' ),
582 'menu_name' => _x( 'Contacts', 'Admin menu name', 'propertyhive' ),
583 'add_new' => __( 'Add Contact', 'propertyhive' ),
584 'add_new_item' => __( 'Add New Contact', 'propertyhive' ),
585 'edit' => __( 'Edit', 'propertyhive' ),
586 'edit_item' => __( 'Edit Contact', 'propertyhive' ),
587 'new_item' => __( 'New Contact', 'propertyhive' ),
588 'view' => __( 'View Contact', 'propertyhive' ),
589 'view_item' => __( 'View Contact', 'propertyhive' ),
590 'search_items' => __( 'Search Contacts', 'propertyhive' ),
591 'not_found' => __( 'No contacts found', 'propertyhive' ),
592 'not_found_in_trash' => __( 'No contacts found in trash', 'propertyhive' ),
593 'parent' => __( 'Parent Contact', 'propertyhive' )
594 ),
595 'description' => __( 'This is where you can add new contacts to your site.', 'propertyhive' ),
596 'public' => false,
597 'show_ui' => true,
598 'capability_type' => 'post',
599 'map_meta_cap' => true,
600 'publicly_queryable' => false,
601 'exclude_from_search' => true,
602 'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
603 'query_var' => true,
604 'supports' => array( 'title' ),
605 'show_in_nav_menus' => false,
606 'show_in_menu' => false
607 )
608 )
609 );
610
611 register_post_type( "office",
612 apply_filters( 'propertyhive_register_post_type_office',
613 array(
614 'labels' => array(
615 'name' => __( 'Offices', 'propertyhive' ),
616 'singular_name' => __( 'Office', 'propertyhive' ),
617 'menu_name' => _x( 'Offices', 'Admin menu name', 'propertyhive' ),
618 'add_new' => __( 'Add Office', 'propertyhive' ),
619 'add_new_item' => __( 'Add New Office', 'propertyhive' ),
620 'edit' => __( 'Edit', 'propertyhive' ),
621 'edit_item' => __( 'Edit Office', 'propertyhive' ),
622 'new_item' => __( 'New Office', 'propertyhive' ),
623 'view' => __( 'View Office', 'propertyhive' ),
624 'view_item' => __( 'View Office', 'propertyhive' ),
625 'search_items' => __( 'Search Offices', 'propertyhive' ),
626 'not_found' => __( 'No offices found', 'propertyhive' ),
627 'not_found_in_trash' => __( 'No offices found in trash', 'propertyhive' ),
628 'parent' => __( 'Parent Office', 'propertyhive' )
629 ),
630 'public' => true,
631 'show_ui' => false,
632 'capability_type' => 'post',
633 'map_meta_cap' => true,
634 'publicly_queryable' => false,
635 'exclude_from_search' => true,
636 'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
637 'query_var' => true,
638 'supports' => array( 'title' ),
639 'show_in_nav_menus' => false,
640 'show_in_menu' => false,
641 'show_in_rest' => true,
642 )
643 )
644 );
645
646 register_post_type( "enquiry",
647 apply_filters( 'propertyhive_register_post_type_enquiry',
648 array(
649 'labels' => array(
650 'name' => __( 'Enquiries', 'propertyhive' ),
651 'singular_name' => __( 'Enquiry', 'propertyhive' ),
652 'menu_name' => _x( 'Enquiries', 'Admin menu name', 'propertyhive' ),
653 'add_new' => __( 'Add Enquiry', 'propertyhive' ),
654 'add_new_item' => __( 'Add New Enquiry', 'propertyhive' ),
655 'edit' => __( 'Edit', 'propertyhive' ),
656 'edit_item' => __( 'Edit Enquiry', 'propertyhive' ),
657 'new_item' => __( 'New Enquiry', 'propertyhive' ),
658 'view' => __( 'View Enquiry', 'propertyhive' ),
659 'view_item' => __( 'View Enquiry', 'propertyhive' ),
660 'search_items' => __( 'Search Enquiries', 'propertyhive' ),
661 'not_found' => __( 'No enquiries found', 'propertyhive' ),
662 'not_found_in_trash' => __( 'No enquiries found in trash', 'propertyhive' ),
663 'parent' => __( 'Parent Enquiry', 'propertyhive' )
664 ),
665 'public' => false,
666 'show_ui' => true,
667 'capability_type' => 'post',
668 'map_meta_cap' => true,
669 'publicly_queryable' => false,
670 'exclude_from_search' => true,
671 'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
672 'query_var' => true,
673 'supports' => array( 'title' ),
674 'show_in_nav_menus' => false,
675 'show_in_menu' => false,
676 'show_in_rest' => true,
677 )
678 )
679 );
680
681 register_post_type( "appraisal",
682 apply_filters( 'propertyhive_register_post_type_appraisal',
683 array(
684 'labels' => array(
685 'name' => __( 'Appraisals', 'propertyhive' ),
686 'singular_name' => __( 'Appraisal', 'propertyhive' ),
687 'menu_name' => _x( 'Appraisals', 'Admin menu name', 'propertyhive' ),
688 'add_new' => __( 'Add Appraisal', 'propertyhive' ),
689 'add_new_item' => __( 'Add New Appraisal', 'propertyhive' ),
690 'edit' => __( 'Edit', 'propertyhive' ),
691 'edit_item' => __( 'Edit Appraisal', 'propertyhive' ),
692 'new_item' => __( 'New Appraisal', 'propertyhive' ),
693 'view' => __( 'View Appraisal', 'propertyhive' ),
694 'view_item' => __( 'View Appraisal', 'propertyhive' ),
695 'search_items' => __( 'Search Appraisals', 'propertyhive' ),
696 'not_found' => __( 'No appraisals found', 'propertyhive' ),
697 'not_found_in_trash' => __( 'No appraisals found in trash', 'propertyhive' ),
698 'parent' => __( 'Parent Appraisal', 'propertyhive' )
699 ),
700 'public' => false,
701 'show_ui' => true,
702 'capability_type' => 'post',
703 'map_meta_cap' => true,
704 'publicly_queryable' => false,
705 'exclude_from_search' => true,
706 'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
707 'query_var' => true,
708 'supports' => false,
709 'show_in_nav_menus' => false,
710 'show_in_menu' => false
711 )
712 )
713 );
714
715 register_post_type( "viewing",
716 apply_filters( 'propertyhive_register_post_type_viewing',
717 array(
718 'labels' => array(
719 'name' => __( 'Viewings', 'propertyhive' ),
720 'singular_name' => __( 'Viewing', 'propertyhive' ),
721 'menu_name' => _x( 'Viewings', 'Admin menu name', 'propertyhive' ),
722 'add_new' => __( 'Add Viewing', 'propertyhive' ),
723 'add_new_item' => __( 'Add New Viewing', 'propertyhive' ),
724 'edit' => __( 'Edit', 'propertyhive' ),
725 'edit_item' => __( 'Edit Viewing', 'propertyhive' ),
726 'new_item' => __( 'New Viewing', 'propertyhive' ),
727 'view' => __( 'View Viewing', 'propertyhive' ),
728 'view_item' => __( 'View Viewing', 'propertyhive' ),
729 'search_items' => __( 'Search Viewings', 'propertyhive' ),
730 'not_found' => __( 'No viewings found', 'propertyhive' ),
731 'not_found_in_trash' => __( 'No viewings found in trash', 'propertyhive' ),
732 'parent' => __( 'Parent Viewing', 'propertyhive' )
733 ),
734 'public' => false,
735 'show_ui' => true,
736 'capability_type' => 'post',
737 'map_meta_cap' => true,
738 'publicly_queryable' => false,
739 'exclude_from_search' => true,
740 'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
741 'query_var' => true,
742 'supports' => false,
743 'show_in_nav_menus' => false,
744 'show_in_menu' => false
745 )
746 )
747 );
748
749 register_post_type( "offer",
750 apply_filters( 'propertyhive_register_post_type_offer',
751 array(
752 'labels' => array(
753 'name' => __( 'Offers', 'propertyhive' ),
754 'singular_name' => __( 'Offer', 'propertyhive' ),
755 'menu_name' => _x( 'Offers', 'Admin menu name', 'propertyhive' ),
756 'add_new' => __( 'Add Offer', 'propertyhive' ),
757 'add_new_item' => __( 'Add New Offer', 'propertyhive' ),
758 'edit' => __( 'Edit', 'propertyhive' ),
759 'edit_item' => __( 'Edit Offer', 'propertyhive' ),
760 'new_item' => __( 'New Offer', 'propertyhive' ),
761 'view' => __( 'View Offer', 'propertyhive' ),
762 'view_item' => __( 'View Offer', 'propertyhive' ),
763 'search_items' => __( 'Search Offers', 'propertyhive' ),
764 'not_found' => __( 'No offers found', 'propertyhive' ),
765 'not_found_in_trash' => __( 'No offers found in trash', 'propertyhive' ),
766 'parent' => __( 'Parent Offer', 'propertyhive' )
767 ),
768 'public' => false,
769 'show_ui' => true,
770 'capability_type' => 'post',
771 'map_meta_cap' => true,
772 'publicly_queryable' => false,
773 'exclude_from_search' => true,
774 'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
775 'query_var' => true,
776 'supports' => false,
777 'show_in_nav_menus' => false,
778 'show_in_menu' => false
779 )
780 )
781 );
782
783 register_post_type( "sale",
784 apply_filters( 'propertyhive_register_post_type_sale',
785 array(
786 'labels' => array(
787 'name' => __( 'Sales', 'propertyhive' ),
788 'singular_name' => __( 'Sale', 'propertyhive' ),
789 'menu_name' => _x( 'Sales', 'Admin menu name', 'propertyhive' ),
790 'add_new' => __( 'Add Sale', 'propertyhive' ),
791 'add_new_item' => __( 'Add New Sale', 'propertyhive' ),
792 'edit' => __( 'Edit', 'propertyhive' ),
793 'edit_item' => __( 'Edit Sale', 'propertyhive' ),
794 'new_item' => __( 'New Sale', 'propertyhive' ),
795 'view' => __( 'View Sale', 'propertyhive' ),
796 'view_item' => __( 'View Sale', 'propertyhive' ),
797 'search_items' => __( 'Search Sales', 'propertyhive' ),
798 'not_found' => __( 'No sales found', 'propertyhive' ),
799 'not_found_in_trash' => __( 'No sales found in trash', 'propertyhive' ),
800 'parent' => __( 'Parent Sale', 'propertyhive' )
801 ),
802 'public' => false,
803 'show_ui' => true,
804 'capability_type' => 'post',
805 'capabilities' => array(
806 'create_posts' => false
807 ),
808 'map_meta_cap' => true,
809 'publicly_queryable' => false,
810 'exclude_from_search' => true,
811 'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
812 'query_var' => true,
813 'supports' => false,
814 'show_in_nav_menus' => false,
815 'show_in_menu' => false
816 )
817 )
818 );
819
820 register_post_type( "tenancy",
821 apply_filters( 'propertyhive_register_post_type_tenancy',
822 array(
823 'labels' => array(
824 'name' => __( 'Tenancies', 'propertyhive' ),
825 'singular_name' => __( 'Tenancy', 'propertyhive' ),
826 'menu_name' => _x( 'Tenancies', 'Admin menu name', 'propertyhive' ),
827 'add_new' => __( 'Add Tenancy', 'propertyhive' ),
828 'add_new_item' => __( 'Add New Tenancy', 'propertyhive' ),
829 'edit' => __( 'Edit', 'propertyhive' ),
830 'edit_item' => __( 'Edit Tenancy', 'propertyhive' ),
831 'new_item' => __( 'New Tenancy', 'propertyhive' ),
832 'view' => __( 'View Tenancy', 'propertyhive' ),
833 'view_item' => __( 'View Tenancy', 'propertyhive' ),
834 'search_items' => __( 'Search Tenancies', 'propertyhive' ),
835 'not_found' => __( 'No tenancies found', 'propertyhive' ),
836 'not_found_in_trash' => __( 'No tenancies found in trash', 'propertyhive' ),
837 'parent' => __( 'Parent Tenancy', 'propertyhive' )
838 ),
839 'public' => false,
840 'show_ui' => true,
841 'capability_type' => 'post',
842 'map_meta_cap' => true,
843 'publicly_queryable' => false,
844 'exclude_from_search' => true,
845 'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
846 'query_var' => true,
847 'supports' => false,
848 'show_in_nav_menus' => false,
849 'show_in_menu' => false
850 )
851 )
852 );
853
854 register_post_type( "key_date",
855 apply_filters( 'propertyhive_register_post_type_key_date',
856 array(
857 'labels' => array(
858 'name' => __( 'Key Dates', 'propertyhive' ),
859 'singular_name' => __( 'Key Date', 'propertyhive' ),
860 'menu_name' => _x( 'Management', 'Admin menu name', 'propertyhive' ),
861 'add_new' => __( 'Add Key Date', 'propertyhive' ),
862 'add_new_item' => __( 'Add New Key Date', 'propertyhive' ),
863 'edit' => __( 'Edit', 'propertyhive' ),
864 'edit_item' => __( 'Edit Key Date', 'propertyhive' ),
865 'new_item' => __( 'New Key Date', 'propertyhive' ),
866 'view' => __( 'View Key Date', 'propertyhive' ),
867 'view_item' => __( 'View Key Date', 'propertyhive' ),
868 'search_items' => __( 'Search Key Dates', 'propertyhive' ),
869 'not_found' => __( 'No key dates found', 'propertyhive' ),
870 'not_found_in_trash' => __( 'No key dates found in trash', 'propertyhive' ),
871 'parent' => __( 'Parent Key Date', 'propertyhive' )
872 ),
873 'public' => false,
874 'show_ui' => true,
875 'capability_type' => 'post',
876 'map_meta_cap' => true,
877 'publicly_queryable' => false,
878 'exclude_from_search' => true,
879 'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records!
880 'query_var' => true,
881 'supports' => false,
882 'show_in_nav_menus' => false,
883 'show_in_menu' => false,
884 'capabilities' => array(
885 'create_posts' => 'do_not_allow'
886 ),
887 )
888 )
889 );
890 do_action( 'propertyhive_after_register_post_types' );
891 }
892
893 public static function trash_property_children( $post_id )
894 {
895 if ( get_post_type($post_id) == 'property' )
896 {
897 // get all children
898 $args = array(
899 'post_type' => 'property',
900 'nopaging' => true,
901 'post_parent' => (int)$post_id,
902 'suppress_filters' => TRUE,
903 'post_status' => array('publish', 'pending', 'private', 'draft', 'auto-draft', 'future'),
904 );
905
906 $properties_query = new WP_Query($args);
907
908 if ( $properties_query->have_posts() )
909 {
910 while( $properties_query->have_posts() )
911 {
912 $properties_query->the_post();
913
914 wp_trash_post( get_the_ID() );
915 }
916 }
917 wp_reset_postdata();
918 }
919 }
920
921 public static function trash_property_enquiries( $post_id )
922 {
923 if ( apply_filters( 'propertyhive_delete_enquiries_on_property_delete', false ) === true && get_post_type($post_id) == 'property' )
924 {
925 $args = array(
926 'post_type' => 'enquiry',
927 'nopaging' => true,
928 'post_status' => array('publish', 'pending', 'private', 'draft', 'auto-draft', 'future', 'trash'),
929 'meta_query' => array(
930 'relation' => 'OR',
931 array(
932 'key' => '_property_id',
933 'value' => $post_id
934 ),
935 array(
936 'key' => 'property_id',
937 'value' => $post_id
938 )
939 )
940 );
941
942 $enquiries_query = new WP_Query($args);
943
944 if ( $enquiries_query->have_posts() )
945 {
946 while( $enquiries_query->have_posts() )
947 {
948 $enquiries_query->the_post();
949
950 wp_trash_post( get_the_ID() );
951 }
952 }
953 wp_reset_postdata();
954 }
955 }
956
957 public static function delete_property_media( $post_id )
958 {
959 if ( get_post_type($post_id) == 'property' && apply_filters( 'propertyhive_remove_media_on_property_delete', TRUE ) === TRUE )
960 {
961 $property = new PH_Property( (int)$post_id );
962
963 $media_ids = $property->get_gallery_attachment_ids();
964 self::do_delete_media_attachments( $post_id, $media_ids );
965
966 $media_ids = $property->get_floorplan_attachment_ids();
967 self::do_delete_media_attachments( $post_id, $media_ids );
968
969 $media_ids = $property->get_brochure_attachment_ids();
970 self::do_delete_media_attachments( $post_id, $media_ids );
971
972 $media_ids = $property->get_epc_attachment_ids();
973 self::do_delete_media_attachments( $post_id, $media_ids );
974 }
975 }
976
977 private static function do_delete_media_attachments( $post_id, $media_ids = array() )
978 {
979 if ( is_array($media_ids) && !empty($media_ids) )
980 {
981 foreach ( $media_ids as $media_id )
982 {
983 // Make sure this media isn't used anywhere else
984 $args = array(
985 'post_type' => 'property',
986 'fields' => 'ids',
987 'suppress_filters' => TRUE,
988 'posts_per_page' => 1,
989 'post__not_in' => array( $post_id ),
990 'meta_query' => array(
991 'relation' => 'OR',
992 array(
993 'key' => '_photos',
994 'value' => ':' . $media_id . ';',
995 'compare' => 'LIKE'
996 ),
997 array(
998 'key' => '_photos',
999 'value' => ':"' . $media_id . '";',
1000 'compare' => 'LIKE'
1001 ),
1002 array(
1003 'key' => '_floorplans',
1004 'value' => ':' . $media_id . ';',
1005 'compare' => 'LIKE'
1006 ),
1007 array(
1008 'key' => '_floorplans',
1009 'value' => ':"' . $media_id . '";',
1010 'compare' => 'LIKE'
1011 ),
1012 array(
1013 'key' => '_epcs',
1014 'value' => ':' . $media_id . ';',
1015 'compare' => 'LIKE'
1016 ),
1017 array(
1018 'key' => '_epcs',
1019 'value' => ':"' . $media_id . '";',
1020 'compare' => 'LIKE'
1021 ),
1022 array(
1023 'key' => '_brochures',
1024 'value' => ':' . $media_id . ';',
1025 'compare' => 'LIKE'
1026 ),
1027 array(
1028 'key' => '_brochures',
1029 'value' => ':"' . $media_id . '";',
1030 'compare' => 'LIKE'
1031 ),
1032 )
1033 );
1034
1035 $property_query = new WP_Query( $args );
1036
1037 if ( !$property_query->have_posts() )
1038 {
1039 // We're ok to delete
1040 wp_delete_attachment( $media_id, true );
1041 }
1042 }
1043 }
1044 }
1045
1046 public static function delete_property_children( $post_id )
1047 {
1048 if ( get_post_type($post_id) == 'property' )
1049 {
1050 // get all children
1051 $args = array(
1052 'post_type' => 'property',
1053 'nopaging' => true,
1054 'suppress_filters' => TRUE,
1055 'post_parent' => (int)$post_id,
1056 'post_status' => array('publish', 'pending', 'private', 'draft', 'auto-draft', 'future', 'trash'),
1057 );
1058
1059 $properties_query = new WP_Query($args);
1060
1061 if ( $properties_query->have_posts() )
1062 {
1063 while( $properties_query->have_posts() )
1064 {
1065 $properties_query->the_post();
1066
1067 wp_delete_post( get_the_ID(), true );
1068 }
1069 }
1070 wp_reset_postdata();
1071 }
1072 }
1073
1074 public static function delete_property_enquiries( $post_id )
1075 {
1076 if ( apply_filters( 'propertyhive_delete_enquiries_on_property_delete', false ) === true && get_post_type($post_id) == 'property' )
1077 {
1078 $args = array(
1079 'post_type' => 'enquiry',
1080 'nopaging' => true,
1081 'post_status' => array('publish', 'pending', 'private', 'draft', 'auto-draft', 'future', 'trash'),
1082 'meta_query' => array(
1083 'relation' => 'OR',
1084 array(
1085 'key' => '_property_id',
1086 'value' => $post_id
1087 ),
1088 array(
1089 'key' => 'property_id',
1090 'value' => $post_id
1091 )
1092 )
1093 );
1094
1095 $enquiries_query = new WP_Query($args);
1096
1097 if ( $enquiries_query->have_posts() )
1098 {
1099 while( $enquiries_query->have_posts() )
1100 {
1101 $enquiries_query->the_post();
1102
1103 wp_delete_post( get_the_ID(), true );
1104 }
1105 }
1106 wp_reset_postdata();
1107 }
1108 }
1109
1110 public static function delete_contact_user( $post_id )
1111 {
1112 if ( get_post_type($post_id) == 'contact' )
1113 {
1114 // If the contact being deleted has a user_id meta_key, delete the user with that ID if they're a contact or agent
1115 $contact_user_id = get_post_meta ($post_id, '_user_id', TRUE );
1116 if ( !empty($contact_user_id) )
1117 {
1118 $user_meta = get_userdata($contact_user_id);
1119 $user_roles = $user_meta->roles;
1120 $user_role = array_shift($user_roles);
1121
1122 if ( in_array($user_role, array( 'property_hive_contact' )) )
1123 {
1124 // Include user admin functions to get access to wp_delete_user().
1125 require_once ABSPATH . 'wp-admin/includes/user.php';
1126 wp_delete_user( $contact_user_id );
1127 }
1128 }
1129 }
1130 }
1131
1132 public static function delete_contact_user_link( $user_id )
1133 {
1134 global $post;
1135
1136 $args = array(
1137 'post_type' => 'contact',
1138 'nopaging' => true,
1139 'meta_query' => array(
1140 array(
1141 'key' => '_user_id',
1142 'value' => (int)$user_id,
1143 )
1144 )
1145 );
1146 $agent_query = new WP_Query( $args );
1147 if ( $agent_query->have_posts() )
1148 {
1149 while ( $agent_query->have_posts() )
1150 {
1151 $agent_query->the_post();
1152
1153 delete_post_meta( $post->ID, '_user_id' );
1154
1155 wp_reset_postdata();
1156 }
1157 }
1158 }
1159
1160 public static function ensure_property_floor_area_to_set( $post_id, $post, $update )
1161 {
1162 // $post_id and $post are required
1163 if ( empty( $post_id ) || empty( $post ) ) {
1164 return;
1165 }
1166
1167 // Dont' save meta boxes for revisions or autosaves
1168 if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
1169 return;
1170 }
1171
1172 if ( $post->post_type !== 'property' ) {
1173 return;
1174 }
1175
1176 $property = new PH_Property( $post_id );
1177
1178
1179 if ( $property->department == 'commercial' || ph_get_custom_department_based_on( $property->department ) == 'commercial' )
1180 {
1181 if ( ($property->floor_area_to_sqft == '' || $property->floor_area_to_sqft == '0') && ( $property->floor_area_from_sqft != '' && $property->floor_area_from_sqft != '0' ) )
1182 {
1183 update_post_meta( $post_id, '_floor_area_to', $property->floor_area_from );
1184 update_post_meta( $post_id, '_floor_area_to_sqft', $property->floor_area_from_sqft);
1185 }
1186 }
1187 }
1188
1189 /**
1190 * Check if we're saving, then trigger an action based on the post type
1191 *
1192 * @param int $post_id
1193 * @param object $post
1194 */
1195 public static function create_concatenated_indexable_meta( $post_id, $post, $update )
1196 {
1197 // $post_id and $post are required
1198 if ( empty( $post_id ) || empty( $post ) ) {
1199 return;
1200 }
1201
1202 // Dont' save meta boxes for revisions or autosaves
1203 if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
1204 return;
1205 }
1206
1207 if ( $post->post_type !== 'property' && $post->post_type !== 'contact' ) {
1208 return;
1209 }
1210
1211 if ( $post->post_type == 'property' )
1212 {
1213 $property = new PH_Property( $post_id );
1214
1215 // Set field of concatenated address
1216 self::remove_all_duplicated_post_meta( $post_id, '_address_concatenated' );
1217
1218 update_post_meta( $post_id, '_address_concatenated', $property->get_formatted_full_address() );
1219
1220 // Set field of concatenated features
1221 self::remove_all_duplicated_post_meta( $post_id, '_features_concatenated' );
1222
1223 $features_concat_array = $property->get_features();
1224
1225 $features_concat = implode('|', array_filter($features_concat_array));
1226 update_post_meta($post_id, '_features_concatenated', $features_concat);
1227
1228 // Set field of concatenated descriptions information
1229 self::remove_all_duplicated_post_meta( $post_id, '_descriptions_concatenated' );
1230
1231 $descs_concat = $property->get_formatted_description();
1232 update_post_meta($post_id, '_descriptions_concatenated', $descs_concat);
1233 }
1234
1235 if ( $post->post_type == 'contact' )
1236 {
1237 $contact = new PH_Contact( $post_id );
1238
1239 // Set field of concatenated address
1240 self::remove_all_duplicated_post_meta( $post_id, '_address_concatenated' );
1241
1242 update_post_meta( $post_id, '_address_concatenated', $contact->get_formatted_full_address() );
1243 }
1244 }
1245
1246 private static function remove_all_duplicated_post_meta( $post_id, $meta_key )
1247 {
1248 $current_meta_array = get_post_meta( $post_id, $meta_key );
1249 if ( is_array($current_meta_array) && count($current_meta_array) > 1 )
1250 {
1251 delete_post_meta( $post_id, $meta_key );
1252 }
1253 }
1254
1255 public static function update_address_concatenated()
1256 {
1257 $args = array(
1258 'post_type' => 'property',
1259 'fields' => 'ids',
1260 'post_status' => array( 'publish', 'draft'),
1261 'meta_query' => array(
1262 array(
1263 'key' => '_address_concatenated',
1264 'compare' => 'NOT EXISTS'
1265 )
1266 ),
1267 'nopaging' => true,
1268 'orderby' => 'rand',
1269 'suppress_filters' => true,
1270 );
1271 $property_query = new WP_Query($args);
1272
1273 if ( $property_query->have_posts() )
1274 {
1275 while ( $property_query->have_posts() )
1276 {
1277 $property_query->the_post();
1278
1279 $property = new PH_Property( get_the_ID() );
1280
1281 // Set field of concatenated address
1282 update_post_meta( get_the_ID(), '_address_concatenated', $property->get_formatted_full_address() );
1283 }
1284 }
1285
1286 wp_reset_postdata();
1287
1288 $args = array(
1289 'post_type' => 'contact',
1290 'fields' => 'ids',
1291 'post_status' => 'publish',
1292 'meta_query' => array(
1293 array(
1294 'key' => '_address_concatenated',
1295 'compare' => 'NOT EXISTS'
1296 )
1297 ),
1298 'nopaging' => true,
1299 'orderby' => 'rand',
1300 'suppress_filters' => true,
1301 );
1302 $contact_query = new WP_Query($args);
1303
1304 if ( $contact_query->have_posts() )
1305 {
1306 while ( $contact_query->have_posts() )
1307 {
1308 $contact_query->the_post();
1309
1310 $contact = new PH_Contact( get_the_ID() );
1311
1312 // Set field of concatenated address
1313 update_post_meta( get_the_ID(), '_address_concatenated', $contact->get_formatted_full_address() );
1314 }
1315 }
1316
1317 wp_reset_postdata();
1318 }
1319
1320 /**
1321 * @param int $post_id
1322 * @param object $post
1323 */
1324 public static function store_related_viewings( $post_id, $post, $update )
1325 {
1326 // $post_id and $post are required
1327 if ( empty( $post_id ) || empty( $post ) ) {
1328 return;
1329 }
1330
1331 // Dont' save meta boxes for revisions or autosaves
1332 if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
1333 return;
1334 }
1335
1336 if ( $post->post_type !== 'property' && $post->post_type !== 'contact' && $post->post_type !== 'viewing' ) {
1337 return;
1338 }
1339
1340 if ( get_option('propertyhive_module_disabled_viewings', '') == 'yes' ) {
1341 return;
1342 }
1343
1344 $viewing_ids = array();
1345
1346 switch ( $post->post_type )
1347 {
1348 case "property":
1349 {
1350 // get all viewings for this property
1351 $meta_query = array(
1352 array(
1353 'key' => '_property_id',
1354 'value' => $post_id,
1355 )
1356 );
1357
1358 $args = array(
1359 'fields' => 'ids',
1360 'post_type' => 'viewing',
1361 'nopaging' => true,
1362 'post_status' => 'publish',
1363 'meta_query' => $meta_query,
1364 'orderby' => 'none'
1365 );
1366
1367 $viewings_query = new WP_Query( $args );
1368
1369 if ( $viewings_query->have_posts() )
1370 {
1371 while ( $viewings_query->have_posts() )
1372 {
1373 $viewings_query->the_post();
1374
1375 $viewing_ids[] = get_the_ID();
1376 }
1377 }
1378 wp_reset_postdata();
1379
1380 break;
1381 }
1382 case "contact":
1383 {
1384 // get all viewings for this contact
1385 $meta_query = array(
1386 array(
1387 'key' => '_applicant_contact_id',
1388 'value' => $post_id,
1389 )
1390 );
1391
1392 $args = array(
1393 'fields' => 'ids',
1394 'post_type' => 'viewing',
1395 'nopaging' => true,
1396 'post_status' => 'publish',
1397 'meta_query' => $meta_query,
1398 'orderby' => 'none'
1399 );
1400
1401 $viewings_query = new WP_Query( $args );
1402
1403 if ( $viewings_query->have_posts() )
1404 {
1405 while ( $viewings_query->have_posts() )
1406 {
1407 $viewings_query->the_post();
1408
1409 $viewing_ids[] = get_the_ID();
1410 }
1411 }
1412 wp_reset_postdata();
1413
1414 break;
1415 }
1416 case "viewing":
1417 {
1418 $viewing_ids[] = $post_id;
1419
1420 break;
1421 }
1422 }
1423
1424 if ( !empty($viewing_ids) )
1425 {
1426 foreach ( $viewing_ids as $viewing_id )
1427 {
1428 $viewing = new PH_Viewing( $viewing_id );
1429
1430 $related_viewings = $viewing->get_related_viewings();
1431
1432 update_post_meta( $post_id, '_related_viewings', $related_viewings );
1433
1434 if ( !empty($related_viewings['all']) )
1435 {
1436 foreach ( $related_viewings['all'] as $related_viewing_id )
1437 {
1438 $other_viewing = new PH_Viewing( $related_viewing_id );
1439
1440 $other_related_viewings = $other_viewing->get_related_viewings();
1441
1442 update_post_meta( $related_viewing_id, '_related_viewings', $other_related_viewings );
1443 }
1444 }
1445 }
1446 }
1447 }
1448
1449 public static function store_related_viewings_meta_change( $meta_id, $object_id, $meta_key, $meta_value )
1450 {
1451 if ( get_post_type($object_id) == 'viewing' && ( $meta_key == '_status' || $meta_key == '_start_date_time' ) )
1452 {
1453 $viewing = new PH_Viewing( $object_id );
1454
1455 $related_viewings = $viewing->get_related_viewings();
1456
1457 update_post_meta( $object_id, '_related_viewings', $related_viewings );
1458
1459 if ( !empty($related_viewings['all']) )
1460 {
1461 foreach ( $related_viewings['all'] as $related_viewing_id )
1462 {
1463 $other_viewing = new PH_Viewing( $related_viewing_id );
1464
1465 $other_related_viewings = $other_viewing->get_related_viewings();
1466
1467 update_post_meta( $related_viewing_id, '_related_viewings', $other_related_viewings );
1468 }
1469 }
1470 }
1471 }
1472 }
1473
1474 new PH_Post_types();
1475