/var/www/html_nl/wp-content/plugins/woocommerce/src/Blocks/Patterns/PTKClient.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Automattic\WooCommerce\Blocks\Patterns;

use 
WP_Error;

/**
 * PatternsToolkit class.
 *
 * @internal
 */
class PTKClient {
    
/**
     *  The Patterns Toolkit API URL
     */
    
const PATTERNS_TOOLKIT_URL 'https://public-api.wordpress.com/rest/v1/ptk/patterns/';

    
/**
     * The schema for the patterns toolkit.
     *
     * @var array
     */
    
private $schema;

    
/**
     * Constructor.
     */
    
public function __construct() {
        
$this->schema = [
            
'type'  => 'array',
            
'items' => [
                
'type'       => 'object',
                
'properties' => [
                    
'ID'         => [
                        
'type'     => 'integer',
                        
'required' => true,
                    ],
                    
'site_id'    => [
                        
'type'     => 'integer',
                        
'required' => true,
                    ],
                    
'title'      => [
                        
'type'     => 'string',
                        
'required' => true,
                    ],
                    
'name'       => [
                        
'type'     => 'string',
                        
'required' => true,
                    ],
                    
'html'       => [
                        
'type'     => 'string',
                        
'required' => true,
                    ],
                    
'categories' => [
                        
'type'                 => 'object',
                        
'additionalProperties' => [
                            
'type'       => 'object',
                            
'properties' => [
                                
'slug'        => [
                                    
'type'     => 'string',
                                    
'required' => true,
                                ],
                                
'title'       => [
                                    
'type'     => 'string',
                                    
'required' => true,
                                ],
                                
'description' => [
                                    
'type'     => 'string',
                                    
'required' => true,
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }

    
/**
     * Fetch the WooCommerce patterns from the Patterns Toolkit (PTK) API.
     *
     * @param array $options Options for fetching patterns.
     * @return array|WP_Error
     */
    
public function fetch_patterns( array $options = array() ) {
        
$locale get_user_locale();
        
$lang   preg_replace'/(_.*)$/'''$locale );

        
$ptk_url self::PATTERNS_TOOLKIT_URL $lang;

        if ( isset( 
$options['site'] ) ) {
            
$ptk_url add_query_arg'site'$options['site'], $ptk_url );
        }

        if ( isset( 
$options['categories'] ) ) {
            
$ptk_url add_query_arg'categories'implode','$options['categories'] ), $ptk_url );
        }

        if ( isset( 
$options['per_page'] ) ) {
            
$ptk_url add_query_arg'per_page'$options['per_page'], $ptk_url );
        }

        
$patterns wp_safe_remote_get$ptk_url );
        if ( 
is_wp_error$patterns ) || 200 !== wp_remote_retrieve_response_code$patterns ) ) {
            return new 
WP_Error(
                
'patterns_toolkit_api_error',
                
__'Failed to connect with the Patterns Toolkit API: try again later.''woocommerce' )
            );
        }

        
$body wp_remote_retrieve_body$patterns );

        if ( empty( 
$body ) ) {
            return new 
WP_Error(
                
'patterns_toolkit_api_error',
                
__'Empty response received from the Patterns Toolkit API.''woocommerce' )
            );
        }

        
$decoded_body json_decode$bodytrue );

        
$is_pattern_payload_valid $this->is_valid_schema$decoded_body );

        if ( ! 
$is_pattern_payload_valid ) {
            return new 
WP_Error(
                
'patterns_toolkit_api_error',
                
__'Wrong response received from the Patterns Toolkit API: try again later.''woocommerce' )
            );
        }

        return 
$decoded_body;
    }

    
/**
     * Validate the patterns toolkit patterns.
     *
     * @param array $patterns The patterns to validate.
     * @return bool
     */
    
public function is_valid_schema$patterns ) {
        
$is_pattern_payload_valid rest_validate_value_from_schema$patterns$this->schema );

        return ! 
is_wp_error$is_pattern_payload_valid );
    }
}