>> */ private $rows_by_tab = array(); /** * Errors from Outercite while loading. * * @var string[] */ private $errors = array(); /** * Set up the list table. */ public function __construct() { parent::__construct( array( 'singular' => 'change', 'plural' => 'changes', 'ajax' => false, ) ); } /** * The tab being shown. */ private function tab(): string { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- tab switch only. $tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'open'; return in_array( $tab, self::TABS, true ) ? $tab : 'open'; } /** * Read the changes, findings and snoozed rows, and sort them into tabs. */ private function load(): void { $linked = Links::all_linked(); $tabs = array_fill_keys( self::TABS, array() ); $ledger = Ledger::changes(); if ( $ledger['ok'] ) { foreach ( (array) ( $ledger['data']['proposed'] ?? array() ) as $row ) { $row['posts'] = $linked[ $row['id'] ] ?? array(); $row['date'] = $row['suggestedAt'] ?? null; $tabs[ $row['posts'] ? 'progress' : 'open' ][] = $row; } foreach ( (array) ( $ledger['data']['shipped'] ?? array() ) as $row ) { $row['posts'] = $linked[ $row['id'] ] ?? array(); $row['date'] = $row['shippedAt'] ?? null; $tabs['live'][] = $row; } } else { $this->errors[] = $ledger['error']; } foreach ( array( 'findings' => Ledger::findings(), 'snoozed' => Ledger::snoozed(), ) as $tab => $res ) { if ( ! $res['ok'] ) { // Older connections were not granted the recommendations scope; leave those tabs empty. if ( 403 !== $res['status'] ) { $this->errors[] = $res['error']; } continue; } foreach ( (array) $res['data'] as $rec ) { $tabs[ $tab ][] = array( 'id' => (string) $rec['id'], 'title' => (string) $rec['title'], 'state' => 'findings' === $tab ? __( 'Needs a fix', 'outercite' ) : __( 'Snoozed', 'outercite' ), 'date' => 'snoozed' === $tab ? ( $rec['snoozeUntil'] ?? null ) : ( $rec['createdAt'] ?? null ), 'posts' => $linked[ (string) $rec['id'] ] ?? array(), ); } } $this->rows_by_tab = $tabs; } /** * Columns of the list. */ public function get_columns() { $date = array( 'live' => __( 'Marked live', 'outercite' ), 'snoozed' => __( 'Snoozed until', 'outercite' ), ); return array( 'title' => __( 'Change', 'outercite' ), 'page' => __( 'Page', 'outercite' ), 'status' => __( 'Status', 'outercite' ), 'date' => $date[ $this->tab() ] ?? __( 'Found', 'outercite' ), ); } /** * Tab links with counts. */ protected function get_views() { $labels = array( 'open' => __( 'Open', 'outercite' ), 'progress' => __( 'In progress', 'outercite' ), 'live' => __( 'Live', 'outercite' ), 'findings' => __( 'Site findings', 'outercite' ), 'snoozed' => __( 'Snoozed', 'outercite' ), ); $views = array(); foreach ( $labels as $tab => $label ) { $views[ $tab ] = sprintf( '%3$s (%4$d)', esc_url( add_query_arg( array( 'page' => 'outercite', 'tab' => $tab, ), admin_url( 'admin.php' ) ) ), $tab === $this->tab() ? ' class="current" aria-current="page"' : '', esc_html( $label ), count( $this->rows_by_tab[ $tab ] ?? array() ) ); } return $views; } /** * Load the rows for the current tab. */ public function prepare_items() { $this->load(); $this->_column_headers = array( $this->get_columns(), array(), array() ); $this->items = $this->rows_by_tab[ $this->tab() ] ?? array(); } /** * Message for an empty tab. */ public function no_items() { $empty = array( 'open' => __( 'No open changes for this site right now.', 'outercite' ), 'progress' => __( 'Nothing in progress. Create a draft from an open change to start.', 'outercite' ), 'live' => __( 'No changes marked live yet.', 'outercite' ), 'findings' => __( 'The weekly site read found nothing to fix.', 'outercite' ), 'snoozed' => __( 'Nothing snoozed.', 'outercite' ), ); echo esc_html( $empty[ $this->tab() ] ); } /** * Show any errors, then the list. */ public function display() { foreach ( array_unique( $this->errors ) as $error ) { echo '
' . esc_html( $error ) . '