| 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 |
// Before data updates |
| 33 |
add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); |
| 34 |
add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) ); |
| 35 |
|
| 36 |
// Admin Columns |
| 37 |
add_filter( 'manage_edit-viewing_columns', array( $this, 'edit_columns' ) ); |
| 38 |
add_action( 'manage_viewing_posts_custom_column', array( $this, 'custom_columns' ), 2 ); |
| 39 |
add_filter( 'manage_edit-viewing_sortable_columns', array( $this, 'custom_columns_sort' ) ); |
| 40 |
add_filter( 'request', array( $this, 'custom_columns_orderby' ) ); |
| 41 |
|
| 42 |
// Bulk / quick edit |
| 43 |
add_filter( 'bulk_actions-edit-viewing', array( $this, 'remove_bulk_actions') ); |
| 44 |
/*add_action( 'bulk_edit_custom_box', array( $this, 'bulk_edit' ), 10, 2 ); |
| 45 |
add_action( 'quick_edit_custom_box', array( $this, 'quick_edit' ), 10, 2 ); |
| 46 |
add_action( 'save_post', array( $this, 'bulk_and_quick_edit_save_post' ), 10, 2 );*/ |
| 47 |
|
| 48 |
// Call PH_Admin_CPT constructor |
| 49 |
parent::__construct(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Check if we're editing or adding a viewing |
| 54 |
* @return boolean |
| 55 |
*/ |
| 56 |
private function is_editing_viewing() { |
| 57 |
if ( ! empty( $_GET['post_type'] ) && 'viewing' == $_GET['post_type'] ) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
if ( ! empty( $_GET['post'] ) && 'viewing' == get_post_type( (int)$_GET['post'] ) ) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
if ( ! empty( $_REQUEST['post_id'] ) && 'viewing' == get_post_type( (int)$_REQUEST['post_id'] ) ) { |
| 64 |
return true; |
| 65 |
} |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @param int $post_id |
| 71 |
*/ |
| 72 |
public function pre_post_update( $post_id ) { |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @param array $data |
| 78 |
* @return array |
| 79 |
*/ |
| 80 |
public function wp_insert_post_data( $data ) { |
| 81 |
|
| 82 |
|
| 83 |
return $data; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Change the columns shown in admin. |
| 88 |
*/ |
| 89 |
public function edit_columns( $existing_columns ) { |
| 90 |
|
| 91 |
if ( empty( $existing_columns ) && ! is_array( $existing_columns ) ) { |
| 92 |
$existing_columns = array(); |
| 93 |
} |
| 94 |
|
| 95 |
unset( $existing_columns['title'], $existing_columns['comments'], $existing_columns['date'] ); |
| 96 |
|
| 97 |
$columns = array(); |
| 98 |
$columns['cb'] = '<input type="checkbox" />'; |
| 99 |
$columns['start_date_time'] = __( 'Viewing Date / Time', 'propertyhive' ); |
| 100 |
$columns['property'] = __( 'Property', 'propertyhive' ); |
| 101 |
$columns['applicant'] = __( 'Applicant', 'propertyhive' ); |
| 102 |
$columns['status'] = __( 'Status', 'propertyhive' ); |
| 103 |
$columns['negotiators'] = __( 'Attending Negotiators', 'propertyhive' ); |
| 104 |
|
| 105 |
return array_merge( $columns, $existing_columns ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Define our custom columns shown in admin. |
| 110 |
* @param string $column |
| 111 |
*/ |
| 112 |
public function custom_columns( $column ) { |
| 113 |
global $post, $propertyhive, $the_viewing; |
| 114 |
|
| 115 |
if ( empty( $the_viewing ) || $the_viewing->ID != $post->ID ) |
| 116 |
{ |
| 117 |
$the_viewing = new PH_Viewing( $post->ID ); |
| 118 |
} |
| 119 |
|
| 120 |
switch ( $column ) { |
| 121 |
case 'start_date_time' : |
| 122 |
|
| 123 |
$edit_link = get_edit_post_link( $post->ID ); |
| 124 |
//$title = _draft_or_post_title(); |
| 125 |
$title = date("H:i jS F Y", strtotime($the_viewing->start_date_time)); |
| 126 |
|
| 127 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 128 |
$can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID ); |
| 129 |
|
| 130 |
echo '<strong><a class="row-title" href="' . esc_url( $edit_link ) .'">' . $title.'</a></strong>'; |
| 131 |
|
| 132 |
// Get actions |
| 133 |
$actions = array(); |
| 134 |
|
| 135 |
if ( $can_edit_post && 'trash' != $post->post_status ) { |
| 136 |
$actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '" title="' . esc_attr( __( 'Edit this item', 'propertyhive' ) ) . '">' . __( 'Edit', 'propertyhive' ) . '</a>'; |
| 137 |
} |
| 138 |
if ( current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) { |
| 139 |
if ( 'trash' == $post->post_status ) { |
| 140 |
$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>'; |
| 141 |
} elseif ( EMPTY_TRASH_DAYS ) { |
| 142 |
//$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>'; |
| 143 |
} |
| 144 |
|
| 145 |
if ( 'trash' == $post->post_status || ! EMPTY_TRASH_DAYS ) { |
| 146 |
$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>'; |
| 147 |
} |
| 148 |
} |
| 149 |
if ( $post_type_object->public ) { |
| 150 |
if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) { |
| 151 |
if ( $can_edit_post ) |
| 152 |
$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>'; |
| 153 |
} elseif ( 'trash' != $post->post_status ) { |
| 154 |
$actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”', 'propertyhive' ), $title ) ) . '" rel="permalink">' . __( 'View', 'propertyhive' ) . '</a>'; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
$actions = apply_filters( 'post_row_actions', $actions, $post ); |
| 159 |
|
| 160 |
echo '<div class="row-actions">'; |
| 161 |
|
| 162 |
$i = 0; |
| 163 |
$action_count = sizeof($actions); |
| 164 |
|
| 165 |
foreach ( $actions as $action => $link ) { |
| 166 |
++$i; |
| 167 |
( $i == $action_count ) ? $sep = '' : $sep = ' | '; |
| 168 |
echo '<span class="' . $action . '">' . $link . $sep . '</span>'; |
| 169 |
} |
| 170 |
echo '</div>'; |
| 171 |
|
| 172 |
break; |
| 173 |
case 'property' : |
| 174 |
|
| 175 |
if ( $the_viewing->property_id != '' ) |
| 176 |
{ |
| 177 |
$property = new PH_Property((int)$the_viewing->property_id); |
| 178 |
echo $property->get_formatted_full_address(); |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
echo '-'; |
| 183 |
} |
| 184 |
break; |
| 185 |
case 'applicant' : |
| 186 |
|
| 187 |
if ( $the_viewing->applicant_contact_id != '' ) |
| 188 |
{ |
| 189 |
echo get_the_title($the_viewing->applicant_contact_id); |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
echo '-'; |
| 194 |
} |
| 195 |
|
| 196 |
break; |
| 197 |
case 'status' : |
| 198 |
|
| 199 |
echo ucwords(str_replace("_", " ", $the_viewing->status)); |
| 200 |
if ( $the_viewing->status == 'pending' ) |
| 201 |
{ |
| 202 |
echo '<br>'; |
| 203 |
// confirmation status |
| 204 |
if ( $the_viewing->all_confirmed == 'yes' ) |
| 205 |
{ |
| 206 |
echo __( 'All Parties Confirmed', 'propertyhive' ); |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
echo __( 'Awaiting Confirmation', 'propertyhive' ); |
| 211 |
} |
| 212 |
} |
| 213 |
if ( $the_viewing->status == 'carried_out' ) |
| 214 |
{ |
| 215 |
echo '<br>'; |
| 216 |
switch ( $the_viewing->feedback_status ) |
| 217 |
{ |
| 218 |
case "interested": { echo 'Applicant Interested'; break; } |
| 219 |
case "not_interested": { echo 'Applicant Not Interested'; break; } |
| 220 |
case "not_required": { echo 'Feedback Not Required'; break; } |
| 221 |
default: { echo 'Awaiting Feedback'; } |
| 222 |
} |
| 223 |
|
| 224 |
if ( $the_viewing->feedback_status == 'interested' || $the_viewing->feedback_status == 'not_interested' ) |
| 225 |
{ |
| 226 |
echo '<br>' . ( ($the_viewing->feedback_passed_on == 'yes') ? 'Feedback Passed On' : 'Feedback Not Passed On' ); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
break; |
| 231 |
case 'negotiators' : |
| 232 |
$negotiator_ids = get_post_meta( $post->ID, '_negotiator_id' ); |
| 233 |
if ( is_array($negotiator_ids) && !empty($negotiator_ids) ) |
| 234 |
{ |
| 235 |
$negotiators = array(); |
| 236 |
foreach ( $negotiator_ids as $negotiator_id ) |
| 237 |
{ |
| 238 |
$user_info = get_userdata($negotiator_id); |
| 239 |
$negotiators[] = $user_info->display_name; |
| 240 |
} |
| 241 |
echo implode(", ", $negotiators); |
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
echo '<em>- ' . __( 'Unattended', 'propertyhive' ) . ' -</em>'; |
| 246 |
} |
| 247 |
break; |
| 248 |
default : |
| 249 |
break; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Make viewing columns sortable |
| 255 |
* |
| 256 |
* @access public |
| 257 |
* @param mixed $columns |
| 258 |
* @return array |
| 259 |
*/ |
| 260 |
public function custom_columns_sort( $columns ) { |
| 261 |
$custom = array( |
| 262 |
'start_date_time' => '_start_date_time', |
| 263 |
); |
| 264 |
return wp_parse_args( $custom, $columns ); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Viewing column orderby |
| 269 |
* |
| 270 |
* @access public |
| 271 |
* @param mixed $vars |
| 272 |
* @return array |
| 273 |
*/ |
| 274 |
public function custom_columns_orderby( $vars ) { |
| 275 |
if ( is_admin() && $vars['post_type'] == 'viewing' ) |
| 276 |
{ |
| 277 |
$vars = array_merge( $vars, array( |
| 278 |
'meta_key' => '_start_date_time', |
| 279 |
'orderby' => 'meta_value' |
| 280 |
) ); |
| 281 |
} |
| 282 |
return $vars; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Remove bulk edit option |
| 287 |
* @param array $actions |
| 288 |
*/ |
| 289 |
public function remove_bulk_actions( $actions ) { |
| 290 |
unset( $actions['edit'] ); |
| 291 |
return $actions; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Show a status filter box |
| 296 |
*/ |
| 297 |
public function propertyhive_filters() { |
| 298 |
global $typenow, $wp_query; |
| 299 |
|
| 300 |
if ( 'viewing' != $typenow ) { |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
echo apply_filters( 'propertyhive_viewing_filters', $output ); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Filter the viewings in admin based on options |
| 309 |
* |
| 310 |
* @param mixed $query |
| 311 |
*/ |
| 312 |
public function viewing_filters_query( $query ) { |
| 313 |
global $typenow, $wp_query; |
| 314 |
|
| 315 |
if ( 'viewing' == $typenow ) { |
| 316 |
|
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
endif; |
| 322 |
|
| 323 |
return new PH_Admin_CPT_Viewing(); |
| 324 |
|