PluginProbe
Property Hive / 1.4.49
Property Hive v1.4.49
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.49, at includes/admin/post-types/class-ph-admin-cpt-contact.php

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