| 1 |
<?php |
| 2 |
/** |
| 3 |
* PropertyHive Office Settings |
| 4 |
* |
| 5 |
* @author PropertyHive |
| 6 |
* @category Admin |
| 7 |
* @package PropertyHive/Admin |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 12 |
|
| 13 |
if ( ! class_exists( 'PH_Settings_Offices' ) ) : |
| 14 |
|
| 15 |
/** |
| 16 |
* PH_Settings_Offices |
| 17 |
*/ |
| 18 |
class PH_Settings_Offices extends PH_Settings_Page { |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructor. |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
$this->id = 'offices'; |
| 25 |
$this->label = __( 'Offices', 'propertyhive' ); |
| 26 |
|
| 27 |
add_filter( 'propertyhive_settings_tabs_array', array( $this, 'add_settings_page' ), 10 ); |
| 28 |
add_action( 'propertyhive_settings_' . $this->id, array( $this, 'output' ) ); |
| 29 |
add_action( 'propertyhive_settings_save_' . $this->id, array( $this, 'save' ) ); |
| 30 |
add_action( 'propertyhive_sections_' . $this->id, array( $this, 'output_sections' ) ); |
| 31 |
add_action( 'propertyhive_admin_field_offices', array( $this, 'offices_setting' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get settings array |
| 36 |
* |
| 37 |
* @return array |
| 38 |
*/ |
| 39 |
public function get_settings() { |
| 40 |
|
| 41 |
return apply_filters( 'propertyhive_office_settings', array( |
| 42 |
|
| 43 |
array( 'title' => __( 'Offices', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'offices_options' ), |
| 44 |
|
| 45 |
array( |
| 46 |
'type' => 'offices', |
| 47 |
), |
| 48 |
|
| 49 |
array( 'type' => 'sectionend', 'id' => 'offices_options' ), |
| 50 |
)); |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get add/edit office settings array |
| 56 |
* |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
public function get_office_settings() { |
| 60 |
|
| 61 |
global $current_section; |
| 62 |
|
| 63 |
$current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id']; |
| 64 |
|
| 65 |
$args = array( |
| 66 |
|
| 67 |
array( 'title' => __( ( $current_section == 'add' ? 'Add New Office' : 'Edit Office Details' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_options' ), |
| 68 |
|
| 69 |
array( |
| 70 |
'title' => __( 'Office Name', 'propertyhive' ), |
| 71 |
'id' => 'office_name', |
| 72 |
//'css' => 'width:50px;', |
| 73 |
'default' => get_the_title($current_id), |
| 74 |
'type' => 'text', |
| 75 |
'desc_tip' => false, |
| 76 |
), |
| 77 |
|
| 78 |
array( |
| 79 |
'title' => __( 'Address Line 1', 'propertyhive' ), |
| 80 |
'id' => '_office_address_1', |
| 81 |
//'css' => 'width:50px;', |
| 82 |
'default' => get_post_meta($current_id, '_office_address_1', TRUE), |
| 83 |
'type' => 'text', |
| 84 |
'desc_tip' => false, |
| 85 |
), |
| 86 |
|
| 87 |
array( |
| 88 |
'title' => __( 'Address Line 2', 'propertyhive' ), |
| 89 |
'id' => '_office_address_2', |
| 90 |
//'css' => 'width:50px;', |
| 91 |
'default' => get_post_meta($current_id, '_office_address_2', TRUE), |
| 92 |
'type' => 'text', |
| 93 |
'desc_tip' => false, |
| 94 |
), |
| 95 |
|
| 96 |
array( |
| 97 |
'title' => __( 'Address Line 3', 'propertyhive' ), |
| 98 |
'id' => '_office_address_3', |
| 99 |
//'css' => 'width:50px;', |
| 100 |
'default' => get_post_meta($current_id, '_office_address_3', TRUE), |
| 101 |
'type' => 'text', |
| 102 |
'desc_tip' => false, |
| 103 |
), |
| 104 |
|
| 105 |
array( |
| 106 |
'title' => __( 'Address Line 4', 'propertyhive' ), |
| 107 |
'id' => '_office_address_4', |
| 108 |
//'css' => 'width:50px;', |
| 109 |
'default' => get_post_meta($current_id, '_office_address_4', TRUE), |
| 110 |
'type' => 'text', |
| 111 |
'desc_tip' => false, |
| 112 |
), |
| 113 |
|
| 114 |
array( |
| 115 |
'title' => __( 'Postcode', 'propertyhive' ), |
| 116 |
'id' => '_office_address_postcode', |
| 117 |
'css' => 'width:85px;', |
| 118 |
'default' => get_post_meta($current_id, '_office_address_postcode', TRUE), |
| 119 |
'type' => 'text', |
| 120 |
'desc_tip' => false, |
| 121 |
), |
| 122 |
|
| 123 |
array( 'type' => 'sectionend', 'id' => 'office_options' ), |
| 124 |
|
| 125 |
array( 'title' => __( 'Contact Details', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_contact_options' ), |
| 126 |
|
| 127 |
); |
| 128 |
|
| 129 |
$departments = ph_get_departments(); |
| 130 |
|
| 131 |
foreach ( $departments as $key => $value ) |
| 132 |
{ |
| 133 |
if ( get_option( 'propertyhive_active_departments_' . str_replace("residential-", "", $key) ) == 'yes' ) |
| 134 |
{ |
| 135 |
$args[] = array( |
| 136 |
'title' => __( 'Telephone Number (' . $value . ')', 'propertyhive' ), |
| 137 |
'id' => '_office_telephone_number_' . str_replace("residential-", "", $key), |
| 138 |
//'css' => 'width:50px;', |
| 139 |
'default' => get_post_meta($current_id, '_office_telephone_number_' . str_replace("residential-", "", $key), TRUE), |
| 140 |
'type' => 'text', |
| 141 |
'desc_tip' => false, |
| 142 |
); |
| 143 |
|
| 144 |
$args[] = array( |
| 145 |
'title' => __( 'Email Address (' . $value . ')', 'propertyhive' ), |
| 146 |
'id' => '_office_email_address_' . str_replace("residential-", "", $key), |
| 147 |
//'css' => 'width:50px;', |
| 148 |
'default' => get_post_meta($current_id, '_office_email_address_' . str_replace("residential-", "", $key), TRUE), |
| 149 |
'type' => 'text', |
| 150 |
'desc_tip' => false, |
| 151 |
); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
$args[] = array( 'type' => 'sectionend', 'id' => 'office_contact_options' ); |
| 156 |
|
| 157 |
$args[] = array( 'title' => __( 'Office Location', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_location_options' ); |
| 158 |
|
| 159 |
$args[] = array( |
| 160 |
'title' => __( 'Latitude', 'propertyhive' ), |
| 161 |
'id' => '_office_latitude', |
| 162 |
//'css' => 'width:50px;', |
| 163 |
'default' => get_post_meta($current_id, '_office_latitude', TRUE), |
| 164 |
'type' => 'text', |
| 165 |
'desc_tip' => false, |
| 166 |
); |
| 167 |
|
| 168 |
$args[] = array( |
| 169 |
'title' => __( 'Longitude', 'propertyhive' ), |
| 170 |
'id' => '_office_longitude', |
| 171 |
//'css' => 'width:50px;', |
| 172 |
'default' => get_post_meta($current_id, '_office_longitude', TRUE), |
| 173 |
'type' => 'text', |
| 174 |
'desc_tip' => false, |
| 175 |
); |
| 176 |
|
| 177 |
$args[] = array( 'type' => 'sectionend', 'id' => 'office_location_options' ); |
| 178 |
|
| 179 |
return apply_filters( 'propertyhive_office_details_settings', $args ); |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Get delete office settings array |
| 185 |
* |
| 186 |
* @return array |
| 187 |
*/ |
| 188 |
public function get_office_delete() { |
| 189 |
|
| 190 |
global $save_button_text, $post; |
| 191 |
|
| 192 |
$save_button_text = __( 'Delete', 'propertyhive' ); |
| 193 |
|
| 194 |
if ( isset($_POST['confirm_removal']) && $_POST['confirm_removal'] == 1 ) |
| 195 |
{ |
| 196 |
// A term has just been deleted |
| 197 |
global $hide_save_button, $show_cancel_button, $cancel_button_href; |
| 198 |
|
| 199 |
$hide_save_button = TRUE; |
| 200 |
$show_cancel_button = TRUE; |
| 201 |
$cancel_button_href = admin_url( 'admin.php?page=ph-settings&tab=offices' ); |
| 202 |
|
| 203 |
$args = array(); |
| 204 |
|
| 205 |
$args[] = array( 'title' => __( 'Successfully Deleted Office', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_delete' ); |
| 206 |
|
| 207 |
$args[] = array( |
| 208 |
'title' => __( 'Office Deleted', 'propertyhive' ), |
| 209 |
'id' => '', |
| 210 |
'html' => __('Office deleted successfully', 'propertyhive' ) . ' <a href="' . admin_url( 'admin.php?page=ph-settings&tab=offices' ) . '">' . __( 'Go Back', 'propertyhive' ) . '</a>', |
| 211 |
'type' => 'html', |
| 212 |
'desc_tip' => false, |
| 213 |
); |
| 214 |
|
| 215 |
$args[] = array( 'type' => 'sectionend', 'id' => 'office_delete' ); |
| 216 |
} |
| 217 |
else |
| 218 |
{ |
| 219 |
$current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id']; |
| 220 |
|
| 221 |
if ($current_id == '') |
| 222 |
{ |
| 223 |
die("ID not passed"); |
| 224 |
} |
| 225 |
else |
| 226 |
{ |
| 227 |
$post_type = get_post_type( $current_id ); |
| 228 |
|
| 229 |
if ( $post_type == 'office' ) |
| 230 |
{ |
| 231 |
$args = array(); |
| 232 |
|
| 233 |
$args[] = array( 'title' => __( 'Delete Office', 'propertyhive' ) . ': ' . get_the_title( $current_id ), 'type' => 'title', 'desc' => '', 'id' => 'office_delete_options' ); |
| 234 |
|
| 235 |
// Get number of properties assigned to this term |
| 236 |
$query_args = array( |
| 237 |
'post_type' => 'property', |
| 238 |
'nopaging' => true, |
| 239 |
'post_status' => array( 'pending', 'auto-draft', 'draft', 'private', 'publish', 'future', 'trash' ), |
| 240 |
'meta_query' => array( |
| 241 |
array( |
| 242 |
'key' => '_office_id', |
| 243 |
'value' => $current_id, |
| 244 |
'compare' => '=' |
| 245 |
) |
| 246 |
) |
| 247 |
); |
| 248 |
$property_query = new WP_Query( $query_args ); |
| 249 |
|
| 250 |
$num_properties = $property_query->found_posts; |
| 251 |
|
| 252 |
// Get number of applicants assigned to this term (future) |
| 253 |
|
| 254 |
if ($num_properties > 0) |
| 255 |
{ |
| 256 |
$alternative_offices = array(); |
| 257 |
|
| 258 |
$query_args = array( |
| 259 |
'post_type' => 'office', |
| 260 |
'nopaging' => true, |
| 261 |
'post__not_in' => array( $current_id ), |
| 262 |
'orderby' => 'title', |
| 263 |
'order' => 'ASC' |
| 264 |
); |
| 265 |
$office_query = new WP_Query( $query_args ); |
| 266 |
|
| 267 |
if ( $office_query->have_posts() ) |
| 268 |
{ |
| 269 |
while ( $office_query->have_posts() ) |
| 270 |
{ |
| 271 |
$office_query->the_post(); |
| 272 |
|
| 273 |
$alternative_terms[$post->ID] = get_the_title(); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
// There are properties assigned to this term |
| 278 |
$args[] = array( |
| 279 |
'title' => __( 'Re-assign to', 'propertyhive' ), |
| 280 |
'id' => 'reassign_to', |
| 281 |
'default' => '', |
| 282 |
'options' => $alternative_terms, |
| 283 |
'type' => 'select', |
| 284 |
'desc_tip' => false, |
| 285 |
'desc' => __( 'There are properties that are assigned to the office being deleted. Which office should they be reassigned to?' , 'propertyhive' ) |
| 286 |
); |
| 287 |
} |
| 288 |
|
| 289 |
$args[] = array( |
| 290 |
'title' => __( 'Confirm removal?', 'propertyhive' ), |
| 291 |
'id' => 'confirm_removal', |
| 292 |
'type' => 'checkbox', |
| 293 |
'desc_tip' => false, |
| 294 |
); |
| 295 |
|
| 296 |
$args[] = array( 'type' => 'sectionend', 'id' => 'office_delete_options' ); |
| 297 |
} |
| 298 |
else |
| 299 |
{ |
| 300 |
die("Trying to delete a post that isn't an office."); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
return apply_filters( 'propertyhive_office_delete_settings', $args ); |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Output the settings |
| 311 |
*/ |
| 312 |
public function output() { |
| 313 |
global $current_section; |
| 314 |
|
| 315 |
if ( $current_section == 'add' ) { |
| 316 |
|
| 317 |
remove_action('propertyhive_admin_field_offices', array( $this, 'offices_setting' )); |
| 318 |
|
| 319 |
$settings = $this->get_office_settings(); |
| 320 |
|
| 321 |
PH_Admin_Settings::output_fields( $settings ); |
| 322 |
|
| 323 |
/*foreach ( $shipping_methods as $method ) { |
| 324 |
if ( strtolower( get_class( $method ) ) == strtolower( $current_section ) && $method->has_settings() ) { |
| 325 |
$method->admin_options(); |
| 326 |
break; |
| 327 |
} |
| 328 |
}*/ |
| 329 |
} elseif ( $current_section == 'edit' ) { |
| 330 |
|
| 331 |
remove_action('propertyhive_admin_field_offices', array( $this, 'offices_setting' )); |
| 332 |
|
| 333 |
$current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id']; |
| 334 |
|
| 335 |
$settings = $this->get_office_settings(); |
| 336 |
|
| 337 |
PH_Admin_Settings::output_fields( $settings ); |
| 338 |
|
| 339 |
} elseif ( $current_section == 'delete' ) { |
| 340 |
|
| 341 |
remove_action('propertyhive_admin_field_offices', array( $this, 'offices_setting' )); |
| 342 |
|
| 343 |
$current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id']; |
| 344 |
|
| 345 |
$settings = $this->get_office_delete(); |
| 346 |
|
| 347 |
PH_Admin_Settings::output_fields( $settings ); |
| 348 |
|
| 349 |
} else { |
| 350 |
$settings = $this->get_settings(); |
| 351 |
|
| 352 |
PH_Admin_Settings::output_fields( $settings ); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Output list of offices |
| 358 |
* |
| 359 |
* @access public |
| 360 |
* @return void |
| 361 |
*/ |
| 362 |
public function offices_setting() { |
| 363 |
global $post; |
| 364 |
|
| 365 |
?> |
| 366 |
<tr valign="top"> |
| 367 |
<th scope="row" class="titledesc"> |
| 368 |
|
| 369 |
</th> |
| 370 |
<td class="forminp forminp-button"> |
| 371 |
<a href="<?php echo admin_url( 'admin.php?page=ph-settings&tab=offices§ion=add' ); ?>" class="button alignright"><?php echo __( 'Add New Office', 'propertyhive' ); ?></a> |
| 372 |
</td> |
| 373 |
</tr> |
| 374 |
<tr valign="top"> |
| 375 |
<th scope="row" class="titledesc"><?php _e( 'Offices', 'propertyhive' ) ?></th> |
| 376 |
<td class="forminp"> |
| 377 |
<table class="ph_offices widefat" cellspacing="0"> |
| 378 |
<thead> |
| 379 |
<tr> |
| 380 |
<th class="primary"><?php _e( 'Primary', 'propertyhive' ); ?></th> |
| 381 |
<th class="name"><?php _e( 'Name', 'propertyhive' ); ?></th> |
| 382 |
<th class="address"><?php _e( 'Address', 'propertyhive' ); ?></th> |
| 383 |
<th class="contact"><?php _e( 'Contact Details', 'propertyhive' ); ?></th> |
| 384 |
<th class="settings"> </th> |
| 385 |
</tr> |
| 386 |
</thead> |
| 387 |
<tbody> |
| 388 |
<?php |
| 389 |
$args = array( |
| 390 |
'post_type' => 'office', |
| 391 |
'nopaging' => true, |
| 392 |
'orderby' => 'title', |
| 393 |
'order' => 'ASC' |
| 394 |
); |
| 395 |
$office_query = new WP_Query($args); |
| 396 |
|
| 397 |
if ($office_query->have_posts()) |
| 398 |
{ |
| 399 |
$num_offices = $office_query->found_posts; |
| 400 |
|
| 401 |
while ($office_query->have_posts()) |
| 402 |
{ |
| 403 |
$office_query->the_post(); |
| 404 |
|
| 405 |
$address = ''; |
| 406 |
$address_part = get_post_meta($post->ID, '_office_address_1', TRUE); |
| 407 |
$address .= $address_part . ', '; |
| 408 |
$address_part = get_post_meta($post->ID, '_office_address_2', TRUE); |
| 409 |
$address .= $address_part . ', '; |
| 410 |
$address_part = get_post_meta($post->ID, '_office_address_3', TRUE); |
| 411 |
$address .= $address_part . ', '; |
| 412 |
$address_part = get_post_meta($post->ID, '_office_address_4', TRUE); |
| 413 |
$address .= $address_part . ' '; |
| 414 |
$address_part = get_post_meta($post->ID, '_office_address_postcode', TRUE); |
| 415 |
$address .= $address_part; |
| 416 |
|
| 417 |
$contact_details = ''; |
| 418 |
|
| 419 |
$departments = ph_get_departments(); |
| 420 |
|
| 421 |
foreach ( $departments as $key => $value ) |
| 422 |
{ |
| 423 |
if ( get_option( 'propertyhive_active_departments_' . str_replace("residential-", "", $key) ) == 'yes' ) |
| 424 |
{ |
| 425 |
$contact_details .= 'T: ' . get_post_meta($post->ID, '_office_telephone_number_' . str_replace("residential-", "", $key), TRUE) . '<br>'; |
| 426 |
$contact_details .= 'E: ' . get_post_meta($post->ID, '_office_email_address_' . str_replace("residential-", "", $key), TRUE) . '<br>'; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
echo '<tr> |
| 431 |
<td width="1%" class="primary"> |
| 432 |
<input type="radio" name="primary" value="' . esc_attr( $post->ID ) . '" ' . checked( get_post_meta($post->ID, 'primary', TRUE), '1', false ) . ' /> |
| 433 |
</td> |
| 434 |
<td class="name"> |
| 435 |
' . get_the_title() . ' |
| 436 |
</td> |
| 437 |
<td class="address"> |
| 438 |
' . $address . ' |
| 439 |
</td> |
| 440 |
<td class="contact"> |
| 441 |
' . $contact_details . ' |
| 442 |
</td> |
| 443 |
<td class="settings"> |
| 444 |
<a class="button" href="' . admin_url( 'admin.php?page=ph-settings&tab=offices§ion=edit&id=' . $post->ID ) . '">' . __( 'Edit', 'propertyhive' ) . '</a> |
| 445 |
'; |
| 446 |
if ( $num_offices > 1 && get_post_meta($post->ID, 'primary', TRUE) != '1' ) |
| 447 |
{ |
| 448 |
echo '<a class="button" href="' . admin_url( 'admin.php?page=ph-settings&tab=offices§ion=delete&id=' . $post->ID ) . '">' . __( 'Delete', 'propertyhive' ) . '</a>'; |
| 449 |
} |
| 450 |
echo ' |
| 451 |
</td> |
| 452 |
</tr>'; |
| 453 |
} |
| 454 |
} |
| 455 |
wp_reset_postdata(); |
| 456 |
?> |
| 457 |
</tbody> |
| 458 |
</table> |
| 459 |
</td> |
| 460 |
</tr> |
| 461 |
<tr valign="top"> |
| 462 |
<th scope="row" class="titledesc"> |
| 463 |
|
| 464 |
</th> |
| 465 |
<td class="forminp forminp-button"> |
| 466 |
<a href="<?php echo admin_url( 'admin.php?page=ph-settings&tab=offices§ion=add' ); ?>" class="button alignright"><?php echo __( 'Add New Office', 'propertyhive' ); ?></a> |
| 467 |
</td> |
| 468 |
</tr> |
| 469 |
<?php |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* Save settings |
| 474 |
*/ |
| 475 |
public function save() { |
| 476 |
global $current_section, $post; |
| 477 |
|
| 478 |
if ( $current_section == 'add' ) { |
| 479 |
|
| 480 |
// TODO: Validate (check for blank fields, and that office name doest exist already) |
| 481 |
|
| 482 |
// Insert office |
| 483 |
$office_post = array( |
| 484 |
'post_title' => ph_clean( $_POST['office_name'] ), |
| 485 |
'post_content' => '', |
| 486 |
'post_status' => 'publish', |
| 487 |
'post_type' => 'office', |
| 488 |
'comment_status' => 'closed', |
| 489 |
'ping_status' => 'closed', |
| 490 |
); |
| 491 |
|
| 492 |
// Insert the post into the database |
| 493 |
$office_post_id = wp_insert_post( $office_post ); |
| 494 |
|
| 495 |
// TODO: Check for errors returned from wp_insert_post() |
| 496 |
// TODO: Add meta information |
| 497 |
|
| 498 |
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>' ); |
| 499 |
|
| 500 |
} elseif ( $current_section == 'edit' ) { |
| 501 |
|
| 502 |
$current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id']; |
| 503 |
|
| 504 |
// TODO: Validate |
| 505 |
// TODO: Make sure this ID belongs to an office |
| 506 |
// TODO: Update slug? |
| 507 |
|
| 508 |
// Update office |
| 509 |
$office_post = array( |
| 510 |
'ID' => $current_id, |
| 511 |
'post_title' => ph_clean( $_POST['office_name'] ) |
| 512 |
); |
| 513 |
|
| 514 |
wp_update_post( $office_post ); |
| 515 |
|
| 516 |
$office_post_id = $current_id; |
| 517 |
|
| 518 |
// TODO: Check for errors returned from wp_update_post() |
| 519 |
// TODO: Update meta information |
| 520 |
|
| 521 |
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>' ); |
| 522 |
|
| 523 |
} elseif ( $current_section == 'delete' ) { |
| 524 |
|
| 525 |
if ( isset($_POST['confirm_removal']) && $_POST['confirm_removal'] == '1' ) |
| 526 |
{ |
| 527 |
$current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id']; |
| 528 |
|
| 529 |
// Get number of properties assigned to this term |
| 530 |
$query_args = array( |
| 531 |
'post_type' => 'property', |
| 532 |
'nopaging' => true, |
| 533 |
'post_status' => array( 'pending', 'auto-draft', 'draft', 'private', 'publish', 'future', 'trash' ), |
| 534 |
'meta_query' => array( |
| 535 |
array( |
| 536 |
'key' => '_office_id', |
| 537 |
'value' => $current_id, |
| 538 |
'compare' => '=' |
| 539 |
) |
| 540 |
) |
| 541 |
); |
| 542 |
$property_query = new WP_Query( $query_args ); |
| 543 |
|
| 544 |
if ( $property_query->have_posts() ) |
| 545 |
{ |
| 546 |
if ( !isset($_POST['reassign_to']) || ( isset( $_POST['reassign_to'] ) && empty( $_POST['reassign_to'] ) ) ) |
| 547 |
{ |
| 548 |
die("Not assigning properties to new office. Please try again"); |
| 549 |
} |
| 550 |
else |
| 551 |
{ |
| 552 |
$post_type = get_post_type( (int)$_POST['reassign_to'] ); |
| 553 |
|
| 554 |
if ( $post_type != 'office' ) |
| 555 |
{ |
| 556 |
die("New office isn't of type office. It's of type: " . $post_type); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
while ( $property_query->have_posts() ) |
| 561 |
{ |
| 562 |
$property_query->the_post(); |
| 563 |
|
| 564 |
update_post_meta( $post->ID, '_office_id', (int)$_POST['reassign_to'] ); |
| 565 |
|
| 566 |
// TODO: Check for WP_ERROR |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
wp_delete_post( $current_id ); |
| 571 |
|
| 572 |
// TODO: Check for WP_ERROR |
| 573 |
} |
| 574 |
|
| 575 |
} |
| 576 |
|
| 577 |
if ( $current_section != 'delete' ) |
| 578 |
{ |
| 579 |
if ( $current_section != 'add' && $current_section != 'edit' ) |
| 580 |
{ |
| 581 |
// Set all offices as not primary |
| 582 |
// Prevents multiple offices being set as primary |
| 583 |
$args = array( |
| 584 |
'post_type' => 'office', |
| 585 |
'nopaging' => true, |
| 586 |
'orderby' => 'title', |
| 587 |
'order' => 'ASC' |
| 588 |
); |
| 589 |
$office_query = new WP_Query($args); |
| 590 |
|
| 591 |
if ($office_query->have_posts()) |
| 592 |
{ |
| 593 |
while ($office_query->have_posts()) |
| 594 |
{ |
| 595 |
$office_query->the_post(); |
| 596 |
|
| 597 |
// Set selected office as primary |
| 598 |
update_post_meta($post->ID, 'primary', '0'); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
wp_reset_postdata(); |
| 603 |
|
| 604 |
// Set selected office as primary |
| 605 |
update_post_meta( (int)$_POST['primary'], 'primary', '1'); |
| 606 |
} |
| 607 |
else |
| 608 |
{ |
| 609 |
update_post_meta($office_post_id, '_office_address_1', ph_clean( $_POST['_office_address_1'] )); |
| 610 |
update_post_meta($office_post_id, '_office_address_2', ph_clean( $_POST['_office_address_2'] )); |
| 611 |
update_post_meta($office_post_id, '_office_address_3', ph_clean( $_POST['_office_address_3'] )); |
| 612 |
update_post_meta($office_post_id, '_office_address_4', ph_clean( $_POST['_office_address_4'] )); |
| 613 |
update_post_meta($office_post_id, '_office_address_postcode', ph_clean( $_POST['_office_address_postcode'] )); |
| 614 |
|
| 615 |
$departments = ph_get_departments(); |
| 616 |
|
| 617 |
foreach ( $departments as $key => $value ) |
| 618 |
{ |
| 619 |
if ( get_option( 'propertyhive_active_departments_' . str_replace("residential-", "", $key) ) == 'yes' ) |
| 620 |
{ |
| 621 |
update_post_meta($office_post_id, '_office_telephone_number_' . str_replace("residential-", "", $key), (isset($_POST['_office_telephone_number_' . str_replace("residential-", "", $key)])) ? ph_clean( $_POST['_office_telephone_number_' . str_replace("residential-", "", $key)] ) : ''); |
| 622 |
update_post_meta($office_post_id, '_office_email_address_' . str_replace("residential-", "", $key), (isset($_POST['_office_email_address_' . str_replace("residential-", "", $key)])) ? ph_clean( $_POST['_office_email_address_' . str_replace("residential-", "", $key)] ) : ''); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
update_post_meta($office_post_id, '_office_latitude', ph_clean( $_POST['_office_latitude'] )); |
| 627 |
update_post_meta($office_post_id, '_office_longitude', ph_clean( $_POST['_office_longitude'] )); |
| 628 |
|
| 629 |
do_action( 'propertyhive_save_office', $office_post_id ); |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
} |
| 635 |
|
| 636 |
endif; |
| 637 |
|
| 638 |
return new PH_Settings_Offices(); |