>|null */ private static $batch = null; /** * Register hooks. */ public static function init(): void { add_action( 'add_meta_boxes', array( __CLASS__, 'meta_box' ), 10, 2 ); foreach ( array( 'page', 'post' ) as $type ) { add_filter( 'manage_' . $type . 's_columns', array( __CLASS__, 'column' ) ); add_action( 'manage_' . $type . 's_custom_column', array( __CLASS__, 'cell' ), 10, 2 ); } } /** * Engine names as people know them. * * @param string $id Engine id from Outercite. */ public static function engine_name( string $id ): string { $names = array( 'chatgpt' => 'ChatGPT', 'claude' => 'Claude', 'google_ai' => 'Gemini', 'google_aio' => 'AI Overviews', 'google_ai_mode' => 'AI Mode', 'perplexity' => 'Perplexity', 'grok' => 'Grok', 'deepseek' => 'DeepSeek', 'meta_ai' => 'Meta AI', ); return $names[ $id ] ?? ucwords( str_replace( '_', ' ', $id ) ); } /** * Results for live linked posts, in one call. * * @param \WP_Post[] $posts Posts to look up; only live linked ones are sent. * @return array> Results keyed by post ID. */ public static function for_posts( array $posts ): array { $pages = array(); $ids = array(); foreach ( $posts as $post ) { if ( ! Links::is_shipped( $post->ID ) ) { continue; } $url = (string) get_post_meta( $post->ID, Links::SHIPPED_TO, true ); if ( '' === $url ) { $url = Links::public_url( $post->ID ); } $pages[] = array( 'url' => $url, 'since' => gmdate( 'c', (int) get_post_meta( $post->ID, Links::SHIPPED_AT, true ) ), ); $ids[] = $post->ID; if ( count( $pages ) >= 50 ) { break; } } if ( ! $pages || ! Connection::is_linked() ) { return array(); } $res = Ledger::results( $pages ); if ( ! $res['ok'] || ! is_array( $res['data'] ) ) { return array(); } $out = array(); foreach ( array_values( $res['data'] ) as $i => $row ) { if ( isset( $ids[ $i ] ) ) { $out[ $ids[ $i ] ] = (array) $row; } } return $out; } /** * One line about a page's result. * * @param array|null $result Result for the page, or null. */ public static function line( ?array $result ): string { if ( ! $result ) { return __( 'Live. Results appear after the next AI checks.', 'outercite' ); } $engines = (array) ( $result['engines'] ?? array() ); if ( ! $engines ) { return __( 'Live, not cited by AI answers yet', 'outercite' ); } return sprintf( /* translators: 1: number of AI engines, 2: their names */ _n( 'Cited on %1$d AI engine since it went live: %2$s', 'Cited on %1$d AI engines since it went live: %2$s', count( $engines ), 'outercite' ), count( $engines ), implode( ', ', array_map( array( __CLASS__, 'engine_name' ), array_map( 'strval', $engines ) ) ) ); } /** * Engine chips for a result, or a short line when there are none. * * @param array|null $result Result for the page, or null. */ public static function chips( ?array $result ): string { $engines = $result ? (array) ( $result['engines'] ?? array() ) : array(); if ( ! $engines ) { return '' . esc_html( $result ? __( 'Not cited by AI answers yet', 'outercite' ) : __( 'Results appear after the next AI checks', 'outercite' ) ) . ''; } $out = ''; foreach ( $engines as $engine ) { $out .= '' . esc_html( self::engine_name( (string) $engine ) ) . ''; } return $out; } /** * Add the results box to the edit screen of a live linked page. * * @param string $post_type Post type being edited. * @param \WP_Post $post The post. */ public static function meta_box( $post_type, $post ): void { if ( ! $post instanceof \WP_Post || ! Links::is_shipped( $post->ID ) || ! current_user_can( Roles::RESULTS ) ) { return; } add_meta_box( 'outercite-results', __( 'Outercite results', 'outercite' ), array( __CLASS__, 'render_meta_box' ), null, 'side', 'default' ); } /** * Render the results box. * * @param \WP_Post $post The post. */ public static function render_meta_box( \WP_Post $post ): void { $results = self::for_posts( array( $post ) ); echo '

' . esc_html( self::line( $results[ $post->ID ] ?? null ) ) . '

'; $at = (int) get_post_meta( $post->ID, Links::SHIPPED_AT, true ); if ( $at ) { echo '

' . esc_html( sprintf( /* translators: %s: date */ __( 'Marked live %s.', 'outercite' ), wp_date( get_option( 'date_format' ), $at ) ) ) . '

'; } } /** * Add the Outercite column to the Pages and Posts lists. * * @param array $columns List columns. * @return array */ public static function column( $columns ) { if ( ! current_user_can( Roles::RESULTS ) && ! current_user_can( Roles::BRIEFS ) ) { return $columns; } $columns['outercite'] = __( 'Outercite', 'outercite' ); return $columns; } /** * One cell of the Outercite column. * * @param string $column Column name. * @param int $post_id Post ID. */ public static function cell( $column, $post_id ): void { if ( 'outercite' !== $column ) { return; } $post_id = (int) $post_id; if ( ! Links::change_for_post( $post_id ) ) { echo ''; return; } $title = (string) get_post_meta( $post_id, Links::TITLE, true ); echo '' . esc_html( $title ) . '
'; if ( ! Links::is_shipped( $post_id ) ) { echo '' . esc_html__( 'In progress', 'outercite' ) . ''; return; } if ( ! current_user_can( Roles::RESULTS ) ) { echo '' . esc_html__( 'Live', 'outercite' ) . ''; return; } if ( null === self::$batch ) { // One call for every live linked row on this screen. global $wp_query; self::$batch = self::for_posts( isset( $wp_query->posts ) ? (array) $wp_query->posts : array() ); } echo '' . esc_html( self::line( self::$batch[ $post_id ] ?? null ) ) . ''; } /** * Results for a change's pages, on the brief screen. * * @param \WP_Post[] $posts Pages linked to the change. */ public static function render_change_box( array $posts ): void { $live = array_filter( $posts, static function ( $p ) { return Links::is_shipped( $p->ID ); } ); if ( ! $live ) { return; } $results = self::for_posts( $live ); echo '

' . esc_html__( 'Results since it went live', 'outercite' ) . '

'; } /** * The Results page: a read-only summary for the client. */ public static function render_page(): void { if ( ! current_user_can( Roles::RESULTS ) ) { return; } echo '
'; Admin::header( __( 'Results', 'outercite' ) ); if ( ! Connection::is_linked() ) { echo '

' . esc_html__( 'This site is not connected to Outercite yet.', 'outercite' ) . '

'; return; } $ledger = Ledger::changes(); if ( ! $ledger['ok'] ) { echo '

' . esc_html( $ledger['error'] ) . '

'; return; } $data = (array) $ledger['data']; $shipped = (array) ( $data['shipped'] ?? array() ); $counts = array( __( 'Changes live', 'outercite' ) => count( $shipped ), __( 'Seen live by Outercite', 'outercite' ) => (int) ( $data['seenLive'] ?? 0 ), __( 'Measured as working', 'outercite' ) => (int) ( $data['working'] ?? 0 ), __( 'Open changes', 'outercite' ) => count( (array) ( $data['proposed'] ?? array() ) ), ); echo '
'; foreach ( $counts as $label => $n ) { echo '
' . esc_html( number_format_i18n( $n ) ) . '
' . esc_html( $label ) . '
'; } echo '
'; $linked = Links::all_linked(); $posts = array(); foreach ( $shipped as $row ) { foreach ( (array) ( $linked[ (string) ( $row['id'] ?? '' ) ] ?? array() ) as $p ) { $posts[] = $p; } } $results = self::for_posts( $posts ); echo '

' . esc_html__( 'Changes that went live', 'outercite' ) . '

'; if ( ! $shipped ) { echo '

' . esc_html__( 'No changes marked live yet.', 'outercite' ) . '

'; return; } echo ''; foreach ( $shipped as $row ) { $row_posts = (array) ( $linked[ (string) ( $row['id'] ?? '' ) ] ?? array() ); $page = $row_posts ? $row_posts[0] : null; echo ''; } echo '
' . esc_html__( 'Change', 'outercite' ) . '' . esc_html__( 'Page', 'outercite' ) . '' . esc_html__( 'Status', 'outercite' ) . '' . esc_html__( 'AI answers', 'outercite' ) . '
' . esc_html( (string) ( $row['title'] ?? '' ) ) . ''; if ( $page ) { echo '' . esc_html( html_entity_decode( get_the_title( $page ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ) . ''; } elseif ( ! empty( $row['targetUrl'] ) ) { echo esc_html( (string) wp_parse_url( (string) $row['targetUrl'], PHP_URL_PATH ) ); } echo '' . wp_kses_post( Admin::pill( (string) ( $row['state'] ?? '' ) ) ) . ''; echo $page ? wp_kses_post( self::chips( $results[ $page->ID ] ?? null ) ) : '' . esc_html__( 'Not linked to a page on this site', 'outercite' ) . ''; echo '
'; } }