PluginProbe
Property Hive / 2.3.0
Property Hive v2.3.0
2.3.1 2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 All 261 releases
propertyhive / includes / admin / settings / class-ph-settings-offices.php

class-ph-settings-offices.php in Property Hive 2.3.0, at includes/admin/settings/class-ph-settings-offices.php

699 lines 33.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:set WordPress.Security.ValidatedSanitizedInput customSanitizingFunctions[] ph_clean
3 // ph_clean() recursively sanitizes text; presence, shape and unslashing checks remain separate.
4
5 /**
6 * PropertyHive Office Settings
7 *
8 * @author PropertyHive
9 * @category Admin
10 * @package PropertyHive/Admin
11 * @version 1.0.0
12 */
13
14 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
15
16 if ( ! class_exists( 'PH_Settings_Offices' ) ) :
17
18 /**
19 * PH_Settings_Offices
20 */
21 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- Legacy public global class PH_Settings_Offices; preserving the existing PH_* class name is required for plugin and extension compatibility.
22 class PH_Settings_Offices extends PH_Settings_Page {
23
24 /**
25 * Constructor.
26 */
27 public function __construct() {
28 $this->id = 'offices';
29 $this->label = __( 'Offices', 'propertyhive' );
30
31 add_filter( 'propertyhive_settings_tabs_array', array( $this, 'add_settings_page' ), 10 );
32 add_action( 'propertyhive_settings_' . $this->id, array( $this, 'output' ) );
33 add_action( 'propertyhive_settings_save_' . $this->id, array( $this, 'save' ) );
34 add_action( 'propertyhive_sections_' . $this->id, array( $this, 'output_sections' ) );
35 add_action( 'propertyhive_admin_field_offices', array( $this, 'offices_setting' ) );
36 }
37
38 /**
39 * Read one scalar value from the verified office settings form.
40 *
41 * @param string $key Posted field name.
42 * @return string
43 */
44 private function get_posted_text( $key ) {
45 // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended -- save() verifies the settings nonce and manage_options before calling this helper; arrays are rejected before the scalar is copied.
46 if ( ! isset( $_POST[ $key ] ) || ! is_scalar( $_POST[ $key ] ) ) {
47 return '';
48 }
49
50 // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- save() verifies the settings nonce before this helper is called; the copied scalar is unslashed immediately below and sanitized before use.
51 $raw_value = $_POST[ $key ];
52 $raw_value = wp_unslash( (string) $raw_value );
53
54 return ph_clean( $raw_value );
55 }
56
57 /**
58 * Get settings array
59 *
60 * @return array
61 */
62 public function get_settings() {
63
64 return apply_filters( 'propertyhive_office_settings', array(
65
66 array( 'title' => __( 'Offices', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'offices_options' ),
67
68 array(
69 'type' => 'offices',
70 ),
71
72 array( 'type' => 'sectionend', 'id' => 'offices_options' ),
73 ));
74
75 }
76
77 /**
78 * Get add/edit office settings array
79 *
80 * @return array
81 */
82 public function get_office_settings() {
83
84 global $current_section;
85
86 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only settings selection; no state change occurs while building the form.
87 $current_id = isset( $_REQUEST['id'] ) && is_scalar( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
88
89 $args = array(
90
91 array( 'title' => ( $current_section == 'add' ? __( 'Add New Office', 'propertyhive' ) : __( 'Edit Office Details', 'propertyhive' ) ), 'type' => 'title', 'desc' => '', 'id' => 'office_options' ),
92
93 array(
94 'title' => __( 'Office Name', 'propertyhive' ),
95 'id' => 'office_name',
96 //'css' => 'width:50px;',
97 'default' => get_the_title($current_id),
98 'type' => 'text',
99 'desc_tip' => false,
100 ),
101
102 array(
103 'title' => __( 'Address Line 1', 'propertyhive' ),
104 'id' => '_office_address_1',
105 //'css' => 'width:50px;',
106 'default' => get_post_meta($current_id, '_office_address_1', TRUE),
107 'type' => 'text',
108 'desc_tip' => false,
109 ),
110
111 array(
112 'title' => __( 'Address Line 2', 'propertyhive' ),
113 'id' => '_office_address_2',
114 //'css' => 'width:50px;',
115 'default' => get_post_meta($current_id, '_office_address_2', TRUE),
116 'type' => 'text',
117 'desc_tip' => false,
118 ),
119
120 array(
121 'title' => __( 'Address Line 3', 'propertyhive' ),
122 'id' => '_office_address_3',
123 //'css' => 'width:50px;',
124 'default' => get_post_meta($current_id, '_office_address_3', TRUE),
125 'type' => 'text',
126 'desc_tip' => false,
127 ),
128
129 array(
130 'title' => __( 'Address Line 4', 'propertyhive' ),
131 'id' => '_office_address_4',
132 //'css' => 'width:50px;',
133 'default' => get_post_meta($current_id, '_office_address_4', TRUE),
134 'type' => 'text',
135 'desc_tip' => false,
136 ),
137
138 array(
139 'title' => __( 'Postcode', 'propertyhive' ),
140 'id' => '_office_address_postcode',
141 'css' => 'width:85px;',
142 'default' => get_post_meta($current_id, '_office_address_postcode', TRUE),
143 'type' => 'text',
144 'desc_tip' => false,
145 ),
146
147 array( 'type' => 'sectionend', 'id' => 'office_options' ),
148
149 array( 'title' => __( 'Contact Details', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_contact_options' ),
150
151 );
152
153 $departments = ph_get_departments();
154
155 foreach ( $departments as $key => $value )
156 {
157 if ( get_option( 'propertyhive_active_departments_' . str_replace("residential-", "", $key) ) == 'yes' )
158 {
159 $args[] = array(
160 'title' => sprintf(
161 /* translators: %s: department (e.g. Sales, Lettings, Commercial) */
162 __( 'Telephone Number (%s)', 'propertyhive' ),
163 $value
164 ),
165 'id' => '_office_telephone_number_' . str_replace("residential-", "", $key),
166 //'css' => 'width:50px;',
167 'default' => get_post_meta($current_id, '_office_telephone_number_' . str_replace("residential-", "", $key), TRUE),
168 'type' => 'text',
169 'desc_tip' => false,
170 );
171
172 $args[] = array(
173 'title' => sprintf(
174 /* translators: %s: department (e.g. Sales, Lettings, Commercial) */
175 __( 'Email Address (%s)', 'propertyhive' ),
176 $value
177 ),
178 'id' => '_office_email_address_' . str_replace("residential-", "", $key),
179 //'css' => 'width:50px;',
180 'default' => get_post_meta($current_id, '_office_email_address_' . str_replace("residential-", "", $key), TRUE),
181 'type' => 'text',
182 'desc_tip' => false,
183 );
184 }
185 }
186
187 $args[] = array( 'type' => 'sectionend', 'id' => 'office_contact_options' );
188
189 $args[] = array( 'title' => __( 'Office Location', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_location_options' );
190
191 $args[] = array(
192 'title' => __( 'Latitude', 'propertyhive' ),
193 'id' => '_office_latitude',
194 //'css' => 'width:50px;',
195 'default' => get_post_meta($current_id, '_office_latitude', TRUE),
196 'type' => 'text',
197 'desc_tip' => false,
198 );
199
200 $args[] = array(
201 'title' => __( 'Longitude', 'propertyhive' ),
202 'id' => '_office_longitude',
203 //'css' => 'width:50px;',
204 'default' => get_post_meta($current_id, '_office_longitude', TRUE),
205 'type' => 'text',
206 'desc_tip' => false,
207 );
208
209 $args[] = array( 'type' => 'sectionend', 'id' => 'office_location_options' );
210
211 return apply_filters( 'propertyhive_office_details_settings', $args );
212
213 }
214
215 /**
216 * Get delete office settings array
217 *
218 * @return array
219 */
220 public function get_office_delete() {
221
222 global $save_button_text, $post;
223
224 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Shared admin settings-view state; this global is intentionally used to control the common settings template and is not an arbitrary application global.
225 $save_button_text = __( 'Delete', 'propertyhive' );
226
227 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- This POST field only selects the delete confirmation display; save() performs the mutation after nonce and capability checks.
228 if ( isset($_POST['confirm_removal']) && $_POST['confirm_removal'] == 1 )
229 {
230 // A term has just been deleted
231 global $hide_save_button, $show_cancel_button, $cancel_button_href;
232
233 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Shared admin settings-view state; this global is intentionally used to control the common settings template and is not an arbitrary application global.
234 $hide_save_button = TRUE;
235 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Shared admin settings-view state; this global is intentionally used to control the common settings template and is not an arbitrary application global.
236 $show_cancel_button = TRUE;
237 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Shared admin settings-view state; this global is intentionally used to control the common settings template and is not an arbitrary application global.
238 $cancel_button_href = admin_url( 'admin.php?page=ph-settings&tab=offices' );
239
240 $args = array();
241
242 $args[] = array( 'title' => __( 'Successfully Deleted Office', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_delete' );
243
244 $args[] = array(
245 'title' => __( 'Office Deleted', 'propertyhive' ),
246 'id' => '',
247 'html' => __('Office deleted successfully', 'propertyhive' ) . ' <a href="' . admin_url( 'admin.php?page=ph-settings&tab=offices' ) . '">' . __( 'Go Back', 'propertyhive' ) . '</a>',
248 'type' => 'html',
249 'desc_tip' => false,
250 );
251
252 $args[] = array( 'type' => 'sectionend', 'id' => 'office_delete' );
253 }
254 else
255 {
256 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only settings selection; no state change occurs while building the form.
257 $current_id = isset( $_REQUEST['id'] ) && is_scalar( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
258
259 if ($current_id == '')
260 {
261 die("ID not passed");
262 }
263 else
264 {
265 $post_type = get_post_type( $current_id );
266
267 if ( $post_type == 'office' )
268 {
269 $args = array();
270
271 $args[] = array( 'title' => __( 'Delete Office', 'propertyhive' ) . ': ' . get_the_title( $current_id ), 'type' => 'title', 'desc' => '', 'id' => 'office_delete_options' );
272
273 // Get number of properties assigned to this term
274 $query_args = array(
275 'post_type' => 'property',
276 'nopaging' => true,
277 'post_status' => array( 'pending', 'auto-draft', 'draft', 'private', 'publish', 'future', 'trash' ),
278 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Count all properties assigned through _office_id across the existing statuses before offering office deletion/reassignment choices.
279 'meta_query' => array(
280 array(
281 'key' => '_office_id',
282 'value' => $current_id,
283 'compare' => '='
284 )
285 )
286 );
287 $property_query = new WP_Query( $query_args );
288
289 $num_properties = $property_query->found_posts;
290
291 // Get number of applicants assigned to this term (future)
292
293 if ($num_properties > 0)
294 {
295 $alternative_terms = array();
296
297 $query_args = array(
298 'post_type' => 'office',
299 'nopaging' => true,
300 // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in -- The delete form must offer every other office as a reassignment target and must exclude the office currently being deleted.
301 'post__not_in' => array( $current_id ),
302 'orderby' => 'title',
303 'order' => 'ASC'
304 );
305 $office_query = new WP_Query( $query_args );
306
307 if ( $office_query->have_posts() )
308 {
309 while ( $office_query->have_posts() )
310 {
311 $office_query->the_post();
312
313 $alternative_terms[$post->ID] = get_the_title();
314 }
315 }
316
317 // There are properties assigned to this term
318 $args[] = array(
319 'title' => __( 'Re-assign to', 'propertyhive' ),
320 'id' => 'reassign_to',
321 'default' => '',
322 'options' => $alternative_terms,
323 'type' => 'select',
324 'desc_tip' => false,
325 'desc' => __( 'There are properties that are assigned to the office being deleted. Which office should they be reassigned to?' , 'propertyhive' )
326 );
327 }
328
329 $args[] = array(
330 'title' => __( 'Confirm removal?', 'propertyhive' ),
331 'id' => 'confirm_removal',
332 'type' => 'checkbox',
333 'desc_tip' => false,
334 );
335
336 $args[] = array( 'type' => 'sectionend', 'id' => 'office_delete_options' );
337 }
338 else
339 {
340 die("Trying to delete a post that isn't an office.");
341 }
342 }
343 }
344
345 return apply_filters( 'propertyhive_office_delete_settings', $args );
346
347 }
348
349 /**
350 * Output the settings
351 */
352 public function output() {
353 global $current_section, $redirect_after_save;
354
355 if ( $current_section == 'add' ) {
356
357 remove_action('propertyhive_admin_field_offices', array( $this, 'offices_setting' ));
358
359 $settings = $this->get_office_settings();
360
361 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Shared admin settings-view state; this global is intentionally used to control the common settings template and is not an arbitrary application global.
362 $redirect_after_save = admin_url('admin.php?page=ph-settings&tab=offices');
363
364 PH_Admin_Settings::output_fields( $settings );
365
366 } elseif ( $current_section == 'edit' ) {
367
368 remove_action('propertyhive_admin_field_offices', array( $this, 'offices_setting' ));
369
370 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only settings selection; no state change occurs while building the form.
371 $current_id = isset( $_REQUEST['id'] ) && is_scalar( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
372
373 $settings = $this->get_office_settings();
374
375 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Shared admin settings-view state; this global is intentionally used to control the common settings template and is not an arbitrary application global.
376 $redirect_after_save = admin_url('admin.php?page=ph-settings&tab=offices');
377
378 PH_Admin_Settings::output_fields( $settings );
379
380 } elseif ( $current_section == 'delete' ) {
381
382 remove_action('propertyhive_admin_field_offices', array( $this, 'offices_setting' ));
383
384 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only settings selection; no state change occurs while building the form.
385 $current_id = isset( $_REQUEST['id'] ) && is_scalar( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
386
387 $settings = $this->get_office_delete();
388
389 PH_Admin_Settings::output_fields( $settings );
390
391 } else {
392 $settings = $this->get_settings();
393
394 PH_Admin_Settings::output_fields( $settings );
395 }
396 }
397
398 /**
399 * Output list of offices
400 *
401 * @access public
402 * @return void
403 */
404 public function offices_setting() {
405 global $post;
406
407 ?>
408 <tr valign="top">
409 <th scope="row" class="titledesc">
410 &nbsp;
411 </th>
412 <td class="forminp forminp-button">
413 <a href="<?php echo esc_url(admin_url( 'admin.php?page=ph-settings&tab=offices&section=add' )); ?>" class="button alignright"><?php echo esc_html(__( 'Add New Office', 'propertyhive' )); ?></a>
414 </td>
415 </tr>
416 <tr valign="top">
417 <th scope="row" class="titledesc"><?php echo esc_html(__( 'Offices', 'propertyhive' )); ?></th>
418 <td class="forminp">
419 <table class="ph_offices widefat" cellspacing="0">
420 <thead>
421 <tr>
422 <th class="primary"><?php echo esc_html(__( 'Primary', 'propertyhive' )); ?></th>
423 <th class="name"><?php echo esc_html(__( 'Name', 'propertyhive' )); ?></th>
424 <th class="address"><?php echo esc_html(__( 'Address', 'propertyhive' )); ?></th>
425 <th class="contact"><?php echo esc_html(__( 'Contact Details', 'propertyhive' )); ?></th>
426 <?php do_action( 'propertyhive_office_table_header_columns' ); ?>
427 <th class="settings">&nbsp;</th>
428 </tr>
429 </thead>
430 <tbody>
431 <?php
432 $args = array(
433 'post_type' => 'office',
434 'nopaging' => true,
435 'orderby' => 'title',
436 'order' => 'ASC'
437 );
438 $office_query = new WP_Query($args);
439
440 if ($office_query->have_posts())
441 {
442 $num_offices = $office_query->found_posts;
443
444 while ($office_query->have_posts())
445 {
446 $office_query->the_post();
447
448 $address = '';
449 $address_part = get_post_meta($post->ID, '_office_address_1', TRUE);
450 $address .= $address_part . ', ';
451 $address_part = get_post_meta($post->ID, '_office_address_2', TRUE);
452 $address .= $address_part . ', ';
453 $address_part = get_post_meta($post->ID, '_office_address_3', TRUE);
454 $address .= $address_part . ', ';
455 $address_part = get_post_meta($post->ID, '_office_address_4', TRUE);
456 $address .= $address_part . ' ';
457 $address_part = get_post_meta($post->ID, '_office_address_postcode', TRUE);
458 $address .= $address_part;
459
460 $contact_details = '';
461
462 $departments = ph_get_departments();
463
464 foreach ( $departments as $key => $value )
465 {
466 if ( get_option( 'propertyhive_active_departments_' . str_replace("residential-", "", $key) ) == 'yes' )
467 {
468 $contact_details .= 'T: ' . esc_html(get_post_meta($post->ID, '_office_telephone_number_' . str_replace("residential-", "", $key), TRUE)) . '<br>';
469 $contact_details .= 'E: ' . esc_html(get_post_meta($post->ID, '_office_email_address_' . str_replace("residential-", "", $key), TRUE)) . '<br>';
470 }
471 }
472
473 echo '<tr>
474 <td width="1%" class="primary">
475 <input type="radio" name="primary" value="' . esc_attr( $post->ID ) . '" ' . checked( get_post_meta($post->ID, 'primary', TRUE), '1', false ) . ' />
476 </td>
477 <td class="name">
478 ' . esc_html(get_the_title()) . '
479 </td>
480 <td class="address">
481 ' . esc_html($address) . '
482 </td>
483 <td class="contact">
484 ' . wp_kses_post( $contact_details ) . '
485 </td>';
486 do_action( 'propertyhive_office_table_row_columns', get_the_ID() );
487 echo '
488 <td class="settings">
489 <a class="button" href="' . esc_url(admin_url( 'admin.php?page=ph-settings&tab=offices&section=edit&id=' . $post->ID )) . '">' . esc_html(__( 'Edit', 'propertyhive' )) . '</a>
490 ';
491 if ( $num_offices > 1 && get_post_meta($post->ID, 'primary', TRUE) != '1' )
492 {
493 echo '<a class="button" href="' . esc_url(admin_url( 'admin.php?page=ph-settings&tab=offices&section=delete&id=' . $post->ID )) . '">' . esc_html(__( 'Delete', 'propertyhive' )) . '</a>';
494 }
495 echo '
496 </td>
497 </tr>';
498 }
499 }
500 wp_reset_postdata();
501 ?>
502 </tbody>
503 </table>
504 </td>
505 </tr>
506 <tr valign="top">
507 <th scope="row" class="titledesc">
508 &nbsp;
509 </th>
510 <td class="forminp forminp-button">
511 <a href="<?php echo esc_url(admin_url( 'admin.php?page=ph-settings&tab=offices&section=add' )); ?>" class="button alignright"><?php echo esc_html(__( 'Add New Office', 'propertyhive' )); ?></a>
512 </td>
513 </tr>
514 <?php
515 }
516
517 /**
518 * Save settings
519 */
520 public function save() {
521 if ( ! current_user_can( 'manage_options' ) || ! isset( $_REQUEST['_wpnonce'] ) || ! is_string( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'propertyhive-settings' ) ) {
522 return;
523 }
524
525 global $current_section, $post;
526
527 if ( in_array( $current_section, array( 'edit', 'delete' ), true ) ) {
528 $office_id = isset( $_REQUEST['id'] ) && is_scalar( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
529 if ( ! $office_id || 'office' !== get_post_type( $office_id ) ) {
530 PH_Admin_Settings::add_error( __( 'Please select a valid office.', 'propertyhive' ) );
531 return;
532 }
533 }
534
535 if ( $current_section == 'add' ) {
536
537 // TODO: Validate (check for blank fields, and that office name doest exist already)
538
539 // Insert office
540 $office_post = array(
541 'post_title' => wp_slash( $this->get_posted_text( 'office_name' ) ),
542 'post_content' => '',
543 'post_status' => 'publish',
544 'post_type' => 'office',
545 'comment_status' => 'closed',
546 'ping_status' => 'closed',
547 );
548
549 // Insert the post into the database
550 $office_post_id = wp_insert_post( $office_post );
551
552 // TODO: Check for errors returned from wp_insert_post()
553 // TODO: Add meta information
554
555 PH_Admin_Settings::add_message( __( 'Office added successfully', 'propertyhive' ) . ' ' . '<a href="' . admin_url( 'admin.php?page=ph-settings&tab=offices' ) . '">' . __( 'Return to offices', 'propertyhive' ) . '</a>' );
556
557 } elseif ( $current_section == 'edit' ) {
558
559 $current_id = isset( $_REQUEST['id'] ) && is_scalar( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
560
561 // TODO: Validate
562 // TODO: Update slug?
563
564 // Update office
565 $office_post = array(
566 'ID' => $current_id,
567 'post_title' => wp_slash( $this->get_posted_text( 'office_name' ) )
568 );
569
570 wp_update_post( $office_post );
571
572 $office_post_id = $current_id;
573
574 // TODO: Check for errors returned from wp_update_post()
575 // TODO: Update meta information
576
577 PH_Admin_Settings::add_message( __( 'Office details updated successfully', 'propertyhive' ) . ' ' . '<a href="' . admin_url( 'admin.php?page=ph-settings&tab=offices' ) . '">' . __( 'Return to offices', 'propertyhive' ) . '</a>' );
578
579 } elseif ( $current_section == 'delete' ) {
580
581 if ( '1' === $this->get_posted_text( 'confirm_removal' ) )
582 {
583 $current_id = isset( $_REQUEST['id'] ) && is_scalar( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
584
585 // Get number of properties assigned to this term
586 $query_args = array(
587 'post_type' => 'property',
588 'nopaging' => true,
589 'post_status' => array( 'pending', 'auto-draft', 'draft', 'private', 'publish', 'future', 'trash' ),
590 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Office deletion must find every property assigned to this office so each one can be reassigned before the office is removed; the office ID is read from the verified admin form.
591 'meta_query' => array(
592 array(
593 'key' => '_office_id',
594 'value' => $current_id,
595 'compare' => '='
596 )
597 )
598 );
599 $property_query = new WP_Query( $query_args );
600
601 if ( $property_query->have_posts() )
602 {
603 if ( !isset($_POST['reassign_to']) || ( isset( $_POST['reassign_to'] ) && empty( $_POST['reassign_to'] ) ) )
604 {
605 die("Not assigning properties to new office. Please try again");
606 }
607 else
608 {
609 $reassign_to = absint( $this->get_posted_text( 'reassign_to' ) );
610 $post_type = get_post_type( $reassign_to );
611
612 if ( $post_type != 'office' )
613 {
614 die("New office isn't of type office. It's of type: " . esc_html($post_type));
615 }
616 }
617
618 while ( $property_query->have_posts() )
619 {
620 $property_query->the_post();
621
622 update_post_meta( $post->ID, '_office_id', $reassign_to );
623
624 // TODO: Check for WP_ERROR
625 }
626 }
627
628 wp_delete_post( $current_id );
629
630 // TODO: Check for WP_ERROR
631 }
632
633 }
634
635 if ( $current_section != 'delete' )
636 {
637 if ( $current_section != 'add' && $current_section != 'edit' )
638 {
639 // Set all offices as not primary
640 // Prevents multiple offices being set as primary
641 $args = array(
642 'post_type' => 'office',
643 'nopaging' => true,
644 'orderby' => 'title',
645 'order' => 'ASC'
646 );
647 $office_query = new WP_Query($args);
648
649 if ($office_query->have_posts())
650 {
651 while ($office_query->have_posts())
652 {
653 $office_query->the_post();
654
655 // Set selected office as primary
656 update_post_meta($post->ID, 'primary', '0');
657 }
658 }
659
660 wp_reset_postdata();
661
662 // Set selected office as primary
663 update_post_meta( absint( $this->get_posted_text( 'primary' ) ), 'primary', '1');
664 }
665 else
666 {
667 update_post_meta($office_post_id, '_office_address_1', wp_slash( $this->get_posted_text( '_office_address_1' ) ));
668 update_post_meta($office_post_id, '_office_address_2', wp_slash( $this->get_posted_text( '_office_address_2' ) ));
669 update_post_meta($office_post_id, '_office_address_3', wp_slash( $this->get_posted_text( '_office_address_3' ) ));
670 update_post_meta($office_post_id, '_office_address_4', wp_slash( $this->get_posted_text( '_office_address_4' ) ));
671 update_post_meta($office_post_id, '_office_address_postcode', wp_slash( $this->get_posted_text( '_office_address_postcode' ) ));
672
673 $departments = ph_get_departments();
674
675 foreach ( $departments as $key => $value )
676 {
677 if ( get_option( 'propertyhive_active_departments_' . str_replace("residential-", "", $key) ) == 'yes' )
678 {
679 $ph_contact_telephone_value = ( isset( $_POST['_office_telephone_number_' . str_replace("residential-", "", $key)] ) && is_string( $_POST['_office_telephone_number_' . str_replace("residential-", "", $key)] ) ) ? sanitize_text_field( wp_unslash( $_POST['_office_telephone_number_' . str_replace("residential-", "", $key)] ) ) : '';
680 update_post_meta( $office_post_id, '_office_telephone_number_' . str_replace("residential-", "", $key), wp_slash( $ph_contact_telephone_value ) );
681 $ph_contact_email_value = ( isset( $_POST['_office_email_address_' . str_replace("residential-", "", $key)] ) && is_string( $_POST['_office_email_address_' . str_replace("residential-", "", $key)] ) ) ? sanitize_text_field( wp_unslash( $_POST['_office_email_address_' . str_replace("residential-", "", $key)] ) ) : '';
682 update_post_meta( $office_post_id, '_office_email_address_' . str_replace("residential-", "", $key), wp_slash( $ph_contact_email_value ) );
683 }
684 }
685
686 update_post_meta($office_post_id, '_office_latitude', wp_slash( $this->get_posted_text( '_office_latitude' ) ));
687 update_post_meta($office_post_id, '_office_longitude', wp_slash( $this->get_posted_text( '_office_longitude' ) ));
688
689 do_action( 'propertyhive_save_office', $office_post_id );
690 }
691 }
692 }
693
694 }
695
696 endif;
697
698 return new PH_Settings_Offices();
699