>> */ 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 ) . '

'; } parent::display(); } /** * A nonce-protected admin-post link for a row action. * * @param string $action Action name, without the outercite_ prefix. * @param string $id Outercite change id. * @param array $extra Extra query arguments. */ private function action_url( string $action, string $id, array $extra = array() ): string { return wp_nonce_url( add_query_arg( array_merge( array( 'action' => 'outercite_' . $action, 'change' => $id, ), $extra ), admin_url( 'admin-post.php' ) ), 'outercite_' . $action ); } /** * Change column: title, next step and row actions. * * @param mixed $item Row data. */ protected function column_title( $item ) { $id = (string) $item['id']; $tab = $this->tab(); $actions = array( 'brief' => '' . ( 'findings' === $tab ? esc_html__( 'View', 'outercite' ) : esc_html__( 'View brief', 'outercite' ) ) . '' ); $can_work = current_user_can( Roles::DRAFTS ); if ( $can_work && in_array( $tab, array( 'open', 'progress' ), true ) ) { $actions['draft'] = '' . esc_html__( 'Create draft', 'outercite' ) . ''; $actions['attach'] = '' . esc_html__( 'Attach to a page', 'outercite' ) . ''; $actions['snooze'] = '' . esc_html__( 'Snooze', 'outercite' ) . ''; } if ( $can_work && 'live' !== $tab ) { $actions['trash'] = '' . esc_html__( 'Dismiss', 'outercite' ) . ''; } return '' . esc_html( (string) $item['title'] ) . '' . $this->row_actions( $actions ); } /** * Page column: linked pages, or the target address. * * @param mixed $item Row data. */ protected function column_page( $item ) { $out = array(); foreach ( (array) $item['posts'] as $post ) { $out[] = '' . esc_html( get_the_title( $post ) ) . ''; } if ( ! $out && ! empty( $item['targetUrl'] ) ) { $target = (string) $item['targetUrl']; $path = (string) wp_parse_url( $target, PHP_URL_PATH ); $out[] = '' . esc_html( '' !== $path ? $path : $target ) . ''; } return $out ? implode( '
', $out ) : '' . esc_html__( 'Not started', 'outercite' ) . ''; } /** * Status column. * * @param mixed $item Row data. */ protected function column_status( $item ) { $live = 'live' === $this->tab(); $out = Admin::pill( (string) ( $item['state'] ?? '' ) ); // Measured: Outercite compared AI answers before and after, against changes it left alone. if ( $live && 'confirmed' === ( $item['tier'] ?? '' ) && isset( $item['movementPoints'] ) && is_numeric( $item['movementPoints'] ) ) { $points = (float) $item['movementPoints']; $out .= '
' . esc_html( sprintf( /* translators: %s: signed number of points, such as +4 */ __( 'Measured: %s points in AI answers', 'outercite' ), ( $points > 0 ? '+' : '' ) . number_format_i18n( $points, 1 ) ) ) . '
'; } elseif ( $live && ! empty( $item['seenLive'] ) ) { $out .= '
' . esc_html__( 'Seen live. Measuring.', 'outercite' ) . '
'; } return $out; } /** * Date column. * * @param mixed $item Row data. */ protected function column_date( $item ) { $ts = ! empty( $item['date'] ) ? strtotime( (string) $item['date'] ) : false; return $ts ? esc_html( wp_date( get_option( 'date_format' ), $ts ) ) : ''; } }