PluginProbe
Property Hive / 2.3.1
Property Hive v2.3.1
2.3.1 2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 All 261 releases
propertyhive / includes / admin / post-types / class-ph-admin-cpt-enquiry.php

class-ph-admin-cpt-enquiry.php in Property Hive 2.3.1, at includes/admin/post-types/class-ph-admin-cpt-enquiry.php

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