| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | if ( ! defined( 'ABSPATH' ) ) { |
| 14: | exit; |
| 15: | } |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 22: | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 23: | } |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | class EPL_Contact_Reports_Table extends WP_List_Table { |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | public $per_page = 30; |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | public $count = 0; |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public $total = 0; |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | public function __construct() { |
| 65: | global $status, $page; |
| 66: | |
| 67: | |
| 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: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 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: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | protected function get_primary_column_name() { |
| 125: | return 'name'; |
| 126: | } |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 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: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 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: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | public function column_type( $item ) { |
| 191: | return '<span class="epl-contact-type">' . $item['type'] . ' </span>'; |
| 192: | } |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 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: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 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: | |
| 246: | |
| 247: | |
| 248: | |
| 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: | |
| 259: | |
| 260: | |
| 261: | |
| 262: | |
| 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: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: | public function get_paged() { |
| 281: | return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
| 282: | } |
| 283: | |
| 284: | |
| 285: | |
| 286: | |
| 287: | |
| 288: | |
| 289: | |
| 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: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 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: | |
| 308: | |
| 309: | |
| 310: | |
| 311: | |
| 312: | |
| 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: | |
| 399: | |
| 400: | |
| 401: | |
| 402: | |
| 403: | |
| 404: | |
| 405: | |
| 406: | public function process_bulk_action() { |
| 407: | |
| 408: | if ( ! current_user_can( 'manage_options' ) ) { |
| 409: | return; |
| 410: | } |
| 411: | |
| 412: | |
| 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: | |
| 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: | |
| 441: | |
| 442: | |
| 443: | |
| 444: | |
| 445: | |
| 446: | |
| 447: | |
| 448: | |
| 449: | |
| 450: | public function prepare_items() { |
| 451: | |
| 452: | |
| 453: | $this->process_bulk_action(); |
| 454: | |
| 455: | $columns = $this->get_columns(); |
| 456: | $hidden = array(); |
| 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: | |
| 476: | |
| 477: | |
| 478: | |
| 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: | |
| 490: | |
| 491: | |
| 492: | |
| 493: | |
| 494: | |
| 495: | |
| 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: | |