-
Notifications
You must be signed in to change notification settings - Fork 0
/
cherry-data-importer.php
692 lines (545 loc) · 14.4 KB
/
cherry-data-importer.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
<?php
/*
Plugin Name: Cherry Data Importer
Plugin URI: http://www.templatemonster.com/wordpress-themes.php
Description: Import posts, pages, comments, custom fields, categories, tags and more from a WordPress export file.
Author: TemplateMonster
Author URI: http://www.templatemonster.com/wordpress-themes.php
Version: 1.1.2
Text Domain: cherry-data-importer
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/**
* Main importer plugin class
*
* @package Cherry_Data_Importer
* @author Cherry Team
* @license GPL-2.0+
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'Cherry_Data_Importer' ) ) {
/**
* Define Cherry_Data_Importer class
*/
class Cherry_Data_Importer {
/**
* A reference to an instance of this class.
*
* @since 1.0.0
* @var object
*/
private static $instance = null;
/**
* Holder for importer object instance.
*
* @var object
*/
private $importer = null;
/**
* Holder for importer object instance.
*
* @var object
*/
private $exporter = null;
/**
* Plugin base url
*
* @var string
*/
private $url = null;
/**
* Plugin base path
*
* @var string
*/
private $path = null;
/**
* Items number in single chunk
*
* @var integer
*/
private $chunk_size = 10;
/**
* Registered page tabs
* @var array
*/
private $page_tabs = array();
/**
* Menu page slug.
* @var string
*/
public $slug = 'cherry-demo-content';
/**
* Dispalying tab data
* @var array
*/
public $current_tab = array();
/**
* Constructor for the class
*/
function __construct() {
add_action( 'init', array( $this, 'start_session' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_assets' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ), 20 );
add_filter( 'upload_mimes', array( $this, 'allow_upload_xml' ) );
add_action( 'init', array( $this, 'init' ) );
add_action( 'admin_menu', array( $this, 'menu_page' ), 30 );
add_filter( 'tm_wizard_template_path', array( $this, 'wizard_success_page' ), 10, 2 );
add_filter( 'tm_wizard_notice_visibility', array( $this, 'wizard_notice_visibility' ) );
add_filter( 'cherry_plugin_wizard_template_path', array( $this, 'wizard_success_page' ), 10, 2 );
add_filter( 'cherry_plugin_wizard_notice_visibility', array( $this, 'wizard_notice_visibility' ) );
define( 'CHERRY_DEBUG', true );
}
/**
* Hide wizard notice on importer pages.
*
* @param bool $is_visible Default visibility value.
* @return bool
*/
public function wizard_notice_visibility( $is_visible ) {
if ( empty( $_GET['page'] ) || $this->slug !== $_GET['page'] ) {
return $is_visible;
}
return false;
}
/**
* Chenge wizard success page template
*
* @param string $file Template path.
* @param string $template Template name.
* @return string
*/
public function wizard_success_page( $file, $template ) {
if ( 'step-after-install.php' !== $template ) {
return $file;
}
if ( function_exists( 'tm_wizard_interface' ) && is_callable( array( tm_wizard_interface(), 'get_skin_data' ) ) ) {
return cdi()->path( 'templates/wizard-after-install.php' );
}
if ( function_exists( 'cherry_plugin_wizard_interface' ) && is_callable( array( cherry_plugin_wizard_interface(), 'get_skin_data' ) ) ) {
return cdi()->path( 'templates/wizard-after-install.php' );
}
return $file;
}
/**
* Init plugin
*
* @return void
*/
public function init() {
$this->set_default_settings();
$this->set_theme_settings();
if ( is_admin() ) {
require $this->path( 'includes/compat/manager.php' );
new Cherry_Data_Importer_Compat();
}
$this->load();
$this->load_import();
$this->load_export();
}
/**
* Register menu page
*/
public function menu_page() {
add_menu_page(
esc_html__( 'Demo Content ', 'cherry-data-importer' ),
esc_html__( 'Demo Content ', 'cherry-data-importer' ),
'manage_options',
$this->slug,
array( $this, 'render_plugin_page' ),
'dashicons-download',
76
);
foreach ( $this->get_page_tabs() as $tab ) {
if ( empty( $tab['id'] ) || empty( $tab['name'] ) ) {
continue;
}
add_submenu_page(
$this->slug,
esc_html__( 'Demo Content ', 'cherry-data-importer' ),
$tab['name'],
'manage_options',
sprintf( '%1$s&tab=%2$s', $this->slug, $tab['id'] ),
array( $this, 'render_plugin_page' )
);
}
remove_submenu_page( $this->slug, $this->slug );
}
/**
* Render plugin page html
*/
public function render_plugin_page() {
$this->get_template( 'page-header.php' );
$tabs = cdi()->get_page_tabs();
if ( ! $tabs ) {
return;
}
$menu = '';
$content = '';
$menu_format = '<li class="cdi-tabs__item"><a class="cdi-tabs__link%3$s" href="%1$s">%2$s</a></li>';
$content_format = '<div class="cdi-tabs_tab">%1$s</div>';
if ( empty( $_GET['tab'] ) ) {
$this->current_tab = $tabs[0];
$current_tab_id = $this->current_tab['id'];
} else {
$current_tab_id = esc_attr( $_GET['tab'] );
}
foreach ( $tabs as $tab ) {
$current = '';
if ( $current_tab_id === $tab['id'] ) {
$current = ' current-tab';
$this->current_tab = $tab;
if ( is_callable( $tab['cb'] ) ) {
$content = sprintf( $content_format, call_user_func( $tab['cb'] ) );
}
}
$menu .= sprintf( $menu_format, $this->get_tab_link( $tab['id'] ), $tab['name'], $current );
}
if ( apply_filters( 'cherry_data_importer_tabs_menu_visibility', true ) ) {
printf( '<ul class="cdi-tabs__menu">%s</ul>', $menu );
}
printf( '<div class="cdi-tabs__content">%s</div>', $content );
$this->get_template( 'page-footer.php' );
}
/**
* Return page tabs array
*
* @return array
*/
public function get_page_tabs() {
return $this->page_tabs;
}
/**
* Returns current tab URL
*
* @param string $tab Current tab ID.
* @return string
*/
public function get_tab_link( $tab = '' ) {
return add_query_arg(
array(
'page' => $this->slug,
'tab' => $tab,
),
esc_url( admin_url( 'admin.php' ) )
);
}
/**
* Register new tab for plugin page.
*
* @param array $tab Tab data to registrate.
* @return void
*/
public function register_tab( $tab = array() ) {
$tab = wp_parse_args( $tab, array(
'id' => 'tab',
'name' => null,
'cb' => null,
) );
$this->page_tabs[] = $tab;
}
/**
* Add XML to alowed MIME types to upload
*
* @param array $mimes Allowed MIME-types.
* @return array
*/
public function allow_upload_xml( $mimes ) {
$mimes = array_merge( $mimes, array( 'xml' => 'application/xml' ) );
return $mimes;
}
/**
* Run session
*
* @return void
*/
public function start_session() {
if ( ! session_id() ) {
session_start();
}
}
/**
* Get plugin template
*
* @param string $template Template name.
* @return void
*/
public function get_template( $template ) {
$file = locate_template( 'cherry-data-importer/' . $template );
if ( ! $file ) {
$file = $this->path( 'templates/' . $template );
}
if ( file_exists( $file ) ) {
include $file;
}
}
/**
* Load globally required files
*/
public function load() {
require $this->path( 'includes/class-cherry-data-importer-cache.php' );
require $this->path( 'includes/class-cherry-data-importer-logger.php' );
require $this->path( 'includes/class-cherry-data-importer-tools.php' );
require $this->path( 'includes/class-cherry-data-importer-slider.php' );
}
/**
* Set default importer settings
*
* @return void
*/
public function set_default_settings() {
include $this->path( 'includes/manifest/cherry-import-manifest.php' );
/**
* @var array $settings defined in manifest file
*/
$this->settings = apply_filters( 'cherry_data_manifest_settings', $settings );
}
/**
* Maybe rewrite settings from active theme
*
* @return void
*/
public function set_theme_settings() {
$manifest = locate_template( 'cherry-import-manifest.php' );
if ( ! $manifest ) {
return;
}
include $manifest;
if ( ! isset( $settings ) ) {
return;
}
$allowed_settings = array_keys( $this->settings );
foreach ( $allowed_settings as $type ) {
if ( ! empty( $settings[ $type ] ) ) {
$this->settings[ $type ] = wp_parse_args( $settings[ $type ], $this->settings[ $type ] );
}
}
}
/**
* Get setting by name
*
* @param array $keys Settings key to get.
* @return void
*/
public function get_setting( $keys = array() ) {
if ( empty( $keys ) || ! is_array( $keys ) ) {
return false;
}
$temp_result = $this->settings;
foreach ( $keys as $key ) {
if ( ! isset( $temp_result[ $key ] ) ) {
continue;
}
$temp_result = $temp_result[ $key ];
}
return $temp_result;
}
/**
* Include import files
*/
public function load_import() {
$this->load_wp_importer();
require $this->path( 'includes/import/class-cherry-data-importer-interface.php' );
cdi_interface();
}
/**
* Load export-related files
*
* @return void
*/
public function load_export() {
if ( ! is_admin() ) {
return;
}
require $this->path( 'includes/export/class-cherry-data-export-interface.php' );
}
/**
* Returns path to file or dir inside plugin folder
*
* @param string $path Path inside plugin dir.
* @return string
*/
public function path( $path = null ) {
if ( ! $this->path ) {
$this->path = trailingslashit( plugin_dir_path( __FILE__ ) );
}
return $this->path . $path;
}
/**
* Returns url to file or dir inside plugin folder
*
* @param string $path Path inside plugin dir.
* @return string
*/
public function url( $path = null ) {
if ( ! $this->url ) {
$this->url = trailingslashit( plugin_dir_url( __FILE__ ) );
}
return $this->url . $path;
}
/**
* Prepare assets URL depending from CHERRY_DEBUG value
*
* @param string $path Base file path.
* @return string
*/
public function assets_url( $path ) {
if ( defined( 'CHERRY_DEBUG' ) && true === CHERRY_DEBUG ) {
$path = str_replace( array( '..', '//' ), array( '.', '/' ), sprintf( $path, null ) );
} else {
$path = sprintf( $path, 'min' );
}
return $this->url( 'assets/' . $path );
}
/**
* Register plugin script and styles
*
* @return void
*/
public function register_assets() {
wp_register_style( 'cherry-data-import', $this->assets_url( 'css/cherry-data-import.css' ) );
wp_register_script(
'cherry-data-import',
$this->assets_url( 'js/%s/cherry-data-import.js' ),
array(),
'1.0.0',
true
);
wp_register_script(
'cherry-data-export',
$this->assets_url( 'js/%s/cherry-data-export.js' ),
array(),
'1.0.0',
true
);
wp_localize_script( 'cherry-data-import', 'CherryDataImportVars', array(
'nonce' => wp_create_nonce( 'cherry-data-import' ),
'autorun' => $this->import_autorun(),
'uploadTitle' => esc_html__( 'Select or upload file with demo content', 'cherry-data-importer' ),
'uploadBtn' => esc_html__( 'Select', 'cherry-data-importer' ),
'file' => ( isset( $_GET['file'] ) ) ? esc_attr( $_GET['file'] ) : false,
'tab' => cdi_interface()->slug,
'error' => esc_html__( 'Data processing error, please try again!', 'cherry-data-importer' ),
'advURLMask' => $this->page_url( array( 'tab' => 'import', 'step' => 2, 'file' => '<-file->' ) ),
) );
wp_localize_script( 'cherry-data-export', 'CherryDataExportVars', array(
'nonce' => wp_create_nonce( 'cherry-data-export' ),
) );
}
/**
* Generate import page URL.
*
* @param array $args Arguments array.
* @return string
*/
public function page_url( $args = array() ) {
$default = array(
'page' => $this->slug,
);
if ( ! empty( $_REQUEST['referrer'] ) ) {
$default['referrer'] = esc_attr( $_REQUEST['referrer'] );
}
if ( empty( $default['referrer'] ) && $this->get_server_ref() ) {
$default['referrer'] = $this->get_server_ref();
}
$args = array_merge( $default, $args );
return add_query_arg( $args, esc_url( admin_url( 'admin.php' ) ) );
}
/**
* Try to get referrer from server vars
*
* @return string|bool false
*/
public function get_server_ref() {
if ( empty( $_SERVER['HTTP_REFERER'] ) ) {
return false;
}
$parts = parse_url( $_SERVER['HTTP_REFERER'] );
if ( ! $parts || empty( $parts['query'] ) ) {
return false;
}
parse_str( $parts['query'], $query );
if ( empty( $query ) || empty( $query['referrer'] ) ) {
return false;
}
return esc_attr( $query['referrer'] );
}
/**
* Check if import autorun is allowed.
*
* @return boolean
*/
public function import_autorun() {
if ( isset( $_GET['type'] ) && 'replace' === $_GET['type'] ) {
return false;
}
if ( isset( $_GET['file'] ) ) {
return esc_attr( $_GET['file'] );
} else {
return false;
}
}
/**
* Enqueue globally required assets
*
* @return void
*/
public function enqueue_assets( $hook ) {
if ( isset( $_GET['page'] ) && $this->slug === $_GET['page'] ) {
wp_enqueue_style( 'cherry-data-import' );
wp_enqueue_media();
}
}
/**
* Loads default WordPress importer
*
* @return void
*/
public function load_wp_importer() {
if ( ! class_exists( 'WP_Importer' ) ) {
require ABSPATH . '/wp-admin/includes/class-wp-importer.php';
}
}
/**
* Return importer instance.
*
* @return object
*/
public function importer() {
return $this->importer;
}
/**
* Return exporter instance
*
* @return object
*/
public function exporter() {
return $this->exporter;
}
/**
* Returns the instance.
*
* @since 1.0.0
* @return object
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
}
}
/**
* Returns instance of Cherry_Data_Importer
*
* @return object
*/
function cdi() {
return Cherry_Data_Importer::get_instance();
}
cdi();