|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Base class for the importers. |
|
5
|
|
|
* |
|
6
|
|
|
* @package WordPoints_Importer |
|
7
|
|
|
* @since 1.0.0 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Container class for the available importers. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 1.0.0 |
|
14
|
|
|
*/ |
|
15
|
|
|
final class WordPoints_Importers { |
|
16
|
|
|
|
|
17
|
|
|
// |
|
18
|
|
|
// Private Vars. |
|
19
|
|
|
// |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The registered importers. |
|
23
|
|
|
* |
|
24
|
|
|
* @since 1.0.0 |
|
25
|
|
|
* |
|
26
|
|
|
* @type array $importers |
|
27
|
|
|
*/ |
|
28
|
|
|
private static $importers = array(); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Whether the class has been initialized yet. |
|
32
|
|
|
* |
|
33
|
|
|
* @since 1.0.0 |
|
34
|
|
|
* |
|
35
|
|
|
* @type bool $initialized |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $initialized = false; |
|
38
|
|
|
|
|
39
|
|
|
// |
|
40
|
|
|
// Private Functions. |
|
41
|
|
|
// |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Initialize the class. |
|
45
|
|
|
* |
|
46
|
|
|
* @since 1.0.0 |
|
47
|
|
|
*/ |
|
48
|
|
|
private static function init() { |
|
49
|
|
|
|
|
50
|
|
|
// We do this first so we avoid infinite loops if this class is called by a |
|
51
|
|
|
// function hooked to the below action. |
|
52
|
|
|
self::$initialized = true; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Register importers. |
|
56
|
|
|
* |
|
57
|
|
|
* @since 1.0.0 |
|
58
|
|
|
*/ |
|
59
|
|
|
do_action( 'wordpoints_register_importers' ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// |
|
63
|
|
|
// Public Functions. |
|
64
|
|
|
// |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get all of the registered importers. |
|
68
|
|
|
* |
|
69
|
|
|
* @since 1.0.0 |
|
70
|
|
|
* |
|
71
|
|
|
* @return array All of the registered importers. |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function get() { |
|
74
|
|
|
|
|
75
|
|
|
if ( ! self::$initialized ) { |
|
76
|
|
|
self::init(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return self::$importers; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Register an importer. |
|
84
|
|
|
* |
|
85
|
|
|
* If the importer is already registered, it will be overwritten. |
|
86
|
|
|
* |
|
87
|
|
|
* @since 1.0.0 |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $slug The unique identifier for this importer. |
|
90
|
|
|
* @param array $args { |
|
91
|
|
|
* Other importer arguments. |
|
92
|
|
|
* |
|
93
|
|
|
* @type string $class The Importer class. |
|
94
|
|
|
* @type string $name The name of this importer. |
|
95
|
|
|
* } |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function register( $slug, array $args ) { |
|
98
|
|
|
self::$importers[ $slug ] = $args; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Deregister an importer. |
|
103
|
|
|
* |
|
104
|
|
|
* @since 1.0.0 |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $slug The slug of the importer to deregister. |
|
107
|
|
|
*/ |
|
108
|
|
|
public static function deregister( $slug ) { |
|
109
|
|
|
unset( self::$importers[ $slug ] ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Check if an importer is registered. |
|
114
|
|
|
* |
|
115
|
|
|
* @since 1.0.0 |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $slug The slug of the importer. |
|
118
|
|
|
* |
|
119
|
|
|
* @return bool True if the importer is registered, otherwise false. |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function is_registered( $slug ) { |
|
122
|
|
|
|
|
123
|
|
|
if ( ! self::$initialized ) { |
|
124
|
|
|
self::init(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return isset( self::$importers[ $slug ] ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get an instance of an importer. |
|
132
|
|
|
* |
|
133
|
|
|
* @since 1.0.0 |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $slug The slug of the importer to get an instance of. |
|
136
|
|
|
* |
|
137
|
|
|
* @return WordPoints_Importer|false The importer, or false if it isn't registered. |
|
138
|
|
|
*/ |
|
139
|
|
|
public static function get_importer( $slug ) { |
|
140
|
|
|
|
|
141
|
|
|
if ( ! self::is_registered( $slug ) ) { |
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$importer = self::$importers[ $slug ]; |
|
146
|
|
|
|
|
147
|
|
|
return new $importer['class']( $importer['name'] ); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Represents an importer. |
|
153
|
|
|
* |
|
154
|
|
|
* @since 1.0.0 |
|
155
|
|
|
*/ |
|
156
|
|
|
abstract class WordPoints_Importer { |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* The name of the importer. |
|
160
|
|
|
* |
|
161
|
|
|
* @since 1.0.0 |
|
162
|
|
|
* |
|
163
|
|
|
* @type string $name |
|
164
|
|
|
*/ |
|
165
|
|
|
protected $name; |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* The components supported by this importer. |
|
169
|
|
|
* |
|
170
|
|
|
* The keys are the component slugs, the values arrays of options for importing |
|
171
|
|
|
* to that component. |
|
172
|
|
|
* |
|
173
|
|
|
* @since 1.0.0 |
|
174
|
|
|
* |
|
175
|
|
|
* @type array[] $components |
|
176
|
|
|
*/ |
|
177
|
|
|
protected $components = array(); |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* The feedback provider object. |
|
181
|
|
|
* |
|
182
|
|
|
* This is only set by self::do_import(). |
|
183
|
|
|
* |
|
184
|
|
|
* @since 1.0.0 |
|
185
|
|
|
* |
|
186
|
|
|
* @type WordPoints_Importer_Feedback $feedback |
|
187
|
|
|
*/ |
|
188
|
|
|
protected $feedback; |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Check if this importer is available. |
|
192
|
|
|
* |
|
193
|
|
|
* @since 1.0.0 |
|
194
|
|
|
* |
|
195
|
|
|
* @return true|WP_Error A WP_Error if the importer is not available. |
|
196
|
|
|
*/ |
|
197
|
|
|
abstract public function is_available(); |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Construct the importer. |
|
201
|
|
|
* |
|
202
|
|
|
* @since 1.0.0 |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $name The name of the importer. |
|
205
|
|
|
*/ |
|
206
|
|
|
public function __construct( $name ) { |
|
207
|
|
|
|
|
208
|
|
|
$this->name = $name; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Check if this importer supports a specific component. |
|
213
|
|
|
* |
|
214
|
|
|
* @since 1.0.0 |
|
215
|
|
|
* |
|
216
|
|
|
* @param string $component The slug of a component. |
|
217
|
|
|
* |
|
218
|
|
|
* @return bool True if the component is supported, otherwise false. |
|
219
|
|
|
*/ |
|
220
|
|
|
public function supports_component( $component ) { |
|
221
|
|
|
|
|
222
|
|
|
return isset( $this->components[ $component ] ); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Get the import options for a component. |
|
227
|
|
|
* |
|
228
|
|
|
* @since 1.0.0 |
|
229
|
|
|
* |
|
230
|
|
|
* @param string $component The slug of a component. |
|
231
|
|
|
* |
|
232
|
|
|
* @return array[] The options for this component. |
|
233
|
|
|
*/ |
|
234
|
|
|
public function get_options_for_component( $component ) { |
|
235
|
|
|
|
|
236
|
|
|
if ( ! $this->supports_component( $component ) ) { |
|
237
|
|
|
return array(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $this->components[ $component ]; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Run the import. |
|
245
|
|
|
* |
|
246
|
|
|
* @since 1.0.0 |
|
247
|
|
|
* |
|
248
|
|
|
* @param array $args The settings for the import. |
|
249
|
|
|
* @param WordPoints_Importer_Feedback $feedback The feedback object. |
|
250
|
|
|
*/ |
|
251
|
|
|
public function do_import( array $args, $feedback = null ) { |
|
252
|
|
|
|
|
253
|
|
|
if ( ! ( $feedback instanceof WordPoints_Importer_Feedback ) ) { |
|
254
|
|
|
$feedback = new WordPoints_Importer_Feedback; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
$this->feedback = $feedback; |
|
258
|
|
|
|
|
259
|
|
|
$this->feedback->info( sprintf( __( 'Importing from %s…', 'wordpoints-importer' ), $this->name ) ); |
|
260
|
|
|
|
|
261
|
|
|
$this->no_interruptions(); |
|
262
|
|
|
|
|
263
|
|
|
foreach ( $args as $component => $options ) { |
|
264
|
|
|
$this->do_import_for_component( $component, $options ); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
$this->feedback->info( __( 'Import complete.', 'wordpoints-importer' ) ); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Prevent any interruptions from occurring during the import. |
|
272
|
|
|
* |
|
273
|
|
|
* @since 1.2.1 |
|
274
|
|
|
*/ |
|
275
|
|
|
protected function no_interruptions() { |
|
276
|
|
|
|
|
277
|
|
|
ignore_user_abort( true ); |
|
278
|
|
|
|
|
279
|
|
|
if ( |
|
280
|
|
|
// Back-compat with WordPoints 2.1. |
|
281
|
|
|
function_exists( 'wordpoints_is_function_disabled' ) |
|
282
|
|
|
&& ! wordpoints_is_function_disabled( 'set_time_limit' ) |
|
283
|
|
|
) { |
|
284
|
|
|
set_time_limit( 0 ); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Validate the import settings for a component. |
|
290
|
|
|
* |
|
291
|
|
|
* @since 1.0.0 |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $component The slug of the component. |
|
294
|
|
|
* @param array $settings The settings supplied for this component. |
|
295
|
|
|
* |
|
296
|
|
|
* @return bool Whether the settings are valid. |
|
297
|
|
|
*/ |
|
298
|
|
|
protected function validate_import_settings( $component, $settings ) { |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Filter whether the settings are valid before importing. |
|
302
|
|
|
* |
|
303
|
|
|
* @since 1.0.0 |
|
304
|
|
|
* |
|
305
|
|
|
* @param bool $valid Whether the settings are valid. |
|
306
|
|
|
* @param array $settings The settings for this component. |
|
307
|
|
|
* @param WordPoints_Importer_Feedback $feedback The feedback object. |
|
308
|
|
|
*/ |
|
309
|
|
|
return apply_filters( "wordpoints_import_settings_valid-{$component}", true, $settings, $this->feedback ); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Run the import for a component. |
|
314
|
|
|
* |
|
315
|
|
|
* @since 1.0.0 |
|
316
|
|
|
* |
|
317
|
|
|
* @param string $component The component to run the import for. |
|
318
|
|
|
* @param array $options The selected options of what to import. |
|
319
|
|
|
*/ |
|
320
|
|
|
protected function do_import_for_component( $component, $options ) { |
|
321
|
|
|
|
|
322
|
|
|
$component_data = WordPoints_Components::instance()->get_component( |
|
323
|
|
|
$component |
|
324
|
|
|
); |
|
325
|
|
|
|
|
326
|
|
View Code Duplication |
if ( false === $component_data ) { |
|
|
|
|
|
|
327
|
|
|
$this->feedback->warning( sprintf( __( 'Skipping %s component—not installed.', 'wordpoints-importer' ), esc_html( $component ) ) ); |
|
328
|
|
|
return; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
View Code Duplication |
if ( true !== $this->supports_component( $component ) ) { |
|
|
|
|
|
|
332
|
|
|
$this->feedback->warning( sprintf( __( 'Skipping the %s component—not supported.', 'wordpoints-importer' ), $component_data['name'] ) ); |
|
333
|
|
|
return; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
$settings = array(); |
|
337
|
|
|
|
|
338
|
|
|
if ( isset( $options['_data'] ) ) { |
|
339
|
|
|
$settings = $options['_data']; |
|
340
|
|
|
unset( $options['_data'] ); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
if ( empty( $options ) || ! $this->validate_import_settings( $component, $settings ) ) { |
|
344
|
|
|
return; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
$this->feedback->info( sprintf( __( 'Importing data to the %s component…', 'wordpoints-importer' ), $component_data['name'] ) ); |
|
348
|
|
|
|
|
349
|
|
|
foreach ( $options as $option => $unused ) { |
|
350
|
|
|
$this->do_import_for_option( $option, $component, $settings ); |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* Run the import for an option. |
|
356
|
|
|
* |
|
357
|
|
|
* The import is split up into different options which the user can select (these |
|
358
|
|
|
* are displayed to the user as checkboxes in the form). This handles the import |
|
359
|
|
|
* for each of the individual things the user has selected to import. These are |
|
360
|
|
|
* all optional, so each is just termed an import "option" here. |
|
361
|
|
|
* |
|
362
|
|
|
* @since 1.0.0 |
|
363
|
|
|
* |
|
364
|
|
|
* @param string $option An import option that has been selected. |
|
365
|
|
|
* @param string $component The component this option is for. |
|
366
|
|
|
* @param array $settings Other settings for this component. |
|
367
|
|
|
*/ |
|
368
|
|
|
protected function do_import_for_option( $option, $component, $settings ) { |
|
369
|
|
|
|
|
370
|
|
|
if ( ! isset( $this->components[ $component ][ $option ] ) ) { |
|
371
|
|
|
$this->feedback->warning( sprintf( __( 'Skipping unrecognized import option “%s”…', 'wordpoints-importer' ), $option ) ); |
|
372
|
|
|
return; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
$option_data = $this->components[ $component ][ $option ]; |
|
376
|
|
|
|
|
377
|
|
|
// Check if we can actually run this option. |
|
378
|
|
|
if ( isset( $option_data['can_import'] ) ) { |
|
379
|
|
|
|
|
380
|
|
|
$cant_import = call_user_func( $option_data['can_import'], $settings ); |
|
381
|
|
|
|
|
382
|
|
|
if ( is_wp_error( $cant_import ) ) { |
|
383
|
|
|
$this->feedback->warning( sprintf( __( 'Skipping importing %1$s. Reason: %2$s', 'wordpoints-importer' ), $option_data['label'], $cant_import->get_error_message() ) ); |
|
384
|
|
|
return; |
|
385
|
|
|
} |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
// OK, we can run the import method for this option. |
|
389
|
|
|
call_user_func( $option_data['function'], $settings ); |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
// EOF |
|
394
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.