Cloudflare Pages > Edit */ if ( ! defined( 'ABSPATH' ) ) { exit; } class CFSP_Cloudflare { const API = 'https://api.cloudflare.com/client/v4'; private $account_id; private $token; private $project; private $jwt = null; public function __construct() { $this->account_id = CFSP_Settings::get( 'account_id' ); $this->token = CFSP_Settings::get( 'api_token' ); $this->project = CFSP_Settings::get( 'project_name' ); } private function project_url( $suffix = '' ) { return self::API . '/accounts/' . rawurlencode( $this->account_id ) . '/pages/projects/' . rawurlencode( $this->project ) . $suffix; } /** * @return array|WP_Error Decoded "result" on success. */ private function request( $method, $url, $body = null, $bearer = null, $extra_headers = array(), $timeout = 60 ) { $headers = array_merge( array( 'Authorization' => 'Bearer ' . ( $bearer ? $bearer : $this->token ) ), $extra_headers ); if ( is_array( $body ) ) { $body = wp_json_encode( $body ); $headers['Content-Type'] = 'application/json'; } $response = wp_remote_request( $url, array( 'method' => $method, 'headers' => $headers, 'body' => $body, 'timeout' => $timeout, ) ); if ( is_wp_error( $response ) ) { return $response; } $code = wp_remote_retrieve_response_code( $response ); $raw = wp_remote_retrieve_body( $response ); $data = json_decode( $raw, true ); if ( $code < 200 || $code >= 300 || ! is_array( $data ) || empty( $data['success'] ) ) { $message = 'Cloudflare API HTTP ' . $code; $list = array(); foreach ( array( 'errors', 'messages' ) as $key ) { if ( is_array( $data ) && ! empty( $data[ $key ] ) && is_array( $data[ $key ] ) ) { foreach ( $data[ $key ] as $e ) { $list[] = is_array( $e ) ? ( isset( $e['code'] ) ? '[' . $e['code'] . '] ' : '' ) . ( $e['message'] ?? '' ) : (string) $e; } } } if ( $list ) { $message .= ': ' . implode( '; ', $list ); } elseif ( '' !== trim( (string) $raw ) ) { // No error list: show the start of the reply so the cause is visible in the log. $message .= ': ' . substr( trim( wp_strip_all_tags( (string) $raw ) ), 0, 300 ); } return new WP_Error( 'cfsp_api', $message, array( 'status' => $code ) ); } return $data['result'] ?? array(); } public function get_project() { return $this->request( 'GET', $this->project_url() ); } public function create_project() { return $this->request( 'POST', self::API . '/accounts/' . rawurlencode( $this->account_id ) . '/pages/projects', array( 'name' => $this->project, 'production_branch' => CFSP_Settings::get( 'branch' ), ) ); } /** Short-lived JWT used for the asset upload endpoints. */ public function upload_token( $refresh = false ) { if ( $this->jwt && ! $refresh ) { return $this->jwt; } $result = $this->request( 'GET', $this->project_url( '/upload-token' ) ); if ( is_wp_error( $result ) ) { return $result; } $this->jwt = $result['jwt'] ?? null; return $this->jwt ? $this->jwt : new WP_Error( 'cfsp_api', 'No upload token returned' ); } /** Calls an asset endpoint with the JWT, refreshing it once if it expired. */ private function asset_request( $path, $body, $timeout = 60 ) { for ( $attempt = 0; $attempt < 2; $attempt++ ) { $jwt = $this->upload_token( $attempt > 0 ); if ( is_wp_error( $jwt ) ) { return $jwt; } $result = $this->request( 'POST', self::API . $path, $body, $jwt, array(), $timeout ); $status = is_wp_error( $result ) ? ( $result->get_error_data()['status'] ?? 0 ) : 0; if ( ! is_wp_error( $result ) || ! in_array( $status, array( 401, 403 ), true ) ) { return $result; } } return $result; } /** @return string[]|WP_Error Hashes Cloudflare doesn't have yet. */ public function check_missing( array $hashes ) { $missing = array(); foreach ( array_chunk( $hashes, 5000 ) as $chunk ) { $result = $this->asset_request( '/pages/assets/check-missing', array( 'hashes' => $chunk ) ); if ( is_wp_error( $result ) ) { return $result; } $missing = array_merge( $missing, (array) $result ); } return $missing; } /** * @param array $files List of [ 'hash' => ..., 'base64' => ..., 'type' => ... ]. */ public function upload( array $files ) { $payload = array(); foreach ( $files as $f ) { $payload[] = array( 'key' => $f['hash'], 'value' => $f['base64'], 'metadata' => array( 'contentType' => $f['type'] ), 'base64' => true, ); } return $this->asset_request( '/pages/assets/upload', $payload, 180 ); } public function upsert_hashes( array $hashes ) { foreach ( array_chunk( $hashes, 5000 ) as $chunk ) { $result = $this->asset_request( '/pages/assets/upsert-hashes', array( 'hashes' => $chunk ) ); if ( is_wp_error( $result ) ) { return $result; } } return true; } /** * Create the deployment from a manifest of "/path" => hash. */ public function create_deployment( array $manifest, $headers_file = '', $redirects_file = '' ) { $boundary = 'cfsp' . wp_generate_password( 24, false, false ); $fields = array( 'manifest' => array( wp_json_encode( (object) $manifest, JSON_UNESCAPED_SLASHES ), null ), 'branch' => array( CFSP_Settings::get( 'branch' ), null ), ); if ( '' !== trim( $headers_file ) ) { $fields['_headers'] = array( $headers_file, '_headers' ); } if ( '' !== trim( $redirects_file ) ) { $fields['_redirects'] = array( $redirects_file, '_redirects' ); } $body = ''; foreach ( $fields as $name => $field ) { list( $value, $filename ) = $field; $body .= "--{$boundary}\r\n"; $body .= 'Content-Disposition: form-data; name="' . $name . '"' . ( $filename ? '; filename="' . $filename . '"' : '' ) . "\r\n"; if ( $filename ) { $body .= "Content-Type: application/octet-stream\r\n"; } $body .= "\r\n" . $value . "\r\n"; } $body .= "--{$boundary}--\r\n"; return $this->request( 'POST', $this->project_url( '/deployments' ), $body, null, array( 'Content-Type' => 'multipart/form-data; boundary=' . $boundary ), 120 ); } }