'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 '
' . 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 '
' . 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' ) . '
' . 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 '
' . esc_html__( 'Sign in to Outercite', 'outercite' ) . '
' . esc_html__( 'Approve access for this site', '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 you reconnect.', 'outercite' ) . '
' . 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
);
}
}