string, 'monthly_limit' => mixed, 'message' => string ]. */ public static function register( $name, $email, $site_url ) { $name = sanitize_text_field( $name ); $email = sanitize_email( $email ); $site_url = esc_url_raw( $site_url ); if ( empty( $email ) || empty( $site_url ) ) { return new WP_Error( 'aisentic_missing_params', __( 'Email and site URL are required to register.', 'aisentic' ), [ 'status' => 422 ] ); } $provider_endpoint = aisentic_get_option( 'llmProviders.aisentic.endpoint', AISENTIC_AI_PROVIDER ); $remote_response = wp_remote_post( trailingslashit( $provider_endpoint ) . 'api/register', [ 'headers' => [ 'Content-Type' => 'application/json' ], 'body' => wp_json_encode( [ 'name' => $name, 'email' => $email, 'domain' => $site_url, ] ), 'timeout' => 30, ] ); if ( is_wp_error( $remote_response ) ) { return new WP_Error( 'aisentic_provider_unreachable', $remote_response->get_error_message(), [ 'status' => 502 ] ); } $status_code = wp_remote_retrieve_response_code( $remote_response ); $body = json_decode( wp_remote_retrieve_body( $remote_response ), true ); if ( 200 !== $status_code ) { $message = isset( $body['message'] ) ? $body['message'] : __( 'Registration failed. Please try again.', 'aisentic' ); return new WP_Error( 'aisentic_registration_failed', $message, [ 'status' => $status_code ] ); } $api_key = isset( $body['api_key'] ) ? sanitize_text_field( $body['api_key'] ) : ''; aisentic_update_option( 'llmProviders.aisentic.apiKey', $api_key ); /* * Token_Usage_Controller reports usage per account email and back-fills this * from the provider when it is missing. Storing it here means every caller * that registers — the REST route and each companion-plugin handshake — * leaves the same state behind, instead of only whichever one remembered to. */ aisentic_update_option( 'llmProviders.aisentic.email', $email ); /** * Fires after a site is successfully registered and connected. * * Lets companion plugins (e.g. Eventin) react to the connection — for * example to surface the admin chat widget once the api_key is set. * * @param string $api_key The stored provider api key. * @param string $email The email the account was registered with. */ do_action( 'aisentic/site_registered', $api_key, $email ); return [ 'api_key' => $api_key, 'monthly_limit' => isset( $body['monthly_limit'] ) ? $body['monthly_limit'] : null, 'message' => isset( $body['message'] ) ? $body['message'] : __( 'Site registered successfully.', 'aisentic' ), ]; } }