prefix . "alert_me"; if ($return) $rows = array(); $sql = "SELECT * FROM $table_name ORDER BY created_at DESC"; $results = $wpdb->get_results( $sql, 'ARRAY_A' ); $rows = array(); foreach ($results as $key => $value) { $row = array(); $row[] = $value['email']; $row[] = get_the_title($value['post_id']); $row[] = date('m/d/Y', strtotime($value['created_at'])); if ($return) $rows[] = $row; else fputcsv($dest, $row); } if ($return) return $rows; } if ( ! class_exists( 'WP_List_Table' ) ) { require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); } class Alert_Me_Subscribers_List extends WP_List_Table { /** Class constructor */ public function __construct() { parent::__construct( [ 'singular' => __( 'Singature', 'sp' ), //singular name of the listed records 'plural' => __( 'Singatures', 'sp' ), //plural name of the listed records 'ajax' => false //does this table support ajax? ] ); } /** * Retrieve customers data from the database * * @param int $per_page * @param int $page_number * * @return mixed */ public static function get_customers( $per_page = 5, $page_number = 1 ) { global $wpdb; $table_name = $wpdb->prefix . "alert_me"; $search = ( isset( $_REQUEST['s'] ) ) ? sanitize_text_field($_REQUEST['s']) : false; $do_search = ( $search ) ? $wpdb->prepare(" email LIKE '%%%s%%'", $search) : ''; $sql = "SELECT * FROM $table_name where id != '' "; $sql .= $do_search; if ( ! empty( $_REQUEST['orderby'] ) ) { $sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] ); $sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC'; } else { $sql .= " ORDER BY created_at DESC"; } $sql .= " LIMIT $per_page"; $sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page; $result = $wpdb->get_results( $sql, 'ARRAY_A' ); //echo "
";		print_r($result);	echo "
"; return $result; } /** * Delete a customer record. * * @param int $id customer id */ public static function delete_donation( $id ) { global $wpdb; $wpdb->delete( "{$wpdb->prefix}alert_me", [ 'id' => $id ], [ '%d' ] ); } /** * Returns the count of records in the database. * * @return null|string */ public static function record_count() { global $wpdb; $table_name = $wpdb->prefix . "alert_me"; $search = ( isset( $_REQUEST['s'] ) ) ? $_REQUEST['s'] : false; $do_search = ( $search ) ? $wpdb->prepare(" AND user_repair_order LIKE '%%%s%%' OR email LIKE '%%%s%%' OR user_car_count LIKE '%%%s%%'", $search, $search, $search) : ''; $sql = "SELECT COUNT(*) FROM $table_name WHERE id != '' "; $sql .= $do_search; return $wpdb->get_var( $sql ); } /** Text displayed when no customer data is available */ public function no_items() { _e( 'No subscribers currently.', 'sp' ); } /** * Render a column when no column specific method exist. * * @param array $item * @param string $column_name * * @return mixed */ public function column_default( $item, $column_name ) { switch ( $column_name ) { case 'id': return $item[ $column_name ]; case 'email': //return $this->column_name($item); return $item[ $column_name ]; case 'post_id': return $this->get_post_name($item); case 'is_unsubscribed': return (($item[ $column_name ] == 0)? 'Active' : 'Unsubscribed' ); case 'created_at': return date('F j, Y', strtotime($item[ $column_name ])); default: return print_r( $item, true ); //Show the whole array for troubleshooting purposes } } /** * Render the bulk edit checkbox * * @param array $item * * @return string */ function column_cb( $item ) { return sprintf( '', $item['id'] ); } /** * Method for name column * * @param array $item an array of DB data * * @return string */ function column_name( $item ) { $delete_nonce = wp_create_nonce( 'rt_delete_alertme_item' ); $title = '' . $item['email'] . ''; $actions = [ 'delete' => sprintf( 'Delete', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['id'] ), $delete_nonce ) ]; //$actions = array(); return $title . $this->row_actions( $actions ); } function get_post_name($item) { return get_the_title($item['post_id']); } /** * Associative array of columns * * @return array */ function get_columns() { $columns = [ 'cb' => '', 'id' => __( 'ID', 'sp' ), 'email' => __( 'Email', 'sp' ), 'post_id' => __( 'Post/Page', 'sp' ), 'is_unsubscribed' => __( 'Subscribe Status', 'sp' ), 'created_at' => __( 'Calculated Date', 'sp' ), ]; return $columns; } /** * Columns to make sortable. * * @return array */ public function get_sortable_columns() { $sortable_columns = array( 'id' => array( 'id', true ), 'email' => array( 'email', true ) ); return $sortable_columns; } /** * Returns an associative array containing the bulk action * * @return array */ public function get_bulk_actions() { $actions = [ 'bulk-delete' => 'Delete' ]; return $actions; } /** * Handles data query and filter, sorting, and pagination. */ public function prepare_items() { /** Process bulk action */ $this->process_bulk_action(); $this->_column_headers = $this->get_column_info(); $per_page = $this->get_items_per_page( 'alert_me_per_page', 10 ); $current_page = $this->get_pagenum(); $total_items = self::record_count(); $this->set_pagination_args( [ 'total_items' => $total_items, //WE have to calculate the total number of items 'per_page' => $per_page //WE have to determine how many items to show on a page ] ); $this->items = self::get_customers( $per_page, $current_page ); } public function process_bulk_action() { //Detect when a bulk action is being triggered... if ( 'delete' === $this->current_action() ) { // In our file that handles the request, verify the nonce. $nonce = esc_attr( $_REQUEST['_wpnonce'] ); if ( ! wp_verify_nonce( $nonce, 'rt_delete_alertme_item' ) ) { die( 'Go get a life script kiddies' ); } else { self::delete_donation( absint( $_GET['id'] ) ); //wp_redirect( esc_url( add_query_arg() ) ); wp_redirect( $_SERVER['HTTP_REFERER'] ); exit; } } // If the delete bulk action is triggered if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' ) ) { $delete_ids = esc_sql( $_POST['bulk-delete'] ); // loop over the array of record ids and delete them foreach ( $delete_ids as $id ) { self::delete_donation( $id ); } wp_redirect( $_SERVER['HTTP_REFERER'] ); exit; } } } class AlertMe_load_Subscriber_page { // class instance static $instance; // customer WP_List_Table object public $customers_obj; // class constructor public function __construct() { add_filter( 'set-screen-option', [ __CLASS__, 'set_screen' ], 10, 3 ); add_action( 'admin_menu', [ $this, 'plugin_menu' ] ); } public static function set_screen( $status, $option, $value ) { return $value; } public function plugin_menu() { $hook = add_submenu_page( 'tools.php', 'Alert Me Subscribers', 'AlertMe Subscribers', 'manage_options', 'alert-me-subscribers', [ $this, 'alert_me_subscribers' ]); add_action( "load-$hook", [ $this, 'screen_option' ] ); } /** * Plugin settings page */ public function alert_me_subscribers() { ?>

Alert Me Subscribers

customers_obj->current_action()) { $message = '

' . sprintf(__('Items deleted: %d', 'cltd_example'), '1' ) . '

'; } elseif ('bulk-delete' === $this->customers_obj->current_action()) { $message = '

' . sprintf(__('Items deleted: %d', 'cltd_example'), count($_REQUEST['bulk-delete'])) . '

'; } ?> Go Back to List',$_SERVER['HTTP_REFERER'] ); ?>
customers_obj->prepare_items(); $this->customers_obj->display(); ?>

'Alert Me', 'default' => 5, 'option' => 'alert_me_per_page' ]; add_screen_option( $option, $args ); $this->customers_obj = new Alert_Me_Subscribers_List(); } /** Singleton instance */ public static function get_instance() { if ( ! isset( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } } ?>