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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
<?php
namespace Automattic\WooCommerce\Blueprint;
use Automattic\WooCommerce\Blueprint\Schemas\JsonSchema; use Automattic\WooCommerce\Blueprint\Schemas\ZipSchema; use Opis\JsonSchema\Errors\ErrorFormatter; use Opis\JsonSchema\Validator;
/** * Class ImportSchema * * Handles the import schema functionality for WooCommerce. * * @package Automattic\WooCommerce\Blueprint */ class ImportSchema { use UseWPFunctions;
/** * JsonSchema object. * * @var JsonSchema The schema instance. */ private JsonSchema $schema;
/** * Validator object. * * @var Validator The JSON schema validator instance. */ private Validator $validator;
/** * Built-in step processors. * * @var BuiltInStepProcessors The built-in step processors instance. */ private BuiltInStepProcessors $builtin_step_processors;
/** * ImportSchema constructor. * * @param JsonSchema $schema The schema instance. * @param Validator|null $validator The validator instance, optional. */ public function __construct( JsonSchema $schema, ?Validator $validator = null ) { $this->schema = $schema; if ( null === $validator ) { $validator = new Validator(); }
$this->validator = $validator;
$this->builtin_step_processors = new BuiltInStepProcessors( $schema ); }
/** * Get the schema. * * @return JsonSchema The schema. */ public function get_schema() { return $this->schema; }
/** * Create an ImportSchema instance from a file. * * @param string $file The file path. * @return ImportSchema The created ImportSchema instance. */ public static function create_from_file( $file ) { // @todo check for mime type // @todo check for allowed types -- json or zip $path_info = pathinfo( $file ); $is_zip = 'zip' === $path_info['extension'];
return $is_zip ? self::create_from_zip( $file ) : self::create_from_json( $file ); }
/** * Create an ImportSchema instance from a JSON file. * * @param string $json_path The JSON file path. * @return ImportSchema The created ImportSchema instance. */ public static function create_from_json( $json_path ) { return new self( new JsonSchema( $json_path ) ); }
/** * Create an ImportSchema instance from a ZIP file. * * @param string $zip_path The ZIP file path. * * @return ImportSchema The created ImportSchema instance. */ public static function create_from_zip( $zip_path ) { return new self( new ZipSchema( $zip_path ) ); }
/** * Import the schema steps. * * @return StepProcessorResult[] */ public function import() { $results = array(); $result = StepProcessorResult::success( 'ImportSchema' ); $results[] = $result;
$step_processors = $this->builtin_step_processors->get_all();
/** * Filters the step processors. * * Allows adding/removing custom step processors. * * @param StepProcessor[] $step_processors The step processors. * * @since 0.0.1 */ $step_processors = $this->wp_apply_filters( 'wooblueprint_importers', $step_processors );
$indexed_step_processors = Util::index_array( $step_processors, function ( $key, $step_processor ) { return $step_processor->get_step_class()::get_step_name(); } );
// validate steps before processing. $this->validate_step_schemas( $indexed_step_processors, $result );
if ( count( $result->get_messages( 'error' ) ) !== 0 ) { return $results; }
foreach ( $this->schema->get_steps() as $step_schema ) { $step_processor = $indexed_step_processors[ $step_schema->step ] ?? null; if ( ! $step_processor instanceof StepProcessor ) { $result->add_error( "Unable to create a step processor for {$step_schema->step}" ); continue; }
$results[] = $step_processor->process( $step_schema ); }
return $results; }
/** * Validate the step schemas. * * @param array $indexed_step_processors Array of step processors indexed by step name. * @param StepProcessorResult $result The result object to add messages to. * * @return void */ protected function validate_step_schemas( array $indexed_step_processors, StepProcessorResult $result ) { $step_schemas = array_map( function ( $step_processor ) { return $step_processor->get_step_class()::get_schema(); }, $indexed_step_processors );
foreach ( $this->schema->get_steps() as $step_json ) { $step_schema = $step_schemas[ $step_json->step ] ?? null; if ( ! $step_schema ) { $result->add_info( "No schema found for step $step_json->step" ); continue; }
// phpcs:ignore $validate = $this->validator->validate( $step_json, json_encode( $step_schema ) );
if ( ! $validate->isValid() ) { $result->add_error( "Schema validation failed for step {$step_json->step}" ); $errors = ( new ErrorFormatter() )->format( $validate->error() ); $formatted_errors = array(); foreach ( $errors as $value ) { $formatted_errors[] = implode( "\n", $value ); }
$result->add_error( implode( "\n", $formatted_errors ) ); } } } }
|