PluginProbe
Property Hive / 1.4.61
Property Hive v1.4.61
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-contact.php

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

643 lines 18.7 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 contact 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_Contact' ) ) :
20
21 /**
22 * PH_Admin_CPT_Contact Class
23 */
24 class PH_Admin_CPT_Contact extends PH_Admin_CPT {
25
26 /**
27 * Constructor
28 */
29 public function __construct() {
30 $this->type = 'contact';
31
32 // Post title fields
33 add_filter( 'enter_title_here', array( $this, 'enter_title_here' ), 1, 2 );
34
35 // Featured image text
36 //add_filter( 'gettext', array( $this, 'featured_image_gettext' ) );
37 //add_filter( 'media_view_strings', array( $this, 'media_view_strings' ), 10, 2 );
38
39 // Visibility option
40 //add_action( 'post_submitbox_misc_actions', array( $this, 'property_data_visibility' ) );
41
42 // Before data updates
43 add_action( 'pre_post_update', array( $this, 'pre_post_update' ) );
44 add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) );
45
46 // Admin Columns
47 add_filter( 'manage_edit-contact_columns', array( $this, 'edit_columns' ) );
48 add_action( 'manage_contact_posts_custom_column', array( $this, 'custom_columns' ), 2 );
49 // add_filter( 'manage_edit-property_sortable_columns', array( $this, 'custom_columns_sort' ) );
50 // add_filter( 'request', array( $this, 'custom_columns_orderby' ) );
51
52 // Sort link
53 // add_filter( 'views_edit-property', array( $this, 'default_sorting_link' ) );
54
55 // Prouct filtering
56 // add_action( 'restrict_manage_posts', array( $this, 'product_filters' ) );
57 // add_filter( 'parse_query', array( $this, 'property_filters_query' ) );
58
59 // Enhanced search
60 add_filter( 'posts_search', array( $this, 'contact_search' ) );
61
62 // Maintain hierarchy of terms
63 // add_filter( 'wp_terms_checklist_args', array( $this, 'disable_checked_ontop' ) );
64
65 // Bulk / quick edit
66 add_filter( 'bulk_actions-edit-contact', array( $this, 'remove_bulk_actions') );
67 /*add_action( 'bulk_edit_custom_box', array( $this, 'bulk_edit' ), 10, 2 );
68 add_action( 'quick_edit_custom_box', array( $this, 'quick_edit' ), 10, 2 );
69 add_action( 'save_post', array( $this, 'bulk_and_quick_edit_save_post' ), 10, 2 );
70
71 // Uploads
72 add_filter( 'upload_dir', array( $this, 'upload_dir' ) );
73 add_action( 'media_upload_downloadable_product', array( $this, 'media_upload_downloadable_product' ) );
74 add_filter( 'mod_rewrite_rules', array( $this, 'ms_protect_download_rewite_rules' ) );
75
76 // Download permissions
77 //add_action( 'propertyhive_process_product_file_download_paths', array( $this, 'process_product_file_download_paths' ), 10, 3 );*/
78
79 add_action( 'admin_notices', array( $this, 'ph_message_admin_notice') );
80
81 add_action( 'manage_posts_extra_tablenav', array( $this, 'generate_applicant_list_action') );
82
83 add_filter( 'admin_url', array( $this, 'append_contact_type_to_add_new_url' ), 10, 2 );
84
85
86 // Call PH_Admin_CPT constructor
87 parent::__construct();
88 }
89
90 public function append_contact_type_to_add_new_url( $url, $path )
91 {
92 if ( $path === 'post-new.php?post_type=contact' )
93 {
94 if ( isset($_GET['_contact_type']) )
95 {
96 $url .= '&contact_type=' . ph_clean($_GET['_contact_type']);
97 }
98 }
99 return $url;
100 }
101
102 public function ph_message_admin_notice()
103 {
104 $message = '';
105
106 if ( isset($_GET['ph_message']) )
107 {
108 switch ( $_GET['ph_message'] )
109 {
110 case "1": {
111 $message = __( 'All done! The selected properties will be emailed to the applicant shortly.', 'propertyhive' );
112 break;
113 }
114 case "2": {
115 $message = __( 'Selected properties successfully removed from applicant matches', 'propertyhive' );
116 break;
117 }
118 }
119 }
120
121 if ( $message != '' )
122 {
123 echo "<div class=\"notice notice-success\">
124 <p>" . $message . "</p>
125 </div>";
126 }
127 }
128
129 /**
130 * Check if we're editing or adding a contact
131 * @return boolean
132 */
133 private function is_editing_contact() {
134 if ( ! empty( $_GET['post_type'] ) && 'contact' == $_GET['post_type'] ) {
135 return true;
136 }
137 if ( ! empty( $_GET['post'] ) && 'contact' == get_post_type( (int)$_GET['post'] ) ) {
138 return true;
139 }
140 if ( ! empty( $_REQUEST['post_id'] ) && 'contact' == get_post_type( (int)$_REQUEST['post_id'] ) ) {
141 return true;
142 }
143 return false;
144 }
145
146 /**
147 * Change title boxes in admin.
148 * @param string $text
149 * @param object $post
150 * @return string
151 */
152 public function enter_title_here( $text, $post ) {
153 if ( is_admin() && $post->post_type == 'contact' ) {
154 return __( 'Enter Contact Name(s)', 'propertyhive' );
155 }
156
157 return $text;
158 }
159
160 /**
161 * Some functions, like the term recount, require the visibility to be set prior. Lets save that here.
162 *
163 * @param int $post_id
164 */
165 public function pre_post_update( $post_id ) {
166
167 }
168
169 /**
170 * Forces certain product data based on the product's type, e.g. grouped products cannot have a parent.
171 *
172 * @param array $data
173 * @return array
174 */
175 public function wp_insert_post_data( $data ) {
176
177
178 return $data;
179 }
180
181 /**
182 * Change the columns shown in admin.
183 */
184 public function edit_columns( $existing_columns ) {
185
186 if ( empty( $existing_columns ) && ! is_array( $existing_columns ) ) {
187 $existing_columns = array();
188 }
189
190 unset( $existing_columns['title'], $existing_columns['comments'], $existing_columns['date'] );
191
192 $columns = array();
193 $columns['cb'] = '<input type="checkbox" />';
194 $columns['name'] = __( 'Name', 'propertyhive' );
195 $columns['address'] = __( 'Address', 'propertyhive' );
196 $columns['contact_details'] = __( 'Contact Details', 'propertyhive' );
197
198 return array_merge( $columns, $existing_columns );
199 }
200
201 /**
202 * Define our custom columns shown in admin.
203 * @param string $column
204 */
205 public function custom_columns( $column ) {
206 global $post, $propertyhive, $the_contact;
207
208 if ( empty( $the_contact ) || $the_contact->ID != $post->ID )
209 {
210 $the_contact = new PH_Contact( $post->ID );
211 }
212
213 switch ( $column ) {
214 case 'name' :
215
216 $edit_link = get_edit_post_link( $post->ID );
217 //$title = _draft_or_post_title();
218 $title = $the_contact->post_title;
219
220 $post_type_object = get_post_type_object( $post->post_type );
221 $can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID );
222
223 echo '<strong><a class="row-title" href="' . esc_url( $edit_link ) .'">' . $title.'</a>';
224 if ( isset( $_GET['_contact_type'] ) && strpos( ph_clean($_GET['_contact_type']), 'applicant' ) !== FALSE && in_array('applicant', $the_contact->_contact_types) && $the_contact->_hot_applicant == 'yes' )
225 {
226 echo ' <span style="color:#C00;">(' . __( 'Hot Applicant', 'propertyhive' ) . ')</span>';
227 }
228 echo '</strong>';
229
230 /*if ( $post->post_parent > 0 ) {
231 echo '&nbsp;&nbsp;&larr; <a href="'. get_edit_post_link( $post->post_parent ) .'">'. get_the_title( $post->post_parent ) .'</a>';
232 }
233
234 // Excerpt view
235 if ( isset( $_GET['mode'] ) && 'excerpt' == $_GET['mode'] ) {
236 echo apply_filters( 'the_excerpt', $post->post_excerpt );
237 }*/
238
239 // Get actions
240 $actions = array();
241
242 if ( $can_edit_post && 'trash' != $post->post_status ) {
243 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '" title="' . esc_attr( __( 'Edit this item', 'propertyhive' ) ) . '">' . __( 'Edit', 'propertyhive' ) . '</a>';
244 }
245 if ( current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) {
246 if ( 'trash' == $post->post_status ) {
247 $actions['untrash'] = '<a title="' . esc_attr( __( 'Restore this item from the Trash', 'propertyhive' ) ) . '" href="' . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&amp;action=untrash', $post->ID ) ), 'untrash-post_' . $post->ID ) . '">' . __( 'Restore', 'propertyhive' ) . '</a>';
248 } elseif ( EMPTY_TRASH_DAYS ) {
249 //$actions['trash'] = '<a class="submitdelete" title="' . esc_attr( __( 'Move this item to the Trash', 'propertyhive' ) ) . '" href="' . get_delete_post_link( $post->ID ) . '">' . __( 'Trash', 'propertyhive' ) . '</a>';
250 }
251
252 if ( 'trash' == $post->post_status || ! EMPTY_TRASH_DAYS ) {
253 $actions['delete'] = '<a class="submitdelete" title="' . esc_attr( __( 'Delete this item permanently', 'propertyhive' ) ) . '" href="' . get_delete_post_link( $post->ID, '', true ) . '">' . __( 'Delete Permanently', 'propertyhive' ) . '</a>';
254 }
255 }
256 if ( $post_type_object->public ) {
257 if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) {
258 if ( $can_edit_post )
259 $actions['view'] = '<a href="' . esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) . '" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;', 'propertyhive' ), $title ) ) . '" rel="permalink">' . __( 'Preview', 'propertyhive' ) . '</a>';
260 } elseif ( 'trash' != $post->post_status ) {
261 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;', 'propertyhive' ), $title ) ) . '" rel="permalink">' . __( 'View', 'propertyhive' ) . '</a>';
262 }
263 }
264
265 $actions = apply_filters( 'post_row_actions', $actions, $post );
266
267 echo '<div class="row-actions">';
268
269 $i = 0;
270 $action_count = sizeof($actions);
271
272 foreach ( $actions as $action => $link ) {
273 ++$i;
274 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
275 echo '<span class="' . $action . '">' . $link . $sep . '</span>';
276 }
277 echo '</div>';
278
279 /*get_inline_data( $post );*/
280
281 /* Custom inline data for propertyhive */
282 /*echo '
283 <div class="hidden" id="propertyhive_inline_' . $post->ID . '">
284 <div class="menu_order">' . $post->menu_order . '</div>
285 </div>
286 ';*/
287
288 break;
289 case 'address' :
290
291 echo $the_contact->get_formatted_full_address();
292
293 break;
294 case 'contact_details' :
295
296 echo 'T: ' . $the_contact->_telephone_number . '<br>';
297 echo 'E: ' . $the_contact->_email_address;
298
299 break;
300 default :
301 break;
302 }
303 }
304
305 /**
306 * Remove bulk edit option
307 * @param array $actions
308 */
309 public function remove_bulk_actions( $actions ) {
310 unset( $actions['edit'] );
311 return $actions;
312 }
313
314 /**
315 * Search by email and phone number
316 * Phone numbers are stripped of any none numeric or comma charactors
317 *
318 * @param string $where
319 * @return string
320 */
321 public function contact_search( $where ) {
322
323 global $pagenow, $wpdb, $wp;
324
325 if ( 'edit.php' != $pagenow || ! is_search() || ! isset( $wp->query_vars['s'] ) || 'contact' != $wp->query_vars['post_type'] ) {
326 return $where;
327 }
328
329 if ( trim($wp->query_vars['s']) == '' )
330 {
331 return $where;
332 }
333
334 $search_ids = array();
335 $terms = explode( ',', $wp->query_vars['s'] );
336
337 foreach ( $terms as $term )
338 {
339 if ( is_numeric( $term ) )
340 {
341 $search_ids[] = $term;
342 }
343
344 $phone_number = preg_replace( "/[^0-9,]/", "", $term );
345
346 // Attempt to get an ID by searching for phone and email address
347 $query = $wpdb->prepare(
348 "SELECT
349 ID
350 FROM
351 {$wpdb->posts}
352 INNER JOIN {$wpdb->postmeta} AS mt1 ON {$wpdb->posts}.ID = mt1.post_id
353 WHERE
354 (
355 (mt1.meta_key='_telephone_number_clean' AND mt1.meta_value LIKE NULLIF(%s,'%%%'))
356 OR
357 (mt1.meta_key='_email_address' AND mt1.meta_value LIKE %s)
358 )
359 AND
360 post_type='contact'
361 GROUP BY ID
362 ",
363 '%' . $wpdb->esc_like( ph_clean( ph_clean_telephone_number( $term ) ) ) . '%',
364 '%' . $wpdb->esc_like( ph_clean( $term ) ) . '%'
365 );
366
367 $search_posts = $wpdb->get_results( $query );
368 $search_posts = wp_list_pluck( $search_posts, 'ID' );
369
370 if ( sizeof( $search_posts ) > 0 )
371 {
372 $search_ids = array_merge( $search_ids, $search_posts );
373 }
374 }
375 $search_ids = array_filter( array_unique( array_map( 'absint', $search_ids ) ) );
376 if ( sizeof( $search_ids ) > 0 )
377 {
378 $where = str_replace( 'AND (((', "AND ( ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . ")) OR ((", $where );
379 }
380 return $where;
381 }
382
383 /**
384 * Make contact columns sortable
385 *
386 * https://gist.github.com/906872
387 *
388 * @access public
389 * @param mixed $columns
390 * @return array
391 */
392 public function custom_columns_sort( $columns ) {
393 $custom = array(
394 'name' => 'title'
395 );
396 return wp_parse_args( $custom, $columns );
397 }
398
399 /**
400 * Product column orderby
401 *
402 * http://scribu.net/wordpress/custom-sortable-columns.html#comment-4732
403 *
404 * @access public
405 * @param mixed $vars
406 * @return array
407 */
408 public function custom_columns_orderby( $vars ) {
409 /*if ( isset( $vars['orderby'] ) ) {
410 if ( 'price' == $vars['orderby'] ) {
411 $vars = array_merge( $vars, array(
412 'meta_key' => 'price',
413 'orderby' => 'meta_value_num'
414 ) );
415 }
416 }*/
417
418 return $vars;
419 }
420
421 /**
422 * Product sorting link
423 *
424 * Based on Simple Page Ordering by 10up (http://wordpress.org/extend/plugins/simple-page-ordering/)
425 *
426 * @param array $views
427 * @return array
428 */
429 public function default_sorting_link( $views ) {
430 global $post_type, $wp_query;
431
432 if ( ! current_user_can('edit_others_pages') ) {
433 return $views;
434 }
435
436 $class = ( isset( $wp_query->query['orderby'] ) && $wp_query->query['orderby'] == 'menu_order title' ) ? 'current' : '';
437 $query_string = remove_query_arg(array( 'orderby', 'order' ));
438 $query_string = add_query_arg( 'orderby', urlencode('menu_order title'), $query_string );
439 $query_string = add_query_arg( 'order', urlencode('ASC'), $query_string );
440 $views['byorder'] = '<a href="'. $query_string . '" class="' . esc_attr( $class ) . '">' . __( 'Sort Contacts', 'propertyhive' ) . '</a>';
441
442 return $views;
443 }
444
445 /**
446 * Show a category filter box
447 */
448 public function propertyhive_filters() {
449 global $typenow, $wp_query;
450
451 if ( 'contact' != $typenow ) {
452 return;
453 }
454
455
456 echo apply_filters( 'propertyhive_contact_filters', $output );
457 }
458
459 /**
460 * Filter the contacts in admin based on options
461 *
462 * @param mixed $query
463 */
464 public function contact_filters_query( $query ) {
465 global $typenow, $wp_query;
466
467 if ( 'contact' == $typenow ) {
468
469 }
470 }
471
472 /**
473 * Maintain term hierarchy when editing a property.
474 * @param array $args
475 * @return array
476 */
477 public function disable_checked_ontop( $args ) {
478 if ( 'product_cat' == $args['taxonomy'] ) {
479 $args['checked_ontop'] = false;
480 }
481
482 return $args;
483 }
484
485 /**
486 * Custom bulk edit - form
487 *
488 * @access public
489 * @param mixed $column_name
490 * @param mixed $post_type
491 */
492 public function bulk_edit( $column_name, $post_type ) {
493 if ( 'price' != $column_name || 'property' != $post_type ) {
494 return;
495 }
496
497 include( HP()->plugin_path() . '/includes/admin/views/html-bulk-edit-product.php' );
498 }
499
500 /**
501 * Custom quick edit - form
502 *
503 * @access public
504 * @param mixed $column_name
505 * @param mixed $post_type
506 */
507 public function quick_edit( $column_name, $post_type ) {
508 if ( 'price' != $column_name || 'property' != $post_type ) {
509 return;
510 }
511
512 include( HP()->plugin_path() . '/includes/admin/views/html-quick-edit-contact.php' );
513 }
514
515 /**
516 * Quick and bulk edit saving
517 *
518 * @access public
519 * @param int $post_id
520 * @param WP_Post $post
521 * @return int
522 */
523 public function bulk_and_quick_edit_save_post( $post_id, $post ) {
524 // If this is an autosave, our form has not been submitted, so we don't want to do anything.
525 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
526 return $post_id;
527 }
528
529 // Don't save revisions and autosaves
530 if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
531 return $post_id;
532 }
533
534 // Check post type is product
535 if ( 'contact' != $post->post_type ) {
536 return $post_id;
537 }
538
539 // Check user permission
540 if ( ! current_user_can( 'edit_post', $post_id ) ) {
541 return $post_id;
542 }
543
544 // Check nonces
545 if ( ! isset( $_REQUEST['propertyhive_quick_edit_nonce'] ) && ! isset( $_REQUEST['propertyhive_bulk_edit_nonce'] ) ) {
546 return $post_id;
547 }
548 if ( isset( $_REQUEST['propertyhive_quick_edit_nonce'] ) && ! wp_verify_nonce( $_REQUEST['propertyhive_quick_edit_nonce'], 'propertyhive_quick_edit_nonce' ) ) {
549 return $post_id;
550 }
551 if ( isset( $_REQUEST['propertyhive_bulk_edit_nonce'] ) && ! wp_verify_nonce( $_REQUEST['propertyhive_bulk_edit_nonce'], 'propertyhive_bulk_edit_nonce' ) ) {
552 return $post_id;
553 }
554
555 // Get the product and save
556 $contact = get_contact( $post );
557
558 if ( ! empty( $_REQUEST['propertyhive_quick_edit'] ) ) {
559 $this->quick_edit_save( $post_id, $contact );
560 } else {
561 $this->bulk_edit_save( $post_id, $contact );
562 }
563
564 // Clear transient
565 //ph_delete_contact_transients( $post_id );
566
567 return $post_id;
568 }
569
570 /**
571 * Quick edit
572 */
573 private function quick_edit_save( $post_id, $contact ) {
574
575 global $wpdb;
576
577 /*
578 // Save fields
579 if ( isset( $_REQUEST['_availability'] ) ) {
580 update_post_meta( $post_id, '_availability', ph_clean( $_REQUEST['_availability'] ) );
581 }*/
582
583 do_action( 'propertyhive_product_quick_edit_save', $contact );
584 }
585
586 /**
587 * Bulk edit
588 */
589 public function bulk_edit_save( $post_id, $contact ) {
590
591 /*
592 // Save fields
593 if ( ! empty( $_REQUEST['_availability'] ) ) {
594 update_post_meta( $post_id, '_availability', ph_clean( $_REQUEST['_availability'] ) );
595 }*/
596
597 do_action( 'propertyhive_contact_bulk_edit_save', $contact );
598 }
599
600 /**
601 * Filter the directory for uploads.
602 *
603 * @param array $pathdata
604 * @return array
605 */
606 public function upload_dir( $pathdata ) {
607 // Change upload dir for downloadable files
608 if ( isset( $_POST['type'] ) && 'downloadable_product' == $_POST['type'] ) {
609 if ( empty( $pathdata['subdir'] ) ) {
610 $pathdata['path'] = $pathdata['path'] . '/propertyhive_uploads';
611 $pathdata['url'] = $pathdata['url']. '/propertyhive_uploads';
612 $pathdata['subdir'] = '/propertyhive_uploads';
613 } else {
614 $new_subdir = '/propertyhive_uploads' . $pathdata['subdir'];
615
616 $pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
617 $pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
618 $pathdata['subdir'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['subdir'] );
619 }
620 }
621
622 return $pathdata;
623 }
624
625 public function generate_applicant_list_action( $which )
626 {
627 global $typenow, $wp_query;
628
629 if ( 'contact' != $typenow ) {
630 return;
631 }
632
633 if ( $which == 'top' && isset($_GET['_contact_type']) && $_GET['_contact_type'] == 'applicant' )
634 {
635 echo '<div class="alignleft actions"><a href="' . admin_url('admin.php?page=ph-generate-applicant-list') . '" id="generate_applicant_list_button" class="button">' . __( 'Generate Applicant List', 'propertyhive' ) . '</a></div>';
636 }
637 }
638 }
639
640 endif;
641
642 return new PH_Admin_CPT_Contact();
643