| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin functions for the enquiry 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_Enquiry' ) ) : |
| 20 |
|
| 21 |
/** |
| 22 |
* PH_Admin_CPT_Enquiry Class |
| 23 |
*/ |
| 24 |
class PH_Admin_CPT_Enquiry extends PH_Admin_CPT { |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$this->type = 'enquiry'; |
| 31 |
|
| 32 |
// Admin notices |
| 33 |
add_action( 'admin_notices', array( $this, 'enquiry_admin_notices') ); |
| 34 |
|
| 35 |
// Post title fields |
| 36 |
add_filter( 'enter_title_here', array( $this, 'enter_title_here' ), 1, 2 ); |
| 37 |
|
| 38 |
// Featured image text |
| 39 |
//add_filter( 'gettext', array( $this, 'featured_image_gettext' ) ); |
| 40 |
//add_filter( 'media_view_strings', array( $this, 'media_view_strings' ), 10, 2 ); |
| 41 |
|
| 42 |
// Visibility option |
| 43 |
//add_action( 'post_submitbox_misc_actions', array( $this, 'property_data_visibility' ) ); |
| 44 |
|
| 45 |
// Before data updates |
| 46 |
add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); |
| 47 |
add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) ); |
| 48 |
|
| 49 |
// Admin Columns |
| 50 |
add_filter( 'manage_edit-enquiry_columns', array( $this, 'edit_columns' ) ); |
| 51 |
add_action( 'manage_enquiry_posts_custom_column', array( $this, 'custom_columns' ), 2 ); |
| 52 |
add_filter( 'list_table_primary_column', array( $this, 'set_primary_column' ), 10, 2 ); |
| 53 |
add_filter( 'post_row_actions', array( $this, 'remove_actions' ), 10, 2 ); |
| 54 |
|
| 55 |
// Bulk / quick edit |
| 56 |
add_filter( 'bulk_actions-edit-enquiry', array( $this, 'remove_bulk_actions') ); |
| 57 |
|
| 58 |
// Call PH_Admin_CPT constructor |
| 59 |
parent::__construct(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Output admin notices relating to enquiry |
| 64 |
*/ |
| 65 |
public function enquiry_admin_notices() |
| 66 |
{ |
| 67 |
$screen = get_current_screen(); |
| 68 |
|
| 69 |
if ( $screen->id == 'enquiry' && isset($_GET['post']) && get_post_type($_GET['post']) == 'enquiry' ) |
| 70 |
{ |
| 71 |
$enquiry = new PH_Enquiry((int)$_GET['post']); |
| 72 |
|
| 73 |
// Get associated property_id(s), from either meta_key |
| 74 |
$property_ids = get_post_meta( $enquiry->id, '_property_id' ); |
| 75 |
if ( empty($property_ids) ) |
| 76 |
{ |
| 77 |
$property_ids = get_post_meta( $enquiry->id, 'property_id' ); |
| 78 |
} |
| 79 |
|
| 80 |
// If the enquiry has a property associated |
| 81 |
if ( !empty($property_ids) ) |
| 82 |
{ |
| 83 |
if ( !is_array($property_ids) ) |
| 84 |
{ |
| 85 |
$property_ids = array( $property_ids ); |
| 86 |
} |
| 87 |
|
| 88 |
$contact_ids = array(); |
| 89 |
$contact_id = get_post_meta( $enquiry->id, '_contact_id', true ); |
| 90 |
if ( !empty( $contact_id ) ) |
| 91 |
{ |
| 92 |
$contact_ids[] = $contact_id; |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
// The enquiry doesn't have a contact_id meta_key, so check for contacts with the same email address |
| 97 |
|
| 98 |
// Get enquiry email address, from either meta_key |
| 99 |
$email_address = get_post_meta( $enquiry->id, 'email_address', true ); |
| 100 |
if ( empty($email_address) ) |
| 101 |
{ |
| 102 |
$email_address = get_post_meta( $enquiry->id, 'email', true ); |
| 103 |
} |
| 104 |
|
| 105 |
if ( !empty($email_address) ) |
| 106 |
{ |
| 107 |
// Get all contact with this email address |
| 108 |
$args = array( |
| 109 |
'post_type' => 'contact', |
| 110 |
'post_status' => 'any', |
| 111 |
'nopaging' => true, |
| 112 |
'fields' => 'ids', |
| 113 |
'meta_query' => array( |
| 114 |
array( |
| 115 |
'key' => '_email_address', |
| 116 |
'value' => $email_address, |
| 117 |
) |
| 118 |
) |
| 119 |
); |
| 120 |
$contacts_query = new WP_Query( $args ); |
| 121 |
$contact_ids = $contacts_query->posts; |
| 122 |
wp_reset_postdata(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if ( count($contact_ids) > 0 ) |
| 127 |
{ |
| 128 |
// Get any viewings for the propert(y/ies) and contact(s) on this enquiry |
| 129 |
$args = array( |
| 130 |
'post_type' => 'viewing', |
| 131 |
'nopaging' => true, |
| 132 |
'fields' => 'ids', |
| 133 |
'post_status' => 'publish', |
| 134 |
'meta_query' => array( |
| 135 |
array( |
| 136 |
'key' => '_property_id', |
| 137 |
'value' => $property_ids, |
| 138 |
'compare' => 'IN', |
| 139 |
), |
| 140 |
array( |
| 141 |
'key' => '_applicant_contact_id', |
| 142 |
'value' => $contact_ids, |
| 143 |
'compare' => 'IN', |
| 144 |
), |
| 145 |
), |
| 146 |
); |
| 147 |
$viewings_query = new WP_Query( $args ); |
| 148 |
$viewing_ids = $viewings_query->posts; |
| 149 |
wp_reset_postdata(); |
| 150 |
|
| 151 |
if ( count($viewing_ids) > 0 ) |
| 152 |
{ |
| 153 |
if ( 1 === $viewing_count ) |
| 154 |
{ |
| 155 |
$message = __( '<p>There is an existing viewing for this applicant at this property.</p>', 'propertyhive' ); |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
$message = sprintf( |
| 160 |
/* translators: %s: number of existing viewings */ |
| 161 |
__( '<p>There are %s existing viewings for this applicant at this property.</p>', 'propertyhive' ), |
| 162 |
number_format_i18n( $viewing_count ) |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
foreach( $viewing_ids as $viewing_id ) |
| 167 |
{ |
| 168 |
$message .= '<p><a href="' . esc_url(get_edit_post_link( $viewing_id )) . '" class="button">' . esc_html(__( 'Edit viewing', 'propertyhive' )) . '</a></p>'; |
| 169 |
} |
| 170 |
echo "<div class=\"notice notice-info\">$message</div>"; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Check if we're editing or adding an enquiry |
| 179 |
* @return boolean |
| 180 |
*/ |
| 181 |
private function is_editing_enquiry() { |
| 182 |
if ( ! empty( $_GET['post_type'] ) && 'enquiry' == $_GET['post_type'] ) { |
| 183 |
return true; |
| 184 |
} |
| 185 |
if ( ! empty( $_GET['post'] ) && 'enquiry' == get_post_type( (int)$_GET['post'] ) ) { |
| 186 |
return true; |
| 187 |
} |
| 188 |
if ( ! empty( $_REQUEST['post_id'] ) && 'enquiry' == get_post_type( (int)$_REQUEST['post_id'] ) ) { |
| 189 |
return true; |
| 190 |
} |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Change title boxes in admin. |
| 196 |
* @param string $text |
| 197 |
* @param object $post |
| 198 |
* @return string |
| 199 |
*/ |
| 200 |
public function enter_title_here( $text, $post ) { |
| 201 |
if ( is_admin() && $post->post_type == 'enquiry' ) { |
| 202 |
return __( 'Enter Enquiry Subject', 'propertyhive' ); |
| 203 |
} |
| 204 |
|
| 205 |
return $text; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Some functions, like the term recount, require the visibility to be set prior. Lets save that here. |
| 210 |
* |
| 211 |
* @param int $post_id |
| 212 |
*/ |
| 213 |
public function pre_post_update( $post_id ) { |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Forces certain product data based on the product's type, e.g. grouped products cannot have a parent. |
| 219 |
* |
| 220 |
* @param array $data |
| 221 |
* @return array |
| 222 |
*/ |
| 223 |
public function wp_insert_post_data( $data ) { |
| 224 |
|
| 225 |
|
| 226 |
return $data; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Change the columns shown in admin. |
| 231 |
*/ |
| 232 |
public function edit_columns( $existing_columns ) { |
| 233 |
|
| 234 |
if ( empty( $existing_columns ) && ! is_array( $existing_columns ) ) { |
| 235 |
$existing_columns = array(); |
| 236 |
} |
| 237 |
|
| 238 |
unset( $existing_columns['title'], $existing_columns['comments'] ); |
| 239 |
|
| 240 |
$columns = array(); |
| 241 |
$columns['cb'] = '<input type="checkbox" />'; |
| 242 |
|
| 243 |
$columns['subject'] = __( 'Subject', 'propertyhive' ); |
| 244 |
|
| 245 |
$columns['status'] = __( 'Status', 'propertyhive' ); |
| 246 |
|
| 247 |
$columns['source'] = __( 'Source', 'propertyhive' ); |
| 248 |
|
| 249 |
$columns['properties'] = __( 'Properties', 'propertyhive' ); |
| 250 |
|
| 251 |
$columns['negotiator'] = __( 'Negotiator', 'propertyhive' ); |
| 252 |
|
| 253 |
$columns['office'] = __( 'Office', 'propertyhive' ); |
| 254 |
|
| 255 |
return array_merge( $columns, $existing_columns ); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Define our custom columns shown in admin. |
| 260 |
* @param string $column |
| 261 |
*/ |
| 262 |
public function custom_columns( $column ) { |
| 263 |
global $post, $propertyhive, $the_enquiry; |
| 264 |
|
| 265 |
if ( empty( $the_enquiry ) || $the_enquiry->ID != $post->ID ) |
| 266 |
{ |
| 267 |
$the_enquiry = new PH_Enquiry( $post->ID ); |
| 268 |
} |
| 269 |
|
| 270 |
switch ( $column ) { |
| 271 |
case 'subject' : |
| 272 |
$edit_link = get_edit_post_link( $post->ID ); |
| 273 |
$title = _draft_or_post_title(); |
| 274 |
|
| 275 |
echo '<strong><a class="row-title" href="' . esc_url( $edit_link ) .'">' . esc_html($title) . '</a></strong>'; |
| 276 |
|
| 277 |
break; |
| 278 |
case 'status' : |
| 279 |
echo $the_enquiry->status; |
| 280 |
break; |
| 281 |
case 'source' : |
| 282 |
|
| 283 |
$sources = array( |
| 284 |
'office' => __( 'Office', 'propertyhive' ), |
| 285 |
'website' => __( 'Website', 'propertyhive' ) |
| 286 |
); |
| 287 |
|
| 288 |
$sources = apply_filters( 'propertyhive_enquiry_sources', $sources ); |
| 289 |
|
| 290 |
echo esc_html( ( isset($sources[$the_enquiry->source]) ) ? $sources[$the_enquiry->source] : $the_enquiry->source ); |
| 291 |
|
| 292 |
$utm_keys = array( |
| 293 |
'utm_source', |
| 294 |
'utm_medium', |
| 295 |
'utm_term', |
| 296 |
'utm_content', |
| 297 |
'utm_campaign', |
| 298 |
'gclid', |
| 299 |
'fbclid' |
| 300 |
); |
| 301 |
|
| 302 |
foreach ( $utm_keys as $utm_key ) |
| 303 |
{ |
| 304 |
$value = $the_enquiry->{$utm_key}; |
| 305 |
if ( !empty($value) ) |
| 306 |
{ |
| 307 |
echo '<br><em>' . esc_html($utm_key) . ': ' . esc_html($value) . '</em>'; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
break; |
| 312 |
case 'properties' : |
| 313 |
$property_ids = $the_enquiry->get_properties(); |
| 314 |
|
| 315 |
if ( count($property_ids) > 0 ) |
| 316 |
{ |
| 317 |
$properties_text_array = array(); |
| 318 |
foreach($property_ids as $property_id) |
| 319 |
{ |
| 320 |
$properties_text_array[] = $the_enquiry->get_list_property_display_text( $property_id ); |
| 321 |
} |
| 322 |
echo implode( '<br>', array_filter($properties_text_array) ); |
| 323 |
} |
| 324 |
else |
| 325 |
{ |
| 326 |
echo '-'; |
| 327 |
} |
| 328 |
break; |
| 329 |
case 'negotiator' : |
| 330 |
if ($the_enquiry->_negotiator_id == '' || $the_enquiry->_negotiator_id == 0) |
| 331 |
{ |
| 332 |
echo '<em>-- ' . esc_html(__( 'Unassigned', 'propertyhive' )) . ' --</em>'; |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
$userdata = get_userdata( $the_enquiry->_negotiator_id ); |
| 337 |
if ( $userdata !== FALSE ) |
| 338 |
{ |
| 339 |
echo esc_html($userdata->display_name); |
| 340 |
} |
| 341 |
else |
| 342 |
{ |
| 343 |
echo '<em>Unknown user</em>'; |
| 344 |
} |
| 345 |
} |
| 346 |
break; |
| 347 |
case 'office' : |
| 348 |
$office_id = $the_enquiry->_office_id; |
| 349 |
if ( !empty($office_id) ) |
| 350 |
{ |
| 351 |
echo esc_html(get_the_title( $office_id )); |
| 352 |
} |
| 353 |
else |
| 354 |
{ |
| 355 |
echo '-'; |
| 356 |
} |
| 357 |
break; |
| 358 |
default : |
| 359 |
break; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
public function set_primary_column( $default, $screen ) { |
| 364 |
|
| 365 |
if ( 'edit-enquiry' === $screen ) |
| 366 |
{ |
| 367 |
$default = 'subject'; |
| 368 |
} |
| 369 |
|
| 370 |
return $default; |
| 371 |
} |
| 372 |
|
| 373 |
public function remove_actions($actions, $post) { |
| 374 |
|
| 375 |
if ( $post->post_type !== 'enquiry' ) |
| 376 |
{ |
| 377 |
return $actions; |
| 378 |
} |
| 379 |
|
| 380 |
// Remove 'Quick Edit' link |
| 381 |
if ( isset($actions['inline hide-if-no-js']) ) |
| 382 |
{ |
| 383 |
unset($actions['inline hide-if-no-js']); |
| 384 |
} |
| 385 |
|
| 386 |
if ( isset($actions['trash']) ) |
| 387 |
{ |
| 388 |
unset($actions['trash']); |
| 389 |
} |
| 390 |
|
| 391 |
return $actions; |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Remove bulk edit option |
| 396 |
* @param array $actions |
| 397 |
*/ |
| 398 |
public function remove_bulk_actions( $actions ) { |
| 399 |
unset( $actions['edit'] ); |
| 400 |
return $actions; |
| 401 |
} |
| 402 |
|
| 403 |
} |
| 404 |
|
| 405 |
endif; |
| 406 |
|
| 407 |
return new PH_Admin_CPT_Enquiry(); |
| 408 |
|