| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- <?php
- /*******************************************************************************
- * Copyright (c) 2019, Code Atlantic LLC
- ******************************************************************************/
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- /**
- * Class PUM_Admin_Subscribers_Table
- */
- class PUM_Admin_Subscribers_Table extends PUM_ListTable {
- /**
- * Constructor.
- *
- * The child class should call this constructor from its own constructor to override
- * the default $args.
- *
- * @param array|string $args {
- * Array or string of arguments.
- *
- * @type string $plural Plural value used for labels and the objects being listed.
- * This affects things such as CSS class-names and nonces used
- * in the list table, e.g. 'posts'. Default empty.
- * @type string $singular Singular label for an object being listed, e.g. 'post'.
- * Default empty
- * @type bool $ajax Whether the list table supports Ajax. This includes loading
- * and sorting data, for example. If true, the class will call
- * the _js_vars() method in the footer to provide variables
- * to any scripts handling Ajax events. Default false.
- * @type string $screen String containing the hook name used to determine the current
- * screen. If left null, the current screen will be automatically set.
- * Default null.
- * }
- */
- public function __construct( $args = array() ) {
- $args = wp_parse_args( $args, array(
- 'plural' => 'subscribers', // Plural value used for labels and the objects being listed.
- 'singular' => 'subscriber', // Singular label for an object being listed, e.g. 'post'.
- 'ajax' => false, // If true, the parent class will call the _js_vars() method in the footer
- ) );
- parent::__construct( $args );
- }
- /**
- * Prepares the list of items for displaying.
- *
- * @uses PUM_ListTable::set_pagination_args()
- */
- public function prepare_items() {
- $this->_column_headers = $this->get_column_info();
- // check and process any actions such as bulk actions.
- $this->handle_table_actions();
- $limit = $this->get_items_per_page( 'pum_subscribers_per_page' );
- $query_args = array(
- 's' => isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : null,
- 'limit' => $limit,
- 'page' => $this->get_pagenum(),
- 'orderby' => isset( $_REQUEST['orderby'] ) ? sanitize_text_field( $_REQUEST['orderby'] ) : null,
- 'order' => isset( $_REQUEST['order'] ) ? sanitize_text_field( $_REQUEST['order'] ) : null,
- );
- $this->items = PUM_DB_Subscribers::instance()->query( $query_args, 'ARRAY_A' );
- $total_subscribers = PUM_DB_Subscribers::instance()->total_rows( $query_args );
- $this->set_pagination_args( array(
- 'total_items' => $total_subscribers,
- 'per_page' => $limit,
- 'total_pages' => ceil( $total_subscribers / $limit ),
- ) );
- }
- /**
- * Get a list of columns. The format is:
- * 'internal-name' => 'Title'
- *
- * @return array
- */
- public function get_columns() {
- return apply_filters( 'pum_subscribers_table_columns', array(
- 'cb' => '<input type="checkbox" />', // to display the checkbox.
- 'email' => __( 'Email', 'popup-maker' ),
- 'name' => __( 'Full Name', 'popup-maker' ),
- 'fname' => __( 'First Name', 'popup-maker' ),
- 'lname' => __( 'Last Name', 'popup-maker' ),
- 'popup_id' => __( 'Popup', 'popup-maker' ),
- //'user_id' => __( 'User ID', 'popup-maker' ),
- 'created' => _x( 'Subscribed On', 'column name', 'popup-maker' ),
- ) );
- }
- /**
- * Get a list of sortable columns. The format is:
- * 'internal-name' => 'orderby'
- * or
- * 'internal-name' => array( 'orderby', true )
- *
- * The second format will make the initial sorting order be descending
- * \ *
- *
- * @return array
- */
- protected function get_sortable_columns() {
- return apply_filters( 'pum_subscribers_table_columns', array(
- 'email' => 'email',
- 'fname' => 'fname',
- 'lname' => 'lname',
- 'popup_id' => 'popup_id',
- 'created' => 'created',
- ) );
- }
- /**
- * Gets the name of the primary column.
- *
- * @return string The name of the primary column.
- */
- protected function get_primary_column_name() {
- return 'email';
- }
- /**
- * Text displayed when no user data is available
- */
- public function no_items() {
- _e( 'No subscribers available.', 'popup-maker' );
- }
- /**
- * Render a column when no column specific method exists.
- *
- * @param array $item
- * @param string $column_name
- *
- * @return mixed
- */
- public function column_default( $item, $column_name ) {
- switch ( $column_name ) {
- case 'created':
- return date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $item[ $column_name ] ) );
- default:
- return $item[ $column_name ];
- }
- }
- /**
- * Get value for checkbox column.
- *
- * The special 'cb' column
- *
- * @param object $item A row's data
- *
- * @return string Text to be placed inside the column <td>.
- */
- protected function column_cb( $item ) {
- $label = sprintf( '<label class="screen-reader-text" for="subscriber_%d">%s</label>', $item['ID'], sprintf( __( 'Select %s' ), $item['name'] ) );
- $input = sprintf( '<input type="checkbox" name="%1$s[]" id="subscriber_%2$d" value="%2$d" />', $this->_args['singular'], $item['ID'] );
- return sprintf( '%s%s', $label, $input );
- }
- /** ************************************************************************
- * Recommended. This is a custom column method and is responsible for what
- * is rendered in any column with a name/slug of 'title'. Every time the class
- * needs to render a column, it first looks for a method named
- * column_{$column_title} - if it exists, that method is run. If it doesn't
- * exist, column_default() is called instead.
- *
- * This example also illustrates how to implement rollover actions. Actions
- * should be an associative array formatted as 'slug'=>'link html' - and you
- * will need to generate the URLs yourself. You could even ensure the links
- *
- *
- * @see WP_List_Table::::single_row_columns()
- *
- * @param array $item A singular item (one full row's worth of data)
- *
- * @return string Text to be placed inside the column <td> (movie title only)
- **************************************************************************/
- function column_email( $item ) {
- $url = add_query_arg( array(
- 'page' => $_REQUEST['page'],
- 'subscriber' => $item['ID'],
- '_wpnonce' => wp_create_nonce( 'pum_subscribers_table_action_nonce' ),
- ), admin_url( 'edit.php?page=pum-subscribers&post_type=popup' ) );
- $edit_url = add_query_arg( array(
- 'action' => 'edit',
- ), $url );
- $delete_url = add_query_arg( array(
- 'action' => 'delete',
- ), $url );
- //Build row actions
- $actions = array(
- //'edit' => sprintf( '<a href="%s">Edit</a>', $edit_url ),
- 'delete' => sprintf( '<a href="%s">Delete</a>', $delete_url ),
- );
- //Return the title contents
- return sprintf( '%1$s <span style="color:silver">(id:%2$s)</span>%3$s', /*$1%s*/
- $item['email'], /*$2%s*/
- $item['ID'], /*$3%s*/
- $this->row_actions( $actions ) );
- }
- /** ************************************************************************
- * Recommended. This is a custom column method and is responsible for what
- * is rendered in any column with a name/slug of 'title'. Every time the class
- * needs to render a column, it first looks for a method named
- * column_{$column_title} - if it exists, that method is run. If it doesn't
- * exist, column_default() is called instead.
- *
- * This example also illustrates how to implement rollover actions. Actions
- * should be an associative array formatted as 'slug'=>'link html' - and you
- * will need to generate the URLs yourself. You could even ensure the links
- *
- *
- * @see WP_List_Table::::single_row_columns()
- *
- * @param array $item A singular item (one full row's worth of data)
- *
- * @return string Text to be placed inside the column <td> (movie title only)
- **************************************************************************/
- function column_name( $item ) {
- $user_id = $item['user_id'] > 0 ? absint( $item['user_id'] ) : null;
- if ( $user_id ) {
- $url = admin_url( "user-edit.php?user_id=$user_id" );
- //Return the title contents
- return sprintf( '%s<br/><small style="color:silver">(%s: <a href="%s">#%s</a>)</small>', $item['name'], __( 'User ID', 'popup-maker' ), $url, $item['user_id'] );
- } else {
- return $item['name'];
- }
- }
- /** ************************************************************************
- * Recommended. This is a custom column method and is responsible for what
- * is rendered in any column with a name/slug of 'title'. Every time the class
- * needs to render a column, it first looks for a method named
- * column_{$column_title} - if it exists, that method is run. If it doesn't
- * exist, column_default() is called instead.
- *
- * This example also illustrates how to implement rollover actions. Actions
- * should be an associative array formatted as 'slug'=>'link html' - and you
- * will need to generate the URLs yourself. You could even ensure the links
- *
- *
- * @see WP_List_Table::::single_row_columns()
- *
- * @param array $item A singular item (one full row's worth of data)
- *
- * @return string Text to be placed inside the column <td> (movie title only)
- **************************************************************************/
- function column_popup_id( $item ) {
- $popup_id = $item['popup_id'] > 0 ? absint( $item['popup_id'] ) : null;
- $popup = pum_get_popup( $popup_id );
- if ( $popup_id && pum_is_popup( $popup ) ) {
- $url = admin_url( "post.php?post={$popup_id}&action=edit" );;
- //Return the title contents
- return sprintf( '%s<br/><small style="color:silver">(%s: <a href="%s">#%s</a>)</small>', $popup->post_title, __( 'ID', 'popup-maker' ), $url, $item['popup_id'] );
- } else {
- return __( 'N/A', 'popup-maker' );
- }
- }
- /**
- * Returns an associative array containing the bulk action
- *
- * @return array
- */
- public function get_bulk_actions() {
- /*
- * on hitting apply in bulk actions the url params are set as
- * ?action=bulk-download&paged=1&action2=-1
- *
- * action and action2 are set based on the triggers above or below the table
- */
- $actions = array(
- 'bulk-delete' => __( 'Delete', 'popup-maker' ),
- );
- return $actions;
- }
- /**
- * Process actions triggered by the user
- *
- * @since 1.0.0
- *
- */
- public function handle_table_actions() {
- //Detect when a bulk action is being triggered...
- $action1 = $this->current_action();
- if ( in_array( $action1, array( 'delete', 'bulk-delete' ) ) ) {
- // verify the nonce.
- if ( ! wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), $action1 == 'delete' ? 'pum_subscribers_table_action_nonce' : 'bulk-subscribers' ) ) {
- $this->invalid_nonce_redirect();
- } else {
- $subscribers = isset( $_REQUEST['subscriber'] ) ? $_REQUEST['subscriber'] : array();
- if ( is_numeric( $subscribers ) ) {
- $subscribers = array( $subscribers );
- }
- $subscribers = wp_parse_id_list( $subscribers );
- if ( $subscribers ) {
- $status = array();
- foreach ( $subscribers as $subscriber_id ) {
- $status[] = PUM_DB_Subscribers::instance()->delete( $subscriber_id );
- }
- if ( ! in_array( false, $status ) ) {
- wp_die( sprintf( _n( 'Subscriber deleted!', '%d Subscribers deleted!', count( $subscribers ), 'popup-maker' ), count( $subscribers ) ), __( 'Success', 'popup-maker' ), array(
- 'response' => 200,
- 'back_link' => esc_url( admin_url( 'edit.php?page=pum-subscribers&post_type=popup' ) ),
- ) );
- } else {
- $succeeded = count( array_filter( $status ) );
- $failed = count( $subscribers ) - $succeeded;
- if ( count( $subscribers ) == 1 ) {
- wp_die( __( 'Deleting subscriber failed.', 'popup-maker' ), __( 'Error', 'popup-maker' ), array(
- 'response' => 200,
- 'back_link' => esc_url( admin_url( 'edit.php?page=pum-subscribers&post_type=popup' ) ),
- ) );
- } else {
- wp_die( sprintf( __( '%d Subscribers deleted, %d failed', 'popup-maker' ), $succeeded, $failed ), __( 'Error', 'popup-maker' ), array(
- 'response' => 200,
- 'back_link' => esc_url( admin_url( 'edit.php?page=pum-subscribers&post_type=popup' ) ),
- ) );
- }
- }
- }
- wp_die( __( 'Uh oh, the subscribers was not deleted successfully!', 'popup-maker' ), __( 'Error', 'popup-maker' ), array(
- 'response' => 200,
- 'back_link' => esc_url( admin_url( 'edit.php?page=pum-subscribers&post_type=popup' ) ),
- ) );
- exit;
- }
- }
- /*
- * Note: Table bulk_actions can be identified by checking $_REQUEST['action'] and $_REQUEST['action2']
- *
- * action - is set if checkbox from top-most select-all is set, otherwise returns -1
- * action2 - is set if checkbox the bottom-most select-all checkbox is set, otherwise returns -1
- */
- // check for individual row actions
- $the_table_action = $this->current_action();
- if ( 'view_usermeta' === $the_table_action ) {
- $nonce = wp_unslash( $_REQUEST['_wpnonce'] );
- // verify the nonce.
- if ( ! wp_verify_nonce( $nonce, 'view_usermeta_nonce' ) ) {
- $this->invalid_nonce_redirect();
- } else {
- $this->page_view_usermeta( absint( $_REQUEST['user_id'] ) );
- $this->graceful_exit();
- }
- }
- if ( 'add_usermeta' === $the_table_action ) {
- $nonce = wp_unslash( $_REQUEST['_wpnonce'] );
- // verify the nonce.
- if ( ! wp_verify_nonce( $nonce, 'add_usermeta_nonce' ) ) {
- $this->invalid_nonce_redirect();
- } else {
- $this->page_add_usermeta( absint( $_REQUEST['user_id'] ) );
- $this->graceful_exit();
- }
- }
- // check for table bulk actions
- if ( ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'bulk-download' ) || ( isset( $_REQUEST['action2'] ) && $_REQUEST['action2'] === 'bulk-download' ) ) {
- $nonce = wp_unslash( $_REQUEST['_wpnonce'] );
- // verify the nonce.
- /*
- * Note: the nonce field is set by the parent class
- * wp_nonce_field( 'bulk-' . $this->_args['plural'] );
- *
- */
- if ( ! wp_verify_nonce( $nonce, 'bulk-users' ) ) {
- $this->invalid_nonce_redirect();
- } else {
- $this->page_bulk_download( $_REQUEST['users'] );
- $this->graceful_exit();
- }
- }
- }
- /**
- * Die when the nonce check fails.
- */
- public function invalid_nonce_redirect() {
- wp_die( __( 'Invalid Nonce', 'popup-maker' ), __( 'Error', 'popup-maker' ), array(
- 'response' => 403,
- 'back_link' => esc_url( admin_url( 'edit.php?page=pum-subscribers&post_type=popup' ) ),
- ) );
- }
- }
|