| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'PH_Admin_CPT' ) ) { |
| 7 |
include( 'class-ph-admin-cpt.php' ); |
| 8 |
} |
| 9 |
|
| 10 |
if ( ! class_exists( 'PH_Admin_CPT_Key_Date' ) ) |
| 11 |
{ |
| 12 |
class PH_Admin_CPT_Key_Date extends PH_Admin_CPT { |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
$this->type = 'key_date'; |
| 16 |
|
| 17 |
add_filter( 'manage_edit-key_date_columns', array( $this, 'edit_columns' ) ); |
| 18 |
add_action( 'manage_key_date_posts_custom_column', array( $this, 'custom_columns' ) ); |
| 19 |
add_filter( 'list_table_primary_column', array( $this, 'set_primary_column' ), 10, 2 ); |
| 20 |
add_filter( 'post_row_actions', array( $this, 'remove_actions' ), 10, 2 ); |
| 21 |
add_filter( 'manage_edit-key_date_sortable_columns', array( $this, 'sortable_columns' ) ); |
| 22 |
add_filter( 'request', array( $this, 'custom_sorts' ) ); |
| 23 |
add_action( 'quick_edit_custom_box', array( $this, 'key_date_custom_quick_edit_box' ), 10, 3 ); |
| 24 |
add_action( 'save_post', array( $this, 'save_key_date' ) ); |
| 25 |
|
| 26 |
add_filter( 'bulk_actions-edit-key_date', array( $this, 'remove_bulk_actions') ); |
| 27 |
|
| 28 |
parent::__construct(); |
| 29 |
} |
| 30 |
|
| 31 |
function key_date_custom_quick_edit_box( $column_name, $post_type, $taxonomy ) { |
| 32 |
global $post; |
| 33 |
|
| 34 |
if ($post_type == 'key_date' && $column_name == 'description') |
| 35 |
{ |
| 36 |
?> |
| 37 |
<fieldset class="inline-edit-col-left inline-edit-ph inline-edit-key_date"> |
| 38 |
<legend class="inline-edit-legend">Quick Edit</legend> |
| 39 |
<div class="inline-edit-col"> |
| 40 |
<label> |
| 41 |
<span class="title">Description</span> |
| 42 |
<span class="input-text-wrap"> |
| 43 |
<input type="text" name="_key_date_description" class="short" style="width:200px;" value=""> |
| 44 |
</span> |
| 45 |
</label> |
| 46 |
<label> |
| 47 |
<span class="title">Property</span> |
| 48 |
<span class="key_date-property"></span> |
| 49 |
</label> |
| 50 |
<label> |
| 51 |
<span class="title">Status</span> |
| 52 |
<span class="input-text-wrap"> |
| 53 |
<?php |
| 54 |
$selected_value = get_post_meta( $post->ID, '_key_date_status', true ); |
| 55 |
|
| 56 |
$output = '<select name="_key_date_status">'; |
| 57 |
|
| 58 |
foreach ( array( 'pending', 'booked', 'complete', 'on_hold', 'cancelled' ) as $status ) |
| 59 |
{ |
| 60 |
$output .= '<option value="' . esc_attr($status) . '"'; |
| 61 |
$output .= selected($status, $selected_value, false ); |
| 62 |
$output .= '>' . esc_html(ucwords(str_replace("_", " ", $status))) . '</option>'; |
| 63 |
} |
| 64 |
|
| 65 |
$output .= '</select>'; |
| 66 |
|
| 67 |
echo $output; |
| 68 |
?> |
| 69 |
</span> |
| 70 |
</label> |
| 71 |
<label id="book_next_key_date_label" style="display: none;"> |
| 72 |
Book next key date? <input type="checkbox" id="book_next_key_date_checkbox" name="book_next_key_date" > |
| 73 |
</label> |
| 74 |
<label id="next_date_due_label" style="display: none;"> |
| 75 |
<span class="title">Next Date</span> |
| 76 |
<span class="input-text-wrap"> |
| 77 |
<input type="text" id="next_date_due" name="next_date_due" class="short" placeholder="yyyy-mm-dd" style="width:120px;" value=""> |
| 78 |
</span> |
| 79 |
</label> |
| 80 |
</div> |
| 81 |
</fieldset> |
| 82 |
<?php |
| 83 |
} |
| 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 |
{ |
| 93 |
$existing_columns = array(); |
| 94 |
} |
| 95 |
|
| 96 |
unset( $existing_columns['title'], $existing_columns['comments'], $existing_columns['date'] ); |
| 97 |
|
| 98 |
$columns = array(); |
| 99 |
$columns['cb'] = '<input type="checkbox" />'; |
| 100 |
$columns['description'] = __( 'Description', 'propertyhive' ); |
| 101 |
$columns['notes'] = __( 'Notes', 'propertyhive' ); |
| 102 |
$columns['property'] = __( 'Property', 'propertyhive' ); |
| 103 |
$columns['tenants'] = __( 'Tenants', 'propertyhive' ); |
| 104 |
$columns['date_due'] = __( 'Date Due', 'propertyhive' ); |
| 105 |
$columns['status'] = __( 'Status', 'propertyhive' ); |
| 106 |
|
| 107 |
return array_merge( $columns, $existing_columns ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Define our custom columns shown in admin. |
| 112 |
* |
| 113 |
* @param string $column |
| 114 |
*/ |
| 115 |
public function custom_columns( $column ) { |
| 116 |
global $post; |
| 117 |
|
| 118 |
$key_date = new PH_Key_Date( $post ); |
| 119 |
$property = $key_date->property(); |
| 120 |
$tenancy = $key_date->tenancy(); |
| 121 |
|
| 122 |
switch ( $column ) { |
| 123 |
|
| 124 |
case 'description' : |
| 125 |
|
| 126 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 127 |
|
| 128 |
echo '<div class="cell-main-content">'; |
| 129 |
$opening_link_tag = false; |
| 130 |
if ( !empty($key_date->tenancy_id) ) |
| 131 |
{ |
| 132 |
echo '<a href="' . esc_url(get_edit_post_link((int)$key_date->tenancy_id)) . '#propertyhive-tenancy-management%7Cpropertyhive-management-dates">'; |
| 133 |
$opening_link_tag = true; |
| 134 |
} |
| 135 |
else |
| 136 |
{ |
| 137 |
if ( !empty($key_date->property_id) ) |
| 138 |
{ |
| 139 |
echo '<a href="' . esc_url(get_edit_post_link((int)$key_date->property_id)) . '#propertyhive-property-tenancies%7Cpropertyhive-management-dates">'; |
| 140 |
$opening_link_tag = true; |
| 141 |
} |
| 142 |
} |
| 143 |
echo $key_date->description() . ( $opening_link_tag ? '</a>' : '' ) . '</div>'; |
| 144 |
echo '<div class="row-actions">'; |
| 145 |
break; |
| 146 |
|
| 147 |
case 'notes' : |
| 148 |
echo '<div class="cell-main-content">' . ( !empty($key_date->notes()) ? nl2br( $key_date->notes() ) : '-' ) . '</div>'; |
| 149 |
break; |
| 150 |
|
| 151 |
case 'property' : |
| 152 |
echo '<div class="cell-main-content">' . esc_html($property->get_formatted_full_address()) . '</div>'; |
| 153 |
break; |
| 154 |
|
| 155 |
case 'tenants' : |
| 156 |
if ( $tenancy->id ) |
| 157 |
{ |
| 158 |
echo $tenancy->get_tenants(false, true); |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
echo '-'; |
| 163 |
} |
| 164 |
break; |
| 165 |
|
| 166 |
case 'date_due' : |
| 167 |
echo '<div class="cell-main-content">' . esc_html($key_date->date_due()->format( 'jS F Y' )) . '</div>'; |
| 168 |
break; |
| 169 |
|
| 170 |
case 'status' : |
| 171 |
echo '<div class="cell-main-content">' . esc_html(ucwords( $key_date->status() )) . '</div>'; |
| 172 |
break; |
| 173 |
|
| 174 |
default : |
| 175 |
break; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
public function set_primary_column( $default, $screen ) { |
| 180 |
|
| 181 |
if ( 'edit-key_date' === $screen ) |
| 182 |
{ |
| 183 |
$default = 'description'; |
| 184 |
} |
| 185 |
|
| 186 |
return $default; |
| 187 |
} |
| 188 |
|
| 189 |
public function remove_actions($actions, $post) { |
| 190 |
|
| 191 |
if ( $post->post_type !== 'key_date' ) |
| 192 |
{ |
| 193 |
return $actions; |
| 194 |
} |
| 195 |
|
| 196 |
if ( isset($actions['edit']) ) |
| 197 |
{ |
| 198 |
unset($actions['edit']); |
| 199 |
} |
| 200 |
|
| 201 |
return $actions; |
| 202 |
} |
| 203 |
|
| 204 |
public function sortable_columns( $columns ) { |
| 205 |
$custom = array( |
| 206 |
'date_due' => 'date_due', |
| 207 |
); |
| 208 |
|
| 209 |
return wp_parse_args( $custom, $columns ); |
| 210 |
} |
| 211 |
|
| 212 |
function custom_sorts( $vars ) { |
| 213 |
|
| 214 |
if ( ! isset( $vars['orderby'] ) ) |
| 215 |
{ |
| 216 |
return $vars; |
| 217 |
} |
| 218 |
|
| 219 |
switch ( $vars['orderby'] ) |
| 220 |
{ |
| 221 |
case 'date_due': |
| 222 |
$vars['orderby'] = 'meta_value'; |
| 223 |
$vars['meta_key'] = '_date_due'; |
| 224 |
break; |
| 225 |
} |
| 226 |
|
| 227 |
return $vars; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Remove bulk edit actions |
| 232 |
* @param array $actions |
| 233 |
*/ |
| 234 |
public function remove_bulk_actions( $actions ) { |
| 235 |
return array(); |
| 236 |
} |
| 237 |
|
| 238 |
function save_key_date( $post_id ) { |
| 239 |
|
| 240 |
if ( $post_id == null || get_post_type($post_id) != 'key_date' || empty( $_POST['_key_date_status'] ) ) |
| 241 |
{ |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
update_post_meta( $post_id, '_key_date_status', $_POST['_key_date_status'] ); |
| 246 |
|
| 247 |
$existing_description = get_the_title($post_id); |
| 248 |
|
| 249 |
if ( !empty( $_POST['_key_date_description'] ) && $_POST['_key_date_description'] != $existing_description ) |
| 250 |
{ |
| 251 |
$post_update = array( |
| 252 |
'ID' => $post_id, |
| 253 |
'post_title' => $_POST['_key_date_description'], |
| 254 |
); |
| 255 |
|
| 256 |
wp_update_post( $post_update ); |
| 257 |
} |
| 258 |
|
| 259 |
if ( isset($_POST['book_next_key_date']) && $_POST['book_next_key_date'] == 'on' && isset($_POST['next_date_due']) && $_POST['next_date_due'] != '' ) |
| 260 |
{ |
| 261 |
// Insert next key date record |
| 262 |
$next_key_date_post = array( |
| 263 |
'post_title' => $_POST['_key_date_description'], |
| 264 |
'post_content' => '', |
| 265 |
'post_type' => 'key_date', |
| 266 |
'post_status' => 'publish', |
| 267 |
'comment_status' => 'closed', |
| 268 |
'ping_status' => 'closed', |
| 269 |
); |
| 270 |
|
| 271 |
// Insert the post into the database |
| 272 |
// Remove save_post hook temporarily to prevent it running again on wp_insert_post |
| 273 |
remove_action( 'save_post', array( $this, 'save_key_date' ) ); |
| 274 |
$next_key_date_post_id = wp_insert_post( $next_key_date_post ); |
| 275 |
add_action( 'save_post', array( $this, 'save_key_date' ) ); |
| 276 |
|
| 277 |
if ( !is_wp_error($next_key_date_post_id) && $next_key_date_post_id != 0 ) |
| 278 |
{ |
| 279 |
add_post_meta( $next_key_date_post_id, '_date_due', $_POST['next_date_due'] ); |
| 280 |
add_post_meta( $next_key_date_post_id, '_key_date_status', 'pending' ); |
| 281 |
add_post_meta( $next_key_date_post_id, '_key_date_type_id', get_post_meta($post_id, '_key_date_type_id', true) ); |
| 282 |
|
| 283 |
if( metadata_exists('post', $post_id, '_property_id') ) { |
| 284 |
add_post_meta( $next_key_date_post_id, '_property_id', get_post_meta($post_id, '_property_id', true) ); |
| 285 |
} |
| 286 |
|
| 287 |
if( metadata_exists('post', $post_id, '_tenancy_id') ) { |
| 288 |
add_post_meta( $next_key_date_post_id, '_tenancy_id', get_post_meta($post_id, '_tenancy_id', true) ); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return new PH_Admin_CPT_Key_Date(); |
| 297 |
|