ID ); if ( ! $change_id || Links::is_shipped( $post->ID ) || ! Connection::is_linked() ) { return; } if ( 'publish' === $old_status ) { update_post_meta( $post->ID, Links::ASK_LIVE, 1 ); return; } self::mark_live( $post->ID, 'publish' ); } /** * Mark the change on this post live, with its public address. * * @param int $post_id Post ID. * @param string $how 'publish' (on publish), 'confirm' (answered yes) or 'retry'. * @return array{ok:bool,message:string} */ public static function mark_live( int $post_id, string $how ): array { $change_id = Links::change_for_post( $post_id ); if ( ! $change_id ) { return self::outcome( false, __( 'This page is not linked to an Outercite change.', 'outercite' ) ); } if ( Site::is_staging() ) { delete_post_meta( $post_id, Links::ASK_LIVE ); return self::notify( false, __( 'Published on a copy of the site, so Outercite was not told. Only the live site marks changes live.', 'outercite' ) ); } if ( ! Api::context_allowed() ) { wp_schedule_single_event( time() + 60, self::RETRY_HOOK, array( $post_id, 1 ) ); return self::outcome( false, '' ); } $url = Links::public_url( $post_id ); $res = Ledger::mark_live( $change_id, array( (string) $url ) ); if ( $res['ok'] ) { update_post_meta( $post_id, Links::SHIPPED_AT, time() ); update_post_meta( $post_id, Links::SHIPPED_TO, (string) $url ); delete_post_meta( $post_id, Links::ASK_LIVE ); Activity::add( 'live', 'publish' === $how ? __( 'Marked a change live on publish', 'outercite' ) : __( 'Marked a change live after an update', 'outercite' ), $post_id ); return self::notify( true, sprintf( 'publish' === $how /* translators: 1: change title, 2: page address */ ? __( 'Published and marked live. Outercite recorded "%1$s" at %2$s. It checks the page is up within a day, then starts measuring AI answers.', 'outercite' ) /* translators: 1: change title, 2: page address */ : __( 'Marked live. Outercite recorded "%1$s" at %2$s. It checks the page is up within a day, then starts measuring AI answers.', 'outercite' ), (string) get_post_meta( $post_id, Links::TITLE, true ), (string) $url ) ); } // Outercite was unreachable: try again in the background, up to three times. if ( 0 === $res['status'] && 'retry' !== $how ) { wp_schedule_single_event( time() + 5 * MINUTE_IN_SECONDS, self::RETRY_HOOK, array( $post_id, 1 ) ); return self::notify( false, __( 'Published, but Outercite could not be reached. It will try again in a few minutes.', 'outercite' ) ); } return self::notify( false, $res['error'] ); } /** * Background retry after Outercite was unreachable at publish time. * * @param mixed $post_id Post ID. * @param mixed $attempt Attempt number, starting at 1. */ public static function retry( $post_id, $attempt ): void { $post_id = (int) $post_id; $attempt = (int) $attempt; if ( 'publish' !== get_post_status( $post_id ) || Links::is_shipped( $post_id ) ) { return; } $result = self::mark_live( $post_id, 'retry' ); if ( ! $result['ok'] && $attempt < 3 ) { wp_schedule_single_event( time() + 15 * MINUTE_IN_SECONDS, self::RETRY_HOOK, array( $post_id, $attempt + 1 ) ); } } /** * A result for the caller. * * @param bool $ok Whether it worked. * @param string $message Message for the user. * @return array{ok:bool,message:string} */ private static function outcome( bool $ok, string $message ): array { return array( 'ok' => $ok, 'message' => $message, ); } /** * Keep the result for the person who published, so whichever editor they * use can show it on its next look. * * @param bool $ok Whether it worked. * @param string $message Message for the user. * @return array{ok:bool,message:string} */ private static function notify( bool $ok, string $message ): array { $user = get_current_user_id(); if ( $user && '' !== $message ) { set_transient( self::NOTICE . $user, self::outcome( $ok, $message ), 10 * MINUTE_IN_SECONDS ); } return self::outcome( $ok, $message ); } /** * The current user's last result, once. * * @return array{ok:bool,message:string}|null */ public static function take_notice(): ?array { $user = get_current_user_id(); $hit = $user ? get_transient( self::NOTICE . $user ) : false; if ( is_array( $hit ) ) { delete_transient( self::NOTICE . $user ); return $hit; } return null; } }