Subscribers screen. */ class NINETYTWO_NOTIFY_Subscribers_List_Table extends WP_List_Table { /** * Sets up the table's singular/plural labels. */ public function __construct() { parent::__construct( array( 'singular' => 'subscriber', 'plural' => 'subscribers', 'ajax' => false, ) ); } /** * Defines the table's columns. * * @return array */ public function get_columns() { return array( 'cb' => '', 'id' => __( 'ID', '92-notify-for-woocommerce' ), 'email' => __( 'Email', '92-notify-for-woocommerce' ), 'product_name' => __( 'Product', '92-notify-for-woocommerce' ), 'match_type' => __( 'Source', '92-notify-for-woocommerce' ), 'status' => __( 'Status', '92-notify-for-woocommerce' ), 'created_at' => __( 'Subscribed', '92-notify-for-woocommerce' ), 'notified_at' => __( 'Last Notified', '92-notify-for-woocommerce' ), ); } /** * Builds the All/Active/Unsubscribed filter links above the table. * * @return array */ public function get_views() { global $wpdb; $table_name = $wpdb->prefix . 'ninetytwo_notify_subscribers'; // Read-only display filter, not a state-changing request; no nonce applies. // phpcs:ignore WordPress.Security.NonceVerification.Recommended $current = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : ''; $base_url = remove_query_arg( 'status' ); $active_count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE status = 1', $table_name ) ); $inactive_count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE status = 0', $table_name ) ); return array( 'all' => sprintf( '%s', esc_url( $base_url ), '' === $current ? ' class="current"' : '', esc_html__( 'All', '92-notify-for-woocommerce' ) . ' (' . ( $active_count + $inactive_count ) . ')' ), 'active' => sprintf( '%s', esc_url( add_query_arg( 'status', 'active', $base_url ) ), 'active' === $current ? ' class="current"' : '', esc_html__( 'Active', '92-notify-for-woocommerce' ) . ' (' . $active_count . ')' ), 'inactive' => sprintf( '%s', esc_url( add_query_arg( 'status', 'inactive', $base_url ) ), 'inactive' === $current ? ' class="current"' : '', esc_html__( 'Unsubscribed', '92-notify-for-woocommerce' ) . ' (' . $inactive_count . ')' ), ); } /** * Defines the available bulk actions. * * @return array */ public function get_bulk_actions() { return array( 'delete' => __( 'Delete', '92-notify-for-woocommerce' ) ); } /** * Renders the row-select checkbox column. * * @param array $item Current row. * @return string */ public function column_cb( $item ) { return sprintf( '', (int) $item['id'] ); } /** * Renders the Status column. * * @param array $item Current row. * @return string */ public function column_status( $item ) { return $item['status'] ? '' . esc_html__( 'Active', '92-notify-for-woocommerce' ) . '' : '' . esc_html__( 'Unsubscribed', '92-notify-for-woocommerce' ) . ''; } /** * Renders the Source (exact/similar) column. * * @param array $item Current row. * @return string */ public function column_match_type( $item ) { return $item['variation_id'] ? esc_html__( 'Exact (variation)', '92-notify-for-woocommerce' ) : esc_html__( 'Exact/Similar', '92-notify-for-woocommerce' ); } /** * Renders the Product column, linked to the product when available. * * @param array $item Current row. * @return string */ public function column_product_name( $item ) { if ( empty( $item['product_url'] ) ) { return esc_html( $item['product_name'] ); } return sprintf( '%s', esc_url( $item['product_url'] ), esc_html( $item['product_name'] ) ); } /** * Renders the Last Notified column. * * @param array $item Current row. * @return string */ public function column_notified_at( $item ) { return $item['notified_at'] ? esc_html( $item['notified_at'] ) : '—'; } /** * Renders the ID column with the row's Delete action link. * * @param array $item Current row. * @return string */ public function column_id( $item ) { $delete_url = wp_nonce_url( add_query_arg( array( 'page' => '92-notify-subscribers', 'action' => 'delete', 'subscriber' => $item['id'], ), admin_url( 'admin.php' ) ), 'ninetytwo_notify_delete_subscriber_' . $item['id'] ); $actions = array( 'delete' => sprintf( '%s', esc_url( $delete_url ), esc_js( __( 'Delete this subscriber?', '92-notify-for-woocommerce' ) ), esc_html__( 'Delete', '92-notify-for-woocommerce' ) ), ); return esc_html( $item['id'] ) . $this->row_actions( $actions ); } /** * Handles both the single row "Delete" link and the bulk-action * dropdown, each gated behind its own capability + nonce check. */ public function process_bulk_action() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } global $wpdb; $table_name = $wpdb->prefix . 'ninetytwo_notify_subscribers'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce is verified via check_admin_referer() just below. if ( 'delete' === $this->current_action() && ! empty( $_GET['subscriber'] ) && ! is_array( $_GET['subscriber'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce is verified via check_admin_referer() just below. $id = absint( $_GET['subscriber'] ); check_admin_referer( 'ninetytwo_notify_delete_subscriber_' . $id ); $wpdb->delete( $table_name, array( 'id' => $id ), array( '%d' ) ); return; } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce is verified via check_admin_referer() just below. if ( 'delete' === $this->current_action() && ! empty( $_POST['subscriber'] ) && is_array( $_POST['subscriber'] ) ) { check_admin_referer( 'bulk-' . $this->_args['plural'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce already verified above. $ids = array_map( 'absint', wp_unslash( $_POST['subscriber'] ) ); foreach ( $ids as $id ) { $wpdb->delete( $table_name, array( 'id' => $id ), array( '%d' ) ); } } } /** * Defines which columns are sortable. * * @return array */ public function get_sortable_columns() { return array( 'id' => array( 'id', false ), 'email' => array( 'email', false ), 'product_name' => array( 'product_name', false ), 'created_at' => array( 'created_at', true ), 'notified_at' => array( 'notified_at', false ), ); } /** * Builds the SQL WHERE/ORDER BY/LIMIT for the current search, filter, * sort, and page, and loads only that one page of rows. */ public function prepare_items() { global $wpdb; $table_name = $wpdb->prefix . 'ninetytwo_notify_subscribers'; $per_page = 20; $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array( $columns, $hidden, $sortable ); $where = array( '1=1' ); $params = array(); // Read-only search/filter/sort parameters; no nonce applies (same as core list tables). // phpcs:ignore WordPress.Security.NonceVerification.Recommended $search = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : ''; if ( '' !== $search ) { $where[] = '(email LIKE %s OR product_name LIKE %s)'; $like = '%' . $wpdb->esc_like( $search ) . '%'; $params[] = $like; $params[] = $like; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $status = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : ''; if ( 'active' === $status ) { $where[] = 'status = 1'; } elseif ( 'inactive' === $status ) { $where[] = 'status = 0'; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $product_filter = isset( $_GET['product_id'] ) ? absint( $_GET['product_id'] ) : 0; if ( $product_filter ) { $where[] = 'product_id = %d'; $params[] = $product_filter; } $where_sql = implode( ' AND ', $where ); $allowed_orderby = array( 'id', 'email', 'product_name', 'created_at', 'notified_at' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended $orderby = ! empty( $_REQUEST['orderby'] ) && in_array( $_REQUEST['orderby'], $allowed_orderby, true ) ? sanitize_key( $_REQUEST['orderby'] ) : 'id'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $order = ! empty( $_REQUEST['order'] ) && 'asc' === strtolower( sanitize_key( $_REQUEST['order'] ) ) ? 'ASC' : 'DESC'; $current_page = $this->get_pagenum(); $offset = ( $current_page - 1 ) * $per_page; // $where_sql is built above only from fixed fragments ('1=1', 'status = 1', // etc.) with values passed as %s/%d placeholders; $orderby is constrained to // $allowed_orderby and $order to the literal 'ASC'/'DESC', so this interpolation // never carries raw user input. $total_items = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE $where_sql", array_merge( array( $table_name ), $params ) ) // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter ); $data_params = array_merge( array( $table_name ), $params, array( $per_page, $offset ) ); $this->items = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i WHERE $where_sql ORDER BY $orderby $order LIMIT %d OFFSET %d", $data_params ), // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, PluginCheck.Security.DirectDB.UnescapedDBParameter ARRAY_A ); $this->set_pagination_args( array( 'total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil( $total_items / $per_page ), ) ); } /** * Renders any column without a dedicated column_* method. * * @param array $item Current row. * @param string $column_name Column being rendered. * @return string */ public function column_default( $item, $column_name ) { return esc_html( $item[ $column_name ] ?? '' ); } /** * Displayed when there are no subscribers to list. */ public function no_items() { esc_html_e( 'No subscribers yet.', '92-notify-for-woocommerce' ); } }