| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin functions for the property post type |
| 4 |
* |
| 5 |
* @author PropertyHive |
| 6 |
* @category Admin |
| 7 |
* @package PropertyHive/Admin/Post Types |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
if ( ! class_exists( 'PH_Admin_CPT' ) ) { |
| 16 |
include( 'class-ph-admin-cpt.php' ); |
| 17 |
} |
| 18 |
|
| 19 |
if ( ! class_exists( 'PH_Admin_CPT_Property' ) ) : |
| 20 |
|
| 21 |
/** |
| 22 |
* PH_Admin_CPT_Property Class |
| 23 |
*/ |
| 24 |
class PH_Admin_CPT_Property extends PH_Admin_CPT { |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$this->type = 'property'; |
| 31 |
|
| 32 |
// Admin notices |
| 33 |
add_action( 'admin_notices', array( $this, 'property_admin_notices') ); |
| 34 |
|
| 35 |
// Post title fields |
| 36 |
add_filter( 'enter_title_here', array( $this, 'enter_title_here' ), 1, 2 ); |
| 37 |
|
| 38 |
// Featured image text |
| 39 |
//add_filter( 'gettext', array( $this, 'featured_image_gettext' ) ); |
| 40 |
//add_filter( 'media_view_strings', array( $this, 'media_view_strings' ), 10, 2 ); |
| 41 |
|
| 42 |
// Visibility option |
| 43 |
//add_action( 'post_submitbox_misc_actions', array( $this, 'property_data_visibility' ) ); |
| 44 |
|
| 45 |
// Before data updates |
| 46 |
add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); |
| 47 |
add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) ); |
| 48 |
|
| 49 |
// Admin Columns |
| 50 |
add_filter( 'manage_edit-property_columns', array( $this, 'edit_columns' ) ); |
| 51 |
add_action( 'manage_property_posts_custom_column', array( $this, 'custom_columns' ), 2 ); |
| 52 |
add_filter( 'manage_edit-property_sortable_columns', array( $this, 'custom_columns_sort' ) ); |
| 53 |
add_filter( 'request', array( $this, 'custom_columns_orderby' ) ); |
| 54 |
|
| 55 |
// Sort link |
| 56 |
add_filter( 'views_edit-property', array( $this, 'remove_mine' ) ); |
| 57 |
|
| 58 |
// Prouct filtering |
| 59 |
/*add_action( 'restrict_manage_posts', array( $this, 'property_filters' ) ); |
| 60 |
add_filter( 'parse_query', array( $this, 'property_filters_query' ) );*/ |
| 61 |
|
| 62 |
// Enhanced search |
| 63 |
add_filter( 'posts_search', array( $this, 'property_search' ) ); |
| 64 |
|
| 65 |
// Maintain hierarchy of terms |
| 66 |
/*add_filter( 'wp_terms_checklist_args', array( $this, 'disable_checked_ontop' ) );*/ |
| 67 |
|
| 68 |
// Bulk / quick edit |
| 69 |
add_action( 'bulk_edit_custom_box', array( $this, 'bulk_edit' ), 10, 2 ); |
| 70 |
/*add_action( 'quick_edit_custom_box', array( $this, 'quick_edit' ), 10, 2 );*/ |
| 71 |
add_action( 'save_post', array( $this, 'bulk_and_quick_edit_save_post' ), 10, 2 ); |
| 72 |
|
| 73 |
// Uploads |
| 74 |
add_filter( 'upload_dir', array( $this, 'upload_dir' ) ); |
| 75 |
add_action( 'media_upload_downloadable_product', array( $this, 'media_upload_downloadable_product' ) ); |
| 76 |
//add_filter( 'mod_rewrite_rules', array( $this, 'ms_protect_download_rewite_rules' ) ); |
| 77 |
|
| 78 |
// Download permissions |
| 79 |
//add_action( 'propertyhive_process_product_file_download_paths', array( $this, 'process_product_file_download_paths' ), 10, 3 );*/ |
| 80 |
|
| 81 |
// Call PH_Admin_CPT constructor |
| 82 |
parent::__construct(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Output admin notices relating to property |
| 87 |
*/ |
| 88 |
public function property_admin_notices() |
| 89 |
{ |
| 90 |
global $post; |
| 91 |
|
| 92 |
$screen = get_current_screen(); |
| 93 |
if ($screen->id == 'property' && $post->post_type == 'property' && $post->post_parent != 0 && $post->post_parent != '') |
| 94 |
{ |
| 95 |
$message = __( "This property is a commercial unit belonging to", 'propertyhive' ) . ' <a href="' . get_edit_post_link( $post->post_parent ) . '">' . get_the_title($post->post_parent) . '</a>'; |
| 96 |
echo "<div class=\"notice notice-info\"> <p>$message</p></div>"; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Check if we're editing or adding a property |
| 102 |
* @return boolean |
| 103 |
*/ |
| 104 |
private function is_editing_property() { |
| 105 |
if ( ! empty( $_GET['post_type'] ) && 'property' == $_GET['post_type'] ) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
if ( ! empty( $_GET['post'] ) && 'property' == get_post_type( (int)$_GET['post'] ) ) { |
| 109 |
return true; |
| 110 |
} |
| 111 |
if ( ! empty( $_REQUEST['post_id'] ) && 'property' == get_post_type( (int)$_REQUEST['post_id'] ) ) { |
| 112 |
return true; |
| 113 |
} |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Change title boxes in admin. |
| 119 |
* @param string $text |
| 120 |
* @param object $post |
| 121 |
* @return string |
| 122 |
*/ |
| 123 |
public function enter_title_here( $text, $post ) { |
| 124 |
if ( is_admin() && $post->post_type == 'property' ) { |
| 125 |
return __( 'Enter Display Address', 'propertyhive' ); |
| 126 |
} |
| 127 |
|
| 128 |
return $text; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Some functions, like the term recount, require the visibility to be set prior. Lets save that here. |
| 133 |
* |
| 134 |
* @param int $post_id |
| 135 |
*/ |
| 136 |
public function pre_post_update( $post_id ) { |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Forces certain product data based on the product's type, e.g. grouped products cannot have a parent. |
| 142 |
* |
| 143 |
* @param array $data |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
public function wp_insert_post_data( $data ) { |
| 147 |
|
| 148 |
|
| 149 |
return $data; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Change the columns shown in admin. |
| 154 |
*/ |
| 155 |
public function edit_columns( $existing_columns ) { |
| 156 |
|
| 157 |
if ( empty( $existing_columns ) && ! is_array( $existing_columns ) ) { |
| 158 |
$existing_columns = array(); |
| 159 |
} |
| 160 |
|
| 161 |
unset( $existing_columns['title'], $existing_columns['comments'], $existing_columns['date'] ); |
| 162 |
|
| 163 |
$columns = array(); |
| 164 |
$columns['cb'] = '<input type="checkbox" />'; |
| 165 |
$columns['thumb'] = '<span class="ph-image tips" data-tip="' . __( 'Image', 'propertyhive' ) . '">' . __( 'Image', 'propertyhive' ) . '</span>'; |
| 166 |
|
| 167 |
$columns['address'] = __( 'Address', 'propertyhive' ); |
| 168 |
|
| 169 |
if ( get_option( 'propertyhive_active_departments_commercial' ) == 'yes' ) |
| 170 |
{ |
| 171 |
$columns['size'] = __( 'Size', 'propertyhive' ); |
| 172 |
} |
| 173 |
|
| 174 |
$columns['price'] = __( 'Price', 'propertyhive' ); |
| 175 |
|
| 176 |
$columns['status'] = __( 'Marketing Status', 'propertyhive' ); |
| 177 |
|
| 178 |
$columns['owner'] = __( 'Owner / Landlord', 'propertyhive' ); |
| 179 |
|
| 180 |
$columns['negotiator_office'] = __( 'Neg / Office', 'propertyhive' ); |
| 181 |
|
| 182 |
return array_merge( $columns, $existing_columns ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Define our custom columns shown in admin. |
| 187 |
* @param string $column |
| 188 |
*/ |
| 189 |
public function custom_columns( $column ) { |
| 190 |
global $post, $propertyhive, $the_property; |
| 191 |
|
| 192 |
if ( empty( $the_property ) || $the_property->ID != $post->ID ) |
| 193 |
{ |
| 194 |
$the_property = new PH_Property( $post->ID ); |
| 195 |
} |
| 196 |
|
| 197 |
switch ( $column ) { |
| 198 |
case 'thumb' : |
| 199 |
|
| 200 |
$thumb_src = $the_property->get_main_photo_src(); |
| 201 |
|
| 202 |
echo '<a href="' . get_edit_post_link( $post->ID ) . '">'; |
| 203 |
if ($thumb_src !== FALSE) |
| 204 |
{ |
| 205 |
echo '<img src="' . $thumb_src . '" alt="" width="50">'; |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
// placeholder image |
| 210 |
} |
| 211 |
echo '</a>'; |
| 212 |
break; |
| 213 |
case 'address' : |
| 214 |
|
| 215 |
$edit_link = get_edit_post_link( $post->ID ); |
| 216 |
//$title = _draft_or_post_title(); |
| 217 |
$title = $the_property->get_formatted_summary_address(); |
| 218 |
if ( empty($title) ) |
| 219 |
{ |
| 220 |
$title = __( '(no address entered)' ); |
| 221 |
} |
| 222 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 223 |
$can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID ); |
| 224 |
|
| 225 |
echo '<strong><a class="row-title" href="' . esc_url( $edit_link ) .'">' . $title.'</a>'; |
| 226 |
|
| 227 |
$post_status = get_post_status( $post->ID ); |
| 228 |
$post_title_output = ''; |
| 229 |
if ( $post_status == 'draft' || $post_status == 'private' ) |
| 230 |
{ |
| 231 |
$post_title_output = ucfirst($post_status); |
| 232 |
} |
| 233 |
$post_title_output = apply_filters( 'propertyhive_admin_property_column_post_address_output', $post_title_output ); |
| 234 |
if ( $post_title_output != '' ) |
| 235 |
{ |
| 236 |
echo ' - ' . $post_title_output; |
| 237 |
} |
| 238 |
|
| 239 |
echo '</strong>'; |
| 240 |
|
| 241 |
// Excerpt view |
| 242 |
if ( isset( $_GET['mode'] ) && 'excerpt' == $_GET['mode'] ) { |
| 243 |
echo apply_filters( 'the_excerpt', $post->post_excerpt ); |
| 244 |
} |
| 245 |
|
| 246 |
// Get actions |
| 247 |
$actions = array(); |
| 248 |
|
| 249 |
$actions['ref'] = 'Ref: ' . $the_property->_reference_number; |
| 250 |
|
| 251 |
//$actions['id'] = 'ID: ' . $post->ID; |
| 252 |
|
| 253 |
if ( $can_edit_post && 'trash' != $post->post_status ) { |
| 254 |
$actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '" title="' . esc_attr( __( 'Edit this item', 'propertyhive' ) ) . '">' . __( 'Edit', 'propertyhive' ) . '</a>'; |
| 255 |
} |
| 256 |
if ( current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) { |
| 257 |
if ( 'trash' == $post->post_status ) { |
| 258 |
$actions['untrash'] = '<a title="' . esc_attr( __( 'Restore this item from the Trash', 'propertyhive' ) ) . '" href="' . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&action=untrash', $post->ID ) ), 'untrash-post_' . $post->ID ) . '">' . __( 'Restore', 'propertyhive' ) . '</a>'; |
| 259 |
} elseif ( EMPTY_TRASH_DAYS ) { |
| 260 |
//$actions['trash'] = '<a class="submitdelete" title="' . esc_attr( __( 'Move this item to the Trash', 'propertyhive' ) ) . '" href="' . get_delete_post_link( $post->ID ) . '">' . __( 'Trash', 'propertyhive' ) . '</a>'; |
| 261 |
} |
| 262 |
|
| 263 |
if ( 'trash' == $post->post_status || ! EMPTY_TRASH_DAYS ) { |
| 264 |
$actions['delete'] = '<a class="submitdelete" title="' . esc_attr( __( 'Delete this item permanently', 'propertyhive' ) ) . '" href="' . get_delete_post_link( $post->ID, '', true ) . '">' . __( 'Delete Permanently', 'propertyhive' ) . '</a>'; |
| 265 |
} |
| 266 |
} |
| 267 |
if ( $post_type_object->public ) { |
| 268 |
if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) { |
| 269 |
if ( $can_edit_post ) |
| 270 |
$actions['view'] = '<a href="' . esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) . '" title="' . esc_attr( sprintf( __( 'Preview “%s”', 'propertyhive' ), $title ) ) . '" rel="permalink">' . __( 'Preview', 'propertyhive' ) . '</a>'; |
| 271 |
} elseif ( 'trash' != $post->post_status ) { |
| 272 |
$actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”', 'propertyhive' ), $title ) ) . '" rel="permalink">' . __( 'View', 'propertyhive' ) . '</a>'; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
$actions = apply_filters( 'post_row_actions', $actions, $post ); |
| 277 |
|
| 278 |
echo '<div class="row-actions">'; |
| 279 |
|
| 280 |
$i = 0; |
| 281 |
$action_count = sizeof($actions); |
| 282 |
|
| 283 |
foreach ( $actions as $action => $link ) { |
| 284 |
++$i; |
| 285 |
( $i == $action_count ) ? $sep = '' : $sep = ' | '; |
| 286 |
echo '<span class="' . $action . '">' . $link . $sep . '</span>'; |
| 287 |
} |
| 288 |
echo '</div>'; |
| 289 |
|
| 290 |
get_inline_data( $post ); |
| 291 |
|
| 292 |
/* Custom inline data for propertyhive */ |
| 293 |
/*echo ' |
| 294 |
<div class="hidden" id="propertyhive_inline_' . $post->ID . '"> |
| 295 |
<div class="on_market">' . $the_property->on_market . '</div> |
| 296 |
<div class="featured">' . $the_property->featured . '</div> |
| 297 |
</div> |
| 298 |
';*/ |
| 299 |
|
| 300 |
break; |
| 301 |
case 'size' : |
| 302 |
|
| 303 |
$floor_area = $the_property->get_formatted_floor_area(); |
| 304 |
if ( $floor_area != '' ) |
| 305 |
{ |
| 306 |
echo 'Floor Area: ' . $floor_area . '<br>'; |
| 307 |
} |
| 308 |
$site_area = $the_property->get_formatted_site_area(); |
| 309 |
if ( $site_area != '' ) |
| 310 |
{ |
| 311 |
echo 'Site Area: ' . $site_area; |
| 312 |
} |
| 313 |
|
| 314 |
if ( $floor_area == '' && $site_area == '' ) |
| 315 |
{ |
| 316 |
echo '-'; |
| 317 |
} |
| 318 |
|
| 319 |
break; |
| 320 |
case 'price' : |
| 321 |
|
| 322 |
$price = $the_property->get_formatted_price(); |
| 323 |
if ( $price == '' ) |
| 324 |
{ |
| 325 |
$price = '-'; |
| 326 |
} |
| 327 |
else |
| 328 |
{ |
| 329 |
if ( $the_property->_department == 'residential-sales' && $the_property->price_qualifier != '' ) |
| 330 |
{ |
| 331 |
$price .= '<br>' . $the_property->price_qualifier; |
| 332 |
} |
| 333 |
} |
| 334 |
echo $price; |
| 335 |
|
| 336 |
break; |
| 337 |
case 'status' : |
| 338 |
|
| 339 |
$term_list = wp_get_post_terms($post->ID, 'availability', array("fields" => "names")); |
| 340 |
|
| 341 |
if ( !is_wp_error($term_list) && is_array($term_list) && !empty($term_list) ) |
| 342 |
{ |
| 343 |
echo $term_list[0]. '<br>'; |
| 344 |
} |
| 345 |
|
| 346 |
if (isset($the_property->_on_market) && $the_property->_on_market == 'yes') |
| 347 |
{ |
| 348 |
echo __( 'On The Market', 'propertyhive' ); |
| 349 |
} |
| 350 |
else |
| 351 |
{ |
| 352 |
echo __( 'Not On The Market', 'propertyhive' ); |
| 353 |
} |
| 354 |
|
| 355 |
if (isset($the_property->_featured) && $the_property->_featured == 'yes') |
| 356 |
{ |
| 357 |
echo '<br>' . __( 'Featured', 'propertyhive' ); |
| 358 |
} |
| 359 |
|
| 360 |
$marketing_flags = $the_property->marketing_flag; |
| 361 |
if ( $marketing_flags != '' ) |
| 362 |
{ |
| 363 |
echo '<br>' . implode( "<br>", explode( ",", $marketing_flags ) ); |
| 364 |
} |
| 365 |
|
| 366 |
break; |
| 367 |
case 'owner' : |
| 368 |
|
| 369 |
$owner_contact_ids = $the_property->_owner_contact_id; |
| 370 |
if ( |
| 371 |
( !is_array($owner_contact_ids) && $owner_contact_ids != '' && $owner_contact_ids != 0 ) |
| 372 |
|| |
| 373 |
( is_array($owner_contact_ids) && !empty($owner_contact_ids) ) |
| 374 |
) |
| 375 |
{ |
| 376 |
if ( !is_array($owner_contact_ids) ) |
| 377 |
{ |
| 378 |
$owner_contact_ids = array($owner_contact_ids); |
| 379 |
} |
| 380 |
|
| 381 |
foreach ( $owner_contact_ids as $owner_contact_id ) |
| 382 |
{ |
| 383 |
echo get_the_title($owner_contact_id) . '<br>'; |
| 384 |
if ( count($owner_contact_ids) == 1 ) |
| 385 |
{ |
| 386 |
echo '<div class="row-actions">'; |
| 387 |
echo 'T: ' . get_post_meta($owner_contact_id, '_telephone_number', TRUE) . '<br>'; |
| 388 |
echo 'E: ' . get_post_meta($owner_contact_id, '_email_address', TRUE); |
| 389 |
echo '</div>'; |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
else |
| 394 |
{ |
| 395 |
echo '-'; |
| 396 |
} |
| 397 |
break; |
| 398 |
case 'negotiator_office' : |
| 399 |
|
| 400 |
$user_info = get_userdata($the_property->_negotiator_id); |
| 401 |
|
| 402 |
if ($user_info !== FALSE) |
| 403 |
{ |
| 404 |
echo $user_info->display_name . '<br>'; |
| 405 |
} |
| 406 |
|
| 407 |
if ($the_property->_office_id != '') |
| 408 |
{ |
| 409 |
echo get_the_title($the_property->_office_id); |
| 410 |
} |
| 411 |
|
| 412 |
break; |
| 413 |
default : |
| 414 |
break; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* Search by ID, address and reference number |
| 420 |
* |
| 421 |
* @param string $where |
| 422 |
* @return string |
| 423 |
*/ |
| 424 |
public function property_search( $where ) { |
| 425 |
global $pagenow, $wpdb, $wp; |
| 426 |
|
| 427 |
if ( 'edit.php' != $pagenow || ! is_search() || ! isset( $wp->query_vars['s'] ) || 'property' != $wp->query_vars['post_type'] ) { |
| 428 |
return $where; |
| 429 |
} |
| 430 |
|
| 431 |
if ( trim($wp->query_vars['s']) == '' ) |
| 432 |
{ |
| 433 |
return $where; |
| 434 |
} |
| 435 |
|
| 436 |
$search_ids = array(); |
| 437 |
$terms = explode( ',', $wp->query_vars['s'] ); |
| 438 |
|
| 439 |
foreach ( $terms as $term ) |
| 440 |
{ |
| 441 |
if ( is_numeric( $term ) ) |
| 442 |
{ |
| 443 |
$search_ids[] = $term; |
| 444 |
} |
| 445 |
|
| 446 |
// Attempt to get an ID by searching for address and reference number |
| 447 |
$query = $wpdb->prepare( |
| 448 |
"SELECT |
| 449 |
ID |
| 450 |
FROM |
| 451 |
{$wpdb->posts} |
| 452 |
INNER JOIN {$wpdb->postmeta} AS mt1 ON {$wpdb->posts}.ID = mt1.post_id |
| 453 |
WHERE |
| 454 |
( |
| 455 |
(mt1.meta_key='_address_name_number' AND mt1.meta_value LIKE %s) |
| 456 |
OR |
| 457 |
(mt1.meta_key='_address_street' AND mt1.meta_value LIKE %s) |
| 458 |
OR |
| 459 |
(mt1.meta_key='_address_2' AND mt1.meta_value LIKE %s) |
| 460 |
OR |
| 461 |
(mt1.meta_key='_address_3' AND mt1.meta_value LIKE %s) |
| 462 |
OR |
| 463 |
(mt1.meta_key='_address_4' AND mt1.meta_value LIKE %s) |
| 464 |
OR |
| 465 |
(mt1.meta_key='_address_postcode' AND mt1.meta_value LIKE %s) |
| 466 |
OR |
| 467 |
(mt1.meta_key='_reference_number' AND mt1.meta_value = %s) |
| 468 |
) |
| 469 |
AND |
| 470 |
post_type='property' |
| 471 |
GROUP BY ID |
| 472 |
", |
| 473 |
'%' . $wpdb->esc_like( ph_clean( $term ) ) . '%', |
| 474 |
'%' . $wpdb->esc_like( ph_clean( $term ) ) . '%', |
| 475 |
'%' . $wpdb->esc_like( ph_clean( $term ) ) . '%', |
| 476 |
'%' . $wpdb->esc_like( ph_clean( $term ) ) . '%', |
| 477 |
'%' . $wpdb->esc_like( ph_clean( $term ) ) . '%', |
| 478 |
'%' . $wpdb->esc_like( ph_clean( $term ) ) . '%', |
| 479 |
'' . $wpdb->esc_like( ph_clean( $term ) ) . '' |
| 480 |
); |
| 481 |
$search_posts = $wpdb->get_results( $query ); |
| 482 |
$search_posts = wp_list_pluck( $search_posts, 'ID' ); |
| 483 |
|
| 484 |
if ( sizeof( $search_posts ) > 0 ) |
| 485 |
{ |
| 486 |
$search_ids = array_merge( $search_ids, $search_posts ); |
| 487 |
} |
| 488 |
} |
| 489 |
$search_ids = array_filter( array_unique( array_map( 'absint', $search_ids ) ) ); |
| 490 |
if ( sizeof( $search_ids ) > 0 ) |
| 491 |
{ |
| 492 |
$where = str_replace( 'AND (((', "AND ( ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . ")) OR ((", $where ); |
| 493 |
} |
| 494 |
return $where; |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Make property columns sortable |
| 499 |
* |
| 500 |
* https://gist.github.com/906872 |
| 501 |
* |
| 502 |
* @access public |
| 503 |
* @param mixed $columns |
| 504 |
* @return array |
| 505 |
*/ |
| 506 |
public function custom_columns_sort( $columns ) { |
| 507 |
$custom = array( |
| 508 |
'price' => '_price_actual', |
| 509 |
'size' => '_floor_area_from_sqft' |
| 510 |
); |
| 511 |
return wp_parse_args( $custom, $columns ); |
| 512 |
} |
| 513 |
|
| 514 |
/** |
| 515 |
* Property column orderby |
| 516 |
* |
| 517 |
* @access public |
| 518 |
* @param mixed $vars |
| 519 |
* @return array |
| 520 |
*/ |
| 521 |
public function custom_columns_orderby( $vars ) { |
| 522 |
if ( isset( $vars['orderby'] ) ) { |
| 523 |
if ( '_price_actual' == $vars['orderby'] ) { |
| 524 |
$vars = array_merge( $vars, array( |
| 525 |
'meta_key' => '_price_actual', |
| 526 |
'orderby' => 'meta_value_num' |
| 527 |
) ); |
| 528 |
} |
| 529 |
elseif ( '_floor_area_from_sqft' == $vars['orderby'] ) { |
| 530 |
$vars = array_merge( $vars, array( |
| 531 |
'meta_key' => '_floor_area_from_sqft', |
| 532 |
'orderby' => 'meta_value_num' |
| 533 |
) ); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
return $vars; |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* Remove 'Mine' view option |
| 542 |
* |
| 543 |
* @param array $views |
| 544 |
* @return array |
| 545 |
*/ |
| 546 |
public function remove_mine( $views ) { |
| 547 |
global $post_type, $wp_query; |
| 548 |
|
| 549 |
if ( ! current_user_can('edit_others_pages') ) { |
| 550 |
return $views; |
| 551 |
} |
| 552 |
|
| 553 |
if( isset( $views['mine'] ) ) |
| 554 |
unset( $views['mine'] ); |
| 555 |
|
| 556 |
return $views; |
| 557 |
} |
| 558 |
|
| 559 |
/** |
| 560 |
* Show a category filter box |
| 561 |
*/ |
| 562 |
public function propertyhive_filters() { |
| 563 |
global $typenow, $wp_query; |
| 564 |
|
| 565 |
if ( 'property' != $typenow ) { |
| 566 |
return; |
| 567 |
} |
| 568 |
|
| 569 |
echo apply_filters( 'propertyhive_property_filters', $output ); |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* Filter the products in admin based on options |
| 574 |
* |
| 575 |
* @param mixed $query |
| 576 |
*/ |
| 577 |
public function property_filters_query( $query ) { |
| 578 |
global $typenow, $wp_query; |
| 579 |
|
| 580 |
if ( 'property' == $typenow ) { |
| 581 |
|
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* Maintain term hierarchy when editing a property. |
| 587 |
* @param array $args |
| 588 |
* @return array |
| 589 |
*/ |
| 590 |
public function disable_checked_ontop( $args ) { |
| 591 |
if ( 'product_cat' == $args['taxonomy'] ) { |
| 592 |
$args['checked_ontop'] = false; |
| 593 |
} |
| 594 |
|
| 595 |
return $args; |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Custom bulk edit - form |
| 600 |
* |
| 601 |
* @access public |
| 602 |
* @param mixed $column_name |
| 603 |
* @param mixed $post_type |
| 604 |
*/ |
| 605 |
public function bulk_edit( $column_name, $post_type ) { |
| 606 |
if ( 'price' != $column_name || 'property' != $post_type ) { |
| 607 |
return; |
| 608 |
} |
| 609 |
|
| 610 |
include( PH()->plugin_path() . '/includes/admin/views/html-bulk-edit-property.php' ); |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Custom quick edit - form |
| 615 |
* |
| 616 |
* @access public |
| 617 |
* @param mixed $column_name |
| 618 |
* @param mixed $post_type |
| 619 |
*/ |
| 620 |
public function quick_edit( $column_name, $post_type ) { |
| 621 |
if ( 'price' != $column_name || 'property' != $post_type ) { |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
include( PH()->plugin_path() . '/includes/admin/views/html-quick-edit-property.php' ); |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* Quick and bulk edit saving |
| 630 |
* |
| 631 |
* @access public |
| 632 |
* @param int $post_id |
| 633 |
* @param WP_Post $post |
| 634 |
* @return int |
| 635 |
*/ |
| 636 |
public function bulk_and_quick_edit_save_post( $post_id, $post ) { |
| 637 |
// If this is an autosave, our form has not been submitted, so we don't want to do anything. |
| 638 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 639 |
return $post_id; |
| 640 |
} |
| 641 |
|
| 642 |
// Don't save revisions and autosaves |
| 643 |
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { |
| 644 |
return $post_id; |
| 645 |
} |
| 646 |
|
| 647 |
// Check post type is product |
| 648 |
if ( 'property' != $post->post_type ) { |
| 649 |
return $post_id; |
| 650 |
} |
| 651 |
|
| 652 |
// Check user permission |
| 653 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 654 |
return $post_id; |
| 655 |
} |
| 656 |
|
| 657 |
// Check nonces |
| 658 |
if ( ! isset( $_REQUEST['propertyhive_quick_edit_nonce'] ) && ! isset( $_REQUEST['propertyhive_bulk_edit_nonce'] ) ) { |
| 659 |
return $post_id; |
| 660 |
} |
| 661 |
if ( isset( $_REQUEST['propertyhive_quick_edit_nonce'] ) && ! wp_verify_nonce( $_REQUEST['propertyhive_quick_edit_nonce'], 'propertyhive_quick_edit_nonce' ) ) { |
| 662 |
return $post_id; |
| 663 |
} |
| 664 |
if ( isset( $_REQUEST['propertyhive_bulk_edit_nonce'] ) && ! wp_verify_nonce( $_REQUEST['propertyhive_bulk_edit_nonce'], 'propertyhive_bulk_edit_nonce' ) ) { |
| 665 |
return $post_id; |
| 666 |
} |
| 667 |
|
| 668 |
// Get the product and save |
| 669 |
$property = get_property( $post ); |
| 670 |
|
| 671 |
if ( ! empty( $_REQUEST['propertyhive_quick_edit'] ) ) { |
| 672 |
$this->quick_edit_save( $post_id, $property ); |
| 673 |
} else { |
| 674 |
$this->bulk_edit_save( $post_id, $property ); |
| 675 |
} |
| 676 |
|
| 677 |
// Clear transient |
| 678 |
//ph_delete_property_transients( $post_id ); |
| 679 |
|
| 680 |
return $post_id; |
| 681 |
} |
| 682 |
|
| 683 |
/** |
| 684 |
* Quick edit |
| 685 |
*/ |
| 686 |
private function quick_edit_save( $post_id, $property ) { |
| 687 |
global $wpdb; |
| 688 |
|
| 689 |
/* |
| 690 |
// Save fields |
| 691 |
if ( isset( $_REQUEST['_address_name_number'] ) ) { |
| 692 |
update_post_meta( $post_id, '_address_name_number', ph_clean( $_REQUEST['_address_name_number'] ) ); |
| 693 |
}*/ |
| 694 |
|
| 695 |
do_action( 'propertyhive_property_quick_edit_save', $property ); |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Bulk edit |
| 700 |
*/ |
| 701 |
public function bulk_edit_save( $post_id, $property ) { |
| 702 |
|
| 703 |
// Save fields |
| 704 |
if ( ! empty( $_REQUEST['_on_market'] ) ) |
| 705 |
{ |
| 706 |
$on_market = ph_clean( $_REQUEST['_on_market'] ); |
| 707 |
if ( $_REQUEST['_on_market'] != 'yes' ) { $on_market = ''; } // can only be 'yes' or blank |
| 708 |
update_post_meta( $post_id, '_on_market', ph_clean( $on_market ) ); |
| 709 |
} |
| 710 |
|
| 711 |
if ( ! empty( $_REQUEST['_availability'] ) && is_numeric( $_REQUEST['_availability'] ) ) |
| 712 |
{ |
| 713 |
wp_set_post_terms( $post_id, (int)$_REQUEST['_availability'], 'availability' ); |
| 714 |
} |
| 715 |
|
| 716 |
if ( ! empty( $_REQUEST['_negotiator_id'] ) && is_numeric( $_REQUEST['_negotiator_id'] ) && $_REQUEST['_negotiator_id'] != '-1' ) |
| 717 |
{ |
| 718 |
update_post_meta( $post_id, '_negotiator_id', (int)$_REQUEST['_negotiator_id'] ); |
| 719 |
} |
| 720 |
|
| 721 |
if ( ! empty( $_REQUEST['_office_id'] ) && is_numeric( $_REQUEST['_office_id'] ) ) |
| 722 |
{ |
| 723 |
update_post_meta( $post_id, '_office_id', (int)$_REQUEST['_office_id'] ); |
| 724 |
} |
| 725 |
|
| 726 |
do_action( 'propertyhive_property_bulk_edit_save', $property ); |
| 727 |
} |
| 728 |
|
| 729 |
/** |
| 730 |
* Filter the directory for uploads. |
| 731 |
* |
| 732 |
* @param array $pathdata |
| 733 |
* @return array |
| 734 |
*/ |
| 735 |
public function upload_dir( $pathdata ) { |
| 736 |
// Change upload dir for downloadable files |
| 737 |
if ( isset( $_POST['type'] ) && 'downloadable_product' == $_POST['type'] ) { |
| 738 |
if ( empty( $pathdata['subdir'] ) ) { |
| 739 |
$pathdata['path'] = $pathdata['path'] . '/propertyhive_uploads'; |
| 740 |
$pathdata['url'] = $pathdata['url']. '/propertyhive_uploads'; |
| 741 |
$pathdata['subdir'] = '/propertyhive_uploads'; |
| 742 |
} else { |
| 743 |
$new_subdir = '/propertyhive_uploads' . $pathdata['subdir']; |
| 744 |
|
| 745 |
$pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] ); |
| 746 |
$pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] ); |
| 747 |
$pathdata['subdir'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['subdir'] ); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
return $pathdata; |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
endif; |
| 756 |
|
| 757 |
return new PH_Admin_CPT_Property(); |
| 758 |
|