30, 'auth' => true, 'anonymous' => false ) ); $headers = array( 'Content-Type' => 'application/json' ); // Anonymous lookups (the vulnerability check) do not say which site is asking. if ( ! $args['anonymous'] ) { $headers['X-Fixwise-Site'] = self::site(); $headers['X-Fixwise-Plugin'] = FIXWISE_VERSION; } if ( $args['auth'] ) { $key = Fixwise_License::key(); if ( ! $key ) { return new WP_Error( 'no_license', __( 'Add your license key in Settings first.', 'fixwise' ) ); } $headers['X-Fixwise-License'] = $key; $secret = Fixwise_License::secret(); if ( $secret ) { $headers['X-Fixwise-Secret'] = $secret; } } $response = wp_remote_request( rtrim( FIXWISE_API, '/' ) . $path, array( 'method' => $method, 'timeout' => $args['timeout'], 'headers' => $headers, 'body' => null === $body ? null : wp_json_encode( $body ), ) ); if ( is_wp_error( $response ) ) { return new WP_Error( 'network', __( 'Could not reach the Fixwise service. Check that your server can make outgoing requests, then try again.', 'fixwise' ) ); } $code = wp_remote_retrieve_response_code( $response ); $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $code >= 400 || ! is_array( $data ) ) { $error = is_array( $data ) && ! empty( $data['error'] ) ? $data['error'] : 'http_' . $code; $message = is_array( $data ) && ! empty( $data['message'] ) ? $data['message'] : __( 'The Fixwise service returned an error.', 'fixwise' ); // License problems: refresh our cached status so the UI updates. if ( in_array( $error, array( 'inactive', 'bad_license', 'not_activated' ), true ) ) { Fixwise_License::mark_invalid( $message ); } return new WP_Error( $error, $message, array( 'status' => $code >= 400 && $code < 600 ? $code : 500 ) ); } return $data; } public static function site() { return home_url( '/' ); } /** * Runs one AI action and returns the structured result. */ public static function ai( $action, array $body ) { $data = self::request( 'POST', '/v1/ai/' . $action, $body, array( 'timeout' => 180 ) ); if ( is_wp_error( $data ) ) { return $data; } Fixwise_License::refresh_soon(); return isset( $data['result'] ) ? $data['result'] : new WP_Error( 'ai_error', __( 'Empty AI response.', 'fixwise' ) ); } }