'outercite', 'view' => 'brief', 'change' => rawurlencode( $change_id ), ), admin_url( 'admin.php' ) ); } /** * Address of the settings screen. * * @param array $args Extra query arguments. */ public static function settings_url( array $args = array() ): string { return add_query_arg( array_merge( array( 'page' => 'outercite-settings' ), $args ), admin_url( 'admin.php' ) ); } /** * Add the Outercite menu and its screens. */ public static function menu(): void { $icon = 'data:image/svg+xml;base64,' . base64_encode( '' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode // A client viewer sees results only, so the menu opens on Results for them. $parent = current_user_can( self::CAP_USE ) ? 'outercite' : 'outercite-results'; add_menu_page( __( 'Outercite', 'outercite' ), __( 'Outercite', 'outercite' ), 'outercite' === $parent ? self::CAP_USE : Roles::RESULTS, $parent, 'outercite' === $parent ? array( __CLASS__, 'render_changes' ) : array( Results::class, 'render_page' ), $icon, 58 ); if ( 'outercite' === $parent ) { add_submenu_page( 'outercite', __( 'Changes', 'outercite' ), __( 'Changes', 'outercite' ), self::CAP_USE, 'outercite', array( __CLASS__, 'render_changes' ) ); } add_submenu_page( $parent, __( 'Outercite results', 'outercite' ), __( 'Results', 'outercite' ), Roles::RESULTS, 'outercite-results', array( Results::class, 'render_page' ) ); add_submenu_page( $parent, __( 'Outercite settings', 'outercite' ), __( 'Settings', 'outercite' ), self::CAP_CONNECT, 'outercite-settings', array( __CLASS__, 'render_settings' ) ); } /** * Load the admin styles on Outercite screens and the dashboard. * * @param string $hook Admin page hook suffix. */ public static function assets( string $hook ): void { if ( false === strpos( $hook, 'outercite' ) && 'index.php' !== $hook ) { return; } wp_enqueue_style( 'outercite-admin', OUTERCITE_URL . 'assets/admin.css', array(), outercite_asset_version( 'assets/admin.css' ) ); wp_enqueue_script( 'outercite-admin', OUTERCITE_URL . 'assets/admin.js', array(), outercite_asset_version( 'assets/admin.js' ), true ); wp_localize_script( 'outercite-admin', 'OuterciteAdmin', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'outercite_ajax' ), 'i18n' => array( 'writing' => __( 'Outercite is writing. This takes about a minute; you can leave this page and come back.', 'outercite' ), 'ready' => __( 'Draft ready.', 'outercite' ), 'open' => __( 'Open it', 'outercite' ), 'slow' => __( 'This is taking longer than usual. Refresh the page in a few minutes to check.', 'outercite' ), 'copied' => __( 'Copied', 'outercite' ), 'again' => __( 'Try again', 'outercite' ), ), ) ); } // Notices. /** * Keep a one-time notice for the next screen this user sees. * * @param string $type Notice type: success, warning, error or info. * @param string $message Message for the user. */ public static function flash( string $type, string $message ): void { set_transient( 'outercite_flash_' . get_current_user_id(), array( 'type' => $type, 'message' => $message, ), MINUTE_IN_SECONDS * 5 ); } /** * Show and clear this user's one-time notice. */ private static function render_flash(): void { $key = 'outercite_flash_' . get_current_user_id(); $flash = get_transient( $key ); if ( is_array( $flash ) ) { delete_transient( $key ); printf( '

%2$s

', esc_attr( $flash['type'] ), esc_html( $flash['message'] ) ); } } /** * Sitewide: a revoked sign-in, and a staging copy, both say so plainly. */ public static function global_notices(): void { if ( ! current_user_can( self::CAP_USE ) ) { return; } $screen = get_current_screen(); $ours = $screen && false !== strpos( (string) $screen->id, 'outercite' ); // On Outercite's own screens the page itself says so, once. // The expired sign-in warning shows on the dashboard and the Plugins screen only. $where = $screen && in_array( (string) $screen->id, array( 'dashboard', 'plugins' ), true ); if ( Connection::is_revoked() && $where ) { echo '

' . esc_html__( 'Outercite: this site\'s sign-in has expired or was revoked, so changes are not syncing.', 'outercite' ); if ( current_user_can( self::CAP_CONNECT ) ) { echo ' ' . esc_html__( 'Reconnect', 'outercite' ) . ''; } echo '

'; } if ( Site::is_staging() && $ours ) { echo '

' . esc_html( sprintf( /* translators: 1: this copy's address, 2: the linked address */ __( 'This copy of the site is at %1$s, but Outercite is linked to %2$s. Briefs show here, but nothing is marked live from this copy.', 'outercite' ), Site::current_host(), (string) Site::linked_host() ) ) . '

'; } } // Connect, link, disconnect. /** * Stop unless the current user has a capability. * * @param string $cap Capability to require. */ private static function require_cap( string $cap ): void { if ( ! current_user_can( $cap ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'outercite' ) ); } } /** * Redirect within wp-admin and stop. * * @param string $url Address within wp-admin. */ private static function back( string $url ): void { wp_safe_redirect( $url ); exit; } /** * Start sign-in: send the administrator to Outercite. */ public static function handle_connect(): void { check_admin_referer( 'outercite_connect' ); self::require_cap( self::CAP_CONNECT ); $url = Connection::authorize_url(); if ( is_wp_error( $url ) ) { self::flash( 'error', $url->get_error_message() ); self::back( self::settings_url() ); } // The sign-in page is on Outercite, not this site, so this is a deliberate external redirect. wp_redirect( $url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect exit; } /** * Outercite sends the administrator back here after sign-in. */ public static function maybe_oauth_callback(): void { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- the OAuth state is the CSRF check here. if ( ! isset( $_GET['page'], $_GET['outercite_oauth'] ) || 'outercite-settings' !== $_GET['page'] || 'callback' !== $_GET['outercite_oauth'] ) { return; } if ( ! current_user_can( self::CAP_CONNECT ) ) { return; } if ( isset( $_GET['error'] ) ) { self::flash( 'warning', __( 'Sign-in was cancelled, so the site is not connected.', 'outercite' ) ); self::back( self::settings_url() ); } $code = isset( $_GET['code'] ) ? sanitize_text_field( wp_unslash( $_GET['code'] ) ) : ''; $state = isset( $_GET['state'] ) ? sanitize_text_field( wp_unslash( $_GET['state'] ) ) : ''; // phpcs:enable $ok = Connection::handle_callback( $code, $state ); if ( is_wp_error( $ok ) ) { self::flash( 'error', $ok->get_error_message() ); self::back( self::settings_url() ); } self::after_sign_in(); } /** * After sign-in: if Outercite already links this address, or only one * workspace is possible, finish now. Otherwise show the workspace picker. */ private static function after_sign_in(): void { $site = Api::request( 'GET', '/api/v1/wordpress/sites', null, Api::SITE_CURRENT ); if ( ! $site['ok'] ) { self::flash( 'error', $site['error'] ); self::back( self::settings_url() ); } $data = (array) $site['data']; if ( ! empty( $data['linked'] ) ) { self::link_to( (string) $data['workspaceId'] ); } $workspaces = isset( $data['workspaces'] ) ? (array) $data['workspaces'] : array(); if ( 1 === count( $workspaces ) ) { self::link_to( (string) $workspaces[0]['id'] ); } // Unlinked in Outercite since the last connection: forget the old workspace and ask again. Connection::update( array( 'workspaces' => $workspaces, 'site_host' => '', 'workspace_id' => '', 'workspace_name' => '', ) ); self::back( self::settings_url() ); } /** * Link this site to a workspace, then return to settings. * * @param string $workspace_id Outercite workspace id. */ private static function link_to( string $workspace_id ): void { $res = Api::request( 'POST', '/api/v1/wordpress/sites', array( 'workspaceId' => $workspace_id ), Api::SITE_CURRENT ); if ( ! $res['ok'] ) { self::flash( 'error', $res['error'] ); self::back( self::settings_url() ); } $data = (array) $res['data']; Connection::update( array( 'site_host' => (string) $data['siteHost'], 'workspace_id' => (string) $data['workspaceId'], 'workspace_name' => (string) $data['workspaceName'], 'workspaces' => array(), ) ); Ledger::flush(); Activity::add( 'connect', sprintf( /* translators: %s: workspace name */ __( 'Connected the site to %s', 'outercite' ), (string) $data['workspaceName'] ) ); self::flash( 'success', sprintf( /* translators: %s: workspace name */ __( 'Connected. This site now shows the changes for %s, and only that workspace.', 'outercite' ), (string) $data['workspaceName'] ) ); self::back( self::settings_url() ); } /** * Link the site to the workspace picked in settings. */ public static function handle_link(): void { check_admin_referer( 'outercite_link' ); self::require_cap( self::CAP_CONNECT ); $workspace = isset( $_POST['workspace'] ) ? sanitize_text_field( wp_unslash( $_POST['workspace'] ) ) : ''; if ( '' === $workspace ) { self::flash( 'error', __( 'Pick the workspace this site belongs to.', 'outercite' ) ); self::back( self::settings_url() ); } self::link_to( $workspace ); } /** * Disconnect: unlink and revoke in Outercite, then forget the sign-in here. */ public static function handle_disconnect(): void { check_admin_referer( 'outercite_disconnect' ); self::require_cap( self::CAP_CONNECT ); if ( Site::is_staging() ) { // A copy must never unlink the live site: forget the sign-in here only. Connection::forget(); self::flash( 'success', __( 'Disconnected this copy of the site. The live site stays connected.', 'outercite' ) ); self::back( self::settings_url() ); } if ( Connection::is_linked() ) { // Unlinking also revokes this site's sign-in in Outercite. Api::request( 'DELETE', '/api/v1/wordpress/sites', null, Api::SITE_CURRENT ); } Connection::revoke_remote(); Connection::forget(); Tracking::stop(); Ledger::flush(); Activity::add( 'disconnect', __( 'Disconnected the site from Outercite', 'outercite' ) ); self::flash( 'success', __( 'Disconnected. This site no longer sends or receives anything from Outercite.', 'outercite' ) ); self::back( self::settings_url() ); } /** * Clear the cache and reload the Changes page. */ public static function handle_refresh(): void { check_admin_referer( 'outercite_refresh' ); self::require_cap( self::CAP_USE ); Ledger::flush(); self::back( admin_url( 'admin.php?page=outercite' ) ); } // Change actions. /** * The change id from the request, if it is well formed. */ private static function change_id_from_request(): string { // phpcs:ignore WordPress.Security.NonceVerification -- every caller has run check_admin_referer() first. $raw = isset( $_REQUEST['change'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['change'] ) ) : ''; return preg_match( '/^[A-Za-z0-9_-]{1,64}$/', $raw ) ? $raw : ''; } /** * Create a draft page from a change and open it. */ public static function handle_create_draft(): void { check_admin_referer( 'outercite_create_draft' ); self::require_cap( self::CAP_DRAFTS ); self::require_cap( 'edit_pages' ); $id = self::change_id_from_request(); $change = Ledger::change( $id ); if ( ! $change['ok'] ) { self::flash( 'error', $change['error'] ); self::back( admin_url( 'admin.php?page=outercite' ) ); } $post_id = Links::create_draft( $id, (array) $change['data'] ); if ( is_wp_error( $post_id ) ) { self::flash( 'error', $post_id->get_error_message() ); self::back( self::brief_url( $id ) ); } Activity::add( 'draft', __( 'Created a draft from a change', 'outercite' ), (int) $post_id ); self::back( (string) get_edit_post_link( $post_id, 'raw' ) ); } /** * Link a change to an existing page or post. */ public static function handle_attach(): void { check_admin_referer( 'outercite_attach' ); self::require_cap( self::CAP_DRAFTS ); $id = self::change_id_from_request(); $post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0; if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) { self::flash( 'error', __( 'Pick a page you can edit.', 'outercite' ) ); self::back( self::brief_url( $id ) ); } $change = Ledger::change( $id ); $title = $change['ok'] && isset( $change['data']['title'] ) ? (string) $change['data']['title'] : ''; Links::link( $post_id, $id, $title ); Activity::add( 'attach', __( 'Linked a change to an existing page', 'outercite' ), $post_id ); self::flash( 'success', sprintf( /* translators: %s: page title */ __( 'Linked to "%s". When you update that page with the change, Outercite asks whether it is live.', 'outercite' ), get_the_title( $post_id ) ) ); self::back( self::brief_url( $id ) ); } /** * Snooze or dismiss a change. */ public static function handle_set_status(): void { check_admin_referer( 'outercite_set_status' ); self::require_cap( self::CAP_DRAFTS ); $id = self::change_id_from_request(); $status = isset( $_REQUEST['status'] ) && 'dismissed' === $_REQUEST['status'] ? 'dismissed' : 'snoozed'; $reason = isset( $_REQUEST['reason'] ) ? substr( sanitize_text_field( wp_unslash( $_REQUEST['reason'] ) ), 0, 500 ) : ''; $res = Ledger::set_status( $id, $status, $reason ); if ( $res['ok'] ) { Activity::add( $status, 'dismissed' === $status ? __( 'Dismissed a change', 'outercite' ) : __( 'Snoozed a change for 14 days', 'outercite' ) ); } self::flash( $res['ok'] ? 'success' : 'error', $res['ok'] ? ( 'dismissed' === $status ? __( 'Dismissed. Outercite will not suggest it for this site again.', 'outercite' ) : __( 'Snoozed for 14 days.', 'outercite' ) ) : $res['error'] ); self::back( admin_url( 'admin.php?page=outercite' ) ); } /** * Mark a change live by hand with up to 10 addresses. */ public static function handle_mark_live(): void { check_admin_referer( 'outercite_mark_live' ); self::require_cap( self::CAP_DRAFTS ); $id = self::change_id_from_request(); $raw = isset( $_POST['urls'] ) ? sanitize_textarea_field( wp_unslash( $_POST['urls'] ) ) : ''; $urls = array_filter( array_map( 'trim', preg_split( '/[\r\n]+/', $raw ) ) ); if ( empty( $urls ) ) { self::flash( 'error', __( 'Add at least one public page address.', 'outercite' ) ); self::back( self::brief_url( $id ) ); } if ( count( $urls ) > 10 ) { self::flash( 'error', __( 'Up to 10 pages per change.', 'outercite' ) ); self::back( self::brief_url( $id ) ); } $res = Ledger::mark_live( $id, $urls ); if ( $res['ok'] ) { Activity::add( 'live', sprintf( /* translators: %d: number of page addresses */ _n( 'Marked a change live by hand at %d address', 'Marked a change live by hand at %d addresses', count( $urls ), 'outercite' ), count( $urls ) ) ); foreach ( Links::posts_for_change( $id ) as $post ) { if ( 'publish' === $post->post_status && in_array( Links::public_url( $post->ID ), $urls, true ) ) { update_post_meta( $post->ID, Links::SHIPPED_AT, time() ); update_post_meta( $post->ID, Links::SHIPPED_TO, Links::public_url( $post->ID ) ); delete_post_meta( $post->ID, Links::ASK_LIVE ); } } } self::flash( $res['ok'] ? 'success' : 'error', $res['ok'] ? __( 'Marked live. Outercite checks the pages are up, then starts measuring AI answers.', 'outercite' ) : $res['error'] ); self::back( self::brief_url( $id ) ); } // Screens. /** * A screen header: back link, kicker, title, pills and actions. * * @param string $title Page title. * @param array $args Optional: back (url), back_label, kicker, pills (HTML), actions (HTML). */ public static function header( string $title, array $args = array() ): void { echo '
'; if ( ! empty( $args['back'] ) ) { echo '← ' . esc_html( $args['back_label'] ?? __( 'Back', 'outercite' ) ) . ''; } $kicker = $args['kicker'] ?? self::kicker(); if ( '' !== $kicker ) { echo '' . esc_html( $kicker ) . ''; } echo '

' . esc_html( $title ) . '

'; if ( ! empty( $args['pills'] ) ) { echo '
' . wp_kses_post( $args['pills'] ) . '
'; } echo '
'; if ( ! empty( $args['actions'] ) ) { // Built by the caller from escaped parts; forms are allowed here. echo '
' . $args['actions'] . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } echo '

'; } /** * "Outercite · " for headers. */ private static function kicker(): string { $conn = Connection::get(); $name = ! empty( $conn['workspace_name'] ) ? (string) $conn['workspace_name'] : ''; return '' !== $name ? 'Outercite · ' . $name : 'Outercite'; } /** * A status pill coloured by what the state means. * * @param string $label State label from Outercite. * @param string $tone Tone, or empty to infer it from the label. */ public static function pill( string $label, string $tone = '' ): string { if ( '' === $label ) { return ''; } if ( '' === $tone ) { $l = strtolower( $label ); $tone = 'neutral'; if ( false !== strpos( $l, 'needs a fix' ) ) { $tone = 'red'; } elseif ( false !== strpos( $l, 'needs' ) ) { $tone = 'amber'; } elseif ( false !== strpos( $l, 'measured' ) || false !== strpos( $l, 'moved' ) ) { $tone = 'violet'; } elseif ( false !== strpos( $l, 'live' ) ) { $tone = 'green'; } elseif ( false !== strpos( $l, 'progress' ) || false !== strpos( $l, 'draft' ) ) { $tone = 'blue'; } } return '' . esc_html( $label ) . ''; } /** * Shown in place of a screen once Outercite has refused this site's sign-in. */ private static function revoked_box(): void { echo '

' . esc_html__( 'Sign-in expired', 'outercite' ) . '

' . esc_html__( 'This site\'s sign-in to Outercite has expired or was revoked, for example because the site was unlinked in Outercite. Nothing syncs until it is connected again.', 'outercite' ) . '

'; if ( current_user_can( self::CAP_CONNECT ) ) { echo ''; } else { echo '

' . esc_html__( 'Ask an administrator to reconnect it.', 'outercite' ) . '

'; } echo '
'; } /** * Shown in place of a screen before the site is connected. */ private static function not_connected_box(): void { echo '

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

' . esc_html__( 'Connect it to see what to change so AI answers name this business.', 'outercite' ) . '

'; if ( current_user_can( self::CAP_CONNECT ) ) { echo ''; } else { echo '

' . esc_html__( 'Ask an administrator to connect it.', 'outercite' ) . '

'; } echo '
'; } /** * Render the Changes page, or the brief screen. */ public static function render_changes(): void { if ( ! current_user_can( self::CAP_USE ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- view switch only. if ( isset( $_GET['view'] ) && 'brief' === $_GET['view'] ) { self::render_brief( self::change_id_from_request() ); return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- view switch only; the screen checks the user may edit the page. if ( isset( $_GET['view'], $_GET['suggestion'] ) && 'suggestion' === $_GET['view'] ) { echo '
'; self::render_flash(); Writer::render_review( absint( $_GET['suggestion'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended echo '
'; return; } echo '
'; self::header( __( 'Changes', 'outercite' ), array( 'actions' => Connection::is_linked() ? '' . esc_html__( 'Refresh', 'outercite' ) . '' : '', ) ); self::render_flash(); if ( ! Connection::is_linked() ) { Connection::is_revoked() ? self::revoked_box() : self::not_connected_box(); echo '
'; return; } require_once OUTERCITE_DIR . 'includes/class-changes-table.php'; $table = new Changes_Table(); $table->prepare_items(); if ( Connection::is_revoked() ) { self::revoked_box(); echo ''; return; } $table->views(); echo '
'; $table->display(); echo '
'; } /** * Render the brief screen for one change. * * @param string $id Outercite change id. */ private static function render_brief( string $id ): void { echo '
'; $back = array( 'back' => admin_url( 'admin.php?page=outercite' ), 'back_label' => __( 'Changes', 'outercite' ), ); if ( ! Connection::is_linked() ) { self::header( __( 'Change', 'outercite' ), $back ); self::render_flash(); Connection::is_revoked() ? self::revoked_box() : self::not_connected_box(); echo '
'; return; } $change = Ledger::change( $id ); if ( ! $change['ok'] ) { self::header( __( 'Change', 'outercite' ), $back ); self::render_flash(); if ( Connection::is_revoked() ) { self::revoked_box(); } else { echo '

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

'; } echo ''; return; } $c = (array) $change['data']; $finding = 'finding' === ( $c['kind'] ?? '' ); $brief = $finding ? null : Ledger::brief( $id ); $posts = Links::posts_for_change( $id ); self::header( (string) ( $c['title'] ?? '' ), $back + array( 'pills' => self::pill( (string) ( $c['state'] ?? '' ) ) ) ); self::render_flash(); echo '
'; if ( ! empty( $c['doFirst'] ) ) { echo '

' . esc_html__( 'Do this first', 'outercite' ) . '

' . esc_html( is_string( $c['doFirst'] ) ? $c['doFirst'] : (string) wp_json_encode( $c['doFirst'] ) ) . '

'; } if ( $finding ) { echo '

' . esc_html__( 'What to do', 'outercite' ) . '

' . esc_html( (string) ( $c['whatToDo'] ?? '' ) ) . '

' . esc_html( (string) ( $c['next'] ?? '' ) ) . '

'; } elseif ( $brief && $brief['ok'] ) { echo '

' . esc_html__( 'Brief', 'outercite' ) . '

' . esc_html( (string) ( $brief['data']['brief'] ?? '' ) ) . '
'; } elseif ( $brief ) { echo '

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

'; } if ( ! empty( $c['outline'] ) && is_string( $c['outline'] ) ) { $outline = Writer::parse( (string) preg_replace( '/^\s*(?:-{3,}|\.{3}|…)\s*$/mu', '', $c['outline'] ), false ); echo '

' . esc_html__( 'Outline', 'outercite' ) . '

' . wp_kses_post( Writer::render( $outline['parts'], false ) ) . '
'; } echo '
'; if ( ! $finding && current_user_can( self::CAP_DRAFTS ) ) { Writer::render_box( $id, $posts ); self::render_work_box( $id, $c, $posts ); } if ( ! $finding && current_user_can( Roles::RESULTS ) ) { Results::render_change_box( $posts ); } echo '
'; } /** * Render the linked pages, start-a-page and mark live boxes. * * @param string $id Outercite change id. * @param array $c The change, as GET /api/v1/changes/{id} returns it. * @param \WP_Post[] $posts Posts linked to the change. */ private static function render_work_box( string $id, array $c, array $posts ): void { $post_action = admin_url( 'admin-post.php' ); echo '

' . esc_html__( 'Pages for this change', 'outercite' ) . '

'; if ( $posts ) { echo ''; } else { echo '

' . esc_html__( 'No page is linked yet. Start a blank draft, attach a page you already have, or let Outercite write it.', 'outercite' ) . '

'; } echo '

' . esc_html__( 'Start a blank draft', 'outercite' ) . '

'; echo '
'; wp_nonce_field( 'outercite_create_draft' ); echo ''; echo '

' . esc_html__( 'A new draft page with the outline as headings, linked to this change.', 'outercite' ) . '

'; echo '
'; echo '

'; echo '
'; wp_nonce_field( 'outercite_attach' ); echo ''; echo '
'; echo '

' . esc_html__( 'Mark live by hand', 'outercite' ) . '

'; if ( Site::is_staging() ) { echo '

' . esc_html__( 'Not available on a copy of the site.', 'outercite' ) . '

'; return; } $prefill = array(); foreach ( $posts as $post ) { if ( 'publish' === $post->post_status ) { $prefill[] = Links::public_url( $post->ID ); } } echo '

' . esc_html__( 'Publishing a linked page does this for you. Use it for pages outside WordPress: one public address per line, up to 10.', 'outercite' ) . '

'; echo '
'; wp_nonce_field( 'outercite_mark_live' ); echo ''; echo ''; echo '
'; } /** * Render the settings screen: connect, pick a workspace, or the connection. */ public static function render_settings(): void { if ( ! current_user_can( self::CAP_CONNECT ) ) { return; } $tabs = array( 'connection' => array( __( 'Connection', 'outercite' ), array( __CLASS__, 'render_connection' ) ), 'roles' => array( __( 'Roles', 'outercite' ), array( Roles::class, 'render' ) ), 'fixes' => array( __( 'Site fixes', 'outercite' ), array( Fixes::class, 'render' ) ), 'visitors' => array( __( 'AI visitors', 'outercite' ), array( Tracking::class, 'render' ) ), 'assistants' => array( __( 'AI assistants', 'outercite' ), array( Assistants::class, 'render' ) ), 'emails' => array( __( 'Emails', 'outercite' ), array( Notify::class, 'render' ) ), 'activity' => array( __( 'Activity', 'outercite' ), array( Activity::class, 'render' ) ), ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- tab switch only. $tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'connection'; $tab = isset( $tabs[ $tab ] ) ? $tab : 'connection'; echo '
'; self::header( __( 'Settings', 'outercite' ) ); echo ''; self::render_flash(); call_user_func( $tabs[ $tab ][1] ); echo '
'; } /** * The connection tab: connect, pick a workspace, or the connection. */ public static function render_connection(): void { $conn = Connection::get(); $post_action = admin_url( 'admin-post.php' ); if ( Connection::needs_workspace() ) { $workspaces = isset( $conn['workspaces'] ) ? (array) $conn['workspaces'] : array(); echo '

' . esc_html__( 'Pick this client\'s workspace', 'outercite' ) . '

'; echo '

' . esc_html__( 'This site will see that workspace\'s changes and nothing else. To move it later, disconnect and connect again.', 'outercite' ) . '

'; echo '
'; wp_nonce_field( 'outercite_link' ); echo '
'; return; } if ( ! Connection::is_linked() && ! Connection::is_revoked() ) { echo '
'; echo '

' . esc_html__( 'Connect this site to Outercite', 'outercite' ) . '

'; echo '

' . esc_html__( 'See what to change so AI answers name this business, write it here, and Outercite measures the result when you publish.', 'outercite' ) . '

'; echo '
  1. ' . esc_html__( 'Sign in to Outercite', 'outercite' ) . '
  2. ' . esc_html__( 'Approve access for this site', 'outercite' ) . '
  3. ' . esc_html__( 'Pick this client\'s workspace', 'outercite' ) . '
'; echo '
'; wp_nonce_field( 'outercite_connect' ); echo '
'; echo '

' . esc_html__( 'Administrators only. Takes about a minute.', 'outercite' ) . '

'; return; } $site = Connection::is_linked() ? Api::request( 'GET', '/api/v1/wordpress/sites' ) : null; $details = $site && $site['ok'] ? (array) $site['data'] : array(); $user = ! empty( $conn['connected_by'] ) ? get_userdata( (int) $conn['connected_by'] ) : null; $healthy = Connection::is_linked() && ! Connection::is_revoked(); echo '

' . esc_html__( 'Connection', 'outercite' ) . '

' . wp_kses_post( $healthy ? self::pill( __( 'Connected', 'outercite' ), 'green' ) : self::pill( __( 'Sign-in expired', 'outercite' ), 'red' ) ) . '
'; if ( Connection::is_revoked() ) { echo '

' . esc_html__( 'This site\'s sign-in to Outercite has expired or was revoked, for example because the site was unlinked in Outercite. Nothing syncs until you reconnect.', 'outercite' ) . '

'; } $rows = array( __( 'Workspace', 'outercite' ) => (string) ( $details['workspaceName'] ?? ( $conn['workspace_name'] ?? '' ) ), __( 'Connected by', 'outercite' ) => trim( ( $user ? $user->display_name : '' ) . ( ! empty( $conn['connected_at'] ) ? ', ' . wp_date( get_option( 'date_format' ), (int) $conn['connected_at'] ) : '' ), ', ' ), __( 'Site address', 'outercite' ) => self::address_line( $details ), __( 'Allowed to', 'outercite' ) => __( 'Read changes and briefs, mark changes live, write drafts with credits, and turn on AI visitor tracking', 'outercite' ), ); echo '
'; foreach ( $rows as $label => $value ) { echo '
' . esc_html( $label ) . '
' . esc_html( $value ) . '
'; } echo '
'; if ( $site && ! $site['ok'] ) { echo '

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

'; } echo '
'; echo '
'; wp_nonce_field( 'outercite_connect' ); echo '
'; echo '
'; wp_nonce_field( 'outercite_disconnect' ); echo '
'; echo '
'; echo '

' . esc_html__( 'Staging and development copies stay quiet', 'outercite' ) . '

' . esc_html__( 'A copy of this site on another address, such as a WP Engine staging site, shows briefs but never marks a change live.', 'outercite' ) . '

'; } /** * The site address line on the settings screen. * * @param array $details The link, as GET /api/v1/wordpress/sites returns it. */ private static function address_line( array $details ): string { $linked = (string) Site::linked_host(); if ( Site::is_staging() ) { return sprintf( /* translators: 1: linked address, 2: this copy's address */ __( '%1$s (this copy is at %2$s, so it stays quiet)', 'outercite' ), $linked, Site::current_host() ); } if ( ! array_key_exists( 'matchesWebsite', $details ) ) { return $linked; } if ( empty( $details['matchesWebsite'] ) ) { return sprintf( /* translators: 1: site address, 2: website on the Outercite workspace */ __( '%1$s, which differs from the workspace website (%2$s). Check this is the right client.', 'outercite' ), $linked, (string) ( $details['website'] ?? __( 'none set', 'outercite' ) ) ); } return sprintf( /* translators: %s: site address */ __( '%s, matches the workspace website', 'outercite' ), $linked ); } }