isset( $secret['access_token'] ) ? (string) $secret['access_token'] : '', 'refresh_token' => isset( $secret['refresh_token'] ) ? (string) $secret['refresh_token'] : '', ); } /** * Persist the scoped token pair. autoload OFF so it never rides along on * unrelated page loads, and is never exposed via alloptions. */ public static function set_tokens( $access_token, $refresh_token ) { // Delete + add to guarantee the autoload flag is 'no' (update_option // keeps the existing autoload value if the row already exists). delete_option( self::SECRET ); add_option( self::SECRET, array( 'access_token' => (string) $access_token, 'refresh_token' => (string) $refresh_token, ), '', 'no' ); } /** * Save everything returned by a successful token exchange/refresh. * * @param array $data Decoded response from wordpress-token. * @param string $site_url The paired site URL (home_url()). */ public static function save_pairing( array $data, $site_url ) { self::set_tokens( isset( $data['access_token'] ) ? $data['access_token'] : '', isset( $data['refresh_token'] ) ? $data['refresh_token'] : '' ); $expires_at = isset( $data['expires_at'] ) ? strtotime( (string) $data['expires_at'] ) : 0; $values = array( 'connection_status' => 'connected', 'paired_project_id' => isset( $data['project_id'] ) ? sanitize_text_field( $data['project_id'] ) : '', 'project_name' => isset( $data['project_name'] ) ? sanitize_text_field( $data['project_name'] ) : '', 'embed_key' => isset( $data['embed_key'] ) ? sanitize_text_field( $data['embed_key'] ) : '', 'paired_plan' => isset( $data['plan'] ) ? sanitize_text_field( $data['plan'] ) : '', 'token_expires_at' => $expires_at ? $expires_at : 0, 'paired_site_url' => esc_url_raw( $site_url ), ); // Record the first pairing time once. if ( '' === (string) self::get( 'paired_at' ) ) { $values['paired_at'] = time(); } // Auto-fill the Widget Key with the embed key so the widget goes live // immediately after connecting — but never clobber a key the user set. $current_key = trim( (string) self::get( 'widget_key' ) ); if ( '' === $current_key && ! empty( $values['embed_key'] ) ) { $values['widget_key'] = $values['embed_key']; } self::update( $values ); } /** * Update only the rotated tokens + new expiry (used after a refresh). */ public static function update_tokens_from_refresh( array $data ) { self::set_tokens( isset( $data['access_token'] ) ? $data['access_token'] : '', isset( $data['refresh_token'] ) ? $data['refresh_token'] : '' ); $expires_at = isset( $data['expires_at'] ) ? strtotime( (string) $data['expires_at'] ) : 0; self::update( array( 'token_expires_at' => $expires_at ? $expires_at : 0 ) ); } /** * Clear all pairing state (disconnect). The Widget Key is intentionally * LEFT in place so the widget keeps rendering after disconnect. */ public static function clear_pairing() { $opts = get_option( self::OPTIONS, array() ); if ( is_array( $opts ) ) { foreach ( self::$pairing_keys as $k ) { unset( $opts[ $k ] ); } update_option( self::OPTIONS, $opts ); } delete_option( self::SECRET ); } }