| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin functions for the tenancy 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_Tenancy' ) ) : |
| 20 |
|
| 21 |
/** |
| 22 |
* PH_Admin_CPT_Tenancy Class |
| 23 |
*/ |
| 24 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- Legacy public global class PH_Admin_CPT_Tenancy; preserving the existing PH_* class name is required for plugin and extension compatibility. |
| 25 |
class PH_Admin_CPT_Tenancy extends PH_Admin_CPT { |
| 26 |
|
| 27 |
/** |
| 28 |
* Constructor |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
$this->type = 'tenancy'; |
| 32 |
|
| 33 |
// Before data updates |
| 34 |
add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); |
| 35 |
add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) ); |
| 36 |
|
| 37 |
// Admin Columns |
| 38 |
add_filter( 'manage_edit-tenancy_columns', array( $this, 'edit_columns' ) ); |
| 39 |
add_action( 'manage_tenancy_posts_custom_column', array( $this, 'custom_columns' ), 2 ); |
| 40 |
add_filter( 'list_table_primary_column', array( $this, 'set_primary_column' ), 10, 2 ); |
| 41 |
add_filter( 'post_row_actions', array( $this, 'remove_actions' ), 10, 2 ); |
| 42 |
add_filter( 'manage_edit-tenancy_sortable_columns', array( $this, 'custom_columns_sort' ) ); |
| 43 |
add_filter( 'request', array( $this, 'custom_columns_orderby' ) ); |
| 44 |
|
| 45 |
// Bulk / quick edit |
| 46 |
add_filter( 'bulk_actions-edit-tenancy', array( $this, 'remove_bulk_actions' ) ); |
| 47 |
|
| 48 |
// Call PH_Admin_CPT constructor |
| 49 |
parent::__construct(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Check if we're editing or adding a tenancy |
| 54 |
* @return boolean |
| 55 |
*/ |
| 56 |
private function is_editing_tenancy() { |
| 57 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen detection; mutations have separate save guards. |
| 58 |
$post_type = isset( $_GET['post_type'] ) && is_string( $_GET['post_type'] ) ? sanitize_key( wp_unslash( $_GET['post_type'] ) ) : ''; |
| 59 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen detection. |
| 60 |
$post_id = isset( $_GET['post'] ) && is_string( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; |
| 61 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen detection for AJAX requests. |
| 62 |
$request_id = isset( $_REQUEST['post_id'] ) && is_string( $_REQUEST['post_id'] ) ? absint( $_REQUEST['post_id'] ) : 0; |
| 63 |
return 'tenancy' === $post_type || ( $post_id > 0 && 'tenancy' === get_post_type( $post_id ) ) || ( $request_id > 0 && 'tenancy' === get_post_type( $request_id ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @param int $post_id |
| 68 |
*/ |
| 69 |
public function pre_post_update( $post_id ) { |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @param array $data |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function wp_insert_post_data( $data ) { |
| 79 |
|
| 80 |
|
| 81 |
return $data; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Change the columns shown in admin. |
| 86 |
*/ |
| 87 |
public function edit_columns( $existing_columns ) { |
| 88 |
|
| 89 |
if ( empty( $existing_columns ) && ! is_array( $existing_columns ) ) |
| 90 |
{ |
| 91 |
$existing_columns = array(); |
| 92 |
} |
| 93 |
|
| 94 |
unset( $existing_columns['title'], $existing_columns['comments'], $existing_columns['date'] ); |
| 95 |
|
| 96 |
$columns = array(); |
| 97 |
$columns['cb'] = '<input type="checkbox" />'; |
| 98 |
$columns['property'] = __( 'Property', 'propertyhive' ); |
| 99 |
$columns['property_owner'] = __( 'Landlord', 'propertyhive' ); |
| 100 |
$columns['applicant'] = __( 'Tenants', 'propertyhive' ); |
| 101 |
$columns['start_date'] = __( 'Start Date', 'propertyhive' ); |
| 102 |
$columns['end_date'] = __( 'End Date', 'propertyhive' ); |
| 103 |
$columns['rent'] = __( 'Rent', 'propertyhive' ); |
| 104 |
$columns['status'] = __( 'Status', 'propertyhive' ); |
| 105 |
|
| 106 |
return array_merge( $columns, $existing_columns ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Define our custom columns shown in admin. |
| 111 |
* |
| 112 |
* @param string $column |
| 113 |
*/ |
| 114 |
public function custom_columns( $column ) { |
| 115 |
global $post, $propertyhive, $the_tenancy; |
| 116 |
|
| 117 |
if ( empty( $the_tenancy ) || $the_tenancy->ID != $post->ID ) |
| 118 |
{ |
| 119 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Retain the legacy global name for compatibility with external admin column callbacks. |
| 120 |
$the_tenancy = new PH_Tenancy( $post->ID ); |
| 121 |
} |
| 122 |
|
| 123 |
switch ( $column ) { |
| 124 |
|
| 125 |
case 'property' : |
| 126 |
|
| 127 |
$edit_link = get_edit_post_link( $post->ID ); |
| 128 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 129 |
$can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID ); |
| 130 |
|
| 131 |
$property = new PH_Property( (int) $the_tenancy->property_id ); |
| 132 |
echo '<strong><a class="row-title" href="' . esc_url( $edit_link ) . '">' . esc_html($property->get_formatted_full_address()) . '</a></strong>'; |
| 133 |
break; |
| 134 |
case 'property_owner' : |
| 135 |
|
| 136 |
$the_property = new PH_Property( (int) $the_tenancy->property_id ); |
| 137 |
$owner_contact_ids = $the_property->_owner_contact_id; |
| 138 |
if ( |
| 139 |
( ! is_array( $owner_contact_ids ) && $owner_contact_ids != '' && $owner_contact_ids != 0 ) |
| 140 |
|| |
| 141 |
( is_array( $owner_contact_ids ) && ! empty( $owner_contact_ids ) ) |
| 142 |
) { |
| 143 |
if ( ! is_array( $owner_contact_ids ) ) |
| 144 |
{ |
| 145 |
$owner_contact_ids = array( $owner_contact_ids ); |
| 146 |
} |
| 147 |
|
| 148 |
foreach ( $owner_contact_ids as $owner_contact_id ) |
| 149 |
{ |
| 150 |
echo esc_html(get_the_title( $owner_contact_id )) . '<br>'; |
| 151 |
if ( count( $owner_contact_ids ) == 1 ) |
| 152 |
{ |
| 153 |
echo '<div class="row-actions">'; |
| 154 |
echo 'T: ' . esc_html(get_post_meta( $owner_contact_id, '_telephone_number', true )) . '<br>'; |
| 155 |
echo 'E: ' . esc_html(get_post_meta( $owner_contact_id, '_email_address', true )); |
| 156 |
echo '</div>'; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
echo '-'; |
| 163 |
} |
| 164 |
|
| 165 |
break; |
| 166 |
case 'applicant' : |
| 167 |
|
| 168 |
echo wp_kses_post( $the_tenancy->get_tenants(false, true) ); |
| 169 |
|
| 170 |
break; |
| 171 |
case 'start_date' : |
| 172 |
echo ( $the_tenancy->_start_date != '' ? esc_html(gmdate( "d/m/Y", strtotime( $the_tenancy->_start_date ) )) : '-' ); |
| 173 |
|
| 174 |
break; |
| 175 |
case 'end_date' : |
| 176 |
echo ( $the_tenancy->_end_date != '' ? esc_html(gmdate( "d/m/Y", strtotime( $the_tenancy->_end_date ) )) : '-' ); |
| 177 |
|
| 178 |
break; |
| 179 |
case 'rent' : |
| 180 |
|
| 181 |
echo wp_kses_post( $the_tenancy->get_formatted_rent() ); |
| 182 |
|
| 183 |
break; |
| 184 |
case 'status' : |
| 185 |
|
| 186 |
echo wp_kses_post( $the_tenancy->get_status() ); |
| 187 |
|
| 188 |
break; |
| 189 |
default : |
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
public function set_primary_column( $default, $screen ) { |
| 195 |
|
| 196 |
if ( 'edit-tenancy' === $screen ) |
| 197 |
{ |
| 198 |
$default = 'property'; |
| 199 |
} |
| 200 |
|
| 201 |
return $default; |
| 202 |
} |
| 203 |
|
| 204 |
public function remove_actions($actions, $post) { |
| 205 |
|
| 206 |
if ( $post->post_type !== 'tenancy' ) |
| 207 |
{ |
| 208 |
return $actions; |
| 209 |
} |
| 210 |
|
| 211 |
// Remove 'Quick Edit' link |
| 212 |
if ( isset($actions['inline hide-if-no-js']) ) |
| 213 |
{ |
| 214 |
unset($actions['inline hide-if-no-js']); |
| 215 |
} |
| 216 |
|
| 217 |
if ( isset($actions['trash']) ) |
| 218 |
{ |
| 219 |
unset($actions['trash']); |
| 220 |
} |
| 221 |
|
| 222 |
return $actions; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Make property columns sortable |
| 227 |
* |
| 228 |
* https://gist.github.com/906872 |
| 229 |
* |
| 230 |
* @access public |
| 231 |
* @param mixed $columns |
| 232 |
* @return array |
| 233 |
*/ |
| 234 |
public function custom_columns_sort( $columns ) { |
| 235 |
$custom = array( |
| 236 |
'start_date' => '_start_date', |
| 237 |
'end_date' => '_end_date' |
| 238 |
); |
| 239 |
return wp_parse_args( $custom, $columns ); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Property column orderby |
| 244 |
* |
| 245 |
* @access public |
| 246 |
* @param mixed $vars |
| 247 |
* @return array |
| 248 |
*/ |
| 249 |
public function custom_columns_orderby( $vars ) { |
| 250 |
if ( isset( $vars['orderby'] ) ) { |
| 251 |
if ( '_start_date' == $vars['orderby'] ) { |
| 252 |
$vars = array_merge( $vars, array( |
| 253 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- All these meta_key values are literal, supported CPT date/status/price keys used by paginated WordPress admin list ordering. Core admin post queries provide pagination; no arbitrary request key is copied into these lines. |
| 254 |
'meta_key' => '_start_date', |
| 255 |
'orderby' => 'meta_value' |
| 256 |
) ); |
| 257 |
} |
| 258 |
elseif ( '_end_date' == $vars['orderby'] ) { |
| 259 |
$vars = array_merge( $vars, array( |
| 260 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- All these meta_key values are literal, supported CPT date/status/price keys used by paginated WordPress admin list ordering. Core admin post queries provide pagination; no arbitrary request key is copied into these lines. |
| 261 |
'meta_key' => '_end_date', |
| 262 |
'orderby' => 'meta_value' |
| 263 |
) ); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
return $vars; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Remove bulk edit option |
| 272 |
* |
| 273 |
* @param array $actions |
| 274 |
*/ |
| 275 |
public function remove_bulk_actions( $actions ) { |
| 276 |
unset( $actions['edit'] ); |
| 277 |
|
| 278 |
return $actions; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
endif; |
| 283 |
|
| 284 |
return new PH_Admin_CPT_Tenancy(); |
| 285 |
|