| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin functions for the appraisal 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_Appraisal' ) ) : |
| 20 |
|
| 21 |
/** |
| 22 |
* PH_Admin_CPT_Appraisal Class |
| 23 |
*/ |
| 24 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- Legacy public global class PH_Admin_CPT_Appraisal; preserving the existing PH_* class name is required for plugin and extension compatibility. |
| 25 |
class PH_Admin_CPT_Appraisal extends PH_Admin_CPT { |
| 26 |
|
| 27 |
/** |
| 28 |
* Constructor |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
$this->type = 'appraisal'; |
| 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-appraisal_columns', array( $this, 'edit_columns' ) ); |
| 39 |
add_action( 'manage_appraisal_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-appraisal_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-appraisal', array( $this, 'remove_bulk_actions') ); |
| 47 |
/*add_action( 'bulk_edit_custom_box', array( $this, 'bulk_edit' ), 10, 2 ); |
| 48 |
add_action( 'quick_edit_custom_box', array( $this, 'quick_edit' ), 10, 2 ); |
| 49 |
add_action( 'save_post', array( $this, 'bulk_and_quick_edit_save_post' ), 10, 2 );*/ |
| 50 |
|
| 51 |
// Call PH_Admin_CPT constructor |
| 52 |
parent::__construct(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Check if we're editing or adding a appraisal |
| 57 |
* @return boolean |
| 58 |
*/ |
| 59 |
private function is_editing_appraisal() { |
| 60 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen detection; mutations have separate save guards. |
| 61 |
$post_type = isset( $_GET['post_type'] ) && is_string( $_GET['post_type'] ) ? sanitize_key( wp_unslash( $_GET['post_type'] ) ) : ''; |
| 62 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen detection. |
| 63 |
$post_id = isset( $_GET['post'] ) && is_string( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; |
| 64 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen detection for AJAX requests. |
| 65 |
$request_id = isset( $_REQUEST['post_id'] ) && is_string( $_REQUEST['post_id'] ) ? absint( $_REQUEST['post_id'] ) : 0; |
| 66 |
return 'appraisal' === $post_type || ( $post_id > 0 && 'appraisal' === get_post_type( $post_id ) ) || ( $request_id > 0 && 'appraisal' === get_post_type( $request_id ) ); |
| 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'] = __( 'Appraisal Date / Time', 'propertyhive' ); |
| 100 |
$columns['property'] = __( 'Property', 'propertyhive' ); |
| 101 |
$columns['owner'] = __( 'Owner / Landlord', 'propertyhive' ); |
| 102 |
$columns['price'] = __( 'Valued Price', 'propertyhive' ); |
| 103 |
$columns['status'] = __( 'Status', 'propertyhive' ); |
| 104 |
$columns['negotiators'] = __( 'Attending Negotiators', 'propertyhive' ); |
| 105 |
|
| 106 |
return array_merge( $columns, $existing_columns ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Define our custom columns shown in admin. |
| 111 |
* @param string $column |
| 112 |
*/ |
| 113 |
public function custom_columns( $column ) { |
| 114 |
global $post, $propertyhive, $the_appraisal; |
| 115 |
|
| 116 |
if ( empty( $the_appraisal ) || $the_appraisal->ID != $post->ID ) |
| 117 |
{ |
| 118 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Retain the legacy global name for compatibility with external admin column callbacks. |
| 119 |
$the_appraisal = new PH_Appraisal( $post->ID ); |
| 120 |
} |
| 121 |
|
| 122 |
switch ( $column ) { |
| 123 |
case 'start_date_time' : |
| 124 |
|
| 125 |
$edit_link = get_edit_post_link( $post->ID ); |
| 126 |
//$title = _draft_or_post_title(); |
| 127 |
$title = gmdate("H:i jS F Y", strtotime($the_appraisal->start_date_time)); |
| 128 |
|
| 129 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 130 |
$can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID ); |
| 131 |
|
| 132 |
echo '<strong><a class="row-title" href="' . esc_url( $edit_link ) .'">' . esc_html($title) . '</a></strong>'; |
| 133 |
break; |
| 134 |
case 'property' : |
| 135 |
|
| 136 |
echo esc_html($the_appraisal->get_formatted_full_address()); |
| 137 |
break; |
| 138 |
case 'owner' : |
| 139 |
|
| 140 |
$owner_contact_ids = $the_appraisal->_property_owner_contact_id; |
| 141 |
if ( |
| 142 |
( !is_array($owner_contact_ids) && $owner_contact_ids != '' && $owner_contact_ids != 0 ) |
| 143 |
|| |
| 144 |
( is_array($owner_contact_ids) && !empty($owner_contact_ids) ) |
| 145 |
) |
| 146 |
{ |
| 147 |
if ( !is_array($owner_contact_ids) ) |
| 148 |
{ |
| 149 |
$owner_contact_ids = array($owner_contact_ids); |
| 150 |
} |
| 151 |
|
| 152 |
foreach ( $owner_contact_ids as $owner_contact_id ) |
| 153 |
{ |
| 154 |
echo esc_html(get_the_title($owner_contact_id)) . '<br>'; |
| 155 |
if ( count($owner_contact_ids) == 1 ) |
| 156 |
{ |
| 157 |
echo '<div class="row-actions">'; |
| 158 |
echo 'T: ' . esc_html(get_post_meta($owner_contact_id, '_telephone_number', TRUE)) . '<br>'; |
| 159 |
echo 'E: ' . esc_html(get_post_meta($owner_contact_id, '_email_address', TRUE)); |
| 160 |
echo '</div>'; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
echo '-'; |
| 167 |
} |
| 168 |
break; |
| 169 |
case 'price' : |
| 170 |
|
| 171 |
if ( $the_appraisal->status == 'carried_out' ) |
| 172 |
{ |
| 173 |
echo wp_kses_post( $the_appraisal->get_formatted_price() ); |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
echo '-'; |
| 178 |
} |
| 179 |
break; |
| 180 |
case 'status' : |
| 181 |
|
| 182 |
echo esc_html(ucwords(str_replace("_", " ", $the_appraisal->status))); |
| 183 |
|
| 184 |
if ( $the_appraisal->status == 'pending' ) |
| 185 |
{ |
| 186 |
echo '<br>'; |
| 187 |
// confirmation status |
| 188 |
if ( $the_appraisal->all_confirmed == 'yes' ) |
| 189 |
{ |
| 190 |
echo esc_html(__( 'All Parties Confirmed', 'propertyhive' )); |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
echo esc_html(__( 'Awaiting Confirmation', 'propertyhive' )); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
break; |
| 199 |
case 'negotiators' : |
| 200 |
$negotiator_ids = get_post_meta( $post->ID, '_negotiator_id' ); |
| 201 |
if ( is_array($negotiator_ids) && !empty($negotiator_ids) ) |
| 202 |
{ |
| 203 |
$negotiators = array(); |
| 204 |
foreach ( $negotiator_ids as $negotiator_id ) |
| 205 |
{ |
| 206 |
$user_info = get_userdata($negotiator_id); |
| 207 |
if ( $user_info === false ) |
| 208 |
{ |
| 209 |
$negotiators[] = __( 'Unknown negotiator', 'propertyhive' ); |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
$negotiators[] = $user_info->display_name; |
| 214 |
} |
| 215 |
} |
| 216 |
echo esc_html(implode(", ", $negotiators)); |
| 217 |
} |
| 218 |
else |
| 219 |
{ |
| 220 |
echo '<em>- ' . esc_html(__( 'None Set', 'propertyhive' )) . ' -</em>'; |
| 221 |
} |
| 222 |
break; |
| 223 |
default : |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
public function set_primary_column( $default, $screen ) { |
| 229 |
|
| 230 |
if ( 'edit-appraisal' === $screen ) |
| 231 |
{ |
| 232 |
$default = 'start_date_time'; |
| 233 |
} |
| 234 |
|
| 235 |
return $default; |
| 236 |
} |
| 237 |
|
| 238 |
public function remove_actions($actions, $post) { |
| 239 |
|
| 240 |
if ( $post->post_type !== 'appraisal' ) |
| 241 |
{ |
| 242 |
return $actions; |
| 243 |
} |
| 244 |
|
| 245 |
// Remove 'Quick Edit' link |
| 246 |
if ( isset($actions['inline hide-if-no-js']) ) |
| 247 |
{ |
| 248 |
unset($actions['inline hide-if-no-js']); |
| 249 |
} |
| 250 |
|
| 251 |
if ( isset($actions['trash']) ) |
| 252 |
{ |
| 253 |
unset($actions['trash']); |
| 254 |
} |
| 255 |
|
| 256 |
return $actions; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Make appraisal columns sortable |
| 261 |
* |
| 262 |
* @access public |
| 263 |
* @param mixed $columns |
| 264 |
* @return array |
| 265 |
*/ |
| 266 |
public function custom_columns_sort( $columns ) { |
| 267 |
$custom = array( |
| 268 |
'start_date_time' => '_start_date_time', |
| 269 |
); |
| 270 |
return wp_parse_args( $custom, $columns ); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Appraisal column orderby |
| 275 |
* |
| 276 |
* @access public |
| 277 |
* @param mixed $vars |
| 278 |
* @return array |
| 279 |
*/ |
| 280 |
public function custom_columns_orderby( $vars ) { |
| 281 |
if ( is_admin() && $vars['post_type'] == 'appraisal' ) |
| 282 |
{ |
| 283 |
$vars = array_merge( $vars, array( |
| 284 |
// 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. |
| 285 |
'meta_key' => '_start_date_time', |
| 286 |
'orderby' => 'meta_value' |
| 287 |
) ); |
| 288 |
} |
| 289 |
return $vars; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Remove bulk edit option |
| 294 |
* @param array $actions |
| 295 |
*/ |
| 296 |
public function remove_bulk_actions( $actions ) { |
| 297 |
unset( $actions['edit'] ); |
| 298 |
return $actions; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
endif; |
| 303 |
|
| 304 |
return new PH_Admin_CPT_Appraisal(); |
| 305 |
|