| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin functions for the viewing 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_Viewing' ) ) : |
| 20 |
|
| 21 |
/** |
| 22 |
* PH_Admin_CPT_Viewing Class |
| 23 |
*/ |
| 24 |
class PH_Admin_CPT_Viewing extends PH_Admin_CPT { |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$this->type = 'viewing'; |
| 31 |
|
| 32 |
// Admin notices |
| 33 |
add_action( 'admin_notices', array( $this, 'viewing_admin_notices') ); |
| 34 |
|
| 35 |
// Before data updates |
| 36 |
add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); |
| 37 |
add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) ); |
| 38 |
|
| 39 |
// Admin Columns |
| 40 |
add_filter( 'manage_edit-viewing_columns', array( $this, 'edit_columns' ) ); |
| 41 |
add_action( 'manage_viewing_posts_custom_column', array( $this, 'custom_columns' ), 2 ); |
| 42 |
add_filter( 'list_table_primary_column', array( $this, 'set_primary_column' ), 10, 2 ); |
| 43 |
add_filter( 'post_row_actions', array( $this, 'remove_actions' ), 10, 2 ); |
| 44 |
add_filter( 'manage_edit-viewing_sortable_columns', array( $this, 'custom_columns_sort' ) ); |
| 45 |
add_filter( 'request', array( $this, 'custom_columns_orderby' ) ); |
| 46 |
|
| 47 |
// Bulk / quick edit |
| 48 |
add_filter( 'bulk_actions-edit-viewing', array( $this, 'remove_bulk_actions') ); |
| 49 |
/*add_action( 'bulk_edit_custom_box', array( $this, 'bulk_edit' ), 10, 2 ); |
| 50 |
add_action( 'quick_edit_custom_box', array( $this, 'quick_edit' ), 10, 2 ); |
| 51 |
add_action( 'save_post', array( $this, 'bulk_and_quick_edit_save_post' ), 10, 2 );*/ |
| 52 |
|
| 53 |
add_action( 'add_post_meta', array( $this, 'check_viewing_feedback_add' ), 10, 3 ); |
| 54 |
add_action( 'update_post_meta', array( $this, 'check_viewing_feedback_update' ), 10, 4 ); |
| 55 |
|
| 56 |
// Call PH_Admin_CPT constructor |
| 57 |
parent::__construct(); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Output admin notices relating to viewing |
| 62 |
*/ |
| 63 |
public function viewing_admin_notices() |
| 64 |
{ |
| 65 |
global $post; |
| 66 |
|
| 67 |
$screen = get_current_screen(); |
| 68 |
|
| 69 |
if ( $screen->id == 'viewing' && isset($_GET['post']) && get_post_type($_GET['post']) == 'viewing' ) |
| 70 |
{ |
| 71 |
$viewing = new PH_Viewing((int)$_GET['post']); |
| 72 |
$related_viewings = $viewing->_related_viewings; |
| 73 |
|
| 74 |
// There is either a previous or next viewing for this applicant/property combination |
| 75 |
if ( |
| 76 |
!in_array( $viewing->_status, array('cancelled', 'no_show') ) |
| 77 |
&& |
| 78 |
is_array($related_viewings) |
| 79 |
&& |
| 80 |
isset($related_viewings['previous']) && isset($related_viewings['next']) |
| 81 |
&& |
| 82 |
( count($related_viewings['previous']) > 0 || count($related_viewings['next']) > 0 ) |
| 83 |
) |
| 84 |
{ |
| 85 |
$message = sprintf( |
| 86 |
/* translators: %s: viewing number (e.g. 1, 2, 3) */ |
| 87 |
__( 'This is viewing number %s for this applicant at this property.', 'propertyhive' ), |
| 88 |
number_format_i18n( count( $related_viewings['previous'] ) + 1 ) |
| 89 |
) . '<br>'; |
| 90 |
|
| 91 |
if ( count($related_viewings['previous']) > 0 ) |
| 92 |
{ |
| 93 |
$previous_viewing_id = end($related_viewings['previous']); |
| 94 |
$message .= '<a href="' . esc_url(get_edit_post_link( $previous_viewing_id, '' )) . '"><< '. esc_html(__( 'Go to previous', 'propertyhive' )) . '</a>'; |
| 95 |
} |
| 96 |
|
| 97 |
if ( count($related_viewings['next']) > 0 ) |
| 98 |
{ |
| 99 |
$next_viewing_id = $related_viewings['next'][0]; |
| 100 |
|
| 101 |
// If there are links to next and previous, show a divider |
| 102 |
if ( isset($previous_viewing_id) ) |
| 103 |
{ |
| 104 |
$message .= ' | '; |
| 105 |
} |
| 106 |
|
| 107 |
$message .= '<a href="' . esc_url(get_edit_post_link( $next_viewing_id, '' )) . '">'. esc_html(__( 'Go to next', 'propertyhive' )) . ' >></a>'; |
| 108 |
} |
| 109 |
|
| 110 |
echo "<div class=\"notice notice-info\"> <p>$message</p></div>"; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Check if we're editing or adding a viewing |
| 117 |
* @return boolean |
| 118 |
*/ |
| 119 |
private function is_editing_viewing() { |
| 120 |
if ( ! empty( $_GET['post_type'] ) && 'viewing' == $_GET['post_type'] ) { |
| 121 |
return true; |
| 122 |
} |
| 123 |
if ( ! empty( $_GET['post'] ) && 'viewing' == get_post_type( (int)$_GET['post'] ) ) { |
| 124 |
return true; |
| 125 |
} |
| 126 |
if ( ! empty( $_REQUEST['post_id'] ) && 'viewing' == get_post_type( (int)$_REQUEST['post_id'] ) ) { |
| 127 |
return true; |
| 128 |
} |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @param int $post_id |
| 134 |
*/ |
| 135 |
public function pre_post_update( $post_id ) { |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* @param array $data |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
public function wp_insert_post_data( $data ) { |
| 144 |
|
| 145 |
|
| 146 |
return $data; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Change the columns shown in admin. |
| 151 |
*/ |
| 152 |
public function edit_columns( $existing_columns ) { |
| 153 |
|
| 154 |
if ( empty( $existing_columns ) && ! is_array( $existing_columns ) ) { |
| 155 |
$existing_columns = array(); |
| 156 |
} |
| 157 |
|
| 158 |
unset( $existing_columns['title'], $existing_columns['comments'], $existing_columns['date'] ); |
| 159 |
|
| 160 |
$columns = array(); |
| 161 |
$columns['cb'] = '<input type="checkbox" />'; |
| 162 |
$columns['start_date_time'] = __( 'Viewing Date / Time', 'propertyhive' ); |
| 163 |
$columns['property'] = __( 'Property', 'propertyhive' ); |
| 164 |
$columns['applicant'] = __( 'Applicant(s)', 'propertyhive' ); |
| 165 |
$columns['status'] = __( 'Status', 'propertyhive' ); |
| 166 |
$columns['negotiators'] = __( 'Attending Negotiators', 'propertyhive' ); |
| 167 |
|
| 168 |
return array_merge( $columns, $existing_columns ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Define our custom columns shown in admin. |
| 173 |
* @param string $column |
| 174 |
*/ |
| 175 |
public function custom_columns( $column ) { |
| 176 |
global $post, $propertyhive, $the_viewing; |
| 177 |
|
| 178 |
if ( empty( $the_viewing ) || $the_viewing->ID != $post->ID ) |
| 179 |
{ |
| 180 |
$the_viewing = new PH_Viewing( $post->ID ); |
| 181 |
} |
| 182 |
|
| 183 |
switch ( $column ) { |
| 184 |
case 'start_date_time' : |
| 185 |
|
| 186 |
$edit_link = get_edit_post_link( $post->ID ); |
| 187 |
//$title = _draft_or_post_title(); |
| 188 |
$title = date("H:i jS F Y", strtotime($the_viewing->start_date_time)); |
| 189 |
|
| 190 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 191 |
$can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID ); |
| 192 |
|
| 193 |
echo '<strong><a class="row-title" href="' . esc_url( $edit_link ) .'">' . esc_html($title) . '</a></strong>'; |
| 194 |
break; |
| 195 |
case 'property' : |
| 196 |
|
| 197 |
if ( $the_viewing->property_id != '' ) |
| 198 |
{ |
| 199 |
$property = new PH_Property((int)$the_viewing->property_id); |
| 200 |
echo esc_html($property->get_formatted_full_address()); |
| 201 |
} |
| 202 |
else |
| 203 |
{ |
| 204 |
echo '-'; |
| 205 |
} |
| 206 |
break; |
| 207 |
case 'applicant' : |
| 208 |
$applicant_contact_ids = get_post_meta( $post->ID, '_applicant_contact_id' ); |
| 209 |
if ( is_array($applicant_contact_ids) && !empty($applicant_contact_ids) ) |
| 210 |
{ |
| 211 |
$applicants = array(); |
| 212 |
foreach ( $applicant_contact_ids as $applicant_contact_id ) |
| 213 |
{ |
| 214 |
$applicants[] = esc_html(get_the_title($applicant_contact_id)); |
| 215 |
} |
| 216 |
echo implode("<br>", $applicants); |
| 217 |
} |
| 218 |
else |
| 219 |
{ |
| 220 |
echo '-'; |
| 221 |
} |
| 222 |
|
| 223 |
break; |
| 224 |
case 'status' : |
| 225 |
|
| 226 |
echo esc_html(__( ucwords(str_replace("_", " ", $the_viewing->status)), 'propertyhive' )); |
| 227 |
if ( $the_viewing->status == 'pending' ) |
| 228 |
{ |
| 229 |
echo '<br>'; |
| 230 |
// confirmation status |
| 231 |
if ( $the_viewing->all_confirmed == 'yes' ) |
| 232 |
{ |
| 233 |
echo esc_html(__( 'All Parties Confirmed', 'propertyhive' )); |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
echo esc_html(__( 'Awaiting Confirmation', 'propertyhive' )); |
| 238 |
} |
| 239 |
} |
| 240 |
if ( $the_viewing->status == 'carried_out' ) |
| 241 |
{ |
| 242 |
echo '<br>'; |
| 243 |
switch ( $the_viewing->feedback_status ) |
| 244 |
{ |
| 245 |
case "interested": { echo esc_html(__( 'Applicant Interested', 'propertyhive' )); break; } |
| 246 |
case "not_interested": { echo esc_html(__( 'Applicant Not Interested', 'propertyhive' )); break; } |
| 247 |
case "not_required": { echo esc_html(__( 'Feedback Not Required', 'propertyhive' )); break; } |
| 248 |
default: { echo esc_html(__( 'Awaiting Feedback', 'propertyhive' )); } |
| 249 |
} |
| 250 |
|
| 251 |
if ( $the_viewing->feedback_status == 'interested' || $the_viewing->feedback_status == 'not_interested' ) |
| 252 |
{ |
| 253 |
echo '<br>' . esc_html( $the_viewing->feedback_passed_on == 'yes' ? __( 'Feedback Passed On', 'propertyhive' ) : __( 'Feedback Not Passed On', 'propertyhive' ) ); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
// Add text if this a second, third etc viewing |
| 258 |
$related_viewings = get_post_meta( $post->ID, '_related_viewings', TRUE ); |
| 259 |
if ( isset($related_viewings['previous']) && count($related_viewings['previous']) > 0 ) |
| 260 |
{ |
| 261 |
echo '<br>' . ph_ordinal_suffix(count($related_viewings['previous'])+1) . ' Viewing' ; |
| 262 |
} |
| 263 |
|
| 264 |
break; |
| 265 |
case 'negotiators' : |
| 266 |
$negotiator_ids = get_post_meta( $post->ID, '_negotiator_id' ); |
| 267 |
if ( is_array($negotiator_ids) && !empty($negotiator_ids) ) |
| 268 |
{ |
| 269 |
$negotiators = array(); |
| 270 |
foreach ( $negotiator_ids as $negotiator_id ) |
| 271 |
{ |
| 272 |
$user_info = get_userdata($negotiator_id); |
| 273 |
if ( $user_info === false ) |
| 274 |
{ |
| 275 |
$negotiators[] = __( 'Unknown negotiator', 'propertyhive' ); |
| 276 |
} |
| 277 |
else |
| 278 |
{ |
| 279 |
$negotiators[] = $user_info->display_name; |
| 280 |
} |
| 281 |
} |
| 282 |
echo implode(", ", $negotiators); |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
echo '<em>- ' . esc_html(__( 'Unaccompanied', 'propertyhive' )) . ' -</em>'; |
| 287 |
} |
| 288 |
break; |
| 289 |
default : |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
public function set_primary_column( $default, $screen ) { |
| 295 |
|
| 296 |
if ( 'edit-viewing' === $screen ) |
| 297 |
{ |
| 298 |
$default = 'start_date_time'; |
| 299 |
} |
| 300 |
|
| 301 |
return $default; |
| 302 |
} |
| 303 |
|
| 304 |
public function remove_actions($actions, $post) { |
| 305 |
|
| 306 |
if ( $post->post_type !== 'viewing' ) |
| 307 |
{ |
| 308 |
return $actions; |
| 309 |
} |
| 310 |
|
| 311 |
// Remove 'Quick Edit' link |
| 312 |
if ( isset($actions['inline hide-if-no-js']) ) |
| 313 |
{ |
| 314 |
unset($actions['inline hide-if-no-js']); |
| 315 |
} |
| 316 |
|
| 317 |
if ( isset($actions['trash']) ) |
| 318 |
{ |
| 319 |
unset($actions['trash']); |
| 320 |
} |
| 321 |
|
| 322 |
return $actions; |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Make viewing columns sortable |
| 327 |
* |
| 328 |
* @access public |
| 329 |
* @param mixed $columns |
| 330 |
* @return array |
| 331 |
*/ |
| 332 |
public function custom_columns_sort( $columns ) { |
| 333 |
$custom = array( |
| 334 |
'start_date_time' => '_start_date_time', |
| 335 |
); |
| 336 |
return wp_parse_args( $custom, $columns ); |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Viewing column orderby |
| 341 |
* |
| 342 |
* @access public |
| 343 |
* @param mixed $vars |
| 344 |
* @return array |
| 345 |
*/ |
| 346 |
public function custom_columns_orderby( $vars ) { |
| 347 |
if ( is_admin() && $vars['post_type'] == 'viewing' ) |
| 348 |
{ |
| 349 |
$vars = array_merge( $vars, array( |
| 350 |
'meta_key' => '_start_date_time', |
| 351 |
'orderby' => 'meta_value' |
| 352 |
) ); |
| 353 |
} |
| 354 |
return $vars; |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Remove bulk edit option |
| 359 |
* @param array $actions |
| 360 |
*/ |
| 361 |
public function remove_bulk_actions( $actions ) { |
| 362 |
unset( $actions['edit'] ); |
| 363 |
return $actions; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Show a status filter box |
| 368 |
*/ |
| 369 |
public function propertyhive_filters() { |
| 370 |
global $typenow, $wp_query; |
| 371 |
|
| 372 |
if ( 'viewing' != $typenow ) { |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
echo apply_filters( 'propertyhive_viewing_filters', $output ); |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Filter the viewings in admin based on options |
| 381 |
* |
| 382 |
* @param mixed $query |
| 383 |
*/ |
| 384 |
public function viewing_filters_query( $query ) { |
| 385 |
global $typenow, $wp_query; |
| 386 |
|
| 387 |
if ( 'viewing' == $typenow ) { |
| 388 |
|
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
public static function check_viewing_feedback_add( $object_id, $meta_key, $meta_value ) |
| 393 |
{ |
| 394 |
if ( get_post_type($object_id) == 'viewing' && $meta_key == '_feedback_status' && in_array($meta_value, array( 'interested', 'not_interested' )) ) |
| 395 |
{ |
| 396 |
update_post_meta( (int)$object_id, '_feedback_received_date', date("Y-m-d H:i:s") ); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
public static function check_viewing_feedback_update( $meta_id, $object_id, $meta_key, $meta_value ) |
| 401 |
{ |
| 402 |
if ( get_post_type($object_id) == 'viewing' && $meta_key == '_feedback_status' && in_array($meta_value, array( 'interested', 'not_interested' )) ) |
| 403 |
{ |
| 404 |
$original_value = get_post_meta( $object_id, '_feedback_status', TRUE ); |
| 405 |
if ( in_array($original_value, array( '', 'not_required' )) ) |
| 406 |
{ |
| 407 |
update_post_meta( (int)$object_id, '_feedback_received_date', date("Y-m-d H:i:s") ); |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
endif; |
| 414 |
|
| 415 |
return new PH_Admin_CPT_Viewing(); |
| 416 |
|