WebflowClient.php
1 month ago
WebflowFetcher.php
1 month ago
WebflowMapper.php
1 month ago
WebflowPlatform.php
1 month ago
WebflowClient.php
169 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Webflow Client |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\Internal\CLI\Migrator\Platforms\Webflow |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types=1 ); |
| 9 | |
| 10 | namespace Automattic\WooCommerce\Internal\CLI\Migrator\Platforms\Webflow; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Handles communication with the Webflow v2 REST API. |
| 16 | * |
| 17 | * @internal This class is part of the CLI Migrator feature and should not be used directly. |
| 18 | */ |
| 19 | class WebflowClient { |
| 20 | |
| 21 | /** |
| 22 | * Base URL for the Webflow v2 API. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | private const API_BASE = 'https://api.webflow.com/v2'; |
| 27 | |
| 28 | /** |
| 29 | * How many times to retry on 429 responses before bailing. |
| 30 | * |
| 31 | * @var int |
| 32 | */ |
| 33 | private const MAX_RETRIES = 3; |
| 34 | |
| 35 | /** |
| 36 | * Platform credentials. |
| 37 | * |
| 38 | * @var array |
| 39 | */ |
| 40 | private array $credentials; |
| 41 | |
| 42 | /** |
| 43 | * Constructor. |
| 44 | * |
| 45 | * @param array $credentials Platform credentials array, expects 'site_id' and 'access_token'. |
| 46 | */ |
| 47 | public function __construct( array $credentials ) { |
| 48 | $this->credentials = $credentials; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Makes a GET request to the Webflow REST API. |
| 53 | * |
| 54 | * @param string $path The API path relative to API base (e.g., '/sites/{id}/products'). |
| 55 | * @param array $query_params Optional query parameters. |
| 56 | * @return object|array|\WP_Error Decoded JSON response (object or array) or WP_Error on failure. |
| 57 | */ |
| 58 | public function rest_request( string $path, array $query_params = array() ) { |
| 59 | $credentials = $this->get_credentials(); |
| 60 | if ( is_wp_error( $credentials ) ) { |
| 61 | return $credentials; |
| 62 | } |
| 63 | |
| 64 | $endpoint = self::API_BASE . $path; |
| 65 | if ( ! empty( $query_params ) ) { |
| 66 | $endpoint = add_query_arg( $query_params, $endpoint ); |
| 67 | } |
| 68 | |
| 69 | $request_args = array( |
| 70 | 'method' => 'GET', |
| 71 | 'headers' => array( |
| 72 | 'Authorization' => 'Bearer ' . $credentials['access_token'], |
| 73 | 'Accept' => 'application/json', |
| 74 | ), |
| 75 | 'timeout' => 60, |
| 76 | ); |
| 77 | |
| 78 | for ( $attempt = 0; $attempt <= self::MAX_RETRIES; $attempt++ ) { |
| 79 | $response = wp_remote_request( $endpoint, $request_args ); |
| 80 | $response_code = is_wp_error( $response ) ? 0 : (int) wp_remote_retrieve_response_code( $response ); |
| 81 | |
| 82 | if ( 429 !== $response_code ) { |
| 83 | return $this->process_response( $response, $path ); |
| 84 | } |
| 85 | |
| 86 | // Rate limited: back off and retry until the budget is exhausted. The final |
| 87 | // attempt falls through to the retry-budget error below rather than retrying. |
| 88 | if ( $attempt < self::MAX_RETRIES ) { |
| 89 | $retry_after = (int) wp_remote_retrieve_header( $response, 'retry-after' ); |
| 90 | $delay = $retry_after > 0 ? $retry_after : ( 2 ** $attempt ); |
| 91 | $delay = min( 30, max( 1, $delay ) ); |
| 92 | if ( function_exists( 'sleep' ) ) { |
| 93 | sleep( $delay ); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return new \WP_Error( 'api_error', "REST request to {$path} exceeded retry budget after repeated 429 responses." ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return the configured site ID, or a WP_Error if not set. |
| 103 | * |
| 104 | * @return string|\WP_Error |
| 105 | */ |
| 106 | public function get_site_id() { |
| 107 | if ( empty( $this->credentials['site_id'] ) ) { |
| 108 | return new \WP_Error( |
| 109 | 'api_error', |
| 110 | 'Webflow site_id is not configured. Please run: wp wc migrate setup --platform=webflow' |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | return (string) $this->credentials['site_id']; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Validate that credentials are present. |
| 119 | * |
| 120 | * @return array|\WP_Error Array with 'site_id' and 'access_token' keys, or WP_Error on failure. |
| 121 | */ |
| 122 | private function get_credentials() { |
| 123 | if ( empty( $this->credentials['site_id'] ) || empty( $this->credentials['access_token'] ) ) { |
| 124 | return new \WP_Error( |
| 125 | 'api_error', |
| 126 | 'Webflow API credentials (site_id, access_token) are not configured. Please run: wp wc migrate setup --platform=webflow' |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | return array( |
| 131 | 'site_id' => (string) $this->credentials['site_id'], |
| 132 | 'access_token' => (string) $this->credentials['access_token'], |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Process the API response. |
| 138 | * |
| 139 | * @param array|\WP_Error $response The HTTP response. |
| 140 | * @param string $path The API path (for error context). |
| 141 | * @return object|array|\WP_Error Decoded response or WP_Error. |
| 142 | */ |
| 143 | private function process_response( $response, string $path ) { |
| 144 | if ( is_wp_error( $response ) ) { |
| 145 | return new \WP_Error( 'api_error', 'REST request failed: ' . $response->get_error_message() ); |
| 146 | } |
| 147 | |
| 148 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 149 | $response_body = wp_remote_retrieve_body( $response ); |
| 150 | |
| 151 | if ( $response_code >= 300 ) { |
| 152 | $decoded = json_decode( $response_body ); |
| 153 | $error_message = is_object( $decoded ) && isset( $decoded->message ) ? $decoded->message : $response_body; |
| 154 | return new \WP_Error( |
| 155 | 'api_error', |
| 156 | "REST request to {$path} failed with status code {$response_code}: " . $error_message |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | $data = json_decode( $response_body ); |
| 161 | |
| 162 | if ( JSON_ERROR_NONE !== json_last_error() ) { |
| 163 | return new \WP_Error( 'api_error', 'Failed to decode REST JSON response: ' . json_last_error_msg() ); |
| 164 | } |
| 165 | |
| 166 | return $data; |
| 167 | } |
| 168 | } |
| 169 |