AIPatterns.php
1 year ago
PTKClient.php
1 year ago
PTKPatternsStore.php
6 months ago
PatternRegistry.php
1 year ago
PTKClient.php
145 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Patterns; |
| 3 | |
| 4 | use WP_Error; |
| 5 | |
| 6 | /** |
| 7 | * PatternsToolkit class. |
| 8 | * |
| 9 | * @internal |
| 10 | */ |
| 11 | class PTKClient { |
| 12 | /** |
| 13 | * The Patterns Toolkit API URL |
| 14 | */ |
| 15 | const PATTERNS_TOOLKIT_URL = 'https://public-api.wordpress.com/rest/v1/ptk/patterns/'; |
| 16 | |
| 17 | /** |
| 18 | * The schema for the patterns toolkit. |
| 19 | * |
| 20 | * @var array |
| 21 | */ |
| 22 | private $schema; |
| 23 | |
| 24 | /** |
| 25 | * Constructor. |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | $this->schema = [ |
| 29 | 'type' => 'array', |
| 30 | 'items' => [ |
| 31 | 'type' => 'object', |
| 32 | 'properties' => [ |
| 33 | 'ID' => [ |
| 34 | 'type' => 'integer', |
| 35 | 'required' => true, |
| 36 | ], |
| 37 | 'site_id' => [ |
| 38 | 'type' => 'integer', |
| 39 | 'required' => true, |
| 40 | ], |
| 41 | 'title' => [ |
| 42 | 'type' => 'string', |
| 43 | 'required' => true, |
| 44 | ], |
| 45 | 'name' => [ |
| 46 | 'type' => 'string', |
| 47 | 'required' => true, |
| 48 | ], |
| 49 | 'html' => [ |
| 50 | 'type' => 'string', |
| 51 | 'required' => true, |
| 52 | ], |
| 53 | 'categories' => [ |
| 54 | 'type' => 'object', |
| 55 | 'additionalProperties' => [ |
| 56 | 'type' => 'object', |
| 57 | 'properties' => [ |
| 58 | 'slug' => [ |
| 59 | 'type' => 'string', |
| 60 | 'required' => true, |
| 61 | ], |
| 62 | 'title' => [ |
| 63 | 'type' => 'string', |
| 64 | 'required' => true, |
| 65 | ], |
| 66 | 'description' => [ |
| 67 | 'type' => 'string', |
| 68 | 'required' => true, |
| 69 | ], |
| 70 | ], |
| 71 | ], |
| 72 | ], |
| 73 | ], |
| 74 | ], |
| 75 | ]; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Fetch the WooCommerce patterns from the Patterns Toolkit (PTK) API. |
| 80 | * |
| 81 | * @param array $options Options for fetching patterns. |
| 82 | * @return array|WP_Error |
| 83 | */ |
| 84 | public function fetch_patterns( array $options = array() ) { |
| 85 | $locale = get_user_locale(); |
| 86 | $lang = preg_replace( '/(_.*)$/', '', $locale ); |
| 87 | |
| 88 | $ptk_url = self::PATTERNS_TOOLKIT_URL . $lang; |
| 89 | |
| 90 | if ( isset( $options['site'] ) ) { |
| 91 | $ptk_url = add_query_arg( 'site', $options['site'], $ptk_url ); |
| 92 | } |
| 93 | |
| 94 | if ( isset( $options['categories'] ) ) { |
| 95 | $ptk_url = add_query_arg( 'categories', implode( ',', $options['categories'] ), $ptk_url ); |
| 96 | } |
| 97 | |
| 98 | if ( isset( $options['per_page'] ) ) { |
| 99 | $ptk_url = add_query_arg( 'per_page', $options['per_page'], $ptk_url ); |
| 100 | } |
| 101 | |
| 102 | $patterns = wp_safe_remote_get( $ptk_url ); |
| 103 | if ( is_wp_error( $patterns ) || 200 !== wp_remote_retrieve_response_code( $patterns ) ) { |
| 104 | return new WP_Error( |
| 105 | 'patterns_toolkit_api_error', |
| 106 | __( 'Failed to connect with the Patterns Toolkit API: try again later.', 'woocommerce' ) |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | $body = wp_remote_retrieve_body( $patterns ); |
| 111 | |
| 112 | if ( empty( $body ) ) { |
| 113 | return new WP_Error( |
| 114 | 'patterns_toolkit_api_error', |
| 115 | __( 'Empty response received from the Patterns Toolkit API.', 'woocommerce' ) |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | $decoded_body = json_decode( $body, true ); |
| 120 | |
| 121 | $is_pattern_payload_valid = $this->is_valid_schema( $decoded_body ); |
| 122 | |
| 123 | if ( ! $is_pattern_payload_valid ) { |
| 124 | return new WP_Error( |
| 125 | 'patterns_toolkit_api_error', |
| 126 | __( 'Wrong response received from the Patterns Toolkit API: try again later.', 'woocommerce' ) |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | return $decoded_body; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Validate the patterns toolkit patterns. |
| 135 | * |
| 136 | * @param array $patterns The patterns to validate. |
| 137 | * @return bool |
| 138 | */ |
| 139 | public function is_valid_schema( $patterns ) { |
| 140 | $is_pattern_payload_valid = rest_validate_value_from_schema( $patterns, $this->schema ); |
| 141 | |
| 142 | return ! is_wp_error( $is_pattern_payload_valid ); |
| 143 | } |
| 144 | } |
| 145 |