PluginProbe ʕ •ᴥ•ʔ
Advanced Import / 1.2.1
Advanced Import v1.2.1
trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 2.0.0
advanced-import / admin / class-advanced-import-admin.php
advanced-import / admin Last commit date
class-advanced-import-admin.php 6 years ago class-reset.php 6 years ago index.php 6 years ago
class-advanced-import-admin.php
2405 lines
1 <?php
2
3 /**
4 * The admin-specific functionality of the plugin.
5 *
6 * @link https://addonspress.com/
7 * @since 1.0.0
8 *
9 * @package Advanced_Import
10 * @subpackage Advanced_Import/admin
11 */
12
13 /**
14 * The admin-specific functionality of the plugin.
15 *
16 * Defines the plugin name, version, and two examples hooks for how to
17 * enqueue the admin-specific stylesheet and JavaScript.
18 *
19 * @package Advanced_Import
20 * @subpackage Advanced_Import/admin
21 * @author Addons Press <addonspress.com>
22 */
23 class Advanced_Import_Admin {
24
25 /**
26 * The Name of this plugin.
27 *
28 * @since 1.0.0
29 * @access private
30 * @var string $plugin_name The ID of this plugin.
31 */
32 private $plugin_name;
33
34 /**
35 * The version of this plugin.
36 *
37 * @since 1.0.0
38 * @access private
39 * @var string $version The current version of this plugin.
40 */
41 private $version;
42
43 /**
44 * The Current theme name
45 *
46 * @since 1.0.0
47 * @access public
48 * @var string $theme_name The Current theme name.
49 */
50 public $theme_name = '';
51
52 /**
53 * Current step
54 *
55 * @since 1.0.0
56 * @access protected
57 * @var string $step Current step.
58 */
59 protected $step = '';
60
61 /**
62 * Array of steps
63 *
64 * @since 1.0.0
65 * @access public
66 * @var array $steps Array of steps.
67 */
68 protected $steps = array();
69
70 /**
71 * Demo lists
72 *
73 * @since 1.0.0
74 * @access public
75 * @var array $demo_lists Array of demo lists.
76 */
77 protected $demo_lists = array();
78
79 /**
80 * Demo lists
81 *
82 * @since 1.0.0
83 * @access public
84 * @var array $demo_lists Array of demo lists.
85 */
86 protected $is_pro_active = false;
87
88 /**
89 * Array of delayed post for late process
90 *
91 * @since 1.0.0
92 * @access public
93 * @var array $delay_posts Array of delayed post for late process.
94 */
95 private $delay_posts = array();
96
97 /**
98 * Store logs and errors
99 *
100 * @since 1.0.0
101 * @access public
102 * @var array $logs Store logs and errors.
103 */
104 public $logs = array();
105
106
107 /**
108 * Store errors
109 *
110 * @since 1.0.0
111 * @access public
112 * @var array $errors Store errors.
113 */
114 public $errors = array();
115
116
117 /**
118 * current added Menu hook_suffix
119 *
120 * @since 1.0.0
121 * @access public
122 * @var array $logs Store logs and errors.
123 */
124 public $hook_suffix;
125
126 /**
127 * Slug of the import page
128 *
129 * @since 1.0.0
130 * @access public
131 * @var string $logs Store logs and errors.
132 */
133 private $current_template_type;
134
135 /**
136 * Slug of the import page
137 *
138 * @since 1.0.0
139 * @access public
140 * @var string $logs Store logs and errors.
141 */
142 private $current_template_url;
143
144 /**
145 * Initialize the class and set its properties.
146 *
147 * @since 1.0.0
148 */
149 public function __construct( ) {}
150
151 /**
152 * Main Advanced_Import_Admin Instance
153 * Initialize the class and set its properties.
154 *
155 * @since 1.0.0
156 * @return object $instance Advanced_Import_Admin Instance
157 */
158 public static function instance() {
159
160 // Store the instance locally to avoid private static replication
161 static $instance = null;
162
163 // Only run these methods if they haven't been ran previously
164 if ( null === $instance ) {
165 $instance = new Advanced_Import_Admin;
166 $instance->plugin_name = ADVANCED_IMPORT_PLUGIN_NAME;
167 $instance->version = ADVANCED_IMPORT_VERSION;
168
169 /*page slug using theme name */
170 $instance->theme_name = sanitize_key( get_option('template') );
171
172 }
173
174 // Always return the instance
175 return $instance;
176 }
177
178 /**
179 * Check if template is available to import
180 *
181 * @since 1.0.8
182 * @param array $item current array of demo list
183 * @return boolean
184 */
185 public function is_template_available( $item ){
186 $is_available = false;
187
188 /*if pro active everything is available*/
189 if( $this->is_pro_active ){
190 $is_available = true;
191 }
192 /*if is_pro not set the $item is available*/
193 elseif( !isset($item['is_pro'] ) ){
194 $is_available = true;/*template available since */
195 }
196 /*if is_pro not set but it is false, it will be free and avialable*/
197 elseif( isset( $item['is_pro'] ) && !$item['is_pro'] ){
198 $is_available = true;
199 }
200
201 return (boolean) apply_filters( 'advanced_import_is_template_available', $is_available );
202 }
203
204 /**
205 * Return Template Button
206 *
207 * @since 1.0.8
208 * @param array $item current array of demo list
209 * @return string
210 */
211 public function template_button( $item ){
212 /*Start buffering*/
213 ob_start();
214
215 if( $this->is_template_available($item)){
216 $plugins = isset( $item['plugins'] ) && is_array( $item['plugins'] )?' data-plugins="'.esc_attr(json_encode($item['plugins'])).'"':'';
217 ?>
218 <a class="button ai-demo-import ai-item-import is-button is-default is-primary is-large button-primary" href="#" aria-label="<?php esc_attr_e( 'Import', 'advanced-import' )?>" <?php echo $plugins;?>>
219 <span class="dashicons dashicons-download"></span><?php esc_html_e( 'Import', 'advanced-import' );?>
220 </a>
221 <?php
222 }
223 else{
224 ?>
225 <a class="button is-button is-default is-primary is-large button-primary"
226 href="<?php echo esc_url( isset($item['pro_url'])?$item['pro_url']:'#' );?>"
227 target="_blank"
228 aria-label="<?php esc_attr_e( 'View Pro', 'advanced-import' )?>">
229 <span class="dashicons dashicons-awards"></span><?php esc_html_e( 'View Pro', 'advanced-import' );?>
230 </a>
231 <?php
232 }
233 /*removes the buffer (without printing it), and returns its content.*/
234 $render_button = ob_get_clean();
235
236 $render_button = apply_filters( 'advanced_import_template_import_button', $render_button );
237 return $render_button;
238
239 }
240
241 /**
242 * Register the stylesheets for the admin area.
243 *
244 * @since 1.0.0
245 */
246 public function enqueue_styles( $hook_suffix ) {
247 if ( !is_array($this->hook_suffix) || !in_array( $hook_suffix, $this->hook_suffix )){
248 return;
249 }
250 wp_enqueue_style( $this->plugin_name, ADVANCED_IMPORT_URL . 'assets/css/advanced-import-admin' . ADVANCED_IMPORT_SCRIPT_PREFIX . '.css',array( 'wp-admin', 'dashicons' ), $this->version, 'all' );
251 wp_enqueue_media();
252 }
253
254 /**
255 * Register the JavaScript for the admin area.
256 *
257 * @since 1.0.0
258 */
259 public function enqueue_scripts( $hook_suffix ) {
260 if ( !is_array($this->hook_suffix) || !in_array( $hook_suffix, $this->hook_suffix )){
261 return;
262 }
263
264 //Isotope Js
265 wp_enqueue_script(
266 'isotope', // Handle.
267 ADVANCED_IMPORT_URL . '/assets/library/isotope/isotope.pkgd' . ADVANCED_IMPORT_SCRIPT_PREFIX . '.js',
268 array('jquery'), // Dependencies, defined above.
269 '3.0.6', // Version: File modification time.
270 true // Enqueue the script in the footer.
271 );
272
273 //Isotope Js
274 wp_enqueue_script(
275 'sweetalert2', // Handle.
276 ADVANCED_IMPORT_URL . '/assets/library/sweetalert2/sweetalert2.all' . ADVANCED_IMPORT_SCRIPT_PREFIX . '.js',
277 array('jquery'), // Dependencies, defined above.
278 '3.0.6', // Version: File modification time.
279 true // Enqueue the script in the footer.
280 );
281
282 wp_enqueue_script( $this->plugin_name, ADVANCED_IMPORT_URL . 'assets/js/advanced-import-admin' . ADVANCED_IMPORT_SCRIPT_PREFIX . '.js', array( 'jquery','masonry'), $this->version, true );
283 wp_localize_script( $this->plugin_name, 'advanced_import_object', array(
284 'ajaxurl' => admin_url( 'admin-ajax.php' ),
285 'wpnonce' => wp_create_nonce( 'advanced_import_nonce' ),
286 'text' => array(
287 'failed' => esc_html__('Failed','advanced-import'),
288 'error' => esc_html__('Error','advanced-import'),
289 'skip' => esc_html__( 'Skipping','advanced-import' ),
290 'confirmImport' => array(
291 'title' => esc_html__( 'Advanced Import! Just a step away','advanced-import' ),
292 'html' => sprintf(
293
294 __( 'Importing demo data is the easiest way to setup your theme. It will allow you to quickly edit everything instead of creating content from scratch. Also, read following points before importing the demo: %1$s %2$s %3$s %4$s', 'advanced-import' ),
295
296 '<ol><li class="warning">' . __( 'It is highly recommended to import demo on fresh WordPress installation to exactly replicate the theme demo. If no important data on your site, you can reset it from Reset Wizard at the top', 'advanced-import' ) . '</li>',
297
298 '<li>' . __( 'No existing posts, pages, categories, images, custom post types or any other data will be deleted or modified.', 'advanced-import' ) . '</li>',
299
300 '<li>' . __( 'It will install the plugins required for demo and activate them. Also posts, pages, images, widgets, & other data will get imported.', 'advanced-import' ) . '</li>',
301
302 '<li>' . __( 'Please click on the Import button and wait, it will take some time to import the data.', 'advanced-import' ) . '</li></ol>'
303
304 ),
305 'confirmButtonText' => esc_html__( 'Yes, Import Demo!','advanced-import' ),
306 'cancelButtonText' => esc_html__( 'Cancel','advanced-import' ),
307 ),
308 'confirmReset' => array(
309 'title' => esc_html__( 'Are you sure?','advanced-import' ),
310 'text' => __( "You won't be able to revert this!",'advanced-import' ),
311 'confirmButtonText' => esc_html__( 'Yes, Reset','advanced-import' ),
312 'cancelButtonText' => esc_html__( 'Cancel','advanced-import' ),
313 ),
314 'resetSuccess' => array(
315 'title' => esc_html__( 'Reset Successful','advanced-import' ),
316 'confirmButtonText' => esc_html__( 'Ok','advanced-import' ),
317 ),
318 'failedImport' => array(
319 'code' => __( "Error Code:",'advanced-import' ),
320 'text' => __( "Contact theme author or try again",'advanced-import' ),
321 ),
322 'successImport' => array(
323 'confirmButtonText' => esc_html__( 'Visit My Site','advanced-import' ),
324 'cancelButtonText' => esc_html__( 'Okay','advanced-import' ),
325 )
326 ),
327 ) );
328 }
329
330 /**
331 * @return array
332 */
333 public function mime_types( $mimes ) {
334 $add_mimes = array(
335 'json' => 'text/plain'
336 );
337
338 return array_merge( $mimes, $add_mimes );
339 }
340
341 /**
342 * Determine if the user already has theme content installed.
343 * @access public
344 */
345 public function is_possible_upgrade() {
346 return false;
347 }
348
349 /**
350 * Add admin menus
351 * @access public
352 */
353 public function import_menu() {
354 $this->hook_suffix[] = add_theme_page( esc_html__( 'Demo Import ','advanced-import' ), esc_html__( 'Demo Import' ), 'manage_options', 'advanced-import', array( $this, 'demo_import_screen' ) );
355 $this->hook_suffix[] = add_management_page( esc_html__( 'Advanced Import', 'advanced-import' ), esc_html__( 'Advanced Import', 'advanced-import' ), 'manage_options', 'advanced-import-tool', array( $this, 'demo_import_screen' ) );
356
357 }
358
359 /**
360 * Show the setup
361 * @access public
362 * @return void
363 */
364 public function demo_import_screen() {
365 do_action('advanced_import_before_demo_import_screen');
366
367 $this->step = isset( $_GET['step'] ) ? sanitize_key( $_GET['step'] ) : current( array_keys( $this->steps ) );
368
369
370 echo '<div class="ai-body">';
371
372 $this->get_header();
373
374 echo '<div class="ai-content">';
375 echo '<div class="ai-content-blocker hidden">';
376 echo '<div class="ai-notification-title"><p>'.esc_html__( 'Processing... Please do not refresh this page or do not go to other url!' ,'advanced-import').'</p></div>';
377 echo '<div id="ai-demo-popup"></div>';
378 echo '</div>';
379 $this->init_demo_import();
380 echo '</div>';
381 echo '</div>';/*ai-body*/
382 do_action('advanced_import_after_demo_import_screen');
383
384 }
385
386 /**
387 * get header of setup
388 * @access public
389 * @return void
390 */
391 public function get_header(){
392 global $pagenow;
393
394 $welcome_msg = "<div class='ai-header'>";
395 $welcome_msg .= "<h1>".sprintf( esc_html__( 'Welcome to the Advanced Import for %s.','advanced-import' ), wp_get_theme() )."</h1>";
396 if( $pagenow != 'tools.php' ){
397 $welcome_msg .= " <p>".sprintf( esc_html__( 'Thank you for choosing the %s theme. This quick demo import setup will help you configure your new website like theme demo. It will install the required WordPress plugins, default content and tell you a little about Help &amp; Support options. It should only take less than 5 minutes.','advanced-import' ), wp_get_theme() )."</p>";
398 }
399 $welcome_msg .= "</div>";
400 echo advanced_import_allowed_html( apply_filters('advanced_import_welcome_message', $welcome_msg ) );
401
402 if ( get_theme_mod( 'advanced_import_setup_complete', false ) && $pagenow != 'tools.php' ) {
403 ?>
404 <p><?php esc_html_e( 'It looks like you have already run the demo import for this theme.','advanced-import' ); ?></p>
405 <?php
406 }
407 }
408
409 /**
410 * Handle the demo content upload and called to process
411 * Ajax callback
412 *
413 * @return void
414 */
415 function upload_zip() {
416 if ( isset( $_FILES[ 'ai-upload-zip-archive' ][ 'name' ] ) && ! empty( $_FILES[ 'ai-upload-zip-archive' ][ 'name' ] ) ) {
417 /*check for security*/
418 if ( ! current_user_can( 'upload_files' ) ) {
419 wp_send_json_error(
420 array(
421 'message' => esc_html__( 'Sorry, you are not allowed to install demo on this site.','advanced-import' )
422 )
423 );
424 }
425 check_admin_referer( 'advanced-import' );
426
427 /*file process*/
428 require_once( ABSPATH . 'wp-admin/includes/file.php' );
429 WP_Filesystem();
430 global $wp_filesystem;
431 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP, true );
432 $upload_zip_archive = $_FILES[ 'ai-upload-zip-archive' ];
433 $unzipfile = unzip_file( $upload_zip_archive['tmp_name'], ADVANCED_IMPORT_TEMP );
434 if ( is_wp_error( $unzipfile ) ) {
435 wp_send_json_error(
436 array(
437 'message' => esc_html__( 'Error on unzipping, Please try again','advanced-import')
438 )
439 );
440 }
441 /*(check) Zip should contain json file and uploads folder only*/
442 $dirlist = $wp_filesystem->dirlist( ADVANCED_IMPORT_TEMP );
443 foreach ( (array) $dirlist as $filename => $fileinfo ) {
444 if( $fileinfo['type'] == 'd'){
445 if( $filename == 'uploads' ){
446 continue;
447 }
448 else{
449 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP, true );
450 wp_send_json_error(
451 array(
452 'message' => esc_html__( 'Invalid Zip File','advanced-import' )
453 )
454 );
455 }
456 }
457 else{
458 $filetype = wp_check_filetype($filename);
459 if( empty( $filetype['ext'] ) || $filetype['ext'] != 'json'){
460 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP, true );
461 wp_send_json_error(
462 array(
463 'message' => esc_html__( 'Invalid Zip File','advanced-import' )
464 )
465 );
466 }
467 }
468
469 }
470 wp_send_json_success(
471 array(
472 'message' => esc_html__( 'Success','advanced-import' )
473 )
474 );
475 }
476 wp_send_json_error(
477 array(
478 'message' => esc_html__( 'No Zip File Found','advanced-import' )
479 )
480 );
481 }
482
483 /**
484 * Download Zip file/ Move it to temp import folder
485 * Ajax callback
486 *
487 * @return mixed
488 */
489 function demo_download_and_unzip() {
490
491 /*check for security*/
492 if ( ! current_user_can( 'upload_files' ) ) {
493 wp_send_json_error(
494 array(
495 'message' => esc_html__( 'Sorry, you are not allowed to install demo on this site.','advanced-import' )
496 )
497 );
498 }
499 check_admin_referer( 'advanced-import' );
500
501 /*get file and what should do*/
502 $demo_file = is_array( $_POST['demo_file'] )? (array) $_POST['demo_file'] : sanitize_text_field( $_POST['demo_file'] );
503 $demo_file_type = sanitize_text_field( $_POST['demo_file_type'] );
504
505 /*from file*/
506 if( $demo_file_type == 'file' ){
507 require_once( ABSPATH . 'wp-admin/includes/file.php' );
508 WP_Filesystem();
509 global $wp_filesystem;
510 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP, true );
511 $unzipfile = unzip_file( $demo_file, ADVANCED_IMPORT_TEMP );
512 if ( is_wp_error( $unzipfile ) ) {
513 wp_send_json_error(
514 array(
515 'message' => esc_html__( 'Error on unzipping, Please try again','advanced-import')
516 )
517 );
518 }
519 /*(check) Zip should contain json file and uploads folder only*/
520 $dirlist = $wp_filesystem->dirlist( ADVANCED_IMPORT_TEMP );
521 foreach ( (array) $dirlist as $filename => $fileinfo ) {
522 if( $fileinfo['type'] == 'd'){
523 if( $filename == 'uploads' ){
524 continue;
525 }
526 else{
527 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP, true );
528 wp_send_json_error(
529 array(
530 'message' => esc_html__( 'Invalid Zip File','advanced-import' )
531 )
532 );
533 }
534 }
535 else{
536 $filetype = wp_check_filetype($filename);
537 if( empty( $filetype['ext'] ) || $filetype['ext'] != 'json'){
538 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP, true );
539 wp_send_json_error(
540 array(
541 'message' => esc_html__( 'Invalid Zip File','advanced-import' )
542 )
543 );
544 }
545 }
546
547 }
548 wp_send_json_success(
549 array(
550 'message' => esc_html__( 'Success','advanced-import' )
551 )
552 );
553 }
554 elseif ( $demo_file_type == 'url' ){
555
556 /*finally fetch the file from remote*/
557 $response = wp_remote_get( $demo_file );
558
559 if ( is_array( $response ) && ! empty( $response['body'] ) && $response['response']['code'] == '200' ) {
560 require_once( ABSPATH . 'wp-admin/includes/file.php' );
561 WP_Filesystem();
562 global $wp_filesystem;
563 $file_permission = 0777;
564 if ( !file_exists( ADVANCED_IMPORT_TEMP_ZIP ) ) {
565 $wp_filesystem->mkdir(ADVANCED_IMPORT_TEMP_ZIP, $file_permission, true);
566 }
567 $temp_import_zip = trailingslashit(ADVANCED_IMPORT_TEMP_ZIP ). "temp-import.zip";
568 $wp_filesystem->put_contents( $temp_import_zip, $response['body'],0777 );
569
570 }
571 else {
572 /*required to download file failed.*/
573 wp_send_json_error(
574 array(
575 'error' => 1,
576 'message' => esc_html__( 'Remote server did not respond, Contact to Theme Author','advanced-import' )
577 )
578 );
579 }
580
581 $file_size = filesize( $temp_import_zip );
582
583 /*if file size is 0*/
584 if ( 0 == $file_size ) {
585 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP_ZIP, true );
586 wp_send_json_error(
587 array(
588 'error' => 1,
589 'message' => esc_html__( 'Zero size file downloaded, Contact to Theme Author','advanced-import' )
590 )
591 );
592 }
593
594 /*if file is too large*/
595 if ( ! empty( $max_size ) && $file_size > $max_size ) {
596 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP_ZIP, true );
597 wp_send_json_error(
598 array(
599 'error' => 1,
600 'message' => sprintf( esc_html__( 'Remote file is too large, limit is %s','advanced-import' ), size_format( $max_size ) )
601 )
602 );
603 }
604
605 /*if we are here then unzip file*/
606 $unzipfile = unzip_file( $temp_import_zip, ADVANCED_IMPORT_TEMP );
607 if ( is_wp_error( $unzipfile ) ) {
608 wp_send_json_error(
609 array(
610 'error' => 1,
611 'message' => esc_html__( 'Error on unzipping, Please try again','advanced-import')
612 )
613 );
614 }
615 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP_ZIP, true );
616
617 /*(check) Zip should contain json file and uploads folder only*/
618 $dirlist = $wp_filesystem->dirlist( ADVANCED_IMPORT_TEMP );
619 foreach ( (array) $dirlist as $filename => $fileinfo ) {
620 if( $fileinfo['type'] == 'd'){
621 if( $filename == 'uploads' ){
622 continue;
623 }
624 else{
625 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP, true );
626 wp_send_json_error(
627 array(
628 'message' => esc_html__( 'Invalid Zip File','advanced-import' )
629 )
630 );
631 }
632 }
633 else{
634 $filetype = wp_check_filetype($filename);
635 if( empty( $filetype['ext'] ) || $filetype['ext'] != 'json'){
636 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP, true );
637 wp_send_json_error(
638 array(
639 'message' => esc_html__( 'Invalid Zip File','advanced-import' )
640 )
641 );
642 }
643 }
644
645 }
646 wp_send_json_success(
647 array(
648 'message' => esc_html__( 'Success','advanced-import' )
649 )
650 );
651
652 }
653 else{
654 wp_send_json_error(
655 array(
656 'error' => 1,
657 'message' => esc_html__( 'File not allowed','advanced-import' )
658 )
659 );
660 }
661 }
662
663 /**
664 * List Demo List
665 *
666 * @return void
667 */
668 public function demo_list( $demo_lists, $total_demo ){
669 ?>
670 <div class="ai-filter-header">
671 <div class="ai-filter-tabs">
672 <ul class="ai-types ai-filter-group" data-filter-group="secondary">
673 <li class="ai-filter-btn-active ai-filter-btn" data-filter="*">
674 <?php esc_html_e( 'All', 'advanced-import' ); ?>
675 <span class="ai-count"></span>
676 </li>
677 <?php
678 $types = array_column($demo_lists, 'type');
679 $unique_types = array_unique( $types );
680 foreach ( $unique_types as $cat_index => $single_type ){
681 ?>
682 <li class="ai-filter-btn" data-filter=".<?php echo strtolower( esc_attr($single_type) );?>">
683 <?php echo ucfirst( esc_html($single_type) );?>
684 <span class="ai-count"></span>
685
686 </li>
687 <?php
688 }
689 ?>
690 </ul>
691 <div class="ai-search-control">
692 <input id="ai-filter-search-input" class="ai-search-filter" type="text" placeholder="<?php esc_attr_e('Search...','advanced-import')?>">
693 </div>
694 <ul class="ai-form-type">
695 <li class="ai-form-file-import">
696 <?php esc_html_e( 'Upload zip', 'advanced-import' ); ?>
697 </li>
698 </ul>
699 </div>
700 </div>
701 <div class="ai-filter-content" id="ai-filter-content">
702 <div class="ai-actions ai-sidebar">
703 <div class="ai-import-available-categories">
704 <h3><?php esc_html_e( 'Categories', 'advanced-import' ); ?></h3>
705 <ul class="ai-import-available-categories-lists ai-filter-group" data-filter-group="primary">
706 <li class="ai-filter-btn-active ai-filter-btn" data-filter="*">
707 <?php esc_html_e( 'All Categories', 'advanced-import' ); ?>
708 <span class="ai-count"></span>
709 </li>
710 <?php
711 $categories = array_column($demo_lists, 'categories');
712 $unique_categories = array();
713 if( is_array( $categories) && !empty( $categories )){
714 foreach ( $categories as $demo_index => $demo_cats ){
715 foreach ( $demo_cats as $cat_index => $single_cat ){
716 if (in_array($single_cat, $unique_categories)){
717 continue;
718 }
719 $unique_categories[] = $single_cat;
720 ?>
721 <li class="ai-filter-btn" data-filter=".<?php echo strtolower( esc_attr($single_cat) );?>">
722 <?php echo ucfirst( esc_html($single_cat) );?>
723 <span class="ai-count"></span>
724 </li>
725 <?php
726 }
727 }
728 }
729 ?>
730 </ul>
731 </div>
732
733 </div>
734 <div class="ai-filter-content-wrapper">
735 <?php
736 foreach ( $demo_lists as $key => $demo_list ){
737
738 /*Check for required fields*/
739 if( !isset( $demo_list['title'] ) || !isset( $demo_list['screenshot_url'] ) || !isset( $demo_list['demo_url']) ){
740 continue;
741 }
742
743 $template_url = isset( $demo_list['template_url'] )?$demo_list['template_url']:'';
744 if( is_array( $template_url )){
745 $data_template = 'data-template_url="'.esc_attr(json_encode($template_url)).'"';
746 $data_template_type = 'data-template_type="array"';
747 }
748 elseif ($template_url){
749 $data_template = 'data-template_url="'.esc_attr($template_url).'"';
750 if ( is_file( $template_url ) && filesize( $template_url ) > 0 ) {
751 $data_template_type = 'data-template_type="file"';
752 }
753 else{
754 $data_template_type = 'data-template_type="url"';
755 }
756 }
757 else{
758 $data_template = 'data-template_url="'.esc_attr(json_encode($template_url)).'"';
759 $data_template_type = 'data-template_type="array"';
760 }
761 ?>
762 <div aria-label="<?php echo esc_attr( $demo_list['title'])?>"
763 class="ai-item <?php
764 echo isset( $demo_list['categories'] )?esc_attr(implode(" ",$demo_list['categories'])):'';
765 echo isset( $demo_list['type'] )?' '.esc_attr($demo_list['type']):'';
766 echo $this->is_template_available($demo_list)?'':' ai-pro-item'?>"
767 <?php echo $this->is_template_available($demo_list)?$data_template.' '.$data_template_type:'';?>
768 >
769 <?php
770 wp_nonce_field( 'advanced-import' );
771 ?>
772 <div class="ai-item-preview">
773 <div class="ai-item-screenshot">
774 <img src="<?php echo esc_url( $demo_list['screenshot_url'])?>">
775
776 </div>
777 <h4 class="ai-author-info"><?php esc_html_e( 'Author: ', 'advanced-import' ) ?><?php echo esc_html( isset($demo_list['author'])?$demo_list['author']:wp_get_theme()->get( 'Author' ))?></h4>
778 <div class="ai-details"><?php esc_html_e( 'Details', 'advanced-import' ) ?></div>
779 <?php
780 if( isset( $demo_list['is_pro'] ) && $demo_list['is_pro'] ){
781 ?>
782 <span class="ai-premium-label"><?php esc_html_e( 'Premium', 'advanced-import' ) ?></span>
783 <?php
784 }
785 ?>
786 </div>
787 <div class="ai-item-footer">
788 <div class="ai-item-footer_meta">
789 <h3 class="theme-name"><?php echo esc_html( $demo_list['title'])?></h3>
790 <div class="ai-item-footer-actions">
791 <a class="button ai-item-demo-link" href="<?php echo esc_url( $demo_list['demo_url'])?>" target="_blank">
792 <span class="dashicons dashicons-visibility"></span><?php esc_html_e( 'Preview', 'advanced-import' );?>
793 </a>
794 <?php
795 echo $this->template_button( $demo_list );
796 ?>
797 </div>
798 <?php
799 $keywords = isset( $demo_list['keywords'] )? $demo_list['keywords']:array();
800 If( !empty( $keywords )){
801 echo '<ul class="ai-keywords hidden">';
802 foreach ( $keywords as $cat_index => $single_keywords ){
803 ?>
804 <li class="<?php echo strtolower( esc_attr($single_keywords) );?>"><?php echo ucfirst( esc_html($single_keywords) );?></li>
805 <?php
806 }
807 echo '</ul>';
808 }
809 ?>
810
811 </div>
812
813 </div>
814 </div>
815 <?php
816 }
817 ?>
818 </div>
819 </div>
820 <?php
821 }
822 /**
823 * List Demo Form
824 *
825 * @return void
826 */
827
828 public function demo_import_form( $total_demo = 0 ){
829 ?>
830 <div class="ai-form <?php echo $total_demo > 0?'hidden':''?>">
831 <form action="" method="post" enctype="multipart/form-data" id="ai-upload-zip-form">
832 <h3 class="media-title"><?php esc_html_e( 'Upload a zip file containing demo content', 'advanced-import' ); ?> </h3>
833 <div class="input-file"><input type="file" name="ai-upload-zip-archive" id="ai-upload-zip-archive" size="50" /></div>
834 <p>
835 <?php
836 wp_nonce_field( 'advanced-import' );
837 printf( __( 'Maximum upload file size: %s','advanced-import' ), size_format( wp_max_upload_size() ) );
838 ?>
839 </p>
840 <div id='ai-empty-file' class="error hidden">
841 <p>
842 <?php esc_html_e('Select File and Try Again!','advanced-import')?>
843 </p>
844 </div>
845 <p class="ai-form-import-actions step">
846 <button class="button-primary button button-large button-upload-demo" type="submit">
847 <?php esc_html_e( 'Upload Demo Zip','advanced-import' ); ?>
848 </button>
849 <a href="<?php echo esc_url( wp_get_referer() && ! strpos( wp_get_referer(), 'update.php' ) ? wp_get_referer() : admin_url( '' ) ); ?>" class="button button-large">
850 <?php esc_html_e( 'Not right now','advanced-import' ); ?>
851 </a>
852 </p>
853 <div id='ai-ajax-install-result'></div>
854 </form>
855 </div>
856 <?php
857 }
858
859 /**
860 * 1st step of demo import view
861 * Upload Zip file
862 * Demo List
863 */
864 public function init_demo_import(){
865
866 global $pagenow;
867 $total_demo = 0;
868 if( $pagenow != 'tools.php' ){
869 $this->demo_lists = apply_filters( 'advanced_import_demo_lists',array() );
870 $this->is_pro_active = apply_filters( 'advanced_import_is_pro_active', $this->is_pro_active);
871 $demo_lists = $this->demo_lists;
872
873 $total_demo = count( $demo_lists );
874 if( $total_demo >= 1 ){
875 $this->demo_list( $demo_lists, $total_demo );
876 }
877 }
878
879 $this->demo_import_form($total_demo);
880 }
881
882 /**
883 * 2nd step Plugin Installation step View
884 * return void || boolean
885 */
886 public function plugin_screen() {
887
888 /*check for security*/
889 if ( ! current_user_can( 'upload_files' ) ) {
890 wp_send_json_error(
891 array(
892 'message' => esc_html__( 'Sorry, you are not allowed to install demo on this site.','advanced-import' )
893 )
894 );
895 }
896
897 check_admin_referer( 'advanced-import' );
898
899 /*delete_transient(); for safety*/
900 delete_transient('content.json');
901 delete_transient('widgets.json');
902 delete_transient('options.json');
903
904 do_action('advanced_import_before_plugin_screen');
905 ?>
906 <div class="ai-notification-title">
907 <p><?php esc_html_e( 'Your website needs a few essential plugins. We are installing them...','advanced-import' ); ?></p>
908 </div>
909 <ul class="ai-plugins-wrap hidden">
910 <?php
911 $recommended_plugins = (array) $_POST['recommendedPlugins'];
912 if ( count( $recommended_plugins ) ) {
913 foreach ( $recommended_plugins as $index => $recommended_plugin ) { ?>
914 <li data-slug="<?php echo esc_attr( $recommended_plugin['slug'] ); ?>" data-main_file ="<?php echo esc_attr( isset($recommended_plugin['main_file'])?$recommended_plugin['main_file']: $recommended_plugin['slug'].'.php'); ?>">
915 <?php echo esc_html( $recommended_plugin['name'] ); ?>
916 </li>
917 <?php
918 }
919 }
920 else{
921 ?>
922 <li id="ai-no-recommended-plugins"><?php esc_html_e( 'No Recommended Plugins','advanced-import' ); ?></li>
923 <?php
924 }
925 ?>
926 </ul>
927 <?php
928 do_action('advanced_import_after_plugin_screen');
929 exit;
930 }
931
932 /**
933 * 3rd steps helper functions
934 * Get json from json file
935 *
936 * @param string $file
937 * @return mixed
938 */
939 public function get_json_data_from_file( $file ) {
940
941 if( get_transient ($file)){
942 return get_transient ($file);
943 }
944
945 If( $this->current_template_type == 'array'){
946 $type = strtok($file, '.');
947 if( isset( $this->current_template_url[$type])){
948 $request = wp_remote_get($this->current_template_url[$type]);
949 $response = wp_remote_retrieve_body( $request );
950 $result = json_decode( $response, true );
951 set_transient($file, $result, 1000 );
952 return $result;
953 }
954 return array();
955 }
956
957 if ( is_file( ADVANCED_IMPORT_TEMP . basename( $file ) ) ) {
958 require_once( ABSPATH . 'wp-admin/includes/file.php' );
959 WP_Filesystem();
960 global $wp_filesystem;
961 $file_name = ADVANCED_IMPORT_TEMP . basename( $file );
962 if ( file_exists( $file_name ) ) {
963 $result = json_decode( $wp_filesystem->get_contents( $file_name ), true );
964 set_transient($file, $result, 1000 );
965 return $result;
966 }
967 }
968 return array();
969 }
970
971 public function get_main_content_json( ) {
972 return $this->get_json_data_from_file( 'content.json' );
973 }
974 public function get_widgets_json(){
975 return $this->get_json_data_from_file( 'widgets.json' );
976 }
977
978 public function get_theme_options_json( ) {
979 return $this->get_json_data_from_file( 'options.json' );
980 }
981
982 /*
983 * return array
984 */
985 private function advanced_import_setup_content_steps() {
986
987 $content = array();
988
989 /*check if there is files*/
990 $content_data = $this->get_main_content_json();
991 foreach ( $content_data as $post_type => $post_data ) {
992 if ( count( $post_data ) ) {
993 $first = current( $post_data );
994 $post_type_title = ! empty( $first['type_title'] ) ? $first['type_title'] : ucwords( $post_type ) . 's';
995 $content[ $post_type ] = array(
996 'title' => $post_type_title,
997 'description' => sprintf( esc_html__( 'This will create default %s as seen in the demo.','advanced-import' ), $post_type_title ),
998 'pending' => esc_html__( 'Pending.','advanced-import'),
999 'installing' => esc_html__( 'Installing.','advanced-import' ),
1000 'success' => esc_html__( 'Success.','advanced-import' ),
1001 'install_callback' => array( $this, 'import_content_post_type_data' ),
1002 'checked' => $this->is_possible_upgrade() ? 0 : 1,
1003 // dont check if already have content installed.
1004 );
1005 }
1006 }
1007 /*check if there is files*/
1008 $widget_data = $this->get_widgets_json();
1009 if( !empty( $widget_data ) ){
1010 $content['widgets'] = array(
1011 'title' => esc_html__( 'Widgets','advanced-import' ),
1012 'description' => esc_html__( 'Insert default sidebar widgets as seen in the demo.','advanced-import' ),
1013 'pending' => esc_html__( 'Pending.','advanced-import' ),
1014 'installing' => esc_html__( 'Installing Default Widgets.','advanced-import' ),
1015 'success' => esc_html__( 'Success.' ,'advanced-import'),
1016 'install_callback' => array( $this, 'import_content_widgets_data' ),
1017 'checked' => $this->is_possible_upgrade() ? 0 : 1,
1018 // dont check if already have content installed.
1019 );
1020 }
1021 $options_data = $this->get_theme_options_json();
1022 if( !empty( $options_data ) ){
1023 $content['settings'] = array(
1024 'title' => esc_html__( 'Settings','advanced-import' ),
1025 'description' => esc_html__( 'Configure default settings.','advanced-import' ),
1026 'pending' => esc_html__( 'Pending.','advanced-import' ),
1027 'installing' => esc_html__( 'Installing Default Settings.' ,'advanced-import'),
1028 'success' => esc_html__( 'Success.','advanced-import' ),
1029 'install_callback' => array( $this, 'import_menu_and_options' ),
1030 'checked' => $this->is_possible_upgrade() ? 0 : 1,
1031 // dont check if already have content installed.
1032 );
1033 }
1034 $content = apply_filters( $this->theme_name . '_theme_view_setup_step_content', $content );
1035
1036 return $content;
1037
1038 }
1039
1040 /**
1041 * 3rd Step Step for content, widget, setting import
1042 * Page setup
1043 */
1044 public function content_screen() {
1045
1046 /*check for security*/
1047 if ( ! current_user_can( 'upload_files' ) ) {
1048 wp_send_json_error(
1049 array(
1050 'message' => esc_html__( 'Sorry, you are not allowed to install demo on this site.','advanced-import' )
1051 )
1052 );
1053 }
1054 check_admin_referer( 'advanced-import' );
1055
1056 $this->current_template_url = is_array( $_POST['template_url'] )? (array) $_POST['template_url'] : sanitize_text_field( $_POST['template_url'] );
1057 $this->current_template_type = sanitize_text_field( $_POST['template_type'] );
1058
1059 do_action('advanced_import_before_content_screen');
1060
1061 ?>
1062 <div class="ai-notification-title">
1063 <p>
1064 <?php
1065 esc_html_e( 'It\'s time to insert some demo content for your new WordPress Site. Once inserted, this content can be managed from the WordPress admin dashboard. ' )
1066 ?>
1067 </p>
1068 </div>
1069
1070 <table class="ai-pages hidden">
1071 <thead>
1072 <tr>
1073 <th class="check"></th>
1074 <th class="item"><?php esc_html_e( 'Item','advanced-import'); ?></th>
1075 <th class="description"><?php esc_html_e( 'Description','advanced-import' ); ?></th>
1076 <th class="status"><?php esc_html_e( 'Status','advanced-import' ); ?></th>
1077 </tr>
1078 </thead>
1079 <tbody>
1080 <?php
1081 $setup_content_steps = $this->advanced_import_setup_content_steps();
1082 foreach ( $setup_content_steps as $slug => $default ) { ?>
1083 <tr class="ai-available-content" data-content="<?php echo esc_attr( $slug ); ?>">
1084 <td>
1085 <input type="checkbox" name="import_content[<?php echo esc_attr( $slug ); ?>]" class="ai-available-content" id="import_content_<?php echo esc_attr( $slug ); ?>" value="1" <?php echo ( ! isset( $default['checked'] ) || $default['checked'] ) ? ' checked' : ''; ?>>
1086 </td>
1087 <td>
1088 <label for="import_content_<?php echo esc_attr( $slug ); ?>">
1089 <?php echo esc_html( $default['title'] ); ?>
1090 </label>
1091 </td>
1092 <td class="description">
1093 <?php echo esc_html( $default['description'] ); ?>
1094 </td>
1095 <td class="status">
1096 <span>
1097 <?php echo esc_html( $default['pending'] ); ?>
1098 </span>
1099 </td>
1100 </tr>
1101 <?php } ?>
1102 </tbody>
1103 </table>
1104 <?php
1105 do_action('advanced_import_after_content_screen');
1106
1107 exit;
1108 }
1109
1110 /*import ajax content
1111 * return JSON
1112 */
1113 public function import_content() {
1114
1115 /*check for security*/
1116 if ( ! current_user_can( 'upload_files' ) ) {
1117 wp_send_json_error(
1118 array(
1119 'message' => esc_html__( 'Sorry, you are not allowed to install demo on this site.','advanced-import' )
1120 )
1121 );
1122 }
1123
1124 $this->current_template_url = is_array( $_POST['template_url'] )? (array) $_POST['template_url'] : sanitize_text_field( $_POST['template_url'] );
1125 $this->current_template_type = sanitize_text_field ( $_POST['template_type'] );
1126
1127 $content_slug = isset( $_POST['content'] )? sanitize_title( $_POST['content'] ): '';
1128
1129 $content = $this->advanced_import_setup_content_steps();
1130
1131 /*check for security again*/
1132 if ( ! check_ajax_referer( 'advanced_import_nonce', 'wpnonce' ) || empty( $content_slug ) || !isset( $content[ $content_slug ] ) ) {
1133 wp_send_json_error(
1134 array(
1135 'error' => 1,
1136 'message' => esc_html__( 'No content Found','advanced-import' )
1137 )
1138 );
1139 }
1140
1141 $json = false;
1142 $this_content = $content[ $content_slug ];
1143
1144 if ( isset( $_POST['proceed'] ) ) {
1145
1146 /*install the content*/
1147 $this->log( esc_html__( 'Starting import for ','advanced-import') . $content_slug );
1148
1149 /*init delayed posts from transient.*/
1150 $this->delay_posts = get_transient( 'delayed_posts' );
1151 if ( ! is_array( $this->delay_posts ) ) {
1152 $this->delay_posts = array();
1153 }
1154
1155 if ( ! empty( $this_content['install_callback'] ) ) {
1156 /* install_callback includes following functions
1157 * import_content_post_type_data
1158 * import_content_widgets_data
1159 * import_menu_and_options
1160 * */
1161 if ( $result = call_user_func( $this_content['install_callback'] ) ) {
1162
1163 $this->log( esc_html__( 'Finish writing ','advanced-import' ) . count( $this->delay_posts, COUNT_RECURSIVE ) . esc_html__(' delayed posts to transient ','advanced-import') );
1164 set_transient( 'delayed_posts', $this->delay_posts, 60 * 60 * 24 );
1165
1166 /*if there is retry, retry it
1167 or finish it*/
1168 if ( is_array( $result ) && isset( $result['retry'] ) ) {
1169 /*we split the stuff up again.*/
1170 $json = array(
1171 'url' => admin_url( 'admin-ajax.php' ),
1172 'action' => 'import_content',
1173 'proceed' => 'true',
1174 'retry' => time(),
1175 'retry_count' => $result['retry_count'],
1176 'content' => $content_slug,
1177 '_wpnonce' => wp_create_nonce( 'advanced_import_nonce' ),
1178 'message' => $this_content['installing'],
1179 'logs' => $this->logs,
1180 'errors' => $this->errors,
1181 'template_url' => $this->current_template_url,
1182 'template_type' => $this->current_template_type,
1183
1184 );
1185 }
1186 else {
1187 $json = array(
1188 'done' => 1,
1189 'message' => $this_content['success'],
1190 'debug' => $result,
1191 'logs' => $this->logs,
1192 'errors' => $this->errors,
1193 );
1194 }
1195 }
1196 }
1197 }
1198 else {
1199
1200 $json = array(
1201 'url' => admin_url( 'admin-ajax.php' ),
1202 'action' => 'import_content',
1203 'proceed' => 'true',
1204 'content' => $content_slug,
1205 '_wpnonce' => wp_create_nonce( 'advanced_import_nonce' ),
1206 'message' => $this_content['installing'],
1207 'logs' => $this->logs,
1208 'errors' => $this->errors,
1209 'template_url' => $this->current_template_url,
1210 'template_type' => $this->current_template_type,
1211 );
1212 }
1213
1214 if ( $json ) {
1215 $json['hash'] = md5( serialize( $json ) ); /*used for checking if duplicates happen, move to next plugin*/
1216 wp_send_json( $json );
1217 }
1218 else {
1219 wp_send_json_error( array(
1220 'message' => esc_html__( 'Error','advanced-import' ),
1221 'logs' => $this->logs,
1222 'errors' => $this->errors,
1223 ) );
1224 }
1225 exit;
1226 }
1227
1228 /* callback function to importing post type
1229 * all post type is imported from here
1230 * return mix
1231 * */
1232 private function import_content_post_type_data() {
1233 $post_type = ! empty( $_POST['content'] ) ? sanitize_text_field( $_POST['content'] ) : false;
1234 $all_data = $this->get_main_content_json();
1235 if ( ! $post_type || ! isset( $all_data[ $post_type ] ) ) {
1236 return false;
1237 }
1238
1239 /*Import 10 posts at a time*/
1240 $limit = 10 + ( isset( $_REQUEST['retry_count'] ) ? (int) $_REQUEST['retry_count'] : 0 );
1241
1242 $limit = apply_filters('advanced_import_limit_at_time', $limit );
1243 $x = 0;
1244 foreach ( $all_data[ $post_type ] as $post_data ) {
1245
1246 $this->process_import_single_post( $post_type, $post_data );
1247
1248 if ( $x ++ > $limit ) {
1249 return array(
1250 'retry' => 1,
1251 'retry_count' => $limit
1252 );
1253 }
1254 }
1255
1256 /*processed delayed posts*/
1257 $this->process_delayed_posts();
1258
1259 /*process child posts*/
1260 $this->processpost_orphans();
1261
1262 return true;
1263
1264 }
1265
1266 /*set and get transient imported_term_ids
1267 return mix*/
1268 private function imported_term_id( $original_term_id, $new_term_id = false ) {
1269 $terms = get_transient( 'imported_term_ids' );
1270 if ( ! is_array( $terms ) ) {
1271 $terms = array();
1272 }
1273 if ( $new_term_id ) {
1274 if ( ! isset( $terms[ $original_term_id ] ) ) {
1275 $this->log( esc_html__('Insert old TERM ID ','advanced-import') . $original_term_id .esc_html__(' as new TERM ID: ' ,'advanced-import'). $new_term_id );
1276 }
1277 else if ( $terms[ $original_term_id ] != $new_term_id ) {
1278 $this->error( 'Replacement OLD TERM ID ' . $original_term_id . ' overwritten by new TERM ID: ' . $new_term_id );
1279 }
1280 $terms[ $original_term_id ] = $new_term_id;
1281 set_transient( 'imported_term_ids', $terms, 60 * 60 * 24 );
1282 }
1283 else if ( $original_term_id && isset( $terms[ $original_term_id ] ) ) {
1284 return $terms[ $original_term_id ];
1285 }
1286
1287 return false;
1288 }
1289
1290 /*set and get imported_post_ids
1291 return mix*/
1292 private function imported_post_id( $original_id = false, $new_id = false ) {
1293 if ( is_array( $original_id ) || is_object( $original_id ) ) {
1294 return false;
1295 }
1296 $post_ids = get_transient( 'imported_post_ids' );
1297 if ( ! is_array( $post_ids ) ) {
1298 $post_ids = array();
1299 }
1300 if ( $new_id ) {
1301 if ( ! isset( $post_ids[ $original_id ] ) ) {
1302 $this->log( esc_html__('Insert old ID ','advanced-import') . $original_id . esc_html__(' as new ID: ','advanced-import') . $new_id );
1303 }
1304 else if ( $post_ids[ $original_id ] != $new_id ) {
1305 $this->error( esc_html__('Replacement OLD ID ','advanced-import'). $original_id . ' overwritten by new ID: ' . $new_id );
1306 }
1307 $post_ids[ $original_id ] = $new_id;
1308 set_transient( 'imported_post_ids', $post_ids, 60 * 60 * 24 );
1309 }
1310 else if ( $original_id && isset( $post_ids[ $original_id ] ) ) {
1311 return $post_ids[ $original_id ];
1312 }
1313 else if ( $original_id === false ) {
1314 return $post_ids;
1315 }
1316 return false;
1317 }
1318
1319 /*set and get post_orphans/post parent
1320 if parent is not already imported the child will be orphan
1321 return mix*/
1322 private function post_orphans( $original_id = false, $missing_parent_id = false ) {
1323 $post_ids = get_transient( 'post_orphans' );
1324 if ( ! is_array( $post_ids ) ) {
1325 $post_ids = array();
1326 }
1327 if ( $missing_parent_id ) {
1328 $post_ids[ $original_id ] = $missing_parent_id;
1329 set_transient( 'post_orphans', $post_ids, 60 * 60 * 24 );
1330 }
1331 else if ( $original_id && isset( $post_ids[ $original_id ] ) ) {
1332 return $post_ids[ $original_id ];
1333 }
1334 else if ( $original_id === false ) {
1335 return $post_ids;
1336 }
1337 return false;
1338 }
1339
1340
1341 /*set delayed post for later process*/
1342 private function delay_post_process( $post_type, $post_data ) {
1343 if ( ! isset( $this->delay_posts[$post_type] ) ) {
1344 $this->delay_posts[ $post_type ] = array();
1345 }
1346 $this->delay_posts[ $post_type ][ $post_data['post_id'] ] = $post_data;
1347 }
1348
1349
1350 /*return the difference in length between two strings
1351 https://www.w3schools.com/php/func_array_uksort.asp
1352 used in method parse_content_for_shortcode_image_etc
1353 */
1354 public function uksort_strlen( $a, $b ) {
1355 return strlen( $b ) - strlen( $a );
1356 }
1357
1358 /*Important Function
1359 Import single Post/Content
1360 */
1361 private function process_import_single_post( $post_type, $post_data, $delayed = 0 ) {
1362
1363 $this->log( esc_html__('Processing ','advanced-import' ) .$post_type.' '.$post_data['post_id'] );
1364 $original_post_data = $post_data;
1365
1366 /*if there is not post type return false*/
1367 if ( ! post_type_exists( $post_type ) ) {
1368 return false;
1369 }
1370
1371 /*if it is aready imported return*/
1372 if ( $this->imported_post_id( $post_data['post_id'] ) ) {
1373 return true; /*already done*/
1374 }
1375
1376 /*set post_name id for empty post name/title*/
1377 if ( empty( $post_data['post_title'] ) && empty( $post_data['post_name'] ) ) {
1378 $post_data['post_name'] = $post_data['post_id'];
1379 }
1380
1381 /*set post_type on $post_data*/
1382 $post_data['post_type'] = $post_type;
1383
1384 /*post_orphans/post parent management */
1385 $post_parent = isset( $post_data['post_parent'] )?absint( $post_data['post_parent'] ):false;
1386 if ( $post_parent ) {
1387 /*if we already know the parent, map it to the new local imported ID*/
1388 if ( $this->imported_post_id( $post_parent ) ) {
1389 $post_data['post_parent'] = $this->imported_post_id( $post_parent );
1390 }
1391 else {
1392 /*if there is not parent imported, child will be orphans*/
1393 $this->post_orphans( absint( $post_data['post_id'] ), $post_parent );
1394 $post_data['post_parent'] = 0;
1395 }
1396 }
1397
1398 /*check if already exists by post_name*/
1399 if ( empty( $post_data['post_title'] ) && ! empty( $post_data['post_name'] ) ) {
1400 global $wpdb;
1401 $sql = "
1402 SELECT ID, post_name, post_parent, post_type
1403 FROM $wpdb->posts
1404 WHERE post_name = %s
1405 AND post_type = %s
1406 ";
1407 $pages = $wpdb->get_results( $wpdb->prepare( $sql, array(
1408 $post_data['post_name'],
1409 $post_type,
1410 ) ), OBJECT_K );
1411
1412 $foundid = 0;
1413 foreach ( (array) $pages as $page ) {
1414 if ( $page->post_name == $post_data['post_name'] && empty( $page->post_title ) ) {
1415 $foundid = $page->ID;
1416 }
1417 }
1418
1419 /*if we have found id by post_name, imported_post_id and return*/
1420 if ( $foundid ) {
1421 $this->imported_post_id( $post_data['post_id'], $foundid );
1422 return true;
1423 }
1424 }
1425
1426 /*check if already exists by post_name and post_title*/
1427 /*don't use post_exists because it will dupe up on media with same name but different slug*/
1428 if ( ! empty( $post_data['post_title'] ) && ! empty( $post_data['post_name'] ) ) {
1429 global $wpdb;
1430 $sql = "
1431 SELECT ID, post_name, post_parent, post_type
1432 FROM $wpdb->posts
1433 WHERE post_name = %s
1434 AND post_title = %s
1435 AND post_type = %s
1436 ";
1437 $pages = $wpdb->get_results( $wpdb->prepare( $sql, array(
1438 $post_data['post_name'],
1439 $post_data['post_title'],
1440 $post_type,
1441 ) ), OBJECT_K );
1442
1443 $foundid = 0;
1444 foreach ( (array) $pages as $page ) {
1445 if ( $page->post_name == $post_data['post_name'] ) {
1446 $foundid = $page->ID;
1447 }
1448 }
1449
1450 /*if we have found id by post_name and post_title, imported_post_id and return*/
1451 if ( $foundid ) {
1452 $this->imported_post_id( $post_data['post_id'], $foundid );
1453 return true;
1454 }
1455 }
1456
1457 /*
1458 * todo it may not required
1459 * backwards compat with old import format.*/
1460 if ( isset( $post_data['meta'] ) ) {
1461 foreach ( $post_data['meta'] as $key => $meta ) {
1462 if(is_array($meta) && count($meta) == 1){
1463 $single_meta = current($meta);
1464 if(!is_array($single_meta)){
1465 $post_data['meta'][$key] = $single_meta;
1466 }
1467 }
1468 }
1469 }
1470
1471 /*finally process*/
1472 switch ( $post_type ) {
1473
1474 /*case attachment*/
1475 case 'attachment':
1476
1477 /*import media via url*/
1478 if ( isset( $post_data['guid'] ) && ! empty( $post_data['guid'] ) ) {
1479
1480 /*check if this has already been imported.*/
1481 $old_guid = $post_data['guid'];
1482 if ( $this->imported_post_id( $old_guid ) ) {
1483 return true; /*already done*/
1484 }
1485
1486 // ignore post parent, we haven't imported those yet.
1487 // $file_data = wp_remote_get($post_data['guid']);
1488 $remote_url = $post_data['guid'];
1489
1490 $post_data['upload_date'] = date( 'Y/m', strtotime( $post_data['post_date_gmt'] ) );
1491
1492 if ( isset( $post_data['meta'] ) ) {
1493 foreach ( $post_data['meta'] as $key => $meta ) {
1494 if ( $key == '_wp_attached_file' ) {
1495 foreach ( (array) $meta as $meta_val ) {
1496 if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta_val, $matches ) ) {
1497 $post_data['upload_date'] = $matches[0];
1498 }
1499 }
1500 }
1501 }
1502 }
1503
1504 /*upload the file*/
1505 $upload = $this->import_image_and_file( $remote_url, $post_data );
1506
1507 /*if error on upload*/
1508 if ( ! is_array( $upload ) || is_wp_error( $upload ) ) {
1509 /*todo: error*/
1510 return false;
1511 }
1512
1513 /*check file type, if file type not found return false*/
1514 if ( $info = wp_check_filetype( $upload['file'] ) ) {
1515 $post_data['post_mime_type'] = $info['type'];
1516 }
1517 else {
1518 return false;
1519 }
1520
1521 /*set guid file url*/
1522 $post_data['guid'] = $upload['url'];
1523
1524 /*
1525 * insert attachment
1526 *https://developer.wordpress.org/reference/functions/wp_insert_attachment/
1527 * */
1528 $attach_id = wp_insert_attachment( $post_data, $upload['file'] );
1529 if( $attach_id ) {
1530
1531 /*update meta*/
1532 if ( ! empty( $post_data['meta'] ) ) {
1533 foreach ( $post_data['meta'] as $meta_key => $meta_val ) {
1534 if( $meta_key != '_wp_attached_file' && !empty( $meta_val ) ) {
1535 update_post_meta( $attach_id, $meta_key, $meta_val );
1536 }
1537 }
1538 }
1539 /* Update metadata for an attachment.*/
1540 wp_update_attachment_metadata( $attach_id, wp_generate_attachment_metadata( $attach_id, $upload['file'] ) );
1541
1542 /*remap resized image URLs, works by stripping the extension and remapping the URL stub.*/
1543 if ( preg_match( '!^image/!', $info['type'] ) ) {
1544 $parts = pathinfo( $remote_url );
1545 $name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2
1546
1547 $parts_new = pathinfo( $upload['url'] );
1548 $name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" );
1549
1550 $this->imported_post_id( $parts['dirname'] . '/' . $name, $parts_new['dirname'] . '/' . $name_new );
1551 }
1552 $this->imported_post_id( $post_data['post_id'], $attach_id );
1553 }
1554 }
1555 break;
1556
1557 default:
1558
1559 /*Process Post Meta*/
1560 if ( ! empty( $post_data['meta'] ) && is_array( $post_data['meta'] ) ) {
1561
1562 /*fix for double json encoded stuff*/
1563 foreach ( $post_data['meta'] as $meta_key => $meta_val ) {
1564 if ( is_string( $meta_val ) && strlen( $meta_val ) && $meta_val[0] == '[' ) {
1565 $test_json = @json_decode( $meta_val, true );
1566 if ( is_array( $test_json ) ) {
1567 $post_data['meta'][ $meta_key ] = $test_json;
1568 }
1569 }
1570 }
1571 /*todo gutenberg and page builders*/
1572
1573 /*replace menu data
1574 work out what we're replacing. a tax, page, term etc..*/
1575 //
1576 if( isset( $post_data['meta']['_menu_item_menu_item_parent'] ) && 0 != $post_data['meta']['_menu_item_menu_item_parent'] ) {
1577 $new_parent_id = $this->imported_post_id( $post_data['meta']['_menu_item_menu_item_parent'] );
1578 if( !$new_parent_id ) {
1579 if ( $delayed ) {
1580 /*already delayed, unable to find this meta value, skip inserting it*/
1581 $this->error( esc_html__('Unable to find replacement. Continue anyway.... content will most likely break..','advanced-import') );
1582 }
1583 else {
1584 /*not found , delay it*/
1585 $this->error( esc_html__('Unable to find replacement. Delaying....','advanced-import') );
1586 $this->delay_post_process( $post_type, $original_post_data );
1587 return false;
1588 }
1589 }
1590 $post_data['meta']['_menu_item_menu_item_parent'] = $new_parent_id;
1591 }
1592
1593 /*if _menu_item_type*/
1594 if( isset( $post_data['meta'][ '_menu_item_type' ] ) ){
1595
1596 switch( $post_data['meta'][ '_menu_item_type' ] ){
1597 case 'post_type':
1598 if( !empty( $post_data['meta']['_menu_item_object_id'] ) ) {
1599 $new_parent_id = $this->imported_post_id( $post_data['meta']['_menu_item_object_id'] );
1600 if( !$new_parent_id ) {
1601 if ( $delayed ) {
1602 /*already delayed, unable to find this meta value, skip inserting it*/
1603 $this->error( esc_html__('Unable to find replacement. Continue anyway.... content will most likely break..','advanced-import') );
1604 }
1605 else {
1606 /*not found , delay it*/
1607 $this->error( esc_html__('Unable to find replacement. Delaying....','advanced-import') );
1608 $this->delay_post_process( $post_type, $original_post_data );
1609 return false;
1610 }
1611 }
1612 $post_data['meta']['_menu_item_object_id'] = $new_parent_id;
1613 }
1614 break;
1615
1616 case 'taxonomy':
1617 if( !empty( $post_data['meta']['_menu_item_object_id'] ) ) {
1618 $new_parent_id = $this->imported_term_id( $post_data['meta']['_menu_item_object_id'] );
1619 if(!$new_parent_id) {
1620 if ( $delayed ) {
1621 /*already delayed, unable to find this meta value, skip inserting it*/
1622 $this->error( esc_html__('Unable to find replacement. Continue anyway.... content will most likely break..','advanced-import') );
1623 }
1624 else {
1625 /*not found , delay it*/
1626 $this->error( esc_html__('Unable to find replacement. Delaying....','advanced-import') );
1627 $this->delay_post_process( $post_type, $original_post_data );
1628 return false;
1629 }
1630 }
1631 $post_data['meta']['_menu_item_object_id'] = $new_parent_id;
1632 }
1633 break;
1634 }
1635 }
1636 }
1637
1638 /*post content parser*/
1639 $post_data['post_content'] = $this->parse_content_for_shortcode_image_etc( $post_data['post_content'] );
1640
1641 $replace_tax_id_keys = array(
1642 'taxonomies',
1643 );
1644 foreach ( $replace_tax_id_keys as $replace_key ) {
1645 if ( preg_match_all( '# ' . $replace_key . '="(\d+)"#', $post_data['post_content'], $matches ) ) {
1646 foreach ( $matches[0] as $match_id => $string ) {
1647 $new_id = $this->imported_term_id( $matches[1][ $match_id ] );
1648 if ( $new_id ) {
1649 $post_data['post_content'] = str_replace( $string, ' ' . $replace_key . '="' . $new_id . '"', $post_data['post_content'] );
1650 }
1651 else {
1652 $this->error( esc_html__('Unable to find TAXONOMY replacement for ','advanced-import' ) . $replace_key . '="' . $matches[1][ $match_id ]. esc_html__('in content.','advanced-import') );
1653 if ( $delayed ) {
1654 /*already delayed, unable to find this meta value, skip inserting it*/
1655 $this->error( esc_html__('Unable to find replacement. Continue anyway.... content will most likely break..','advanced-import') );
1656 }
1657 else {
1658 /*not found , delay it*/
1659 $this->delay_post_process( $post_type, $original_post_data );
1660 return false;
1661 }
1662 }
1663 }
1664 }
1665 }
1666
1667 /*do further filter if you need*/
1668 $post_data = apply_filters('advanced_import_post_data',$post_data );
1669
1670 /*finally insert post data*/
1671 $post_id = wp_insert_post( $post_data, true );
1672 if ( ! is_wp_error( $post_id ) ) {
1673
1674 /*set id on imported_post_id*/
1675 $this->imported_post_id( $post_data['post_id'], $post_id );
1676
1677 /*add/update post meta*/
1678 if ( ! empty( $post_data['meta'] ) ) {
1679 foreach ( $post_data['meta'] as $meta_key => $meta_val ) {
1680 /*if the post has a featured image, take note of this in case of remap*/
1681 if ( '_thumbnail_id' == $meta_key ) {
1682 /*find this inserted id and use that instead.*/
1683 $inserted_id = $this->imported_post_id( intval( $meta_val ) );
1684 if ( $inserted_id ) {
1685 $meta_val = $inserted_id;
1686 }
1687 }
1688 /*update meta*/
1689 update_post_meta( $post_id, $meta_key, $meta_val );
1690 }
1691 }
1692
1693 if ( ! empty( $post_data['terms'] ) ) {
1694 $terms_to_set = array();
1695 foreach ( $post_data['terms'] as $term_slug => $terms ) {
1696 foreach ( $terms as $term ) {
1697 $taxonomy = $term['taxonomy'];
1698 if ( taxonomy_exists( $taxonomy ) ) {
1699 $term_exists = term_exists( $term['slug'], $taxonomy );
1700 $term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
1701 if ( ! $term_id ) {
1702 if ( ! empty( $term['parent'] ) ) {
1703 /*see if we have imported this yet?*/
1704 $term['parent'] = $this->imported_term_id( $term['parent'] );
1705 }
1706 $term_id_tax_id = wp_insert_term( $term['name'], $taxonomy, $term );
1707 if ( ! is_wp_error( $term_id_tax_id ) ) {
1708 $term_id = $term_id_tax_id['term_id'];
1709 }
1710 else {
1711 // todo - error
1712 continue;
1713 }
1714 }
1715 /*set term_id on imported_term_id*/
1716 $this->imported_term_id( $term['term_id'], $term_id );
1717
1718 /*add the term meta.*/
1719 if( $term_id && !empty( $term['meta'] ) && is_array( $term['meta'] ) ){
1720 foreach( $term['meta'] as $meta_key => $meta_val ){
1721 // we have to replace certain meta_key/meta_val
1722 // e.g. thumbnail id from woocommerce product categories.
1723 switch( $meta_key ){
1724 case 'thumbnail_id':
1725 if( $new_meta_val = $this->imported_post_id($meta_val) ){
1726 /*use this new id.*/
1727 $meta_val = $new_meta_val;
1728 }
1729 break;
1730 }
1731 update_term_meta( $term_id, $meta_key, $meta_val );
1732 }
1733 }
1734 $terms_to_set[ $taxonomy ][] = intval( $term_id );
1735 }
1736 }
1737 }
1738 foreach ( $terms_to_set as $tax => $ids ) {
1739 wp_set_post_terms( $post_id, $ids, $tax );
1740 }
1741 }
1742 }
1743 break;
1744 }
1745
1746 return true;
1747 }
1748
1749 /*helper function for post content
1750 return modified content*/
1751 private function parse_content_for_shortcode_image_etc( $content ){
1752 /*we have to format the post content. rewriting images and gallery stuff*/
1753 $replace = $this->imported_post_id();
1754
1755 /*filters urls for replace*/
1756 $urls_replace = array();
1757 foreach ( $replace as $key => $val ) {
1758 if ( $key && $val && ! is_numeric( $key ) && ! is_numeric( $val ) ) {
1759 $urls_replace[ $key ] = $val;
1760 }
1761 }
1762 /*replace image/file urls*/
1763 if ( $urls_replace ) {
1764 uksort( $urls_replace, array( &$this, 'uksort_strlen' ) );
1765 foreach ( $urls_replace as $from_url => $to_url ) {
1766 $content = str_replace( $from_url, $to_url, $content );
1767 }
1768 }
1769
1770 /*gallery fixed*/
1771 if ( preg_match_all( '#\[gallery[^\]]*\]#', $content, $matches ) ) {
1772 foreach ( $matches[0] as $match_id => $string ) {
1773 if ( preg_match( '#ids="([^"]+)"#', $string, $ids_matches ) ) {
1774 $ids = explode( ',', $ids_matches[1] );
1775 foreach ( $ids as $key => $val ) {
1776 $new_id = $val ? $this->imported_post_id( $val ) : false;
1777 if ( ! $new_id ) {
1778 unset( $ids[ $key ] );
1779 }
1780 else {
1781 $ids[ $key ] = $new_id;
1782 }
1783 }
1784 $new_ids = implode( ',', $ids );
1785 $content = str_replace( $ids_matches[0], 'ids="' . $new_ids . '"', $content );
1786 }
1787 }
1788 }
1789
1790 /*contact form 7 id fixes.*/
1791 if ( preg_match_all( '#\[contact-form-7[^\]]*\]#', $content, $matches ) ) {
1792 foreach ( $matches[0] as $match_id => $string ) {
1793 if ( preg_match( '#id="(\d+)"#', $string, $id_match ) ) {
1794 $new_id = $this->imported_post_id( $id_match[1] );
1795 if ( $new_id ) {
1796 $content = str_replace( $id_match[0], 'id="' . $new_id . '"', $content );
1797 }
1798 else {
1799 /*no imported ID found. remove this entry.*/
1800 $content = str_replace( $matches[0], '(insert contact form here)', $content );
1801 }
1802 }
1803 }
1804 }
1805 return $content;
1806 }
1807
1808 /*update parent page id for child page*/
1809 private function processpost_orphans() {
1810
1811 /*get post orphans to find it parent*/
1812 $orphans = $this->post_orphans();
1813 foreach ( $orphans as $original_post_id => $original_post_parent_id ) {
1814 if ( $original_post_parent_id ) {
1815 if ( $this->imported_post_id( $original_post_id ) && $this->imported_post_id( $original_post_parent_id ) ) {
1816 $post_data = array();
1817 $post_data['ID'] = $this->imported_post_id( $original_post_id );
1818 $post_data['post_parent'] = $this->imported_post_id( $original_post_parent_id );
1819 wp_update_post( $post_data );
1820 $this->post_orphans( $original_post_id, 0 ); /*ignore future*/
1821 }
1822 }
1823 }
1824 }
1825
1826 /*Process delayed post
1827 */
1828 private function process_delayed_posts( $last_delay = false ) {
1829
1830 $this->log( esc_html__('Processing ','advanced-import'). count( $this->delay_posts, COUNT_RECURSIVE ) . esc_html__('delayed posts','advanced-import') );
1831 for ( $x = 1; $x < 4; $x ++ ) {
1832 foreach ( $this->delay_posts as $delayed_post_type => $delayed_post_data_s ) {
1833 foreach ( $delayed_post_data_s as $delayed_post_id => $delayed_post_data ) {
1834
1835 /*already processed*/
1836 if ( $this->imported_post_id( $delayed_post_data['post_id'] ) ) {
1837 $this->log( $x . esc_html__('- Successfully processed ','advanced-import') . $delayed_post_type . esc_html__(' ID ','advanced-import') . $delayed_post_data['post_id'] . esc_html__(' previously.','advanced-import') );
1838
1839 /*already processed, remove it from delay_posts*/
1840 unset( $this->delay_posts[ $delayed_post_type ][ $delayed_post_id ] );
1841 $this->log( esc_html__(' ( ','advanced-import') . count( $this->delay_posts, COUNT_RECURSIVE ) . esc_html__(' delayed posts remain ) ','advanced-import') );
1842 }
1843 /*Process it*/
1844 else if ( $this->process_import_single_post( $delayed_post_type, $delayed_post_data, $last_delay ) ) {
1845 $this->log( $x . esc_html__(' - Successfully found delayed replacement for ','advanced-import') . $delayed_post_type . esc_html__(' ID ','advanced-import') . $delayed_post_data['post_id'] );
1846
1847 /*successfully processed, remove it from delay_posts*/
1848 unset( $this->delay_posts[ $delayed_post_type ][ $delayed_post_id ] );
1849 $this->log( esc_html__(' ( ','advanced-import') . count( $this->delay_posts, COUNT_RECURSIVE ) .esc_html__(' delayed posts remain ) ','advanced-import') );
1850 }
1851 else{
1852 $this->log( $x . esc_html__(' - Not found delayed replacement for ','advanced-import') . $delayed_post_type .esc_html__(' ID ' ,'advanced-import'). $delayed_post_data['post_id'] );
1853 }
1854 }
1855 }
1856 }
1857 }
1858
1859 /*Get file from url , download it and add to local*/
1860 private function import_image_and_file( $url, $post ) {
1861
1862 /*extract the file name and extension from the url*/
1863 $file_name = basename( $url );
1864 $local_file = ADVANCED_IMPORT_TEMP_UPLOADS. $file_name;
1865 $upload = false;
1866
1867 /*if file is already on local, return file information
1868 It means media is on local, while exporting media*/
1869 if ( is_file( $local_file ) && filesize( $local_file ) > 0 ) {
1870 require_once( ABSPATH . 'wp-admin/includes/file.php' );
1871 WP_Filesystem();
1872 global $wp_filesystem;
1873 $file_data = $wp_filesystem->get_contents( $local_file );
1874 $upload = wp_upload_bits( $file_name, 0, $file_data, $post['upload_date'] );
1875 if ( $upload['error'] ) {
1876 return new WP_Error( 'upload_dir_error', $upload['error'] );
1877 }
1878 }
1879
1880 /*if there is no file on local or error on local file need to fetch it*/
1881 if ( ! $upload || $upload['error'] ) {
1882
1883 /*get placeholder file in the upload dir with a unique, sanitized filename*/
1884 $upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
1885 if ( $upload['error'] ) {
1886 return new WP_Error( 'upload_dir_error', $upload['error'] );
1887 }
1888
1889 $max_size = (int) apply_filters( 'import_attachment_size_limit', 0 );
1890
1891 /*finally fetch the file from remote*/
1892 $response = wp_remote_get( $url );
1893 if ( is_array( $response ) && ! empty( $response['body'] ) && $response['response']['code'] == '200' ) {
1894 require_once( ABSPATH . 'wp-admin/includes/file.php' );
1895 $headers = $response['headers'];
1896 WP_Filesystem();
1897 global $wp_filesystem;
1898 $wp_filesystem->put_contents( $upload['file'], $response['body'] );
1899 }
1900 else {
1901 /*required to download file failed.*/
1902 wp_delete_file( $upload['file'] );
1903 return new WP_Error( 'import_file_error', esc_html__( 'Remote server did not respond' ,'advanced-import') );
1904 }
1905
1906 $file_size = filesize( $upload['file'] );
1907
1908 /*check for size*/
1909 if ( isset( $headers['content-length'] ) && $file_size != $headers['content-length'] ) {
1910 wp_delete_file( $upload['file'] );
1911 return new WP_Error( 'import_file_error', esc_html__( 'Remote file is incorrect size','advanced-import' ) );
1912 }
1913
1914 /*if file size is 0*/
1915 if ( 0 == $file_size ) {
1916 wp_delete_file( $upload['file'] );
1917 return new WP_Error( 'import_file_error', esc_html__( 'Zero size file downloaded','advanced-import' ) );
1918 }
1919
1920 /*if file is too large*/
1921 if ( ! empty( $max_size ) && $file_size > $max_size ) {
1922 wp_delete_file( $upload['file'] );
1923 return new WP_Error( 'import_file_error', sprintf( esc_html__( 'Remote file is too large, limit is %s','advanced-import' ), size_format( $max_size ) ) );
1924 }
1925 }
1926
1927 /*keep track of the old and new urls so we can substitute them later*/
1928 $this->imported_post_id( $url, $upload['url'] );
1929 $this->imported_post_id( $post['guid'], $upload['url'] );
1930
1931 /*keep track of the destination if the remote url is redirected somewhere else*/
1932 if ( isset( $headers['x-final-location'] ) && $headers['x-final-location'] != $url ) {
1933 $this->imported_post_id( $headers['x-final-location'], $upload['url'] );
1934 }
1935 return $upload;
1936 }
1937
1938 /*Replace necessary ID by Local imported ID
1939 'Posts IDS : page_id','post_id','image_id','selectpage','page_on_front','page_for_posts'
1940 'Terms IDS : 'cat_id','nav_menu'
1941 */
1942 private function replace_old_id_to_new( $option_value, $index_key = false ){
1943
1944 /*Post IDS*/
1945 $replace_post_ids = apply_filters('advanced_import_replace_post_ids',
1946 array(
1947 'page_id',
1948 'post_id',
1949 'image_id',
1950 'selectpage',
1951 'page_on_front',
1952 'page_for_posts',
1953 'first_page_id',
1954 'second_page_id',
1955 /*woocommerce pages*/
1956 'woocommerce_shop_page_id',
1957 'woocommerce_cart_page_id',
1958 'woocommerce_checkout_page_id',
1959 'woocommerce_pay_page_id',
1960 'woocommerce_thanks_page_id',
1961 'woocommerce_myaccount_page_id',
1962 'woocommerce_edit_address_page_id',
1963 'woocommerce_view_order_page_id',
1964 'woocommerce_terms_page_id'
1965 )
1966 );
1967
1968 /*Terms IDS*/
1969 $replace_term_ids = apply_filters('advanced_import_replace_term_ids',
1970 array(
1971 'cat_id',
1972 'nav_menu',
1973 'online_shop_featured_cats',
1974 'online_shop_wc_product_cat',
1975 'online_shop_wc_product_tag',
1976 ) );
1977
1978 /*replace terms in keys*/
1979
1980 if( is_array( $option_value ) ){
1981 foreach ( $option_value as $key => $replace_old_value ){
1982
1983 if( is_array( $replace_old_value) && !is_null( $replace_old_value )){
1984 $option_value[$key] = $this->replace_old_id_to_new( $replace_old_value );
1985 }
1986 elseif ( $this->isJson( $replace_old_value ) && is_string( $replace_old_value ) && !is_null( $replace_old_value ) ){
1987 $value_array = json_decode( $replace_old_value, true );
1988 if( is_array( $value_array ) ){
1989 $option_value[$key]= json_encode( $this->replace_old_id_to_new( $value_array ) );
1990 }
1991 else{
1992 if ( in_array( $key, $replace_post_ids ) && $key !== 0 ) {
1993 $new_id = $this->imported_post_id( $replace_old_value );
1994 if ( $new_id ) {
1995 $option_value[$key] = $new_id;
1996 }
1997 }
1998 elseif ( in_array( $key, $replace_term_ids ) && $key !== 0 ) {
1999 $new_id = $this->imported_term_id( $replace_old_value );
2000 if ( $new_id ) {
2001 $option_value[$key] = $new_id;
2002 }
2003 }
2004 else{
2005 $option_value[$key] = $replace_old_value;
2006 }
2007 }
2008 }
2009 else{
2010
2011 if ( in_array( $key, $replace_post_ids ) && $key !== 0) {
2012
2013 $new_id = $this->imported_post_id( $replace_old_value );
2014 if ( ! $new_id ) {
2015 /**/
2016 } else {
2017 $option_value[$key] = $new_id;
2018 }
2019 }
2020 elseif ( in_array( $key, $replace_term_ids ) && $key !== 0 ) {
2021 $new_id = $this->imported_term_id( $replace_old_value );
2022 if ( $new_id ) {
2023 $option_value[$key] = $new_id;
2024 }
2025 }
2026 else{
2027 $option_value[$key] = $replace_old_value;
2028 }
2029 }
2030 }
2031 }
2032 elseif ( is_numeric( $option_value ) && $index_key ){
2033
2034 if ( in_array( $index_key, $replace_post_ids ) && $index_key !== 0) {
2035
2036 $new_id = $this->imported_post_id( $option_value );
2037 if ( ! $new_id ) {
2038 /**/
2039 } else {
2040 $option_value = $new_id;
2041 }
2042 }
2043 elseif ( in_array( $index_key, $replace_term_ids ) && $index_key !== 0 ) {
2044 $new_id = $this->imported_term_id( $option_value );
2045 if ( $new_id ) {
2046 $option_value = $new_id;
2047 }
2048 }
2049 }
2050
2051 return $option_value;
2052 }
2053
2054 /* callback function to importing widgets data
2055 * all widgets data is imported from here
2056 * return mix
2057 * */
2058 private function import_content_widgets_data() {
2059 $import_widget_data = $this->get_widgets_json();
2060 $import_widget_positions = $import_widget_data['widget_positions'];
2061 $import_widget_options = $import_widget_data['widget_options'];
2062
2063 /* get sidebars_widgets */
2064 $widget_positions = get_option( 'sidebars_widgets' );
2065 if ( ! is_array( $widget_positions ) ) {
2066 $widget_positions = array();
2067 }
2068
2069 foreach ( $import_widget_options as $widget_name => $widget_options ) {
2070
2071 /*replace $widget_options elements with updated imported entries.*/
2072 foreach ( $widget_options as $widget_option_id => $widget_option ) {
2073 $widget_options[$widget_option_id]= $this->replace_old_id_to_new( $widget_option, $widget_option_id );
2074 }
2075 $existing_options = get_option( 'widget_' . $widget_name, array() );
2076 if ( ! is_array( $existing_options ) ) {
2077 $existing_options = array();
2078 }
2079 $new_options = $widget_options + $existing_options ;
2080
2081 $new_options = apply_filters('advanced_import_new_options', $new_options );
2082
2083 update_option( 'widget_' . $widget_name, $new_options );
2084 }
2085
2086 $sidebars_widgets = array_merge( $widget_positions, $import_widget_positions );
2087 $sidebars_widgets = apply_filters('advanced_import_sidebars_widgets', $sidebars_widgets,$this );
2088 update_option( 'sidebars_widgets', $sidebars_widgets);
2089
2090 return true;
2091
2092 }
2093
2094 /*check if string is json*/
2095 function isJson( $string ) {
2096 $test_json = @json_decode( $string, true );
2097 if ( is_array( $test_json ) ) {
2098 return true;
2099 }
2100 return false;
2101 }
2102
2103 /* callback function to importing menus and options data
2104 * all menus and import data is imported from here
2105 * return mix
2106 * */
2107 public function import_menu_and_options() {
2108
2109 /*final wrap up of delayed posts.*/
2110 $this->process_delayed_posts( true );
2111
2112 /*it includes options and menu data*/
2113 $theme_options = $this->get_theme_options_json();
2114
2115 /*options data*/
2116 $custom_options = $theme_options['options'];
2117
2118 /*menu data*/
2119 $menu_ids = $theme_options['menu'];
2120
2121 /*we also want to update the widget area manager options.*/
2122 if( is_array( $custom_options )){
2123 foreach ( $custom_options as $option => $value ) {
2124 /*replace old entries with updated imported entries.*/
2125 $value= $this->replace_old_id_to_new( $value, $option );
2126
2127 /*we have to update widget page numbers with imported page numbers.*/
2128 if (
2129 preg_match( '#(wam__position_)(\d+)_#', $option, $matches ) ||
2130 preg_match( '#(wam__area_)(\d+)_#', $option, $matches )
2131 ) {
2132 $new_page_id = $this->imported_post_id( $matches[2] );
2133 if ( $new_page_id ) {
2134 // we have a new page id for this one. import the new setting value.
2135 $option = str_replace( $matches[1] . $matches[2] . '_', $matches[1] . $new_page_id . '_', $option );
2136 }
2137 }
2138 if ( $value && ! empty( $value['custom_logo'] ) ) {
2139 $new_logo_id = $this->imported_post_id( $value['custom_logo'] );
2140 if ( $new_logo_id ) {
2141 $value['custom_logo'] = $new_logo_id;
2142 }
2143 }
2144 update_option( $option, $value );
2145 }
2146 }
2147
2148 /*Options completed
2149 Menu Start*/
2150 $save = array();
2151 foreach ( $menu_ids as $menu_id => $term_id ) {
2152 $new_term_id = $this->imported_term_id( $term_id );
2153 if ( $new_term_id ) {
2154 $save[ $menu_id ] = $new_term_id;
2155 }
2156 }
2157
2158 if ( $save ) {
2159 set_theme_mod( 'nav_menu_locations', array_map( 'absint', $save ) );
2160 }
2161
2162 global $wp_rewrite;
2163 $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
2164 update_option( 'rewrite_rules', false );
2165 $wp_rewrite->flush_rules( true );
2166
2167 return true;
2168 }
2169
2170 public function log( $message ) {
2171 $this->logs[] = $message;
2172 }
2173
2174
2175 public function error( $message ) {
2176 $this->logs[] = esc_html__('ERROR!!!! ' ,'advanced-import'). $message;
2177 }
2178
2179 /* callback function to completed
2180 * Show Completed Message
2181 * */
2182 public function complete_screen() {
2183
2184 /*check for security*/
2185 if ( ! current_user_can( 'upload_files' ) ) {
2186 wp_send_json_error(
2187 array(
2188 'message' => esc_html__( 'Sorry, you are not allowed to install demo on this site.','advanced-import' )
2189 )
2190 );
2191 }
2192
2193 require_once( ABSPATH . 'wp-admin/includes/file.php' );
2194 WP_Filesystem();
2195 global $wp_filesystem;
2196 $wp_filesystem->rmdir( ADVANCED_IMPORT_TEMP, true );
2197
2198 set_theme_mod('advanced_import_setup_complete', time() );
2199 /*delete_transient();*/
2200 delete_transient('content.json');
2201 delete_transient('widgets.json');
2202 delete_transient('options.json');
2203
2204 $message = '<div class="ai-notification-title">';
2205 $message .= '<p>'.esc_html__( "Your Website is Ready!" ,'advanced-import').'</p>';
2206 $message .= '<p class="ai-actions-buttons">'.sprintf( esc_html__( ' %sVisit your Site%s ','advanced-import' ),'<a target="_blank" href="'.esc_url(home_url('/')).'">','</a>').'</p>';
2207 $message .= '<p>'.sprintf( esc_html__( 'Congratulations! All Data is imported successfully. From %s WordPress dashboard%s you can make changes and modify any of the default content to suit your needs.','advanced-import' ),'<a href="'.esc_url(admin_url()).'">','</a>').'</p>';
2208 $message .= '</div>';
2209
2210 apply_filters('advanced_import_complete_message', $message);
2211
2212 do_action('advanced_import_before_complete_screen');
2213 echo $message;
2214 do_action('advanced_import_after_complete_screen');
2215 exit;
2216 }
2217
2218
2219 /* callback function for wp_ajax_install_plugin
2220 * Install plugin
2221 * */
2222 function install_plugin() {
2223
2224 /*check for security*/
2225 if ( ! current_user_can( 'install_plugins' ) ) {
2226 $status['errorMessage'] = __( 'Sorry, you are not allowed to install plugins on this site.', 'advanced-import' );
2227 wp_send_json_error( $status );
2228 }
2229
2230 if ( empty( $_POST['plugin'] ) || empty( $_POST['slug'] ) ) {
2231 wp_send_json_error(
2232 array(
2233 'slug' => '',
2234 'errorCode' => 'no_plugin_specified',
2235 'errorMessage' => __( 'No plugin specified.', 'advanced-import' ),
2236 )
2237 );
2238 }
2239
2240 $slug = sanitize_key( wp_unslash( $_POST['slug'] ) );
2241 $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) );
2242
2243 if ( is_plugin_active_for_network( $plugin ) || is_plugin_active( $plugin ) ) {
2244 // Plugin is activated
2245 wp_send_json_success();
2246
2247 }
2248 $status = array(
2249 'install' => 'plugin',
2250 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),
2251 );
2252
2253 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
2254 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
2255
2256 // Looks like a plugin is installed, but not active.
2257 if ( file_exists( WP_PLUGIN_DIR . '/' . $slug ) ) {
2258 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
2259 $status['plugin'] = $plugin;
2260 $status['pluginName'] = $plugin_data['Name'];
2261
2262 if ( current_user_can( 'activate_plugin', $plugin ) && is_plugin_inactive( $plugin ) ) {
2263 $result = activate_plugin( $plugin );
2264
2265 if ( is_wp_error( $result ) ) {
2266 $status['errorCode'] = $result->get_error_code();
2267 $status['errorMessage'] = $result->get_error_message();
2268 wp_send_json_error( $status );
2269 }
2270
2271 wp_send_json_success( $status );
2272 }
2273 }
2274
2275 $api = plugins_api(
2276 'plugin_information',
2277 array(
2278 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),
2279 'fields' => array(
2280 'sections' => false,
2281 ),
2282 )
2283 );
2284
2285 if ( is_wp_error( $api ) ) {
2286 $status['errorMessage'] = $api->get_error_message();
2287 wp_send_json_error( $status );
2288 }
2289
2290 $status['pluginName'] = $api->name;
2291
2292 $skin = new WP_Ajax_Upgrader_Skin();
2293 $upgrader = new Plugin_Upgrader( $skin );
2294 $result = $upgrader->install( $api->download_link );
2295
2296 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
2297 $status['debug'] = $skin->get_upgrade_messages();
2298 }
2299
2300 if ( is_wp_error( $result ) ) {
2301 $status['errorCode'] = $result->get_error_code();
2302 $status['errorMessage'] = $result->get_error_message();
2303 wp_send_json_error( $status );
2304 } elseif ( is_wp_error( $skin->result ) ) {
2305 $status['errorCode'] = $skin->result->get_error_code();
2306 $status['errorMessage'] = $skin->result->get_error_message();
2307 wp_send_json_error( $status );
2308 } elseif ( $skin->get_errors()->get_error_code() ) {
2309 $status['errorMessage'] = $skin->get_error_messages();
2310 wp_send_json_error( $status );
2311 } elseif ( is_null( $result ) ) {
2312 require_once( ABSPATH . 'wp-admin/includes/file.php' );
2313 WP_Filesystem();
2314 global $wp_filesystem;
2315
2316 $status['errorCode'] = 'unable_to_connect_to_filesystem';
2317 $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'advanced-import' );
2318
2319 // Pass through the error from WP_Filesystem if one was raised.
2320 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
2321 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
2322 }
2323
2324 wp_send_json_error( $status );
2325 }
2326
2327 $install_status = install_plugin_install_status( $api );
2328
2329 if ( current_user_can( 'activate_plugin', $install_status['file'] ) && is_plugin_inactive( $install_status['file'] ) ) {
2330 $result = activate_plugin( $install_status['file'] );
2331
2332 if ( is_wp_error( $result ) ) {
2333 $status['errorCode'] = $result->get_error_code();
2334 $status['errorMessage'] = $result->get_error_message();
2335 wp_send_json_error( $status );
2336 }
2337 }
2338
2339 wp_send_json_success( $status );
2340 }
2341
2342 /* callback function to current_screen
2343 * Add help Text
2344 * @param $screen object screen
2345 * */
2346 public function help_tabs($screen) {
2347 if ( !is_array($this->hook_suffix) || !in_array( $screen->base, $this->hook_suffix )){
2348 return;
2349 }
2350 $current_url = advanced_import_current_url();
2351
2352 $screen->add_help_tab(
2353 array(
2354 'id' => 'ai_help_tab_info',
2355 'title' => __( 'Information', 'advanced-import' ),
2356 'content' =>
2357 '<h2>' . __( 'Information', 'advanced-import' ) . '</h2>' .
2358 '<p>' . sprintf(
2359 __( 'Export you content via, <a href="%s" target="_blank">Advanced Export</a>. You can import export content, widget, customizer and media files too.', 'advanced-import' ),
2360 'https://wordpress.org/plugins/advanced-export/'
2361 ) . '</p>' .
2362 '<p>' . sprintf(
2363 __( 'The zip file exported via <a href="%1$s" target="_blank">Advanced Export</a>. can be imported from this plugin <a href="%2$s">Advanced Import</a>.', 'advanced-import' ),
2364 'https://wordpress.org/support/plugin/advanced-export',
2365 'https://wordpress.org/support/plugin/advanced-import'
2366 ) . '</p>' .
2367 '<p><a href="' . 'https://wordpress.org/support/plugin/advanced-import' . '" class="button button-primary" target="_blank">' . __( 'Community forum', 'advanced-import' ) . '</a> <a href="' . 'https://www.addonspress.com/' . '" class="button" target="_blank">' . __( 'Author', 'advanced-import' ) . '</a></p>',
2368 )
2369 );
2370
2371 $reset_url = wp_nonce_url(
2372 add_query_arg( 'ai_reset_wordpress', 'true', $current_url ),
2373 'ai_reset_wordpress',
2374 'ai_reset_wordpress_nonce'
2375 );
2376 $screen->add_help_tab(
2377 array(
2378 'id' => 'ai_help_tab_reset',
2379 'title' => __( 'Reset wizard', 'advanced-import' ),
2380 'content' =>
2381 '<h2>'.__( '<strong>WordPress Reset</strong>', 'advanced-import' ).'</h2>'.
2382 '<p>'.__( 'If no important data on your site. You can reset the WordPress back to default again!', 'advanced-import' ).'</p>'.
2383 '<p class="submit"><a href="'.esc_url( $reset_url ).'" class="button button-primary ai-wp-reset">'. __( 'Run the Reset Wizard', 'advanced-import' ).'</a></p>' )
2384 );
2385
2386 $screen->set_help_sidebar(
2387 '<p><strong>' . __( 'More information:', 'advanced-import' ) . '</strong></p>' .
2388 '<p><a href="' . 'https://wordpress.org/plugins/advanced-export/' . '" target="_blank">' . __( 'Advanced Export', 'advanced-import' ) . '</a></p>' .
2389 '<p><a href="' . 'https://wordpress.org/plugins/advanced-import/' . '" target="_blank">' . __( 'Advanced Import', 'advanced-import' ) . '</a></p>'
2390 );
2391 }
2392 }
2393
2394 /**
2395 * Begins execution of the plugin.
2396 *
2397 * Since everything within the plugin is registered via hooks,
2398 * then kicking off the plugin from this point in the file does
2399 * not affect the page life cycle.
2400 *
2401 * @since 1.0.0
2402 */
2403 function advanced_import_admin( ) {
2404 return Advanced_Import_Admin::instance();
2405 }