the queue itself, where the order is edited * * Creating and deleting are ordinary form posts handled before anything is rendered, so * they can redirect afterwards - the pattern core uses to keep a reload from repeating * the action. */ class Editor extends Component { const PAGE_SLUG = 'tools-postqueue'; const NONCE_ACTION = 'postqueue_admin'; private ?QueuesListTable $table = null; public function onCreate() { add_action( 'admin_menu', array( $this, 'tools_page' ) ); } /** * @return string */ public function getCapability() { return apply_filters( Plugin::FILTER_POSTQUEUE_EDIT_CAPABILITY, 'manage_options' ); } public function tools_page() { $hook = add_submenu_page( 'tools.php', _x( 'Postqueues', 'admin page', 'postqueue' ), _x( 'Postqueues', 'admin menu', 'postqueue' ), $this->getCapability(), self::PAGE_SLUG, array( $this, 'render' ) ); if ( $hook ) { // Runs before the page is rendered, which is what makes a redirect after a // create or delete possible, and it is where WP_List_Table wants building. add_action( "load-$hook", array( $this, 'load' ) ); } } public function load() { if ( ! current_user_can( $this->getCapability() ) ) { return; } $this->handleActions(); if ( ! $this->currentQueueId() ) { $this->table = new QueuesListTable( $this->plugin->store, self::PAGE_SLUG ); $this->table->prepare_items(); } } /** * The queue being edited, or 0 for the overview. */ private function currentQueueId(): int { $id = isset( $_GET['queue'] ) ? (int) $_GET['queue'] : 0; return ( $id > 0 && $this->plugin->store->queueExists( $id ) ) ? $id : 0; } private function pageUrl( array $args = array() ): string { return add_query_arg( array_merge( array( 'page' => self::PAGE_SLUG ), $args ), admin_url( 'tools.php' ) ); } /** * Create and delete, then redirect. Nothing is rendered from here. */ private function handleActions(): void { $action = isset( $_REQUEST['action'] ) ? sanitize_key( $_REQUEST['action'] ) : ''; $bulk = isset( $_REQUEST['action2'] ) && 'delete' === sanitize_key( $_REQUEST['action2'] ); if ( 'create' === $action ) { check_admin_referer( self::NONCE_ACTION ); $name = isset( $_POST['postqueue_name'] ) ? sanitize_text_field( wp_unslash( $_POST['postqueue_name'] ) ) : ''; $result = $this->plugin->store->create( $name ); wp_safe_redirect( $this->pageUrl( array( 'message' => ! empty( $result->success ) ? 'created' : ( ( isset( $result->reason ) && 'duplicate' === $result->reason ) ? 'duplicate' : 'invalid' ), ) ) ); exit; } if ( 'delete' === $action || $bulk ) { $ids = array(); if ( isset( $_REQUEST['queues'] ) && is_array( $_REQUEST['queues'] ) ) { $ids = array_map( 'intval', $_REQUEST['queues'] ); } elseif ( isset( $_REQUEST['queue'] ) ) { $ids = array( (int) $_REQUEST['queue'] ); } if ( empty( $ids ) ) { return; } // A single delete is a link, so it carries _wpnonce; a bulk delete comes from // the list table's form, which carries its own nonce field. if ( isset( $_REQUEST['queues'] ) ) { check_admin_referer( 'bulk-postqueues' ); } else { check_admin_referer( self::NONCE_ACTION ); } foreach ( $ids as $id ) { if ( $id > 0 ) { do_action( 'ph_postqueue_deleting', $id ); $this->plugin->store->delete_queue( $id ); } } wp_safe_redirect( $this->pageUrl( array( 'message' => 'deleted', 'count' => count( $ids ) ) ) ); exit; } } private function renderNotice(): void { $message = isset( $_GET['message'] ) ? sanitize_key( $_GET['message'] ) : ''; if ( '' === $message ) { return; } $count = isset( $_GET['count'] ) ? (int) $_GET['count'] : 1; $notices = array( 'created' => array( 'success', __( 'Postqueue created.', 'postqueue' ) ), 'duplicate' => array( 'error', __( 'A postqueue with that name already exists.', 'postqueue' ) ), 'invalid' => array( 'error', __( 'Please enter a name for the postqueue.', 'postqueue' ) ), 'deleted' => array( 'success', sprintf( /* translators: %s: number of deleted postqueues */ _n( '%s postqueue deleted.', '%s postqueues deleted.', $count, 'postqueue' ), number_format_i18n( $count ) ), ), ); if ( ! isset( $notices[ $message ] ) ) { return; } printf( '

%s

', esc_attr( $notices[ $message ][0] ), esc_html( $notices[ $message ][1] ) ); } public function render() { $queueId = $this->currentQueueId(); echo '
'; $queueId ? $this->renderQueue( $queueId ) : $this->renderOverview(); echo '
'; } private function renderOverview(): void { ?>


renderNotice(); ?>

table->search_box( __( 'Search postqueues', 'postqueue' ), 'postqueue' ); ?>
table->display(); ?>
$queueId, ) ) . ';', 'before' ); ?>

plugin->store->get_queue_name( $queueId ) ) ); ?>