1: <?php
2: /**
3: * Contact Reports Table Class
4: *
5: * @package EPL
6: * @subpackage Admin/Classes/ContactsTable
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: // phpcs:disable WordPress.Security.NonceVerification
18: // phpcs:disable WordPress.DB.SlowDBQuery
19:
20: // Load WP_List_Table if not loaded.
21: if ( ! class_exists( 'WP_List_Table' ) ) {
22: require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
23: }
24:
25: /**
26: * EPL_Contact_Reports_Table Class.
27: *
28: * Renders the Contact Reports table
29: *
30: * @since 3.0
31: */
32: class EPL_Contact_Reports_Table extends WP_List_Table {
33:
34: /**
35: * Number of items per page
36: *
37: * @var int
38: * @since 3.0
39: */
40: public $per_page = 30;
41:
42: /**
43: * Number of contacts found
44: *
45: * @var int
46: * @since 3.0
47: */
48: public $count = 0;
49:
50: /**
51: * Total contacts
52: *
53: * @var int
54: * @since 3.0
55: */
56: public $total = 0;
57:
58: /**
59: * Get things started
60: *
61: * @since 3.0
62: * @see WP_List_Table::__construct()
63: */
64: public function __construct() {
65: global $status, $page;
66:
67: // Set parent defaults.
68: parent::__construct(
69: array(
70: 'singular' => __( 'Contact', 'easy-property-listings' ),
71: 'plural' => __( 'Contacts', 'easy-property-listings' ),
72: 'ajax' => false,
73: )
74: );
75: }
76:
77: /**
78: * Show the search field
79: *
80: * @since 3.0
81: * @access public
82: *
83: * @param string $text Label for the search box.
84: * @param string $input_id ID of the search box.
85: *
86: * @return void
87: */
88: public function search_box( $text, $input_id ) {
89:
90: $input_id = $input_id . '-search-input';
91:
92: if ( isset( $_REQUEST['_epl_admin_contact_search_action'] )
93: && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_epl_admin_contact_search_action'] ) ), '_epl_admin_contact_search_action' )
94: ) {
95:
96: if ( ! empty( $_REQUEST['orderby'] ) ) {
97: echo '<input type="hidden" name="orderby" value="' . esc_attr( sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ) ) . '" />';
98: }
99: if ( ! empty( $_REQUEST['order'] ) ) {
100: echo '<input type="hidden" name="order" value="' . esc_attr( sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ) ) . '" />';
101: }
102: }
103:
104: $s_contact_name = isset( $_GET['s_contact_name'] ) ? sanitize_text_field( wp_unslash( $_GET['s_contact_name'] ) ) : '';
105: ?>
106: <p class="search-box">
107: <label class="screen-reader-text" for="<?php echo esc_attr( wp_unslash( $input_id ) ); ?>"><?php echo esc_attr( wp_unslash( $text ) ); ?>:</label>
108: <input type="search" placeholder="<?php esc_attr_e( 'Search by Summary', 'easy-property-listings' ); ?>" id="<?php echo esc_attr( wp_unslash( $input_id ) ); ?>" name="s" value="<?php esc_attr( wp_unslash( _admin_search_query() ) ); ?>" />
109: <input type="search" placeholder="<?php esc_attr_e( 'Search by Name', 'easy-property-listings' ); ?>" id="" name="s_contact_name" value="<?php echo esc_attr( wp_unslash( $s_contact_name ) ); ?>" />
110: <?php wp_nonce_field( '_epl_admin_contact_search_action', '_epl_admin_contact_search_action' ); ?>
111: <?php submit_button( $text, 'button', false, false, array( 'ID' => 'search-submit' ) ); ?>
112: </p>
113: <?php
114: }
115:
116: /**
117: * Gets the name of the primary column.
118: *
119: * @since 3.0
120: * @access protected
121: *
122: * @return string Name of the primary column.
123: */
124: protected function get_primary_column_name() {
125: return 'name';
126: }
127:
128: /**
129: * This function renders most of the columns in the list table.
130: *
131: * @access public
132: * @since 3.0
133: *
134: * @param array $item Contains all the data of the contacts.
135: * @param string $column_name The name of the column.
136: *
137: * @return string Column Name
138: */
139: public function column_default( $item, $column_name ) {
140:
141: switch ( $column_name ) {
142:
143: case 'num_listings':
144: $value = 0 === intval( $item['listing_count'] ) ? '' : intval( $item['listing_count'] );
145: break;
146:
147: case 'background_info':
148: $value = esc_html( $item['background_info'] );
149: $value = substr( $value, 0, apply_filters( 'epl_contact_bg_info_length', 200 ) );
150: break;
151:
152: case 'date_created':
153: $value = date_i18n( get_option( 'date_format' ), strtotime( $item['date_created'] ) );
154: break;
155:
156: default:
157: $value = isset( $item[ $column_name ] ) ? $item[ $column_name ] : null;
158: break;
159: }
160: return apply_filters( 'epl_contacts_column_' . $column_name, $value, $item['id'] );
161: }
162:
163: /**
164: * Name for each columns
165: *
166: * @param array $item Column items.
167: *
168: * @return string
169: */
170: public function column_name( $item ) {
171:
172: $name = ! empty( $item['name'] ) ? $item['name'] : '<em>' . __( 'Unnamed Contact', 'easy-property-listings' ) . '</em>';
173: $user = ! empty( $item['user_id'] ) ? $item['user_id'] : $item['email'];
174: $view_url = admin_url( 'admin.php?page=epl-contacts&view=overview&id=' . $item['ID'] );
175: $actions = array(
176: 'view' => '<a href="' . $view_url . '">' . __( 'View', 'easy-property-listings' ) . '</a>',
177: 'delete' => '<a href="' . admin_url( 'admin.php?page=epl-contacts&view=delete&id=' . $item['ID'] ) . '">' . __( 'Delete', 'easy-property-listings' ) . '</a>',
178: );
179:
180: $contact = new EPL_Contact( $item['ID'] );
181: return '<span class="epl-contact-name">' . $name . '</span>' . $this->row_actions( $actions );
182: }
183:
184: /**
185: * Type for each column
186: *
187: * @param array $item Column items.
188: * @return string
189: */
190: public function column_type( $item ) {
191: return '<span class="epl-contact-type">' . $item['type'] . ' </span>';
192: }
193:
194: /**
195: * Column summary for each column.
196: *
197: * @param array $item Column items.
198: * @return string
199: */
200: public function column_summary( $item ) {
201: ob_start();
202: ?>
203: <div class="epl-contact-assigned-tags-wrap epl-contact-list-table-tags">
204: <h4><?php echo esc_attr( $item['heading'] ); ?> </h4>
205: <ul class="epl-contact-assigned-tags">
206: <?php
207: $contact_tags = wp_get_object_terms( $item['ID'], 'epl_contact_tag' );
208: if ( ! empty( $contact_tags ) ) {
209: if ( ! is_wp_error( $contact_tags ) ) {
210: foreach ( $contact_tags as $term ) {
211: $bgcolor = epl_get_contact_tag_bgcolor( $term->term_id );
212: echo '<li data-id="' . esc_attr( $term->term_id ) . '" id="contact-tag-' . esc_attr( $term->term_id ) . '" style="background:' . esc_attr( $bgcolor ) . '">' . esc_html( $term->name ) . '</li>';
213: }
214: }
215: }
216: ?>
217: </ul>
218: </div>
219: <?php
220: return ob_get_clean();
221: }
222:
223: /**
224: * Retrieve the table columns
225: *
226: * @access public
227: * @since 3.0
228: * @return array $columns Array of all the list table columns
229: */
230: public function get_columns() {
231: $columns = array(
232: 'cb' => '<input type="checkbox" />',
233: 'name' => __( 'Contact', 'easy-property-listings' ),
234: 'summary' => __( 'Summary', 'easy-property-listings' ),
235: 'background_info' => __( 'Background Info', 'easy-property-listings' ),
236: 'type' => __( 'Type', 'easy-property-listings' ),
237: 'num_listings' => __( 'Listings', 'easy-property-listings' ),
238: 'date_created' => __( 'Date Created', 'easy-property-listings' ),
239: );
240:
241: return apply_filters( 'epl_report_contact_columns', $columns );
242: }
243:
244: /**
245: * Render checkbox column
246: *
247: * @param array $item Column items.
248: * @return string
249: */
250: public function column_cb( $item ) {
251: return sprintf(
252: '<input type="checkbox" name="bulk-delete[]" value="%s" />',
253: $item['ID']
254: );
255: }
256:
257: /**
258: * Get the sortable columns
259: *
260: * @access public
261: * @since 3.0
262: * @return array Array of all the sortable columns
263: */
264: public function get_sortable_columns() {
265: return array(
266: 'date_created' => array( 'date_created', true ),
267: 'name' => array( 'name', true ),
268: 'num_listings' => array( 'listing_count', false ),
269: );
270: }
271:
272:
273: /**
274: * Retrieve the current page number
275: *
276: * @access public
277: * @since 3.0
278: * @return int Current page number
279: */
280: public function get_paged() {
281: return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
282: }
283:
284: /**
285: * Retrieves the search query string
286: *
287: * @access public
288: * @since 1.7
289: * @return false|string string If search is present, false otherwise
290: */
291: public function get_search() {
292: return ! empty( $_GET['s'] ) ? urldecode( trim( sanitize_text_field( wp_unslash( $_GET['s'] ) ) ) ) : false;
293: }
294:
295: /**
296: * Retrieves the search query string
297: *
298: * @access public
299: * @since 3.3
300: * @return mixed string If search is present, false otherwise
301: */
302: public function get_name_search() {
303: return ! empty( $_GET['s_contact_name'] ) ? urldecode( trim( sanitize_text_field( wp_unslash( $_GET['s_contact_name'] ) ) ) ) : false;
304: }
305:
306: /**
307: * Build all the reports data
308: *
309: * @access public
310: * @since 3.0
311: * @global object $wpdb Used to query the database using the WordPress Database API
312: * @return array $reports_data All the data for contact reports
313: */
314: public function reports_data() {
315: global $wpdb;
316:
317: $data = array();
318: $paged = $this->get_paged();
319: $offset = $this->get_items_per_page( 'contacts_per_page', $this->per_page ) * ( $paged - 1 );
320: $search = $this->get_search();
321: $name_search = $this->get_name_search();
322: $order = isset( $_GET['order'] ) ? sanitize_text_field( wp_unslash( $_GET['order'] ) ) : 'DESC';
323: $orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : 'id';
324:
325: $args = array(
326: 'post_type' => 'epl_contact',
327: 'posts_per_page' => $this->get_items_per_page( 'contacts_per_page', $this->per_page ),
328: 'offset' => $offset,
329: 'order' => $order,
330: 'orderby' => $orderby,
331: 'meta_query' => array(),
332: 'tax_query' => array(),
333: );
334:
335: if ( ! empty( $search ) ) {
336: $args['s'] = $search;
337: }
338:
339: if ( ! empty( $name_search ) ) {
340:
341: $name_search_array = explode( ' ', sanitize_text_field( $name_search ) );
342:
343: if ( ! empty( $name_search_array ) ) {
344:
345: $meta_name = array(
346: 'relation' => 'OR',
347: );
348:
349: foreach ( $name_search_array as $component ) {
350:
351: $meta_name[] = array(
352: 'key' => 'contact_first_name',
353: 'value' => sanitize_text_field( $component ),
354: 'compare' => 'LIKE',
355: );
356:
357: $meta_name[] = array(
358: 'key' => 'contact_last_name',
359: 'value' => sanitize_text_field( $component ),
360: 'compare' => 'LIKE',
361: );
362: }
363:
364: $args['meta_query'][] = $meta_name;
365: }
366: }
367:
368: if ( isset( $_GET['cat_filter'] ) && ! empty( $_GET['cat_filter'] ) ) {
369: $args['meta_query'][] = array(
370: 'key' => 'contact_category',
371: 'value' => sanitize_text_field( wp_unslash( $_GET['cat_filter'] ) ),
372: );
373: }
374:
375: if ( isset( $_GET['tag_filter'] ) && ! empty( $_GET['tag_filter'] ) ) {
376: $args['tax_query'][] = array(
377: 'taxonomy' => 'epl_contact_tag',
378: 'field' => 'id',
379: 'terms' => intval( $_GET['tag_filter'] ),
380: );
381: }
382:
383: $args = apply_filters( 'epl_get_contacts_args', $args );
384:
385: $contacts = epl_get_contacts( $args );
386:
387: if ( $contacts ) {
388:
389: foreach ( $contacts as $contact ) {
390: $contact_object = new EPL_Contact( $contact->ID );
391: $data[] = (array) $contact_object;
392: }
393: }
394: return $data;
395: }
396:
397: /**
398: * Process bulk contact operations
399: *
400: * @access public
401: *
402: * @since 3.3
403: * @since 3.4.38 Fix: Bulk action delete not working.
404: * @since 3.5.4 Fix: Contacts bulk processing nonce & permission confirmation before processing.
405: */
406: public function process_bulk_action() {
407:
408: if ( ! current_user_can( 'manage_options' ) ) {
409: return;
410: }
411:
412: // Detect when a bulk action is being triggered.
413: if ( 'bulk-delete' === $this->current_action() ) {
414:
415: if ( isset( $_GET['_wpnonce'] ) && ! empty( $_GET['_wpnonce'] ) ) {
416: $nonce = sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) );
417: } else {
418: wp_die( 'fail' );
419: }
420:
421: $action = 'bulk-' . $this->_args['plural'];
422:
423: if ( ! wp_verify_nonce( $nonce, $action ) ) {
424: wp_die( 'fail' );
425: }
426:
427: $delete_ids = isset( $_GET['bulk-delete'] ) ? (array) array_map( 'absint', wp_unslash( $_GET['bulk-delete'] ) ) : array();
428:
429: // Loop over the array of record IDs and delete them.
430: if ( ! empty( $delete_ids ) ) {
431: foreach ( $delete_ids as $id ) {
432: $contact = new EPL_Contact( $id );
433: $contact->delete();
434: }
435: }
436: }
437: }
438:
439: /**
440: * Setup the final data for the table
441: *
442: * @access public
443: * @since 3.0
444: * @uses EPL_Contact_Reports_Table::get_columns()
445: * @uses WP_List_Table::get_sortable_columns()
446: * @uses EPL_Contact_Reports_Table::get_pagenum()
447: * @uses EPL_Contact_Reports_Table::get_total_contacts()
448: * @return void
449: */
450: public function prepare_items() {
451:
452: /** Process bulk action */
453: $this->process_bulk_action();
454:
455: $columns = $this->get_columns();
456: $hidden = array(); // No hidden columns.
457: $sortable = $this->get_sortable_columns();
458:
459: $this->_column_headers = $this->get_column_info();
460:
461: $this->items = $this->reports_data();
462:
463: $this->total = epl_count_total_contacts();
464:
465: $this->set_pagination_args(
466: array(
467: 'total_items' => $this->total,
468: 'per_page' => $this->get_items_per_page( 'contacts_per_page', $this->per_page ),
469: 'total_pages' => ceil( $this->total / $this->get_items_per_page( 'contacts_per_page', $this->per_page ) ),
470: )
471: );
472: }
473:
474: /**
475: * Get bulk contact actions
476: *
477: * @access public
478: * @since 3.3
479: */
480: public function get_bulk_actions() {
481: $actions = array(
482: 'bulk-delete' => __( 'Delete', 'easy-property-listings' ),
483: );
484:
485: return $actions;
486: }
487:
488: /**
489: * Output the contact categories
490: *
491: * @param string $which nav location.
492: *
493: * @access public
494: * @since 3.0
495: * @since 3.5 Added accessibility labels to select elements.
496: */
497: public function extra_tablenav( $which ) {
498: if ( 'top' === $which ) {
499: ?>
500: <div class="alignleft actions bulkactions">
501: <?php
502: $cats = epl_get_contact_categories();
503: if ( $cats ) {
504: ?>
505: <select aria-label="<?php esc_attr_e( 'Category Filter', 'easy-property-listings' ); ?>" name="cat_filter" class="epl_contact_type_filter">
506: <option value=""><?php esc_html_e( 'Type:', 'easy-property-listings' ); ?></option>
507: <?php
508: foreach ( $cats as $cat_key => $cat_value ) {
509: $selected = '';
510: if ( isset( $_GET['cat_filter'] ) && $_GET['cat_filter'] === $cat_key ) {
511: $selected = ' selected ';
512: }
513: ?>
514: <option value="<?php echo esc_attr( $cat_key ); ?>" <?php echo esc_attr( $selected ); ?>>
515: <?php echo esc_attr( $cat_value ); ?>
516: </option>
517: <?php
518: }
519: ?>
520: </select>
521: <?php
522: }
523: ?>
524: </div>
525: <?php
526: }
527: }
528: }
529: