FameTheme Demo Importer - Version 1.0.1

Version Description

Download this release

Release Info

Developer FameThemes
Plugin Icon wp plugin FameTheme Demo Importer
Version 1.0.1
Comparing to
See all releases

Version 1.0.1

assets/css/importer.css ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .ft-demo-import-now.button.updating-message:before {
2
+ margin: 13px 5px 0 -2px;
3
+ }
4
+
5
+ .ft-import-box {
6
+ background: #fff none repeat scroll 0 0;
7
+ box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.1);
8
+ font-size: 14px;
9
+ line-height: 2.1em;
10
+ margin-bottom: 25px;
11
+ overflow: auto;
12
+ padding: 0px 12px 0px 12px;
13
+ position: relative;
14
+ max-width: 1050px;
15
+ }
16
+ .ft-import-box.ft-import-welcome {
17
+ padding: 0px 20px 10px 25px;
18
+ }
19
+ .ft-import-box h3 {
20
+ font-size: 22px;
21
+ font-weight: 500;
22
+ }
23
+ .ft-import-box li {
24
+ margin: 0px;
25
+ padding-left: 15px;
26
+ list-style: inside;
27
+ }
28
+ .ft-export-config {
29
+ left: 20px;
30
+ position: relative;
31
+ top: 10px;
32
+ }
33
+ .ft-import-action {
34
+ margin-bottom: 25px;
35
+ }
36
+ .ft-import-theme {
37
+ border-left: 4px solid #0085ba;
38
+ }
39
+ .ft-import-success {
40
+ border-left-color: #46b450;
41
+ }
42
+ .ft-import-error {
43
+ border-left-color: #dc3232;
44
+ }
assets/js/importer.js ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ var ft_import_running = false;
2
+ window.onbeforeunload = function() {
3
+ if ( ft_import_running ) {
4
+ return FT_IMPORT_DEMO.confirm_leave;
5
+ }
6
+ };
7
+
8
+
9
+
10
+ jQuery( document ).ready( function( $ ){
11
+
12
+ $( '.ft-demo-import-now').on( 'click', function( e ){
13
+ e.preventDefault();
14
+ var btn = $( this);
15
+ if ( btn.hasClass( 'disabled' ) ) {
16
+ return ;
17
+ }
18
+ // Make sure user want to import
19
+ var c = confirm( FT_IMPORT_DEMO.confirm_import );
20
+ if ( ! c ) {
21
+ return;
22
+ }
23
+
24
+ btn.addClass( 'disabled' );
25
+ btn.addClass( 'updating-message' );
26
+
27
+ var url = btn.attr( 'href' );
28
+ var frame = $( '<iframe style="display: none;"></iframe>' );
29
+ frame.appendTo('body');
30
+ // Thanks http://jsfiddle.net/KSXkS/1/
31
+ try { // simply checking may throw in ie8 under ssl or mismatched protocol
32
+ doc = frame[0].contentDocument ? frame[0].contentDocument : frame[0].document;
33
+ } catch(err) {
34
+ doc = frame[0].document;
35
+ }
36
+ doc.open();
37
+
38
+ // Download url first
39
+ url = btn.attr( 'data-download' );
40
+ btn.html( FT_IMPORT_DEMO.downloading );
41
+ var notice = $( '.ft-ajax-notice' );
42
+ notice.html( );
43
+
44
+ ft_import_running = true;
45
+ $.ajax( {
46
+ url: url,
47
+ error: function(){
48
+ var msg = $( '<div class="ft-import-box ft-import-theme"></div>' );
49
+ btn.removeClass( 'updating-message' );
50
+ btn.html( FT_IMPORT_DEMO.import_error );
51
+ msg.addClass( 'ft-import-error').html( '<p>'+FT_IMPORT_DEMO.no_data_found+'</p>' );
52
+ notice.html( msg );
53
+ ft_import_running = false;
54
+ console.log( 'download_error' );
55
+ },
56
+ success: function( res ){
57
+ // btn.removeClass( 'disabled' );
58
+ //btn.removeClass( 'updating-message' );
59
+ //doc.close();
60
+
61
+ // Import
62
+ setTimeout( function(){
63
+ btn.html( FT_IMPORT_DEMO.importing );
64
+ url = btn.attr( 'data-import' );
65
+ $.ajax( {
66
+ url: url,
67
+ dataType: 'html',
68
+ error: function( jqXHR, status, errorThrown ){
69
+
70
+ btn.removeClass( 'updating-message' );
71
+ doc.close();
72
+ btn.html( FT_IMPORT_DEMO.import_error );
73
+ btn.removeClass( 'button-primary' );
74
+ btn.addClass( 'button-secondary' );
75
+ var msg = $( '<div class="ft-import-box ft-import-theme"></div>' );
76
+ var err = jqXHR.statusText + ' ('+jqXHR.status+')';
77
+ msg.addClass( 'ft-import-error').html( '<p>'+err+'</p>' );
78
+ msg.append( '<p>'+FT_IMPORT_DEMO.import_error_msg+'</p>' );
79
+ notice.html( msg );
80
+
81
+ ft_import_running = false;
82
+
83
+ },
84
+ success: function( res ){
85
+ //btn.removeClass( 'disabled' );
86
+ btn.removeClass( 'updating-message' );
87
+ doc.close();
88
+ btn.html( FT_IMPORT_DEMO.imported );
89
+ btn.removeClass( 'button-primary' );
90
+ btn.addClass( 'button-secondary' );
91
+
92
+ var msg = $( '<div class="ft-import-box ft-import-theme"></div>' );
93
+ if ( res.indexOf( 'demo_imported' ) > -1 ) {
94
+ res = res.replace(/demo_imported/i, '');
95
+ msg.addClass( 'ft-import-success').html( '<p>'+FT_IMPORT_DEMO.demo_imported+'</p>' );
96
+ msg.append( '<div class="import_log">'+res+'</div>' );
97
+ } else if ( res.indexOf( 'no_data_found' ) > -1 ){
98
+ res = res.replace(/no_data_found/i, '');
99
+ msg.addClass( 'ft-import-error').html( '<p>'+FT_IMPORT_DEMO.no_data_found+'</p>' );
100
+ msg.append( '<div class="import_log">'+res+'</div>' );
101
+ } else if ( res.indexOf( 'demo_import_failed' ) > -1 ){
102
+ msg.addClass( 'ft-import-error').html( '<p>'+FT_IMPORT_DEMO.demo_import_failed+'</p>' );
103
+ } else {
104
+ res = res.replace(/demo_imported/i, '');
105
+ msg.addClass( 'ft-import-success').html( '<p>'+FT_IMPORT_DEMO.demo_imported+'</p>' );
106
+ msg.append( '<div class="import_log">'+res+'</div>' );
107
+ }
108
+
109
+ notice.html( msg );
110
+
111
+ ft_import_running = false;
112
+
113
+ }
114
+ } );
115
+ }, 1000 );
116
+
117
+ }
118
+ } );
119
+
120
+
121
+
122
+ } );
123
+ } );
famethemes-demo-importer.php ADDED
@@ -0,0 +1,465 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: FameTheme Demo Importer
4
+ Plugin URI: https://github.com/FameThemes/famethemes-demo-importer
5
+ Description: Demo data import tool for FameThemes's themes.
6
+ Author: famethemes
7
+ Author URI: http://www.famethemes.com/
8
+ Version: 1.0.1
9
+ Text Domain: ftdi
10
+ License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11
+ */
12
+
13
+
14
+ class FT_Demo_Content_Importer {
15
+ public $dir;
16
+ public $url;
17
+ public $git_repo = 'https://raw.githubusercontent.com/FameThemes/famethemes-xml-demos/master/';
18
+ function __construct( ){
19
+
20
+ $this->url = trailingslashit( plugins_url('', __FILE__) );
21
+ $this->dir = trailingslashit( plugin_dir_path( __FILE__) );
22
+
23
+ require_once $this->dir.'inc/class-demo-content.php';
24
+ add_action( 'wp_ajax_ft_demo_import_content', array( $this, 'ajax_import' ) );
25
+ add_action( 'wp_ajax_ft_demo_import_download', array( $this, 'ajax_download' ) );
26
+ add_action( 'wp_ajax_ft_demo_export', array( $this, 'ajax_export' ) );
27
+
28
+ $template_slug = get_option( 'template' );
29
+ $theme_slug = get_option( 'stylesheet' );
30
+ // child theme active
31
+ if ( $template_slug != $theme_slug ) {
32
+ add_action( $template_slug.'_demo_import_content_tab', array( $this, 'display_import' ) );
33
+ } else {
34
+ add_action( $theme_slug.'_demo_import_content_tab', array( $this, 'display_import' ) );
35
+ }
36
+
37
+
38
+ add_action( 'customize_controls_print_footer_scripts', array( $this, 'update_customizer_keys' ) );
39
+ if ( is_admin() ) {
40
+ add_action( 'admin_enqueue_scripts', array( $this, 'css' ) );
41
+ }
42
+
43
+ }
44
+
45
+ function update_customizer_keys(){
46
+ $key = 'ft_demo_customizer_keys';
47
+ $theme_slug = get_option( 'stylesheet' );
48
+ $data = get_option( $key );
49
+ if ( ! is_array( $data ) ) {
50
+ $data = array();
51
+ }
52
+
53
+ global $wp_customize;
54
+
55
+ $pages = get_pages();
56
+ $option_pages = array();
57
+ $option_pages[0] = esc_html__( 'Select page', 'ftdi' );
58
+ foreach( $pages as $p ){
59
+ $option_pages[ $p->ID ] = $p->post_title;
60
+ }
61
+
62
+ foreach ( $wp_customize->controls() as $k => $c ) {
63
+ if ( $c->type == 'repeatable' ) {
64
+ $keys = array();
65
+ if ( property_exists( $c, 'fields' ) ){
66
+ foreach ( $c->fields as $field_id => $field ) {
67
+ if( isset( $field['options'] ) && is_array( $field['options'] ) ) {
68
+ $result = array_diff( $option_pages, $field['options'] );
69
+ if ( empty( $result ) ) { // if this option use page
70
+ $info = array(
71
+ 'key' => $field_id,
72
+ 'type' => 'page',
73
+ );
74
+ array_push( $keys, $info );
75
+ }
76
+ } elseif( isset( $field['data_type'] ) && in_array( $field['data_type'] , array( 'post', 'page' ) ) ) {
77
+ $info = array(
78
+ 'key' => $field_id,
79
+ 'type' => 'page',
80
+ );
81
+ array_push( $keys, $info );
82
+ } elseif( $field['type'] == 'media' ) {
83
+ $info = array(
84
+ 'key' => $field_id,
85
+ 'type' => 'media',
86
+ );
87
+ array_push( $keys, $info );
88
+ }
89
+ }
90
+ }
91
+
92
+ $data[ $theme_slug ][ $k ] = $keys;
93
+
94
+ } else if ( $c->type == 'media' ) { // wp media
95
+ $data[ $theme_slug ][ $k ] = $c->type;
96
+ }
97
+
98
+ }
99
+ update_option( $key, $data );
100
+ }
101
+
102
+ function display_import(){
103
+ $nonce = wp_create_nonce( 'ft_demo_import' );
104
+ $url = admin_url('admin-ajax.php');
105
+ $url = remove_query_arg( array( '_nonce', 'action' ) , $url );
106
+
107
+ $current_item = apply_filters( 'ft_demo_import_current_item', false );
108
+
109
+ if ( ! $current_item ) {
110
+ $current_item = get_option( 'template' );
111
+ $current_item = untrailingslashit( $current_item );
112
+ }
113
+
114
+ $current_item_name = str_replace( array( '-', '_' ), ' ', $current_item );
115
+ $current_item_name = ucwords( $current_item_name );
116
+
117
+
118
+ $import_url = add_query_arg( array(
119
+ '_nonce' => $nonce,
120
+ 'action' => 'ft_demo_import_content',
121
+ 'data' => $current_item
122
+ ), $url );
123
+
124
+ $download_url = add_query_arg( array(
125
+ '_nonce' => $nonce,
126
+ 'action' => 'ft_demo_import_download',
127
+ 'data' => $current_item
128
+ ), $url );
129
+
130
+ $import_export_config_url = add_query_arg( array(
131
+ '_nonce' => $nonce,
132
+ 'action' => 'ft_demo_export',
133
+ 'type' => 'config',
134
+ 'data' => $current_item
135
+ ), $url );
136
+
137
+ $this->js();
138
+
139
+ ?>
140
+ <div class="ft-import-box ft-import-welcome">
141
+ <h3><?php esc_html_e( 'Welcome to FameThemes Demo Importer!', 'ftdi' ); ?></h3>
142
+ <p>
143
+ <?php esc_html_e( 'Importing demo data (post, pages, images, theme settings, ...) is the easiest way to setup your theme. It will allow you to quickly edit everything instead of creating content from scratch. When you import the data, the following things might happen:', 'ftdi' ); ?>
144
+ </p>
145
+ <ul>
146
+ <li><?php esc_html_e( 'No existing posts, pages, categories, images, custom post types or any other data will be deleted or modified.', 'ftdi' ); ?></li>
147
+ <li><?php esc_html_e( 'Posts, pages, images, widgets and menus will get imported.', 'ftdi' ); ?></li>
148
+ <li><?php esc_html_e( 'Please click "Import Demo Data" button only once and wait, it can take a couple of minutes.', 'ftdi' ); ?></li>
149
+ </ul>
150
+ <p><?php esc_html_e( 'Notice: If your site already has content, please make sure you backup your database and WordPress files before import demo data.', 'ftdi' ); ?></p>
151
+ </div>
152
+
153
+
154
+ <?php if ( $this->check_data_exists( $current_item ) ){ ?>
155
+ <div class="ft-import-box ft-import-theme">
156
+ <p><?php printf( esc_html__( 'You are ready to import demo data for %1$s', 'ftdi' ), '<strong>'.esc_html( $current_item_name ).'</strong>' ); ?></p>
157
+ </div>
158
+
159
+ <div class="ft-import-action">
160
+ <a class="button button-primary button-hero ft-demo-import-now" data-download="<?php echo esc_url( $download_url ); ?>" data-import="<?php echo esc_url( $import_url ); ?>" href="#"><?php esc_html_e( 'Import Demo Data', 'ftdi' ); ?></a>
161
+ <?php if ( isset( $_REQUEST['export'] ) ) { ?>
162
+ <a class="button button-secondary ft-export-config" href="<?php echo esc_url( $import_export_config_url ); ?>"><?php esc_html_e( 'Export Config', 'ftdi' ); ?></a>
163
+ <?php } ?>
164
+ </div>
165
+ <div class="ft-ajax-notice"></div>
166
+
167
+ <?php } else { ?>
168
+ <div class="ft-import-box ft-import-theme">
169
+ <p><strong><?php esc_html_e( 'Notice:', 'ftdi' ); ?></strong> <?php esc_html_e( "No FameThemes's themes data detected, please make sure you are using one of our theme.", 'ftdi' ); ?></p>
170
+ </div>
171
+ <?php } ?>
172
+ <?php
173
+
174
+ }
175
+
176
+ function js(){
177
+ wp_enqueue_script( 'ft-demo-importer', $this->url.'assets/js/importer.js', array( 'jquery' ) );
178
+ wp_localize_script( 'ft-demo-importer', 'FT_IMPORT_DEMO', array(
179
+ 'downloading' => esc_html__( 'Downloading...', 'ftid' ),
180
+ 'importing' => esc_html__( 'Importing...', 'ftid' ),
181
+ 'import' => esc_html__( 'Import Now', 'ftid' ),
182
+ 'import_again' => esc_html__( 'Import Again.', 'ftid' ),
183
+ 'imported' => esc_html__( 'Demo Data Imported !', 'ftid' ),
184
+ 'import_error' => esc_html__( 'Demo Data Import Error', 'ftid' ),
185
+ 'import_error_msg' => sprintf( esc_html__( 'Check your %1$s, demo data may imported.', 'ftid' ), '<a target="blank" href="'.home_url('/').'">'.esc_html__( 'front end', 'ftid' ).'</a>' ),
186
+ 'confirm_import' => esc_html__( 'Are you sure ?', 'ftid' ),
187
+ 'confirm_leave' => esc_html__( 'Importing script is running, do you want to stop it ?', 'ftid' ),
188
+ 'demo_imported' => sprintf( esc_html__( 'The demo import has finished. Please check your %1$s and make sure that everything has imported correctly. If it did, you can deactivate the FameThemes Demo Importer plugin, because it has done its job.', 'ftdi' ),
189
+ '<a target="_blank" href="'.esc_url( home_url( '/' ) ).'">'.esc_html__( 'front page', 'ftdi' ).'</a>' ),
190
+ 'no_data_found' => esc_html__( 'No data found.', 'ftid' ),
191
+ 'demo_import_failed' => sprintf( esc_html__( 'Demo data import failed, please %1$s to get help.', 'ftdi' ), '<a target="_blank" href="https://www.famethemes.com/contact">'.esc_html__( 'contact us', 'ftdi' ).'</a>' ),
192
+
193
+ ) );
194
+ }
195
+
196
+ function css( ){
197
+ wp_enqueue_style( 'ft-demo-importer', $this->url . 'assets/css/importer.css', false );
198
+ }
199
+
200
+ function check_data_exists( $item_name ){
201
+ $file = $this->git_repo.$item_name.'/dummy-data.xml';
202
+ $response = wp_remote_get( $file );
203
+ $response_code = wp_remote_retrieve_response_code( $response );
204
+ if ( 200 != $response_code ) {
205
+ return false;
206
+ }
207
+
208
+ return true;
209
+ }
210
+
211
+ /**
212
+ * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
213
+ *
214
+ * @since 2.6.0
215
+ *
216
+ * @param array $file_array Array similar to a `$_FILES` upload array.
217
+ * @param int $post_id The post ID the media is associated with.
218
+ * @param string $desc Optional. Description of the side-loaded file. Default null.
219
+ * @param array $post_data Optional. Post data to override. Default empty array.
220
+ * @return int|object The ID of the attachment or a WP_Error on failure.
221
+ */
222
+ static function media_handle_sideload( $file_array, $post_id, $desc = null, $post_data = array(), $save_attachment = true ) {
223
+ $overrides = array(
224
+ 'test_form'=>false,
225
+ 'test_type'=>false
226
+ );
227
+
228
+ $time = current_time( 'mysql' );
229
+ if ( $post = get_post( $post_id ) ) {
230
+ if ( substr( $post->post_date, 0, 4 ) > 0 )
231
+ $time = $post->post_date;
232
+ }
233
+
234
+ $file = wp_handle_sideload( $file_array, $overrides, $time );
235
+ if ( isset($file['error']) )
236
+ return new WP_Error( 'upload_error', $file['error'] );
237
+
238
+ $url = $file['url'];
239
+ $type = $file['type'];
240
+ $file = $file['file'];
241
+ $title = preg_replace('/\.[^.]+$/', '', basename($file));
242
+ $content = '';
243
+
244
+ if ( $save_attachment ) {
245
+ if (isset($desc)) {
246
+ $title = $desc;
247
+ }
248
+
249
+ // Construct the attachment array.
250
+ $attachment = array_merge(array(
251
+ 'post_mime_type' => $type,
252
+ 'guid' => $url,
253
+ 'post_parent' => $post_id,
254
+ 'post_title' => $title,
255
+ 'post_content' => $content,
256
+ ), $post_data);
257
+
258
+ // This should never be set as it would then overwrite an existing attachment.
259
+ unset($attachment['ID']);
260
+
261
+ // Save the attachment metadata
262
+ $id = wp_insert_attachment($attachment, $file, $post_id);
263
+
264
+ return $id;
265
+ } else {
266
+ return $file;
267
+ }
268
+ }
269
+
270
+ /**
271
+ * Download image form url
272
+ *
273
+ * @return bool
274
+ */
275
+ static function download_file( $url, $name = '', $save_attachment = true ){
276
+ if ( ! $url || empty ( $url ) ) {
277
+ return false;
278
+ }
279
+ // These files need to be included as dependencies when on the front end.
280
+ require_once (ABSPATH . 'wp-admin/includes/image.php');
281
+ require_once (ABSPATH . 'wp-admin/includes/file.php');
282
+ require_once (ABSPATH . 'wp-admin/includes/media.php');
283
+ $file_array = array();
284
+ // Download file to temp location.
285
+ $file_array['tmp_name'] = download_url( $url );
286
+
287
+ // If error storing temporarily, return the error.
288
+ if ( empty( $file_array['tmp_name'] ) || is_wp_error( $file_array['tmp_name'] ) ) {
289
+ return false;
290
+ }
291
+
292
+ if ( $name ) {
293
+ $file_array['name'] = $name;
294
+ }
295
+ // Do the validation and storage stuff.
296
+ $file_path_or_id = self::media_handle_sideload( $file_array, 0, null, array(), $save_attachment );
297
+
298
+ // If error storing permanently, unlink.
299
+ if ( is_wp_error( $file_path_or_id ) ) {
300
+ @unlink( $file_array['tmp_name'] );
301
+ return false;
302
+ }
303
+
304
+ return $file_path_or_id;
305
+ }
306
+
307
+ function download_dummy_files( $item_name ){
308
+ $files = array(
309
+ 'dummy-data' => 'xml', // file ext
310
+ 'config' => 'json'
311
+ );
312
+ $downloaded_file = array();
313
+ foreach ( $files as $k => $ext ) {
314
+ $file = $item_name.'-'.$k.'.'.$ext;
315
+ $file_path = self::download_file( $this->git_repo.$item_name.'/'.$k.'.'.$ext, $file, false );
316
+ echo $file_path;
317
+ $downloaded_file[ $k ] = $file_path;
318
+ }
319
+
320
+ return $downloaded_file;
321
+ }
322
+
323
+
324
+
325
+ function data_dir( $param ){
326
+
327
+ $siteurl = get_option( 'siteurl' );
328
+ $upload_path = trim( get_option( 'upload_path' ) );
329
+
330
+ if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
331
+ $dir = WP_CONTENT_DIR . '/uploads';
332
+ } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
333
+ // $dir is absolute, $upload_path is (maybe) relative to ABSPATH
334
+ $dir = path_join( ABSPATH, $upload_path );
335
+ } else {
336
+ $dir = $upload_path;
337
+ }
338
+
339
+ if ( !$url = get_option( 'upload_url_path' ) ) {
340
+ if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )
341
+ $url = WP_CONTENT_URL . '/uploads';
342
+ else
343
+ $url = trailingslashit( $siteurl ) . $upload_path;
344
+ }
345
+
346
+
347
+ $param['path'] = $dir . '/ft-dummy-data';
348
+ $param['url'] = $url . '/ft-dummy-data';
349
+
350
+ return $param;
351
+ }
352
+
353
+
354
+ function ajax_download(){
355
+ $nonce = $_REQUEST['_nonce'];
356
+ if ( ! wp_verify_nonce( $nonce, 'ft_demo_import' ) ) {
357
+ die( 'Security check' );
358
+ }
359
+ add_filter('upload_dir', array( $this, 'data_dir' ), 99 );
360
+
361
+ $item = wp_strip_all_tags( $_REQUEST['data'] );
362
+ delete_transient( 'ft_demo_import_downloaded_'.$item );
363
+ $import_files = $this->download_dummy_files( $item );
364
+ set_transient( 'ft_demo_import_downloaded_'.$item, $import_files, 3 * HOUR_IN_SECONDS );
365
+
366
+ remove_filter('upload_dir', array( $this, 'data_dir' ), 99 );
367
+
368
+ wp_die('downloaded');
369
+ }
370
+
371
+ function ajax_import(){
372
+
373
+ $nonce = $_REQUEST['_nonce'];
374
+ if ( ! wp_verify_nonce( $nonce, 'ft_demo_import' ) ) {
375
+ die( 'Security check' );
376
+ }
377
+
378
+ $item = wp_strip_all_tags( $_REQUEST['data'] );
379
+ $import_files = get_transient( 'ft_demo_import_downloaded_'.$item );
380
+
381
+ if ( $import_files ) {
382
+ $import = new FT_Demo_Content( $import_files );
383
+ $import->import();
384
+ } else {
385
+ echo 'no_data_found';
386
+ }
387
+
388
+ // Remove data files
389
+ /*
390
+ foreach ( $import_files as $k => $f ) {
391
+ if ( file_exists( $f ) ) {
392
+ @unlink( $f );
393
+ }
394
+ }
395
+ */
396
+
397
+ die();
398
+ }
399
+
400
+
401
+ function ajax_export(){
402
+ $type = $_REQUEST['type'];
403
+ ob_start();
404
+ ob_end_clean();
405
+
406
+ /**
407
+ * Filters the export filename.
408
+ *
409
+ * @since 4.4.0
410
+ *
411
+ * @param string $wp_filename The name of the file for download.
412
+ * @param string $sitename The site name.
413
+ * @param string $date Today's date, formatted.
414
+ */
415
+ $filename = 'config.json';
416
+
417
+ header( 'Content-Description: File Transfer' );
418
+ header( 'Content-Disposition: attachment; filename=' . $filename );
419
+ header( 'Content-Type: application/xml; charset=' . get_option( 'blog_charset' ), true );
420
+
421
+ switch ( $type ) {
422
+ case 'options':
423
+ case 'option':
424
+ case 'config':
425
+ case 'widget':
426
+ case 'widgets':
427
+ case 'theme_mod':
428
+ case 'theme_mods':
429
+ echo FT_Demo_Content::generate_config();
430
+ break;
431
+ }
432
+ die();
433
+ }
434
+
435
+ }
436
+
437
+ if ( is_admin() ) {
438
+ function ft_demo_importer(){
439
+ new FT_Demo_Content_Importer();
440
+ }
441
+ add_action( 'plugins_loaded', 'ft_demo_importer' );
442
+ }
443
+
444
+ /**
445
+ * Redirect to import page
446
+ *
447
+ * @param $plugin
448
+ * @param bool|false $network_wide
449
+ */
450
+ function ft_demo_importer_plugin_activate( $plugin, $network_wide = false ) {
451
+ if ( ! $network_wide && $plugin == plugin_basename( __FILE__ ) ) {
452
+ $template_slug = get_option('template');
453
+ $url = add_query_arg(
454
+ array(
455
+ 'page' => 'ft_' . $template_slug,
456
+ 'tab' => 'demo-data-importer',
457
+ ),
458
+ admin_url('themes.php')
459
+ );
460
+ wp_redirect($url);
461
+ die();
462
+ }
463
+ }
464
+ add_action( 'activated_plugin', 'ft_demo_importer_plugin_activate', 90, 2 );
465
+
inc/class-demo-content.php ADDED
@@ -0,0 +1,716 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class FT_Demo_Content {
3
+
4
+ public $dir;
5
+ public $url;
6
+
7
+ public $processed_posts;
8
+
9
+
10
+ /**
11
+ * XML file to import
12
+ * Export by WP Exporter
13
+ * @var string file path
14
+ */
15
+ public $xml_file;
16
+
17
+
18
+ public $config_file;
19
+
20
+ /**
21
+ * Option key to store data in $option_file
22
+ *
23
+ * Data type: JSON string
24
+ *
25
+ * @see update_option
26
+ * @var string option_key
27
+ */
28
+ public $option_key;
29
+
30
+ public $config_data;
31
+
32
+ function __construct( $options = array() ){
33
+
34
+ $options = wp_parse_args( $options, array(
35
+ 'option_key' => '',
36
+ 'dummy-data' => '', // file ext
37
+ 'config' => ''
38
+ ));
39
+
40
+ $this->xml_file = $options['dummy-data'];
41
+ $this->config_file = $options['config'];
42
+ $this->option_key = $options['option_key'];
43
+
44
+ }
45
+
46
+
47
+
48
+ /**
49
+ * Import options
50
+ *
51
+ * Export by function get_options
52
+ *
53
+ * @see get_option
54
+ * @param $option_key
55
+ * @param $file
56
+ * @return bool
57
+ */
58
+ function import_options( $option_key, $file ) {
59
+ if ( ! file_exists( $file ) ) {
60
+ return false;
61
+ }
62
+ if ( ! $option_key ) {
63
+ return false;
64
+ }
65
+ $data = file_get_contents( $file );
66
+ $data = json_decode( $data , true );
67
+
68
+ update_option( $option_key, $data );
69
+ }
70
+
71
+ /**
72
+ * Import widget JSON data
73
+ *
74
+ * @since 0.4
75
+ * @global array $wp_registered_sidebars
76
+ * @param object $data JSON widget data from .wie file
77
+ * @return array Results array
78
+ */
79
+ function import_widgets( $data ) {
80
+
81
+ global $wp_registered_sidebars;
82
+
83
+ // Have valid data?
84
+ // If no data or could not decode
85
+ if ( empty( $data ) ) {
86
+ return false;
87
+ }
88
+
89
+ // Get all available widgets site supports
90
+ $available_widgets = $this->get_available_widgets();
91
+
92
+ // Get all existing widget instances
93
+ $widget_instances = array();
94
+ foreach ( $available_widgets as $widget_data ) {
95
+ $widget_instances[$widget_data['id_base']] = get_option( 'widget_' . $widget_data['id_base'] );
96
+ }
97
+
98
+ // Begin results
99
+ $results = array();
100
+
101
+ // Loop import data's sidebars
102
+ foreach ( $data as $sidebar_id => $widgets ) {
103
+
104
+ // Skip inactive widgets
105
+ // (should not be in export file)
106
+ if ( 'wp_inactive_widgets' == $sidebar_id ) {
107
+ continue;
108
+ }
109
+
110
+ // Check if sidebar is available on this site
111
+ // Otherwise add widgets to inactive, and say so
112
+ if ( isset( $wp_registered_sidebars[$sidebar_id] ) ) {
113
+ $sidebar_available = true;
114
+ $use_sidebar_id = $sidebar_id;
115
+ $sidebar_message_type = 'success';
116
+ $sidebar_message = '';
117
+ } else {
118
+ $sidebar_available = false;
119
+ $use_sidebar_id = 'wp_inactive_widgets'; // add to inactive if sidebar does not exist in theme
120
+ $sidebar_message_type = 'error';
121
+ $sidebar_message = __( 'Sidebar does not exist in theme (using Inactive)', 'onepress-import' );
122
+ }
123
+
124
+ // Result for sidebar
125
+ $results[$sidebar_id]['name'] = ! empty( $wp_registered_sidebars[$sidebar_id]['name'] ) ? $wp_registered_sidebars[$sidebar_id]['name'] : $sidebar_id; // sidebar name if theme supports it; otherwise ID
126
+ $results[$sidebar_id]['message_type'] = $sidebar_message_type;
127
+ $results[$sidebar_id]['message'] = $sidebar_message;
128
+ $results[$sidebar_id]['widgets'] = array();
129
+
130
+ // Loop widgets
131
+ foreach ( $widgets as $widget_instance_id => $widget ) {
132
+
133
+ $fail = false;
134
+
135
+ // Get id_base (remove -# from end) and instance ID number
136
+ $id_base = preg_replace( '/-[0-9]+$/', '', $widget_instance_id );
137
+ $instance_id_number = str_replace( $id_base . '-', '', $widget_instance_id );
138
+
139
+ // Does site support this widget?
140
+ if ( ! $fail && ! isset( $available_widgets[$id_base] ) ) {
141
+ $fail = true;
142
+ $widget_message_type = 'error';
143
+ $widget_message = __( 'Site does not support widget', 'onepress-import' ); // explain why widget not imported
144
+ }
145
+
146
+ // Filter to modify settings object before conversion to array and import
147
+ // Leave this filter here for backwards compatibility with manipulating objects (before conversion to array below)
148
+ // Ideally the newer wie_widget_settings_array below will be used instead of this
149
+ $widget = apply_filters( 'wie_widget_settings', $widget ); // object
150
+
151
+ // Convert multidimensional objects to multidimensional arrays
152
+ // Some plugins like Jetpack Widget Visibility store settings as multidimensional arrays
153
+ // Without this, they are imported as objects and cause fatal error on Widgets page
154
+ // If this creates problems for plugins that do actually intend settings in objects then may need to consider other approach: https://wordpress.org/support/topic/problem-with-array-of-arrays
155
+ // It is probably much more likely that arrays are used than objects, however
156
+ $widget = json_decode( json_encode( $widget ), true );
157
+
158
+ // Filter to modify settings array
159
+ // This is preferred over the older wie_widget_settings filter above
160
+ // Do before identical check because changes may make it identical to end result (such as URL replacements)
161
+ $widget = apply_filters( 'wie_widget_settings_array', $widget );
162
+
163
+ // Does widget with identical settings already exist in same sidebar?
164
+ if ( ! $fail && isset( $widget_instances[$id_base] ) ) {
165
+
166
+ // Get existing widgets in this sidebar
167
+ $sidebars_widgets = get_option( 'sidebars_widgets' );
168
+ $sidebar_widgets = isset( $sidebars_widgets[$use_sidebar_id] ) ? $sidebars_widgets[$use_sidebar_id] : array(); // check Inactive if that's where will go
169
+
170
+ // Loop widgets with ID base
171
+ $single_widget_instances = ! empty( $widget_instances[$id_base] ) ? $widget_instances[$id_base] : array();
172
+ foreach ( $single_widget_instances as $check_id => $check_widget ) {
173
+
174
+ // Is widget in same sidebar and has identical settings?
175
+ if ( in_array( "$id_base-$check_id", $sidebar_widgets ) && (array) $widget == $check_widget ) {
176
+
177
+ $fail = true;
178
+ $widget_message_type = 'warning';
179
+ $widget_message = __( 'Widget already exists', 'onepress-import' ); // explain why widget not imported
180
+
181
+ break;
182
+
183
+ }
184
+
185
+ }
186
+
187
+ }
188
+
189
+ // No failure
190
+ if ( ! $fail ) {
191
+
192
+ // Add widget instance
193
+ $single_widget_instances = get_option( 'widget_' . $id_base ); // all instances for that widget ID base, get fresh every time
194
+ $single_widget_instances = ! empty( $single_widget_instances ) ? $single_widget_instances : array( '_multiwidget' => 1 ); // start fresh if have to
195
+ $single_widget_instances[] = $widget; // add it
196
+
197
+ // Get the key it was given
198
+ end( $single_widget_instances );
199
+ $new_instance_id_number = key( $single_widget_instances );
200
+
201
+ // If key is 0, make it 1
202
+ // When 0, an issue can occur where adding a widget causes data from other widget to load, and the widget doesn't stick (reload wipes it)
203
+ if ( '0' === strval( $new_instance_id_number ) ) {
204
+ $new_instance_id_number = 1;
205
+ $single_widget_instances[$new_instance_id_number] = $single_widget_instances[0];
206
+ unset( $single_widget_instances[0] );
207
+ }
208
+
209
+ // Move _multiwidget to end of array for uniformity
210
+ if ( isset( $single_widget_instances['_multiwidget'] ) ) {
211
+ $multiwidget = $single_widget_instances['_multiwidget'];
212
+ unset( $single_widget_instances['_multiwidget'] );
213
+ $single_widget_instances['_multiwidget'] = $multiwidget;
214
+ }
215
+
216
+ // Update option with new widget
217
+ update_option( 'widget_' . $id_base, $single_widget_instances );
218
+
219
+ // Assign widget instance to sidebar
220
+ $sidebars_widgets = get_option( 'sidebars_widgets' ); // which sidebars have which widgets, get fresh every time
221
+ $new_instance_id = $id_base . '-' . $new_instance_id_number; // use ID number from new widget instance
222
+ $sidebars_widgets[$use_sidebar_id][] = $new_instance_id; // add new instance to sidebar
223
+ update_option( 'sidebars_widgets', $sidebars_widgets ); // save the amended data
224
+
225
+ // After widget import action
226
+ $after_widget_import = array(
227
+ 'sidebar' => $use_sidebar_id,
228
+ 'sidebar_old' => $sidebar_id,
229
+ 'widget' => $widget,
230
+ 'widget_type' => $id_base,
231
+ 'widget_id' => $new_instance_id,
232
+ 'widget_id_old' => $widget_instance_id,
233
+ 'widget_id_num' => $new_instance_id_number,
234
+ 'widget_id_num_old' => $instance_id_number
235
+ );
236
+ do_action( 'wie_after_widget_import', $after_widget_import );
237
+
238
+ // Success message
239
+ if ( $sidebar_available ) {
240
+ $widget_message_type = 'success';
241
+ $widget_message = __( 'Imported', 'onepress-import' );
242
+ } else {
243
+ $widget_message_type = 'warning';
244
+ $widget_message = __( 'Imported to Inactive', 'onepress-import' );
245
+ }
246
+
247
+ }
248
+
249
+ // Result for widget instance
250
+ $results[$sidebar_id]['widgets'][$widget_instance_id]['name'] = isset( $available_widgets[$id_base]['name'] ) ? $available_widgets[$id_base]['name'] : $id_base; // widget name or ID if name not available (not supported by site)
251
+ $results[$sidebar_id]['widgets'][$widget_instance_id]['title'] = ! empty( $widget['title'] ) ? $widget['title'] : __( 'No Title', 'onepress-import' ); // show "No Title" if widget instance is untitled
252
+ $results[$sidebar_id]['widgets'][$widget_instance_id]['message_type'] = $widget_message_type;
253
+ $results[$sidebar_id]['widgets'][$widget_instance_id]['message'] = $widget_message;
254
+
255
+ }
256
+
257
+ }
258
+
259
+ // Return results
260
+ return $results;
261
+
262
+ }
263
+
264
+ /**
265
+ * JSON customize data
266
+ *
267
+ * Export by function get_theme_mods
268
+ *
269
+ * @see get_theme_mods
270
+ *
271
+ * @param $file
272
+ * @return bool
273
+ */
274
+ function import_customize( $data ) {
275
+ if ( ! is_array( $data ) ) {
276
+ return false;
277
+ }
278
+ // Loop through the mods.
279
+ foreach ( $data as $key => $val ) {
280
+ // Save the mod.
281
+ set_theme_mod( $key, $val );
282
+ }
283
+
284
+ }
285
+
286
+ /**
287
+ * Import XML file
288
+ *
289
+ * xml file export by WP exporter
290
+ *
291
+ * @param $file
292
+ */
293
+ function import_xml( $file ){
294
+
295
+ if ( ! is_file( $file ) ) {
296
+ die( 'xml_not_found' );
297
+ }
298
+
299
+ if ( ! defined('WP_LOAD_IMPORTERS') ) {
300
+ define( 'WP_LOAD_IMPORTERS', true );
301
+ }
302
+
303
+ require_once ABSPATH . 'wp-admin/includes/import.php';
304
+ $importer_error = false;
305
+
306
+ if ( !class_exists( 'WP_Importer' ) ) {
307
+ $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
308
+ if ( file_exists( $class_wp_importer ) ){
309
+ require_once($class_wp_importer);
310
+ } else {
311
+ $importer_error = true;
312
+ }
313
+ }
314
+
315
+ if ( !class_exists( 'WP_Import' ) ) {
316
+ $class_wp_import = dirname( __FILE__ ) .'/wordpress-importer.php';
317
+ if ( file_exists( $class_wp_import ) ) {
318
+ require_once $class_wp_import;
319
+ } else {
320
+ $importer_error = true;
321
+ }
322
+ }
323
+
324
+ if( $importer_error ){
325
+ esc_html_e( "Error load importer.", 'ftdi');
326
+ } else {
327
+ $wp_import = new WP_Import();
328
+ $wp_import->fetch_attachments = true; // download attachment
329
+ $wp_import->import( $file );
330
+ $this->processed_posts = $wp_import->processed_posts;
331
+ do_action( 'ft_import_after_xml_imported', $wp_import );
332
+ return $wp_import;
333
+ }
334
+ return false;
335
+ }
336
+
337
+ /**
338
+ * Available widgets
339
+ *
340
+ * Gather site's widgets into array with ID base, name, etc.
341
+ * Used by export and import functions.
342
+ *
343
+ * @since 0.4
344
+ * @global array $wp_registered_widget_updates
345
+ * @return array Widget information
346
+ */
347
+ static function get_available_widgets() {
348
+
349
+ global $wp_registered_widget_controls;
350
+
351
+ $widget_controls = $wp_registered_widget_controls;
352
+
353
+ $available_widgets = array();
354
+
355
+ foreach ( $widget_controls as $widget ) {
356
+
357
+ if ( ! empty( $widget['id_base'] ) && ! isset( $available_widgets[$widget['id_base']] ) ) { // no dupes
358
+
359
+ $available_widgets[$widget['id_base']]['id_base'] = $widget['id_base'];
360
+ $available_widgets[$widget['id_base']]['name'] = $widget['name'];
361
+
362
+ }
363
+
364
+ }
365
+
366
+ return $available_widgets;
367
+
368
+ }
369
+
370
+
371
+ /**
372
+ * Generate Widgets export data
373
+ *
374
+ * @since 0.1
375
+ * @return string Export file contents
376
+ */
377
+ static function generate_widgets_export_data() {
378
+
379
+ // Get all available widgets site supports
380
+ $available_widgets = self::get_available_widgets();
381
+
382
+ // Get all widget instances for each widget
383
+ $widget_instances = array();
384
+ foreach ( $available_widgets as $widget_data ) {
385
+
386
+ // Get all instances for this ID base
387
+ $instances = get_option( 'widget_' . $widget_data['id_base'] );
388
+
389
+ // Have instances
390
+ if ( ! empty( $instances ) ) {
391
+
392
+ // Loop instances
393
+ foreach ( $instances as $instance_id => $instance_data ) {
394
+
395
+ // Key is ID (not _multiwidget)
396
+ if ( is_numeric( $instance_id ) ) {
397
+ $unique_instance_id = $widget_data['id_base'] . '-' . $instance_id;
398
+ $widget_instances[$unique_instance_id] = $instance_data;
399
+ }
400
+
401
+ }
402
+
403
+ }
404
+
405
+ }
406
+
407
+ // Gather sidebars with their widget instances
408
+ $sidebars_widgets = get_option( 'sidebars_widgets' ); // get sidebars and their unique widgets IDs
409
+ $sidebars_widget_instances = array();
410
+ foreach ( $sidebars_widgets as $sidebar_id => $widget_ids ) {
411
+
412
+ // Skip inactive widgets
413
+ if ( 'wp_inactive_widgets' == $sidebar_id ) {
414
+ continue;
415
+ }
416
+
417
+ // Skip if no data or not an array (array_version)
418
+ if ( ! is_array( $widget_ids ) || empty( $widget_ids ) ) {
419
+ continue;
420
+ }
421
+
422
+ // Loop widget IDs for this sidebar
423
+ foreach ( $widget_ids as $widget_id ) {
424
+
425
+ // Is there an instance for this widget ID?
426
+ if ( isset( $widget_instances[$widget_id] ) ) {
427
+
428
+ // Add to array
429
+ $sidebars_widget_instances[$sidebar_id][$widget_id] = $widget_instances[$widget_id];
430
+
431
+ }
432
+
433
+ }
434
+
435
+ }
436
+
437
+ // Filter pre-encoded data
438
+ $data = apply_filters( 'ft_demo_export_widgets_data', $sidebars_widget_instances );
439
+
440
+ // Encode the data for file contents
441
+ return $data;
442
+
443
+ }
444
+
445
+ /**
446
+ * generate theme customize data
447
+ *
448
+ * @return mixed|string|void
449
+ */
450
+ static function generate_theme_mods_export_data(){
451
+ $data = get_theme_mods();
452
+ if ( ! $data ) {
453
+ return '';
454
+ }
455
+ return json_encode( $data ) ;
456
+ }
457
+
458
+ /**
459
+ * generate option data
460
+ *
461
+ * @param $option_key
462
+ * @return mixed|string|void
463
+ */
464
+ static function generate_options_export_data( $option_key ){
465
+ $options = get_option( $option_key , array() );
466
+ if ( ! $options ) {
467
+ return '';
468
+ }
469
+ $options = stripslashes_deep( $options );
470
+ return json_encode( $options ) ;
471
+ }
472
+
473
+ static function get_update_keys(){
474
+
475
+ $key = 'ft_demo_customizer_keys';
476
+ $theme_slug = get_option( 'stylesheet' );
477
+ $data = get_option( $key );
478
+ if ( ! is_array( $data ) ) {
479
+ $data = array();
480
+ }
481
+ if ( isset( $data[ $theme_slug ] ) ){
482
+ return $data[ $theme_slug ];
483
+ }
484
+
485
+ $r = wp_remote_post( admin_url( 'customize.php' ), array(
486
+ 'method' => 'POST',
487
+ 'timeout' => 45,
488
+ 'redirection' => 5,
489
+ 'httpversion' => '1.0',
490
+ 'blocking' => true,
491
+ 'headers' => array(),
492
+ 'cookies' => array(
493
+ SECURE_AUTH_COOKIE => $_COOKIE[ SECURE_AUTH_COOKIE ],
494
+ AUTH_COOKIE => $_COOKIE[ AUTH_COOKIE ],
495
+ LOGGED_IN_COOKIE => $_COOKIE[ LOGGED_IN_COOKIE ],
496
+ )
497
+ )
498
+ );
499
+
500
+ if ( is_wp_error( $r ) ) {
501
+ return false;
502
+ } else {
503
+ global $wpdb;
504
+
505
+ $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $key ) );
506
+ $notoptions = wp_cache_get( 'notoptions', 'options' );
507
+ // Has to be get_row instead of get_var because of funkiness with 0, false, null values
508
+ if ( is_object( $row ) ) {
509
+ $value = $row->option_value;
510
+ $data = apply_filters( 'option_' . $key, maybe_unserialize( $value ), $key );
511
+ wp_cache_add( $key, $value, 'options' );
512
+ } else { // option does not exist, so we must cache its non-existence
513
+ if ( ! is_array( $notoptions ) ) {
514
+ $notoptions = array();
515
+ }
516
+ $notoptions[$key] = true;
517
+ wp_cache_set( 'notoptions', $notoptions, 'options' );
518
+
519
+ /** This filter is documented in wp-includes/option.php */
520
+ $data = apply_filters( 'default_option_' . $key, '', $key );
521
+ }
522
+
523
+ if ( ! is_array( $data ) ) {
524
+ $data = array();
525
+ }
526
+
527
+ if ( isset( $data[ $theme_slug ] ) ) {
528
+ return $data[ $theme_slug ];
529
+ }
530
+ }
531
+
532
+ return false;
533
+ }
534
+
535
+
536
+ static function generate_config(){
537
+ $nav_menu_locations = get_theme_mod( 'nav_menu_locations' );
538
+ // Just update the customizer keys
539
+
540
+ $regen_keys = self::get_update_keys();
541
+
542
+ $config = array(
543
+ 'home_url' => home_url('/'),
544
+ 'menus' => $nav_menu_locations,
545
+ 'pages' => array(
546
+ 'page_on_front' => get_option( 'page_on_front' ),
547
+ 'page_for_posts' => get_option( 'page_for_posts' ),
548
+ ),
549
+ 'options' => array(
550
+ 'show_on_front' => get_option( 'show_on_front' )
551
+ ),
552
+ 'theme_mods' => get_theme_mods(),
553
+ 'widgets' => self::generate_widgets_export_data(),
554
+ 'customizer_keys' => $regen_keys
555
+ );
556
+
557
+ $config = apply_filters( 'ft_demo_generate_config', $config );
558
+
559
+ return json_encode( $config );
560
+ }
561
+
562
+ /**
563
+ * Run import
564
+ */
565
+ function import(){
566
+ $wp_import = $this->import_xml( $this->xml_file );
567
+
568
+ $this->config_data = file_get_contents( $this->config_file );
569
+ $this->config_data = json_decode( $this->config_data , true );
570
+ $this->config_data = wp_parse_args( $this->config_data, array(
571
+ 'home_url' => '',
572
+ 'menus' => array(),
573
+ 'pages' => array(),
574
+ 'options' => array(),
575
+ 'theme_mods' => array(),
576
+ 'widgets' => array(),
577
+ 'customizer_keys' => array()
578
+ ) );
579
+ $this->import_customize( $this->config_data['theme_mods'] );
580
+ $this->import_widgets( $this->config_data['widgets'] );
581
+
582
+ if ( $wp_import ) {
583
+
584
+ // Update options
585
+ foreach ( ( array ) $this->config_data['options'] as $k => $v ) {
586
+ update_option( $k, $v );
587
+ }
588
+
589
+ // Update general pages.
590
+ if ( is_array( $this->config_data['pages'] ) ) {
591
+ foreach ( $this->config_data['pages'] as $k => $post_id ) {
592
+ if ( $post_id ) {
593
+ $id = isset( $wp_import->processed_posts[ $post_id ] ) ? $wp_import->processed_posts[ $post_id ] : $post_id;
594
+ update_option( $k, $id );
595
+ }
596
+ }
597
+ }
598
+
599
+ // Update menus
600
+ $nav_menu_locations = get_theme_mod( 'nav_menu_locations' );
601
+ foreach ( ( array ) $this->config_data['menus'] as $k => $menu_id ) {
602
+ if ( $menu_id ) {
603
+ $id = isset( $wp_import->processed_terms[ $menu_id ] ) ? $wp_import->processed_terms[ $menu_id ] : $menu_id;
604
+ $nav_menu_locations[ $k ] = $id;
605
+ }
606
+ }
607
+ set_theme_mod( 'nav_menu_locations', $nav_menu_locations );
608
+
609
+ // Update menu links
610
+ if ( $this->config_data['home_url'] ) {
611
+
612
+ $demo_url = trailingslashit( $this->config_data['home_url'] );
613
+ $home_url = site_url('/');
614
+
615
+ global $wpdb;
616
+
617
+ $sql = $wpdb->prepare(
618
+ "UPDATE {$wpdb->postmeta} SET `meta_value` = REPLACE (`meta_value`, '%s', '%s')",
619
+ $demo_url,
620
+ $home_url
621
+ );
622
+
623
+ $wpdb->query($sql);
624
+
625
+ }
626
+
627
+ // Re-setup meta keys
628
+ foreach ( ( array ) $this->config_data['customizer_keys'] as $k => $replace_keys ) {
629
+ $this->resetup_repeater_page_ids( $k , $replace_keys, $wp_import->processed_posts, $this->config_data['home_url'] );
630
+ }
631
+
632
+ echo 'demo_imported';
633
+ } else {
634
+ echo 'demo_import_failed';
635
+ }
636
+
637
+ do_action( 'ft_import_after_imported', $wp_import );
638
+ }
639
+
640
+
641
+ function resetup_repeater_page_ids( $theme_mod_name, $list_keys, $processed_posts = array(), $url ='', $option_type = 'theme_mod' ){
642
+ // Setup service
643
+ $data = get_theme_mod( $theme_mod_name );
644
+ if ( is_string( $list_keys ) ) {
645
+ switch( $list_keys ) {
646
+ case 'media':
647
+ $new_data = $processed_posts[ $data ];
648
+ if ( $option_type == 'option' ) {
649
+ update_option( $theme_mod_name , $new_data );
650
+ } else {
651
+ set_theme_mod( $theme_mod_name , $new_data );
652
+ }
653
+ break;
654
+ }
655
+ return;
656
+ }
657
+
658
+ if ( is_string( $data ) ) {
659
+ $data = json_decode( $data, true );
660
+ }
661
+ if ( ! is_array( $data ) ) {
662
+ return false;
663
+ }
664
+ if ( ! is_array( $processed_posts ) ) {
665
+ return false;
666
+ }
667
+
668
+ if ( $url ) {
669
+ $url = trailingslashit( $this->config_data['home_url'] );
670
+ }
671
+
672
+ $home = home_url('/');
673
+
674
+
675
+ foreach ($list_keys as $key_info) {
676
+ if ($key_info['type'] == 'post' || $key_info['type'] == 'page') {
677
+ foreach ($data as $k => $item) {
678
+ if (isset($item[$key_info['key']]) && isset ($processed_posts[$item[$key_info['key']]])) {
679
+ $data[$k][$key_info['key']] = $processed_posts[$item[$key_info['key']]];
680
+ }
681
+ }
682
+ } elseif ($key_info['type'] == 'media') {
683
+
684
+ $main_key = $key_info['key'];
685
+ $sub_key_id = 'id';
686
+ $sub_key_url = 'url';
687
+ if ($main_key) {
688
+
689
+ foreach ($data as $k => $item) {
690
+ if (isset($item[$sub_key_id]) && is_array($item[$sub_key_id])) {
691
+ if (isset ($item[$main_key][$sub_key_id])) {
692
+ $data[$item][$main_key][$sub_key_id] = $processed_posts[$item[$main_key][$sub_key_id]];
693
+ }
694
+ if (isset ($item[$main_key][$sub_key_url])) {
695
+ $data[$item][$main_key][$sub_key_url] = str_replace($url, $home, $item[$main_key][$sub_key_url]);
696
+ }
697
+ }
698
+ }
699
+
700
+ }
701
+
702
+
703
+ }
704
+ }
705
+
706
+
707
+ if ( $option_type == 'option' ) {
708
+ update_option( $theme_mod_name , $data );
709
+ } else {
710
+ set_theme_mod( $theme_mod_name , $data );
711
+ }
712
+
713
+
714
+ }
715
+
716
+ }
inc/parsers.php ADDED
@@ -0,0 +1,691 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * WordPress eXtended RSS file parser implementations
4
+ *
5
+ * @package WordPress
6
+ * @subpackage Importer
7
+ */
8
+
9
+ /**
10
+ * WordPress Importer class for managing parsing of WXR files.
11
+ */
12
+ class WXR_Parser {
13
+ function parse( $file ) {
14
+ // Attempt to use proper XML parsers first
15
+ if ( extension_loaded( 'simplexml' ) ) {
16
+ $parser = new WXR_Parser_SimpleXML;
17
+ $result = $parser->parse( $file );
18
+
19
+ // If SimpleXML succeeds or this is an invalid WXR file then return the results
20
+ if ( ! is_wp_error( $result ) || 'SimpleXML_parse_error' != $result->get_error_code() )
21
+ return $result;
22
+ } else if ( extension_loaded( 'xml' ) ) {
23
+ $parser = new WXR_Parser_XML;
24
+ $result = $parser->parse( $file );
25
+
26
+ // If XMLParser succeeds or this is an invalid WXR file then return the results
27
+ if ( ! is_wp_error( $result ) || 'XML_parse_error' != $result->get_error_code() )
28
+ return $result;
29
+ }
30
+
31
+ // We have a malformed XML file, so display the error and fallthrough to regex
32
+ if ( isset($result) && defined('IMPORT_DEBUG') && IMPORT_DEBUG ) {
33
+ echo '<pre>';
34
+ if ( 'SimpleXML_parse_error' == $result->get_error_code() ) {
35
+ foreach ( $result->get_error_data() as $error )
36
+ echo $error->line . ':' . $error->column . ' ' . esc_html( $error->message ) . "\n";
37
+ } else if ( 'XML_parse_error' == $result->get_error_code() ) {
38
+ $error = $result->get_error_data();
39
+ echo $error[0] . ':' . $error[1] . ' ' . esc_html( $error[2] );
40
+ }
41
+ echo '</pre>';
42
+ echo '<p><strong>' . __( 'There was an error when reading this WXR file', 'wordpress-importer' ) . '</strong><br />';
43
+ echo __( 'Details are shown above. The importer will now try again with a different parser...', 'wordpress-importer' ) . '</p>';
44
+ }
45
+
46
+ // use regular expressions if nothing else available or this is bad XML
47
+ $parser = new WXR_Parser_Regex;
48
+ return $parser->parse( $file );
49
+ }
50
+ }
51
+
52
+ /**
53
+ * WXR Parser that makes use of the SimpleXML PHP extension.
54
+ */
55
+ class WXR_Parser_SimpleXML {
56
+ function parse( $file ) {
57
+ $authors = $posts = $categories = $tags = $terms = array();
58
+
59
+ $internal_errors = libxml_use_internal_errors(true);
60
+
61
+ $dom = new DOMDocument;
62
+ $old_value = null;
63
+ if ( function_exists( 'libxml_disable_entity_loader' ) ) {
64
+ $old_value = libxml_disable_entity_loader( true );
65
+ }
66
+ $success = $dom->loadXML( file_get_contents( $file ) );
67
+ if ( ! is_null( $old_value ) ) {
68
+ libxml_disable_entity_loader( $old_value );
69
+ }
70
+
71
+ if ( ! $success || isset( $dom->doctype ) ) {
72
+ return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
73
+ }
74
+
75
+ $xml = simplexml_import_dom( $dom );
76
+ unset( $dom );
77
+
78
+ // halt if loading produces an error
79
+ if ( ! $xml )
80
+ return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
81
+
82
+ $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version');
83
+ if ( ! $wxr_version )
84
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
85
+
86
+ $wxr_version = (string) trim( $wxr_version[0] );
87
+ // confirm that we are dealing with the correct file format
88
+ if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) )
89
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
90
+
91
+ $base_url = $xml->xpath('/rss/channel/wp:base_site_url');
92
+ $base_url = (string) trim( $base_url[0] );
93
+
94
+ $namespaces = $xml->getDocNamespaces();
95
+ if ( ! isset( $namespaces['wp'] ) )
96
+ $namespaces['wp'] = 'http://wordpress.org/export/1.1/';
97
+ if ( ! isset( $namespaces['excerpt'] ) )
98
+ $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/';
99
+
100
+ // grab authors
101
+ foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) {
102
+ $a = $author_arr->children( $namespaces['wp'] );
103
+ $login = (string) $a->author_login;
104
+ $authors[$login] = array(
105
+ 'author_id' => (int) $a->author_id,
106
+ 'author_login' => $login,
107
+ 'author_email' => (string) $a->author_email,
108
+ 'author_display_name' => (string) $a->author_display_name,
109
+ 'author_first_name' => (string) $a->author_first_name,
110
+ 'author_last_name' => (string) $a->author_last_name
111
+ );
112
+ }
113
+
114
+ // grab cats, tags and terms
115
+ foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) {
116
+ $t = $term_arr->children( $namespaces['wp'] );
117
+ $category = array(
118
+ 'term_id' => (int) $t->term_id,
119
+ 'category_nicename' => (string) $t->category_nicename,
120
+ 'category_parent' => (string) $t->category_parent,
121
+ 'cat_name' => (string) $t->cat_name,
122
+ 'category_description' => (string) $t->category_description
123
+ );
124
+
125
+ foreach ( $t->termmeta as $meta ) {
126
+ $category['termmeta'][] = array(
127
+ 'key' => (string) $meta->meta_key,
128
+ 'value' => (string) $meta->meta_value
129
+ );
130
+ }
131
+
132
+ $categories[] = $category;
133
+ }
134
+
135
+ foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) {
136
+ $t = $term_arr->children( $namespaces['wp'] );
137
+ $tag = array(
138
+ 'term_id' => (int) $t->term_id,
139
+ 'tag_slug' => (string) $t->tag_slug,
140
+ 'tag_name' => (string) $t->tag_name,
141
+ 'tag_description' => (string) $t->tag_description
142
+ );
143
+
144
+ foreach ( $t->termmeta as $meta ) {
145
+ $tag['termmeta'][] = array(
146
+ 'key' => (string) $meta->meta_key,
147
+ 'value' => (string) $meta->meta_value
148
+ );
149
+ }
150
+
151
+ $tags[] = $tag;
152
+ }
153
+
154
+ foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) {
155
+ $t = $term_arr->children( $namespaces['wp'] );
156
+ $term = array(
157
+ 'term_id' => (int) $t->term_id,
158
+ 'term_taxonomy' => (string) $t->term_taxonomy,
159
+ 'slug' => (string) $t->term_slug,
160
+ 'term_parent' => (string) $t->term_parent,
161
+ 'term_name' => (string) $t->term_name,
162
+ 'term_description' => (string) $t->term_description
163
+ );
164
+
165
+ foreach ( $t->termmeta as $meta ) {
166
+ $term['termmeta'][] = array(
167
+ 'key' => (string) $meta->meta_key,
168
+ 'value' => (string) $meta->meta_value
169
+ );
170
+ }
171
+
172
+ $terms[] = $term;
173
+ }
174
+
175
+ // grab posts
176
+ foreach ( $xml->channel->item as $item ) {
177
+ $post = array(
178
+ 'post_title' => (string) $item->title,
179
+ 'guid' => (string) $item->guid,
180
+ );
181
+
182
+ $dc = $item->children( 'http://purl.org/dc/elements/1.1/' );
183
+ $post['post_author'] = (string) $dc->creator;
184
+
185
+ $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' );
186
+ $excerpt = $item->children( $namespaces['excerpt'] );
187
+ $post['post_content'] = (string) $content->encoded;
188
+ $post['post_excerpt'] = (string) $excerpt->encoded;
189
+
190
+ $wp = $item->children( $namespaces['wp'] );
191
+ $post['post_id'] = (int) $wp->post_id;
192
+ $post['post_date'] = (string) $wp->post_date;
193
+ $post['post_date_gmt'] = (string) $wp->post_date_gmt;
194
+ $post['comment_status'] = (string) $wp->comment_status;
195
+ $post['ping_status'] = (string) $wp->ping_status;
196
+ $post['post_name'] = (string) $wp->post_name;
197
+ $post['status'] = (string) $wp->status;
198
+ $post['post_parent'] = (int) $wp->post_parent;
199
+ $post['menu_order'] = (int) $wp->menu_order;
200
+ $post['post_type'] = (string) $wp->post_type;
201
+ $post['post_password'] = (string) $wp->post_password;
202
+ $post['is_sticky'] = (int) $wp->is_sticky;
203
+
204
+ if ( isset($wp->attachment_url) )
205
+ $post['attachment_url'] = (string) $wp->attachment_url;
206
+
207
+ foreach ( $item->category as $c ) {
208
+ $att = $c->attributes();
209
+ if ( isset( $att['nicename'] ) )
210
+ $post['terms'][] = array(
211
+ 'name' => (string) $c,
212
+ 'slug' => (string) $att['nicename'],
213
+ 'domain' => (string) $att['domain']
214
+ );
215
+ }
216
+
217
+ foreach ( $wp->postmeta as $meta ) {
218
+ $post['postmeta'][] = array(
219
+ 'key' => (string) $meta->meta_key,
220
+ 'value' => (string) $meta->meta_value
221
+ );
222
+ }
223
+
224
+ foreach ( $wp->comment as $comment ) {
225
+ $meta = array();
226
+ if ( isset( $comment->commentmeta ) ) {
227
+ foreach ( $comment->commentmeta as $m ) {
228
+ $meta[] = array(
229
+ 'key' => (string) $m->meta_key,
230
+ 'value' => (string) $m->meta_value
231
+ );
232
+ }
233
+ }
234
+
235
+ $post['comments'][] = array(
236
+ 'comment_id' => (int) $comment->comment_id,
237
+ 'comment_author' => (string) $comment->comment_author,
238
+ 'comment_author_email' => (string) $comment->comment_author_email,
239
+ 'comment_author_IP' => (string) $comment->comment_author_IP,
240
+ 'comment_author_url' => (string) $comment->comment_author_url,
241
+ 'comment_date' => (string) $comment->comment_date,
242
+ 'comment_date_gmt' => (string) $comment->comment_date_gmt,
243
+ 'comment_content' => (string) $comment->comment_content,
244
+ 'comment_approved' => (string) $comment->comment_approved,
245
+ 'comment_type' => (string) $comment->comment_type,
246
+ 'comment_parent' => (string) $comment->comment_parent,
247
+ 'comment_user_id' => (int) $comment->comment_user_id,
248
+ 'commentmeta' => $meta,
249
+ );
250
+ }
251
+
252
+ $posts[] = $post;
253
+ }
254
+
255
+ return array(
256
+ 'authors' => $authors,
257
+ 'posts' => $posts,
258
+ 'categories' => $categories,
259
+ 'tags' => $tags,
260
+ 'terms' => $terms,
261
+ 'base_url' => $base_url,
262
+ 'version' => $wxr_version
263
+ );
264
+ }
265
+ }
266
+
267
+ /**
268
+ * WXR Parser that makes use of the XML Parser PHP extension.
269
+ */
270
+ class WXR_Parser_XML {
271
+ var $wp_tags = array(
272
+ 'wp:post_id', 'wp:post_date', 'wp:post_date_gmt', 'wp:comment_status', 'wp:ping_status', 'wp:attachment_url',
273
+ 'wp:status', 'wp:post_name', 'wp:post_parent', 'wp:menu_order', 'wp:post_type', 'wp:post_password',
274
+ 'wp:is_sticky', 'wp:term_id', 'wp:category_nicename', 'wp:category_parent', 'wp:cat_name', 'wp:category_description',
275
+ 'wp:tag_slug', 'wp:tag_name', 'wp:tag_description', 'wp:term_taxonomy', 'wp:term_parent',
276
+ 'wp:term_name', 'wp:term_description', 'wp:author_id', 'wp:author_login', 'wp:author_email', 'wp:author_display_name',
277
+ 'wp:author_first_name', 'wp:author_last_name',
278
+ );
279
+ var $wp_sub_tags = array(
280
+ 'wp:comment_id', 'wp:comment_author', 'wp:comment_author_email', 'wp:comment_author_url',
281
+ 'wp:comment_author_IP', 'wp:comment_date', 'wp:comment_date_gmt', 'wp:comment_content',
282
+ 'wp:comment_approved', 'wp:comment_type', 'wp:comment_parent', 'wp:comment_user_id',
283
+ );
284
+
285
+ function parse( $file ) {
286
+ $this->wxr_version = $this->in_post = $this->cdata = $this->data = $this->sub_data = $this->in_tag = $this->in_sub_tag = false;
287
+ $this->authors = $this->posts = $this->term = $this->category = $this->tag = array();
288
+
289
+ $xml = xml_parser_create( 'UTF-8' );
290
+ xml_parser_set_option( $xml, XML_OPTION_SKIP_WHITE, 1 );
291
+ xml_parser_set_option( $xml, XML_OPTION_CASE_FOLDING, 0 );
292
+ xml_set_object( $xml, $this );
293
+ xml_set_character_data_handler( $xml, 'cdata' );
294
+ xml_set_element_handler( $xml, 'tag_open', 'tag_close' );
295
+
296
+ if ( ! xml_parse( $xml, file_get_contents( $file ), true ) ) {
297
+ $current_line = xml_get_current_line_number( $xml );
298
+ $current_column = xml_get_current_column_number( $xml );
299
+ $error_code = xml_get_error_code( $xml );
300
+ $error_string = xml_error_string( $error_code );
301
+ return new WP_Error( 'XML_parse_error', 'There was an error when reading this WXR file', array( $current_line, $current_column, $error_string ) );
302
+ }
303
+ xml_parser_free( $xml );
304
+
305
+ if ( ! preg_match( '/^\d+\.\d+$/', $this->wxr_version ) )
306
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
307
+
308
+ return array(
309
+ 'authors' => $this->authors,
310
+ 'posts' => $this->posts,
311
+ 'categories' => $this->category,
312
+ 'tags' => $this->tag,
313
+ 'terms' => $this->term,
314
+ 'base_url' => $this->base_url,
315
+ 'version' => $this->wxr_version
316
+ );
317
+ }
318
+
319
+ function tag_open( $parse, $tag, $attr ) {
320
+ if ( in_array( $tag, $this->wp_tags ) ) {
321
+ $this->in_tag = substr( $tag, 3 );
322
+ return;
323
+ }
324
+
325
+ if ( in_array( $tag, $this->wp_sub_tags ) ) {
326
+ $this->in_sub_tag = substr( $tag, 3 );
327
+ return;
328
+ }
329
+
330
+ switch ( $tag ) {
331
+ case 'category':
332
+ if ( isset($attr['domain'], $attr['nicename']) ) {
333
+ $this->sub_data['domain'] = $attr['domain'];
334
+ $this->sub_data['slug'] = $attr['nicename'];
335
+ }
336
+ break;
337
+ case 'item': $this->in_post = true;
338
+ case 'title': if ( $this->in_post ) $this->in_tag = 'post_title'; break;
339
+ case 'guid': $this->in_tag = 'guid'; break;
340
+ case 'dc:creator': $this->in_tag = 'post_author'; break;
341
+ case 'content:encoded': $this->in_tag = 'post_content'; break;
342
+ case 'excerpt:encoded': $this->in_tag = 'post_excerpt'; break;
343
+
344
+ case 'wp:term_slug': $this->in_tag = 'slug'; break;
345
+ case 'wp:meta_key': $this->in_sub_tag = 'key'; break;
346
+ case 'wp:meta_value': $this->in_sub_tag = 'value'; break;
347
+ }
348
+ }
349
+
350
+ function cdata( $parser, $cdata ) {
351
+ if ( ! trim( $cdata ) )
352
+ return;
353
+
354
+ if ( false !== $this->in_tag || false !== $this->in_sub_tag ) {
355
+ $this->cdata .= $cdata;
356
+ } else {
357
+ $this->cdata .= trim( $cdata );
358
+ }
359
+ }
360
+
361
+ function tag_close( $parser, $tag ) {
362
+ switch ( $tag ) {
363
+ case 'wp:comment':
364
+ unset( $this->sub_data['key'], $this->sub_data['value'] ); // remove meta sub_data
365
+ if ( ! empty( $this->sub_data ) )
366
+ $this->data['comments'][] = $this->sub_data;
367
+ $this->sub_data = false;
368
+ break;
369
+ case 'wp:commentmeta':
370
+ $this->sub_data['commentmeta'][] = array(
371
+ 'key' => $this->sub_data['key'],
372
+ 'value' => $this->sub_data['value']
373
+ );
374
+ break;
375
+ case 'category':
376
+ if ( ! empty( $this->sub_data ) ) {
377
+ $this->sub_data['name'] = $this->cdata;
378
+ $this->data['terms'][] = $this->sub_data;
379
+ }
380
+ $this->sub_data = false;
381
+ break;
382
+ case 'wp:postmeta':
383
+ if ( ! empty( $this->sub_data ) )
384
+ $this->data['postmeta'][] = $this->sub_data;
385
+ $this->sub_data = false;
386
+ break;
387
+ case 'item':
388
+ $this->posts[] = $this->data;
389
+ $this->data = false;
390
+ break;
391
+ case 'wp:category':
392
+ case 'wp:tag':
393
+ case 'wp:term':
394
+ $n = substr( $tag, 3 );
395
+ array_push( $this->$n, $this->data );
396
+ $this->data = false;
397
+ break;
398
+ case 'wp:author':
399
+ if ( ! empty($this->data['author_login']) )
400
+ $this->authors[$this->data['author_login']] = $this->data;
401
+ $this->data = false;
402
+ break;
403
+ case 'wp:base_site_url':
404
+ $this->base_url = $this->cdata;
405
+ break;
406
+ case 'wp:wxr_version':
407
+ $this->wxr_version = $this->cdata;
408
+ break;
409
+
410
+ default:
411
+ if ( $this->in_sub_tag ) {
412
+ $this->sub_data[$this->in_sub_tag] = ! empty( $this->cdata ) ? $this->cdata : '';
413
+ $this->in_sub_tag = false;
414
+ } else if ( $this->in_tag ) {
415
+ $this->data[$this->in_tag] = ! empty( $this->cdata ) ? $this->cdata : '';
416
+ $this->in_tag = false;
417
+ }
418
+ }
419
+
420
+ $this->cdata = false;
421
+ }
422
+ }
423
+
424
+ /**
425
+ * WXR Parser that uses regular expressions. Fallback for installs without an XML parser.
426
+ */
427
+ class WXR_Parser_Regex {
428
+ var $authors = array();
429
+ var $posts = array();
430
+ var $categories = array();
431
+ var $tags = array();
432
+ var $terms = array();
433
+ var $base_url = '';
434
+
435
+ function __construct() {
436
+ $this->has_gzip = is_callable( 'gzopen' );
437
+ }
438
+
439
+ function parse( $file ) {
440
+ $wxr_version = $in_post = false;
441
+
442
+ $fp = $this->fopen( $file, 'r' );
443
+ if ( $fp ) {
444
+ while ( ! $this->feof( $fp ) ) {
445
+ $importline = rtrim( $this->fgets( $fp ) );
446
+
447
+ if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) )
448
+ $wxr_version = $version[1];
449
+
450
+ if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) {
451
+ preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url );
452
+ $this->base_url = $url[1];
453
+ continue;
454
+ }
455
+ if ( false !== strpos( $importline, '<wp:category>' ) ) {
456
+ preg_match( '|<wp:category>(.*?)</wp:category>|is', $importline, $category );
457
+ $this->categories[] = $this->process_category( $category[1] );
458
+ continue;
459
+ }
460
+ if ( false !== strpos( $importline, '<wp:tag>' ) ) {
461
+ preg_match( '|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag );
462
+ $this->tags[] = $this->process_tag( $tag[1] );
463
+ continue;
464
+ }
465
+ if ( false !== strpos( $importline, '<wp:term>' ) ) {
466
+ preg_match( '|<wp:term>(.*?)</wp:term>|is', $importline, $term );
467
+ $this->terms[] = $this->process_term( $term[1] );
468
+ continue;
469
+ }
470
+ if ( false !== strpos( $importline, '<wp:author>' ) ) {
471
+ preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author );
472
+ $a = $this->process_author( $author[1] );
473
+ $this->authors[$a['author_login']] = $a;
474
+ continue;
475
+ }
476
+ if ( false !== strpos( $importline, '<item>' ) ) {
477
+ $post = '';
478
+ $in_post = true;
479
+ continue;
480
+ }
481
+ if ( false !== strpos( $importline, '</item>' ) ) {
482
+ $in_post = false;
483
+ $this->posts[] = $this->process_post( $post );
484
+ continue;
485
+ }
486
+ if ( $in_post ) {
487
+ $post .= $importline . "\n";
488
+ }
489
+ }
490
+
491
+ $this->fclose($fp);
492
+ }
493
+
494
+ if ( ! $wxr_version )
495
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
496
+
497
+ return array(
498
+ 'authors' => $this->authors,
499
+ 'posts' => $this->posts,
500
+ 'categories' => $this->categories,
501
+ 'tags' => $this->tags,
502
+ 'terms' => $this->terms,
503
+ 'base_url' => $this->base_url,
504
+ 'version' => $wxr_version
505
+ );
506
+ }
507
+
508
+ function get_tag( $string, $tag ) {
509
+ preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return );
510
+ if ( isset( $return[1] ) ) {
511
+ if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) {
512
+ if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) {
513
+ preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches );
514
+ $return = '';
515
+ foreach( $matches[1] as $match )
516
+ $return .= $match;
517
+ } else {
518
+ $return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] );
519
+ }
520
+ } else {
521
+ $return = $return[1];
522
+ }
523
+ } else {
524
+ $return = '';
525
+ }
526
+ return $return;
527
+ }
528
+
529
+ function process_category( $c ) {
530
+ return array(
531
+ 'term_id' => $this->get_tag( $c, 'wp:term_id' ),
532
+ 'cat_name' => $this->get_tag( $c, 'wp:cat_name' ),
533
+ 'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ),
534
+ 'category_parent' => $this->get_tag( $c, 'wp:category_parent' ),
535
+ 'category_description' => $this->get_tag( $c, 'wp:category_description' ),
536
+ );
537
+ }
538
+
539
+ function process_tag( $t ) {
540
+ return array(
541
+ 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
542
+ 'tag_name' => $this->get_tag( $t, 'wp:tag_name' ),
543
+ 'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ),
544
+ 'tag_description' => $this->get_tag( $t, 'wp:tag_description' ),
545
+ );
546
+ }
547
+
548
+ function process_term( $t ) {
549
+ return array(
550
+ 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
551
+ 'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ),
552
+ 'slug' => $this->get_tag( $t, 'wp:term_slug' ),
553
+ 'term_parent' => $this->get_tag( $t, 'wp:term_parent' ),
554
+ 'term_name' => $this->get_tag( $t, 'wp:term_name' ),
555
+ 'term_description' => $this->get_tag( $t, 'wp:term_description' ),
556
+ );
557
+ }
558
+
559
+ function process_author( $a ) {
560
+ return array(
561
+ 'author_id' => $this->get_tag( $a, 'wp:author_id' ),
562
+ 'author_login' => $this->get_tag( $a, 'wp:author_login' ),
563
+ 'author_email' => $this->get_tag( $a, 'wp:author_email' ),
564
+ 'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ),
565
+ 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ),
566
+ 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ),
567
+ );
568
+ }
569
+
570
+ function process_post( $post ) {
571
+ $post_id = $this->get_tag( $post, 'wp:post_id' );
572
+ $post_title = $this->get_tag( $post, 'title' );
573
+ $post_date = $this->get_tag( $post, 'wp:post_date' );
574
+ $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
575
+ $comment_status = $this->get_tag( $post, 'wp:comment_status' );
576
+ $ping_status = $this->get_tag( $post, 'wp:ping_status' );
577
+ $status = $this->get_tag( $post, 'wp:status' );
578
+ $post_name = $this->get_tag( $post, 'wp:post_name' );
579
+ $post_parent = $this->get_tag( $post, 'wp:post_parent' );
580
+ $menu_order = $this->get_tag( $post, 'wp:menu_order' );
581
+ $post_type = $this->get_tag( $post, 'wp:post_type' );
582
+ $post_password = $this->get_tag( $post, 'wp:post_password' );
583
+ $is_sticky = $this->get_tag( $post, 'wp:is_sticky' );
584
+ $guid = $this->get_tag( $post, 'guid' );
585
+ $post_author = $this->get_tag( $post, 'dc:creator' );
586
+
587
+ $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
588
+ $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt );
589
+ $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt );
590
+ $post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt );
591
+
592
+ $post_content = $this->get_tag( $post, 'content:encoded' );
593
+ $post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content );
594
+ $post_content = str_replace( '<br>', '<br />', $post_content );
595
+ $post_content = str_replace( '<hr>', '<hr />', $post_content );
596
+
597
+ $postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt',
598
+ 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent',
599
+ 'menu_order', 'post_type', 'post_password', 'is_sticky'
600
+ );
601
+
602
+ $attachment_url = $this->get_tag( $post, 'wp:attachment_url' );
603
+ if ( $attachment_url )
604
+ $postdata['attachment_url'] = $attachment_url;
605
+
606
+ preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER );
607
+ foreach ( $terms as $t ) {
608
+ $post_terms[] = array(
609
+ 'slug' => $t[2],
610
+ 'domain' => $t[1],
611
+ 'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ),
612
+ );
613
+ }
614
+ if ( ! empty( $post_terms ) ) $postdata['terms'] = $post_terms;
615
+
616
+ preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments );
617
+ $comments = $comments[1];
618
+ if ( $comments ) {
619
+ foreach ( $comments as $comment ) {
620
+ preg_match_all( '|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta );
621
+ $commentmeta = $commentmeta[1];
622
+ $c_meta = array();
623
+ foreach ( $commentmeta as $m ) {
624
+ $c_meta[] = array(
625
+ 'key' => $this->get_tag( $m, 'wp:meta_key' ),
626
+ 'value' => $this->get_tag( $m, 'wp:meta_value' ),
627
+ );
628
+ }
629
+
630
+ $post_comments[] = array(
631
+ 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ),
632
+ 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ),
633
+ 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ),
634
+ 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ),
635
+ 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ),
636
+ 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ),
637
+ 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ),
638
+ 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ),
639
+ 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ),
640
+ 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ),
641
+ 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ),
642
+ 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ),
643
+ 'commentmeta' => $c_meta,
644
+ );
645
+ }
646
+ }
647
+ if ( ! empty( $post_comments ) ) $postdata['comments'] = $post_comments;
648
+
649
+ preg_match_all( '|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta );
650
+ $postmeta = $postmeta[1];
651
+ if ( $postmeta ) {
652
+ foreach ( $postmeta as $p ) {
653
+ $post_postmeta[] = array(
654
+ 'key' => $this->get_tag( $p, 'wp:meta_key' ),
655
+ 'value' => $this->get_tag( $p, 'wp:meta_value' ),
656
+ );
657
+ }
658
+ }
659
+ if ( ! empty( $post_postmeta ) ) $postdata['postmeta'] = $post_postmeta;
660
+
661
+ return $postdata;
662
+ }
663
+
664
+ function _normalize_tag( $matches ) {
665
+ return '<' . strtolower( $matches[1] );
666
+ }
667
+
668
+ function fopen( $filename, $mode = 'r' ) {
669
+ if ( $this->has_gzip )
670
+ return gzopen( $filename, $mode );
671
+ return fopen( $filename, $mode );
672
+ }
673
+
674
+ function feof( $fp ) {
675
+ if ( $this->has_gzip )
676
+ return gzeof( $fp );
677
+ return feof( $fp );
678
+ }
679
+
680
+ function fgets( $fp, $len = 8192 ) {
681
+ if ( $this->has_gzip )
682
+ return gzgets( $fp, $len );
683
+ return fgets( $fp, $len );
684
+ }
685
+
686
+ function fclose( $fp ) {
687
+ if ( $this->has_gzip )
688
+ return gzclose( $fp );
689
+ return fclose( $fp );
690
+ }
691
+ }
inc/wordpress-importer.php ADDED
@@ -0,0 +1,1215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin URI: http://wordpress.org/extend/plugins/wordpress-importer/
4
+ Description: Import posts, pages, comments, custom fields, categories, tags and more from a WordPress export file.
5
+ Author: wordpressdotorg
6
+ Author URI: http://wordpress.org/
7
+ Version: 0.6.3
8
+ Text Domain: wordpress-importer
9
+ License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10
+ */
11
+
12
+
13
+ if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
14
+ return;
15
+
16
+ /** Display verbose errors */
17
+ define( 'IMPORT_DEBUG', true );
18
+
19
+ // Load Importer API
20
+ require_once ABSPATH . 'wp-admin/includes/import.php';
21
+
22
+ if ( ! class_exists( 'WP_Importer' ) ) {
23
+ $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
24
+ if ( file_exists( $class_wp_importer ) )
25
+ require $class_wp_importer;
26
+ }
27
+
28
+ // include WXR file parsers
29
+ require dirname( __FILE__ ) . '/parsers.php';
30
+
31
+ /**
32
+ * WordPress Importer class for managing the import process of a WXR file
33
+ *
34
+ * @package WordPress
35
+ * @subpackage Importer
36
+ */
37
+ if ( class_exists( 'WP_Importer' ) ) {
38
+ class WP_Import extends WP_Importer {
39
+ var $max_wxr_version = 1.2; // max. supported WXR version
40
+
41
+ var $id; // WXR attachment ID
42
+
43
+ // information to import from WXR file
44
+ var $version;
45
+ var $authors = array();
46
+ var $posts = array();
47
+ var $terms = array();
48
+ var $categories = array();
49
+ var $tags = array();
50
+ var $base_url = '';
51
+
52
+ // mappings from old information to new
53
+ var $processed_authors = array();
54
+ var $author_mapping = array();
55
+ var $processed_terms = array();
56
+ var $processed_posts = array();
57
+ var $post_orphans = array();
58
+ var $processed_menu_items = array();
59
+ var $menu_item_orphans = array();
60
+ var $missing_menu_items = array();
61
+
62
+ var $fetch_attachments = false;
63
+ var $url_remap = array();
64
+ var $featured_images = array();
65
+
66
+ /**
67
+ * Registered callback function for the WordPress Importer
68
+ *
69
+ * Manages the three separate stages of the WXR import process
70
+ */
71
+ function dispatch() {
72
+ $this->header();
73
+
74
+ $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
75
+ switch ( $step ) {
76
+ case 0:
77
+ $this->greet();
78
+ break;
79
+ case 1:
80
+ check_admin_referer( 'import-upload' );
81
+ if ( $this->handle_upload() )
82
+ $this->import_options();
83
+ break;
84
+ case 2:
85
+ check_admin_referer( 'import-wordpress' );
86
+ $this->fetch_attachments = ( ! empty( $_POST['fetch_attachments'] ) && $this->allow_fetch_attachments() );
87
+ $this->id = (int) $_POST['import_id'];
88
+ $file = get_attached_file( $this->id );
89
+ set_time_limit(0);
90
+ $this->import( $file );
91
+ break;
92
+ }
93
+
94
+ $this->footer();
95
+ }
96
+
97
+ /**
98
+ * The main controller for the actual import stage.
99
+ *
100
+ * @param string $file Path to the WXR file for importing
101
+ */
102
+ function import( $file ) {
103
+ add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
104
+ add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
105
+
106
+ $this->import_start( $file );
107
+
108
+ $this->get_author_mapping();
109
+
110
+ wp_suspend_cache_invalidation( true );
111
+ $this->process_categories();
112
+ $this->process_tags();
113
+ $this->process_terms();
114
+ $this->process_posts();
115
+ wp_suspend_cache_invalidation( false );
116
+
117
+ // update incorrect/missing information in the DB
118
+ $this->backfill_parents();
119
+ $this->backfill_attachment_urls();
120
+ $this->remap_featured_images();
121
+
122
+ $this->import_end();
123
+ }
124
+
125
+ /**
126
+ * Parses the WXR file and prepares us for the task of processing parsed data
127
+ *
128
+ * @param string $file Path to the WXR file for importing
129
+ */
130
+ function import_start( $file ) {
131
+ if ( ! is_file($file) ) {
132
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
133
+ echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
134
+ $this->footer();
135
+ die();
136
+ }
137
+
138
+ $import_data = $this->parse( $file );
139
+
140
+ if ( is_wp_error( $import_data ) ) {
141
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
142
+ echo esc_html( $import_data->get_error_message() ) . '</p>';
143
+ $this->footer();
144
+ die();
145
+ }
146
+
147
+ $this->version = $import_data['version'];
148
+ $this->get_authors_from_import( $import_data );
149
+ $this->posts = $import_data['posts'];
150
+ $this->terms = $import_data['terms'];
151
+ $this->categories = $import_data['categories'];
152
+ $this->tags = $import_data['tags'];
153
+ $this->base_url = esc_url( $import_data['base_url'] );
154
+
155
+ wp_defer_term_counting( true );
156
+ wp_defer_comment_counting( true );
157
+
158
+ do_action( 'import_start' );
159
+ }
160
+
161
+ /**
162
+ * Performs post-import cleanup of files and the cache
163
+ */
164
+ function import_end() {
165
+ wp_import_cleanup( $this->id );
166
+
167
+ wp_cache_flush();
168
+ foreach ( get_taxonomies() as $tax ) {
169
+ delete_option( "{$tax}_children" );
170
+ _get_term_hierarchy( $tax );
171
+ }
172
+
173
+ wp_defer_term_counting( false );
174
+ wp_defer_comment_counting( false );
175
+
176
+ echo '<p>' . __( 'All done.', 'wordpress-importer' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>';
177
+ echo '<p>' . __( 'Remember to update the passwords and roles of imported users.', 'wordpress-importer' ) . '</p>';
178
+
179
+ do_action( 'import_end' );
180
+ }
181
+
182
+ /**
183
+ * Handles the WXR upload and initial parsing of the file to prepare for
184
+ * displaying author import options
185
+ *
186
+ * @return bool False if error uploading or invalid file, true otherwise
187
+ */
188
+ function handle_upload() {
189
+ $file = wp_import_handle_upload();
190
+
191
+ if ( isset( $file['error'] ) ) {
192
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
193
+ echo esc_html( $file['error'] ) . '</p>';
194
+ return false;
195
+ } else if ( ! file_exists( $file['file'] ) ) {
196
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
197
+ printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'wordpress-importer' ), esc_html( $file['file'] ) );
198
+ echo '</p>';
199
+ return false;
200
+ }
201
+
202
+ $this->id = (int) $file['id'];
203
+ $import_data = $this->parse( $file['file'] );
204
+ if ( is_wp_error( $import_data ) ) {
205
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
206
+ echo esc_html( $import_data->get_error_message() ) . '</p>';
207
+ return false;
208
+ }
209
+
210
+ $this->version = $import_data['version'];
211
+ if ( $this->version > $this->max_wxr_version ) {
212
+ echo '<div class="error"><p><strong>';
213
+ printf( __( 'This WXR file (version %s) may not be supported by this version of the importer. Please consider updating.', 'wordpress-importer' ), esc_html($import_data['version']) );
214
+ echo '</strong></p></div>';
215
+ }
216
+
217
+ $this->get_authors_from_import( $import_data );
218
+
219
+ return true;
220
+ }
221
+
222
+ /**
223
+ * Retrieve authors from parsed WXR data
224
+ *
225
+ * Uses the provided author information from WXR 1.1 files
226
+ * or extracts info from each post for WXR 1.0 files
227
+ *
228
+ * @param array $import_data Data returned by a WXR parser
229
+ */
230
+ function get_authors_from_import( $import_data ) {
231
+ if ( ! empty( $import_data['authors'] ) ) {
232
+ $this->authors = $import_data['authors'];
233
+ // no author information, grab it from the posts
234
+ } else {
235
+ foreach ( $import_data['posts'] as $post ) {
236
+ $login = sanitize_user( $post['post_author'], true );
237
+ if ( empty( $login ) ) {
238
+ printf( __( 'Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $post['post_author'] ) );
239
+ echo '<br />';
240
+ continue;
241
+ }
242
+
243
+ if ( ! isset($this->authors[$login]) )
244
+ $this->authors[$login] = array(
245
+ 'author_login' => $login,
246
+ 'author_display_name' => $post['post_author']
247
+ );
248
+ }
249
+ }
250
+ }
251
+
252
+ /**
253
+ * Display pre-import options, author importing/mapping and option to
254
+ * fetch attachments
255
+ */
256
+ function import_options() {
257
+ $j = 0;
258
+ ?>
259
+ <form action="<?php echo admin_url( 'admin.php?import=wordpress&amp;step=2' ); ?>" method="post">
260
+ <?php wp_nonce_field( 'import-wordpress' ); ?>
261
+ <input type="hidden" name="import_id" value="<?php echo $this->id; ?>" />
262
+
263
+ <?php if ( ! empty( $this->authors ) ) : ?>
264
+ <h3><?php _e( 'Assign Authors', 'wordpress-importer' ); ?></h3>
265
+ <p><?php _e( 'To make it easier for you to edit and save the imported content, you may want to reassign the author of the imported item to an existing user of this site. For example, you may want to import all the entries as <code>admin</code>s entries.', 'wordpress-importer' ); ?></p>
266
+ <?php if ( $this->allow_create_users() ) : ?>
267
+ <p><?php printf( __( 'If a new user is created by WordPress, a new password will be randomly generated and the new user&#8217;s role will be set as %s. Manually changing the new user&#8217;s details will be necessary.', 'wordpress-importer' ), esc_html( get_option('default_role') ) ); ?></p>
268
+ <?php endif; ?>
269
+ <ol id="authors">
270
+ <?php foreach ( $this->authors as $author ) : ?>
271
+ <li><?php $this->author_select( $j++, $author ); ?></li>
272
+ <?php endforeach; ?>
273
+ </ol>
274
+ <?php endif; ?>
275
+
276
+ <?php if ( $this->allow_fetch_attachments() ) : ?>
277
+ <h3><?php _e( 'Import Attachments', 'wordpress-importer' ); ?></h3>
278
+ <p>
279
+ <input type="checkbox" value="1" name="fetch_attachments" id="import-attachments" />
280
+ <label for="import-attachments"><?php _e( 'Download and import file attachments', 'wordpress-importer' ); ?></label>
281
+ </p>
282
+ <?php endif; ?>
283
+
284
+ <p class="submit"><input type="submit" class="button" value="<?php esc_attr_e( 'Submit', 'wordpress-importer' ); ?>" /></p>
285
+ </form>
286
+ <?php
287
+ }
288
+
289
+ /**
290
+ * Display import options for an individual author. That is, either create
291
+ * a new user based on import info or map to an existing user
292
+ *
293
+ * @param int $n Index for each author in the form
294
+ * @param array $author Author information, e.g. login, display name, email
295
+ */
296
+ function author_select( $n, $author ) {
297
+ _e( 'Import author:', 'wordpress-importer' );
298
+ echo ' <strong>' . esc_html( $author['author_display_name'] );
299
+ if ( $this->version != '1.0' ) echo ' (' . esc_html( $author['author_login'] ) . ')';
300
+ echo '</strong><br />';
301
+
302
+ if ( $this->version != '1.0' )
303
+ echo '<div style="margin-left:18px">';
304
+
305
+ $create_users = $this->allow_create_users();
306
+ if ( $create_users ) {
307
+ if ( $this->version != '1.0' ) {
308
+ _e( 'or create new user with login name:', 'wordpress-importer' );
309
+ $value = '';
310
+ } else {
311
+ _e( 'as a new user:', 'wordpress-importer' );
312
+ $value = esc_attr( sanitize_user( $author['author_login'], true ) );
313
+ }
314
+
315
+ echo ' <input type="text" name="user_new['.$n.']" value="'. $value .'" /><br />';
316
+ }
317
+
318
+ if ( ! $create_users && $this->version == '1.0' )
319
+ _e( 'assign posts to an existing user:', 'wordpress-importer' );
320
+ else
321
+ _e( 'or assign posts to an existing user:', 'wordpress-importer' );
322
+ wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __( '- Select -', 'wordpress-importer' ) ) );
323
+ echo '<input type="hidden" name="imported_authors['.$n.']" value="' . esc_attr( $author['author_login'] ) . '" />';
324
+
325
+ if ( $this->version != '1.0' )
326
+ echo '</div>';
327
+ }
328
+
329
+ /**
330
+ * Map old author logins to local user IDs based on decisions made
331
+ * in import options form. Can map to an existing user, create a new user
332
+ * or falls back to the current user in case of error with either of the previous
333
+ */
334
+ function get_author_mapping() {
335
+ if ( ! isset( $_POST['imported_authors'] ) )
336
+ return;
337
+
338
+ $create_users = $this->allow_create_users();
339
+
340
+ foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) {
341
+ // Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts.
342
+ $santized_old_login = sanitize_user( $old_login, true );
343
+ $old_id = isset( $this->authors[$old_login]['author_id'] ) ? intval($this->authors[$old_login]['author_id']) : false;
344
+
345
+ if ( ! empty( $_POST['user_map'][$i] ) ) {
346
+ $user = get_userdata( intval($_POST['user_map'][$i]) );
347
+ if ( isset( $user->ID ) ) {
348
+ if ( $old_id )
349
+ $this->processed_authors[$old_id] = $user->ID;
350
+ $this->author_mapping[$santized_old_login] = $user->ID;
351
+ }
352
+ } else if ( $create_users ) {
353
+ if ( ! empty($_POST['user_new'][$i]) ) {
354
+ $user_id = wp_create_user( $_POST['user_new'][$i], wp_generate_password() );
355
+ } else if ( $this->version != '1.0' ) {
356
+ $user_data = array(
357
+ 'user_login' => $old_login,
358
+ 'user_pass' => wp_generate_password(),
359
+ 'user_email' => isset( $this->authors[$old_login]['author_email'] ) ? $this->authors[$old_login]['author_email'] : '',
360
+ 'display_name' => $this->authors[$old_login]['author_display_name'],
361
+ 'first_name' => isset( $this->authors[$old_login]['author_first_name'] ) ? $this->authors[$old_login]['author_first_name'] : '',
362
+ 'last_name' => isset( $this->authors[$old_login]['author_last_name'] ) ? $this->authors[$old_login]['author_last_name'] : '',
363
+ );
364
+ $user_id = wp_insert_user( $user_data );
365
+ }
366
+
367
+ if ( ! is_wp_error( $user_id ) ) {
368
+ if ( $old_id )
369
+ $this->processed_authors[$old_id] = $user_id;
370
+ $this->author_mapping[$santized_old_login] = $user_id;
371
+ } else {
372
+ printf( __( 'Failed to create new user for %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html($this->authors[$old_login]['author_display_name']) );
373
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
374
+ echo ' ' . $user_id->get_error_message();
375
+ echo '<br />';
376
+ }
377
+ }
378
+
379
+ // failsafe: if the user_id was invalid, default to the current user
380
+ if ( ! isset( $this->author_mapping[$santized_old_login] ) ) {
381
+ if ( $old_id )
382
+ $this->processed_authors[$old_id] = (int) get_current_user_id();
383
+ $this->author_mapping[$santized_old_login] = (int) get_current_user_id();
384
+ }
385
+ }
386
+ }
387
+
388
+ /**
389
+ * Create new categories based on import information
390
+ *
391
+ * Doesn't create a new category if its slug already exists
392
+ */
393
+ function process_categories() {
394
+ $this->categories = apply_filters( 'wp_import_categories', $this->categories );
395
+
396
+ if ( empty( $this->categories ) )
397
+ return;
398
+
399
+ foreach ( $this->categories as $cat ) {
400
+ // if the category already exists leave it alone
401
+ $term_id = term_exists( $cat['category_nicename'], 'category' );
402
+ if ( $term_id ) {
403
+ if ( is_array($term_id) ) $term_id = $term_id['term_id'];
404
+ if ( isset($cat['term_id']) )
405
+ $this->processed_terms[intval($cat['term_id'])] = (int) $term_id;
406
+ continue;
407
+ }
408
+
409
+ $category_parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] );
410
+ $category_description = isset( $cat['category_description'] ) ? $cat['category_description'] : '';
411
+ $catarr = array(
412
+ 'category_nicename' => $cat['category_nicename'],
413
+ 'category_parent' => $category_parent,
414
+ 'cat_name' => $cat['cat_name'],
415
+ 'category_description' => $category_description
416
+ );
417
+ $catarr = wp_slash( $catarr );
418
+
419
+ $id = wp_insert_category( $catarr );
420
+ if ( ! is_wp_error( $id ) ) {
421
+ if ( isset($cat['term_id']) )
422
+ $this->processed_terms[intval($cat['term_id'])] = $id;
423
+ } else {
424
+ printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) );
425
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
426
+ echo ': ' . $id->get_error_message();
427
+ echo '<br />';
428
+ continue;
429
+ }
430
+
431
+ $this->process_termmeta( $cat, $id['term_id'] );
432
+ }
433
+
434
+ unset( $this->categories );
435
+ }
436
+
437
+ /**
438
+ * Create new post tags based on import information
439
+ *
440
+ * Doesn't create a tag if its slug already exists
441
+ */
442
+ function process_tags() {
443
+ $this->tags = apply_filters( 'wp_import_tags', $this->tags );
444
+
445
+ if ( empty( $this->tags ) )
446
+ return;
447
+
448
+ foreach ( $this->tags as $tag ) {
449
+ // if the tag already exists leave it alone
450
+ $term_id = term_exists( $tag['tag_slug'], 'post_tag' );
451
+ if ( $term_id ) {
452
+ if ( is_array($term_id) ) $term_id = $term_id['term_id'];
453
+ if ( isset($tag['term_id']) )
454
+ $this->processed_terms[intval($tag['term_id'])] = (int) $term_id;
455
+ continue;
456
+ }
457
+
458
+ $tag = wp_slash( $tag );
459
+ $tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
460
+ $tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc );
461
+
462
+ $id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr );
463
+ if ( ! is_wp_error( $id ) ) {
464
+ if ( isset($tag['term_id']) )
465
+ $this->processed_terms[intval($tag['term_id'])] = $id['term_id'];
466
+ } else {
467
+ printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html($tag['tag_name']) );
468
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
469
+ echo ': ' . $id->get_error_message();
470
+ echo '<br />';
471
+ continue;
472
+ }
473
+
474
+ $this->process_termmeta( $tag, $id['term_id'] );
475
+ }
476
+
477
+ unset( $this->tags );
478
+ }
479
+
480
+ /**
481
+ * Create new terms based on import information
482
+ *
483
+ * Doesn't create a term its slug already exists
484
+ */
485
+ function process_terms() {
486
+ $this->terms = apply_filters( 'wp_import_terms', $this->terms );
487
+
488
+ if ( empty( $this->terms ) )
489
+ return;
490
+
491
+ foreach ( $this->terms as $term ) {
492
+ // if the term already exists in the correct taxonomy leave it alone
493
+ $term_id = term_exists( $term['slug'], $term['term_taxonomy'] );
494
+ if ( $term_id ) {
495
+ if ( is_array($term_id) ) $term_id = $term_id['term_id'];
496
+ if ( isset($term['term_id']) )
497
+ $this->processed_terms[intval($term['term_id'])] = (int) $term_id;
498
+ continue;
499
+ }
500
+
501
+ if ( empty( $term['term_parent'] ) ) {
502
+ $parent = 0;
503
+ } else {
504
+ $parent = term_exists( $term['term_parent'], $term['term_taxonomy'] );
505
+ if ( is_array( $parent ) ) $parent = $parent['term_id'];
506
+ }
507
+ $term = wp_slash( $term );
508
+ $description = isset( $term['term_description'] ) ? $term['term_description'] : '';
509
+ $termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) );
510
+
511
+ $id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr );
512
+ if ( ! is_wp_error( $id ) ) {
513
+ if ( isset($term['term_id']) )
514
+ $this->processed_terms[intval($term['term_id'])] = $id['term_id'];
515
+ } else {
516
+ printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($term['term_taxonomy']), esc_html($term['term_name']) );
517
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
518
+ echo ': ' . $id->get_error_message();
519
+ echo '<br />';
520
+ continue;
521
+ }
522
+
523
+ $this->process_termmeta( $term, $id['term_id'] );
524
+ }
525
+
526
+ unset( $this->terms );
527
+ }
528
+
529
+ /**
530
+ * Add metadata to imported term.
531
+ *
532
+ * @since 0.6.2
533
+ *
534
+ * @param array $term Term data from WXR import.
535
+ * @param int $term_id ID of the newly created term.
536
+ */
537
+ protected function process_termmeta( $term, $term_id ) {
538
+ if ( ! isset( $term['termmeta'] ) ) {
539
+ $term['termmeta'] = array();
540
+ }
541
+
542
+ /**
543
+ * Filters the metadata attached to an imported term.
544
+ *
545
+ * @since 0.6.2
546
+ *
547
+ * @param array $termmeta Array of term meta.
548
+ * @param int $term_id ID of the newly created term.
549
+ * @param array $term Term data from the WXR import.
550
+ */
551
+ $term['termmeta'] = apply_filters( 'wp_import_term_meta', $term['termmeta'], $term_id, $term );
552
+
553
+ if ( empty( $term['termmeta'] ) ) {
554
+ return;
555
+ }
556
+
557
+ foreach ( $term['termmeta'] as $meta ) {
558
+ /**
559
+ * Filters the meta key for an imported piece of term meta.
560
+ *
561
+ * @since 0.6.2
562
+ *
563
+ * @param string $meta_key Meta key.
564
+ * @param int $term_id ID of the newly created term.
565
+ * @param array $term Term data from the WXR import.
566
+ */
567
+ $key = apply_filters( 'import_term_meta_key', $meta['key'], $term_id, $term );
568
+ if ( ! $key ) {
569
+ continue;
570
+ }
571
+
572
+ // Export gets meta straight from the DB so could have a serialized string
573
+ $value = maybe_unserialize( $meta['value'] );
574
+
575
+ add_term_meta( $term_id, $key, $value );
576
+
577
+ /**
578
+ * Fires after term meta is imported.
579
+ *
580
+ * @since 0.6.2
581
+ *
582
+ * @param int $term_id ID of the newly created term.
583
+ * @param string $key Meta key.
584
+ * @param mixed $value Meta value.
585
+ */
586
+ do_action( 'import_term_meta', $term_id, $key, $value );
587
+ }
588
+ }
589
+
590
+ /**
591
+ * Create new posts based on import information
592
+ *
593
+ * Posts marked as having a parent which doesn't exist will become top level items.
594
+ * Doesn't create a new post if: the post type doesn't exist, the given post ID
595
+ * is already noted as imported or a post with the same title and date already exists.
596
+ * Note that new/updated terms, comments and meta are imported for the last of the above.
597
+ */
598
+ function process_posts() {
599
+ $this->posts = apply_filters( 'wp_import_posts', $this->posts );
600
+
601
+ foreach ( $this->posts as $post ) {
602
+ $post = apply_filters( 'wp_import_post_data_raw', $post );
603
+
604
+ if ( ! post_type_exists( $post['post_type'] ) ) {
605
+ printf( __( 'Failed to import &#8220;%s&#8221;: Invalid post type %s', 'wordpress-importer' ),
606
+ esc_html($post['post_title']), esc_html($post['post_type']) );
607
+ echo '<br />';
608
+ do_action( 'wp_import_post_exists', $post );
609
+ continue;
610
+ }
611
+
612
+ if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) )
613
+ continue;
614
+
615
+ if ( $post['status'] == 'auto-draft' )
616
+ continue;
617
+
618
+ if ( 'nav_menu_item' == $post['post_type'] ) {
619
+ $this->process_menu_item( $post );
620
+ continue;
621
+ }
622
+
623
+ $post_type_object = get_post_type_object( $post['post_type'] );
624
+
625
+ $post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
626
+
627
+ /**
628
+ * Filter ID of the existing post corresponding to post currently importing.
629
+ *
630
+ * Return 0 to force the post to be imported. Filter the ID to be something else
631
+ * to override which existing post is mapped to the imported post.
632
+ *
633
+ * @see post_exists()
634
+ * @since 0.6.2
635
+ *
636
+ * @param int $post_exists Post ID, or 0 if post did not exist.
637
+ * @param array $post The post array to be inserted.
638
+ */
639
+ $post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post );
640
+
641
+ if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
642
+ printf( __('%s &#8220;%s&#8221; already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) );
643
+ echo '<br />';
644
+ $comment_post_ID = $post_id = $post_exists;
645
+ $this->processed_posts[ intval( $post['post_id'] ) ] = intval( $post_exists );
646
+ } else {
647
+ $post_parent = (int) $post['post_parent'];
648
+ if ( $post_parent ) {
649
+ // if we already know the parent, map it to the new local ID
650
+ if ( isset( $this->processed_posts[$post_parent] ) ) {
651
+ $post_parent = $this->processed_posts[$post_parent];
652
+ // otherwise record the parent for later
653
+ } else {
654
+ $this->post_orphans[intval($post['post_id'])] = $post_parent;
655
+ $post_parent = 0;
656
+ }
657
+ }
658
+
659
+ // map the post author
660
+ $author = sanitize_user( $post['post_author'], true );
661
+ if ( isset( $this->author_mapping[$author] ) )
662
+ $author = $this->author_mapping[$author];
663
+ else
664
+ $author = (int) get_current_user_id();
665
+
666
+ $postdata = array(
667
+ 'import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'],
668
+ 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'],
669
+ 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'],
670
+ 'post_status' => $post['status'], 'post_name' => $post['post_name'],
671
+ 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'],
672
+ 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'],
673
+ 'post_type' => $post['post_type'], 'post_password' => $post['post_password']
674
+ );
675
+
676
+ $original_post_ID = $post['post_id'];
677
+ $postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
678
+
679
+ $postdata = wp_slash( $postdata );
680
+
681
+ if ( 'attachment' == $postdata['post_type'] ) {
682
+ $remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
683
+
684
+ // try to use _wp_attached file for upload folder placement to ensure the same location as the export site
685
+ // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
686
+ $postdata['upload_date'] = $post['post_date'];
687
+ if ( isset( $post['postmeta'] ) ) {
688
+ foreach( $post['postmeta'] as $meta ) {
689
+ if ( $meta['key'] == '_wp_attached_file' ) {
690
+ if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) )
691
+ $postdata['upload_date'] = $matches[0];
692
+ break;
693
+ }
694
+ }
695
+ }
696
+
697
+ $comment_post_ID = $post_id = $this->process_attachment( $postdata, $remote_url );
698
+ } else {
699
+ $comment_post_ID = $post_id = wp_insert_post( $postdata, true );
700
+ do_action( 'wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post );
701
+ }
702
+
703
+ if ( is_wp_error( $post_id ) ) {
704
+ printf( __( 'Failed to import %s &#8220;%s&#8221;', 'wordpress-importer' ),
705
+ $post_type_object->labels->singular_name, esc_html($post['post_title']) );
706
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
707
+ echo ': ' . $post_id->get_error_message();
708
+ echo '<br />';
709
+ continue;
710
+ }
711
+
712
+ if ( $post['is_sticky'] == 1 )
713
+ stick_post( $post_id );
714
+ }
715
+
716
+ // map pre-import ID to local ID
717
+ $this->processed_posts[intval($post['post_id'])] = (int) $post_id;
718
+
719
+ if ( ! isset( $post['terms'] ) )
720
+ $post['terms'] = array();
721
+
722
+ $post['terms'] = apply_filters( 'wp_import_post_terms', $post['terms'], $post_id, $post );
723
+
724
+ // add categories, tags and other terms
725
+ if ( ! empty( $post['terms'] ) ) {
726
+ $terms_to_set = array();
727
+ foreach ( $post['terms'] as $term ) {
728
+ // back compat with WXR 1.0 map 'tag' to 'post_tag'
729
+ $taxonomy = ( 'tag' == $term['domain'] ) ? 'post_tag' : $term['domain'];
730
+ $term_exists = term_exists( $term['slug'], $taxonomy );
731
+ $term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
732
+ if ( ! $term_id ) {
733
+ $t = wp_insert_term( $term['name'], $taxonomy, array( 'slug' => $term['slug'] ) );
734
+ if ( ! is_wp_error( $t ) ) {
735
+ $term_id = $t['term_id'];
736
+ do_action( 'wp_import_insert_term', $t, $term, $post_id, $post );
737
+ } else {
738
+ printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($taxonomy), esc_html($term['name']) );
739
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
740
+ echo ': ' . $t->get_error_message();
741
+ echo '<br />';
742
+ do_action( 'wp_import_insert_term_failed', $t, $term, $post_id, $post );
743
+ continue;
744
+ }
745
+ }
746
+ $terms_to_set[$taxonomy][] = intval( $term_id );
747
+ }
748
+
749
+ foreach ( $terms_to_set as $tax => $ids ) {
750
+ $tt_ids = wp_set_post_terms( $post_id, $ids, $tax );
751
+ do_action( 'wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post );
752
+ }
753
+ unset( $post['terms'], $terms_to_set );
754
+ }
755
+
756
+ if ( ! isset( $post['comments'] ) )
757
+ $post['comments'] = array();
758
+
759
+ $post['comments'] = apply_filters( 'wp_import_post_comments', $post['comments'], $post_id, $post );
760
+
761
+ // add/update comments
762
+ if ( ! empty( $post['comments'] ) ) {
763
+ $num_comments = 0;
764
+ $inserted_comments = array();
765
+ foreach ( $post['comments'] as $comment ) {
766
+ $comment_id = $comment['comment_id'];
767
+ $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID;
768
+ $newcomments[$comment_id]['comment_author'] = $comment['comment_author'];
769
+ $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email'];
770
+ $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP'];
771
+ $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url'];
772
+ $newcomments[$comment_id]['comment_date'] = $comment['comment_date'];
773
+ $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt'];
774
+ $newcomments[$comment_id]['comment_content'] = $comment['comment_content'];
775
+ $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved'];
776
+ $newcomments[$comment_id]['comment_type'] = $comment['comment_type'];
777
+ $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent'];
778
+ $newcomments[$comment_id]['commentmeta'] = isset( $comment['commentmeta'] ) ? $comment['commentmeta'] : array();
779
+ if ( isset( $this->processed_authors[$comment['comment_user_id']] ) )
780
+ $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']];
781
+ }
782
+ ksort( $newcomments );
783
+
784
+ foreach ( $newcomments as $key => $comment ) {
785
+ // if this is a new post we can skip the comment_exists() check
786
+ if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) {
787
+ if ( isset( $inserted_comments[$comment['comment_parent']] ) )
788
+ $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
789
+ $comment = wp_filter_comment( $comment );
790
+ $inserted_comments[$key] = wp_insert_comment( $comment );
791
+ do_action( 'wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post );
792
+
793
+ foreach( $comment['commentmeta'] as $meta ) {
794
+ $value = maybe_unserialize( $meta['value'] );
795
+ add_comment_meta( $inserted_comments[$key], $meta['key'], $value );
796
+ }
797
+
798
+ $num_comments++;
799
+ }
800
+ }
801
+ unset( $newcomments, $inserted_comments, $post['comments'] );
802
+ }
803
+
804
+ if ( ! isset( $post['postmeta'] ) )
805
+ $post['postmeta'] = array();
806
+
807
+ $post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post );
808
+
809
+ // add/update post meta
810
+ if ( ! empty( $post['postmeta'] ) ) {
811
+ foreach ( $post['postmeta'] as $meta ) {
812
+ $key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post );
813
+ $value = false;
814
+
815
+ if ( '_edit_last' == $key ) {
816
+ if ( isset( $this->processed_authors[intval($meta['value'])] ) )
817
+ $value = $this->processed_authors[intval($meta['value'])];
818
+ else
819
+ $key = false;
820
+ }
821
+
822
+ if ( $key ) {
823
+ // export gets meta straight from the DB so could have a serialized string
824
+ if ( ! $value )
825
+ $value = maybe_unserialize( $meta['value'] );
826
+
827
+ add_post_meta( $post_id, $key, $value );
828
+ do_action( 'import_post_meta', $post_id, $key, $value );
829
+
830
+ // if the post has a featured image, take note of this in case of remap
831
+ if ( '_thumbnail_id' == $key )
832
+ $this->featured_images[$post_id] = (int) $value;
833
+ }
834
+ }
835
+ }
836
+ }
837
+
838
+ unset( $this->posts );
839
+ }
840
+
841
+ /**
842
+ * Attempt to create a new menu item from import data
843
+ *
844
+ * Fails for draft, orphaned menu items and those without an associated nav_menu
845
+ * or an invalid nav_menu term. If the post type or term object which the menu item
846
+ * represents doesn't exist then the menu item will not be imported (waits until the
847
+ * end of the import to retry again before discarding).
848
+ *
849
+ * @param array $item Menu item details from WXR file
850
+ */
851
+ function process_menu_item( $item ) {
852
+ // skip draft, orphaned menu items
853
+ if ( 'draft' == $item['status'] )
854
+ return;
855
+
856
+ $menu_slug = false;
857
+ if ( isset($item['terms']) ) {
858
+ // loop through terms, assume first nav_menu term is correct menu
859
+ foreach ( $item['terms'] as $term ) {
860
+ if ( 'nav_menu' == $term['domain'] ) {
861
+ $menu_slug = $term['slug'];
862
+ break;
863
+ }
864
+ }
865
+ }
866
+
867
+ // no nav_menu term associated with this menu item
868
+ if ( ! $menu_slug ) {
869
+ _e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' );
870
+ echo '<br />';
871
+ return;
872
+ }
873
+
874
+ $menu_id = term_exists( $menu_slug, 'nav_menu' );
875
+ if ( ! $menu_id ) {
876
+ printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) );
877
+ echo '<br />';
878
+ return;
879
+ } else {
880
+ $menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
881
+ }
882
+
883
+ if ( version_compare( PHP_VERSION, '7.0' ) >= 0 ) {
884
+ foreach ( $item['postmeta'] as $meta ) {
885
+ ${$meta['key']} = $meta['value'];
886
+ }
887
+ } else {
888
+ foreach ( $item['postmeta'] as $meta ) {
889
+ $$meta['key'] = $meta['value'];
890
+ }
891
+ }
892
+
893
+ if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) {
894
+ $_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
895
+ } else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) {
896
+ $_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
897
+ } else if ( 'custom' != $_menu_item_type ) {
898
+ // associated object is missing or not imported yet, we'll retry later
899
+ $this->missing_menu_items[] = $item;
900
+ return;
901
+ }
902
+
903
+ if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) {
904
+ $_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)];
905
+ } else if ( $_menu_item_menu_item_parent ) {
906
+ $this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent;
907
+ $_menu_item_menu_item_parent = 0;
908
+ }
909
+
910
+ // wp_update_nav_menu_item expects CSS classes as a space separated string
911
+ $_menu_item_classes = maybe_unserialize( $_menu_item_classes );
912
+ if ( is_array( $_menu_item_classes ) )
913
+ $_menu_item_classes = implode( ' ', $_menu_item_classes );
914
+
915
+ $args = array(
916
+ 'menu-item-object-id' => $_menu_item_object_id,
917
+ 'menu-item-object' => $_menu_item_object,
918
+ 'menu-item-parent-id' => $_menu_item_menu_item_parent,
919
+ 'menu-item-position' => intval( $item['menu_order'] ),
920
+ 'menu-item-type' => $_menu_item_type,
921
+ 'menu-item-title' => $item['post_title'],
922
+ 'menu-item-url' => $_menu_item_url,
923
+ 'menu-item-description' => $item['post_content'],
924
+ 'menu-item-attr-title' => $item['post_excerpt'],
925
+ 'menu-item-target' => $_menu_item_target,
926
+ 'menu-item-classes' => $_menu_item_classes,
927
+ 'menu-item-xfn' => $_menu_item_xfn,
928
+ 'menu-item-status' => $item['status']
929
+ );
930
+
931
+ $id = wp_update_nav_menu_item( $menu_id, 0, $args );
932
+ if ( $id && ! is_wp_error( $id ) )
933
+ $this->processed_menu_items[intval($item['post_id'])] = (int) $id;
934
+ }
935
+
936
+ /**
937
+ * If fetching attachments is enabled then attempt to create a new attachment
938
+ *
939
+ * @param array $post Attachment post details from WXR
940
+ * @param string $url URL to fetch attachment from
941
+ * @return int|WP_Error Post ID on success, WP_Error otherwise
942
+ */
943
+ function process_attachment( $post, $url ) {
944
+ if ( ! $this->fetch_attachments )
945
+ return new WP_Error( 'attachment_processing_error',
946
+ __( 'Fetching attachments is not enabled', 'wordpress-importer' ) );
947
+
948
+ // if the URL is absolute, but does not contain address, then upload it assuming base_site_url
949
+ if ( preg_match( '|^/[\w\W]+$|', $url ) )
950
+ $url = rtrim( $this->base_url, '/' ) . $url;
951
+
952
+ $upload = $this->fetch_remote_file( $url, $post );
953
+ if ( is_wp_error( $upload ) )
954
+ return $upload;
955
+
956
+ if ( $info = wp_check_filetype( $upload['file'] ) )
957
+ $post['post_mime_type'] = $info['type'];
958
+ else
959
+ return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'wordpress-importer') );
960
+
961
+ $post['guid'] = $upload['url'];
962
+
963
+ // as per wp-admin/includes/upload.php
964
+ $post_id = wp_insert_attachment( $post, $upload['file'] );
965
+ wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
966
+
967
+ // remap resized image URLs, works by stripping the extension and remapping the URL stub.
968
+ if ( preg_match( '!^image/!', $info['type'] ) ) {
969
+ $parts = pathinfo( $url );
970
+ $name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2
971
+
972
+ $parts_new = pathinfo( $upload['url'] );
973
+ $name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" );
974
+
975
+ $this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
976
+ }
977
+
978
+ return $post_id;
979
+ }
980
+
981
+ /**
982
+ * Attempt to download a remote file attachment
983
+ *
984
+ * @param string $url URL of item to fetch
985
+ * @param array $post Attachment details
986
+ * @return array|WP_Error Local file location details on success, WP_Error otherwise
987
+ */
988
+ function fetch_remote_file( $url, $post ) {
989
+ // extract the file name and extension from the url
990
+ $file_name = basename( $url );
991
+
992
+ // get placeholder file in the upload dir with a unique, sanitized filename
993
+ $upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
994
+ if ( $upload['error'] )
995
+ return new WP_Error( 'upload_dir_error', $upload['error'] );
996
+
997
+ // fetch the remote url and write it to the placeholder file
998
+ $headers = wp_get_http( $url, $upload['file'] );
999
+
1000
+ // request failed
1001
+ if ( ! $headers ) {
1002
+ @unlink( $upload['file'] );
1003
+ return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
1004
+ }
1005
+
1006
+ // make sure the fetch was successful
1007
+ if ( $headers['response'] != '200' ) {
1008
+ @unlink( $upload['file'] );
1009
+ return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) );
1010
+ }
1011
+
1012
+ $filesize = filesize( $upload['file'] );
1013
+
1014
+ if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
1015
+ @unlink( $upload['file'] );
1016
+ return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') );
1017
+ }
1018
+
1019
+ if ( 0 == $filesize ) {
1020
+ @unlink( $upload['file'] );
1021
+ return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') );
1022
+ }
1023
+
1024
+ $max_size = (int) $this->max_attachment_size();
1025
+ if ( ! empty( $max_size ) && $filesize > $max_size ) {
1026
+ @unlink( $upload['file'] );
1027
+ return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size) ) );
1028
+ }
1029
+
1030
+ // keep track of the old and new urls so we can substitute them later
1031
+ $this->url_remap[$url] = $upload['url'];
1032
+ $this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed?
1033
+ // keep track of the destination if the remote url is redirected somewhere else
1034
+ if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url )
1035
+ $this->url_remap[$headers['x-final-location']] = $upload['url'];
1036
+
1037
+ return $upload;
1038
+ }
1039
+
1040
+ /**
1041
+ * Attempt to associate posts and menu items with previously missing parents
1042
+ *
1043
+ * An imported post's parent may not have been imported when it was first created
1044
+ * so try again. Similarly for child menu items and menu items which were missing
1045
+ * the object (e.g. post) they represent in the menu
1046
+ */
1047
+ function backfill_parents() {
1048
+ global $wpdb;
1049
+
1050
+ // find parents for post orphans
1051
+ foreach ( $this->post_orphans as $child_id => $parent_id ) {
1052
+ $local_child_id = $local_parent_id = false;
1053
+ if ( isset( $this->processed_posts[$child_id] ) )
1054
+ $local_child_id = $this->processed_posts[$child_id];
1055
+ if ( isset( $this->processed_posts[$parent_id] ) )
1056
+ $local_parent_id = $this->processed_posts[$parent_id];
1057
+
1058
+ if ( $local_child_id && $local_parent_id )
1059
+ $wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' );
1060
+ }
1061
+
1062
+ // all other posts/terms are imported, retry menu items with missing associated object
1063
+ $missing_menu_items = $this->missing_menu_items;
1064
+ foreach ( $missing_menu_items as $item )
1065
+ $this->process_menu_item( $item );
1066
+
1067
+ // find parents for menu item orphans
1068
+ foreach ( $this->menu_item_orphans as $child_id => $parent_id ) {
1069
+ $local_child_id = $local_parent_id = 0;
1070
+ if ( isset( $this->processed_menu_items[$child_id] ) )
1071
+ $local_child_id = $this->processed_menu_items[$child_id];
1072
+ if ( isset( $this->processed_menu_items[$parent_id] ) )
1073
+ $local_parent_id = $this->processed_menu_items[$parent_id];
1074
+
1075
+ if ( $local_child_id && $local_parent_id )
1076
+ update_post_meta( $local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id );
1077
+ }
1078
+ }
1079
+
1080
+ /**
1081
+ * Use stored mapping information to update old attachment URLs
1082
+ */
1083
+ function backfill_attachment_urls() {
1084
+ global $wpdb;
1085
+ // make sure we do the longest urls first, in case one is a substring of another
1086
+ uksort( $this->url_remap, array(&$this, 'cmpr_strlen') );
1087
+
1088
+ foreach ( $this->url_remap as $from_url => $to_url ) {
1089
+ // remap urls in post_content
1090
+ $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) );
1091
+ // remap enclosure urls
1092
+ $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) );
1093
+ }
1094
+ }
1095
+
1096
+ /**
1097
+ * Update _thumbnail_id meta to new, imported attachment IDs
1098
+ */
1099
+ function remap_featured_images() {
1100
+ // cycle through posts that have a featured image
1101
+ foreach ( $this->featured_images as $post_id => $value ) {
1102
+ if ( isset( $this->processed_posts[$value] ) ) {
1103
+ $new_id = $this->processed_posts[$value];
1104
+ // only update if there's a difference
1105
+ if ( $new_id != $value )
1106
+ update_post_meta( $post_id, '_thumbnail_id', $new_id );
1107
+ }
1108
+ }
1109
+ }
1110
+
1111
+ /**
1112
+ * Parse a WXR file
1113
+ *
1114
+ * @param string $file Path to WXR file for parsing
1115
+ * @return array Information gathered from the WXR file
1116
+ */
1117
+ function parse( $file ) {
1118
+ $parser = new WXR_Parser();
1119
+ return $parser->parse( $file );
1120
+ }
1121
+
1122
+ // Display import page title
1123
+ function header() {
1124
+ echo '<div class="wrap">';
1125
+ screen_icon();
1126
+ echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>';
1127
+
1128
+ $updates = get_plugin_updates();
1129
+ $basename = plugin_basename(__FILE__);
1130
+ if ( isset( $updates[$basename] ) ) {
1131
+ $update = $updates[$basename];
1132
+ echo '<div class="error"><p><strong>';
1133
+ printf( __( 'A new version of this importer is available. Please update to version %s to ensure compatibility with newer export files.', 'wordpress-importer' ), $update->update->new_version );
1134
+ echo '</strong></p></div>';
1135
+ }
1136
+ }
1137
+
1138
+ // Close div.wrap
1139
+ function footer() {
1140
+ echo '</div>';
1141
+ }
1142
+
1143
+ /**
1144
+ * Display introductory text and file upload form
1145
+ */
1146
+ function greet() {
1147
+ echo '<div class="narrow">';
1148
+ echo '<p>'.__( 'Howdy! Upload your WordPress eXtended RSS (WXR) file and we&#8217;ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'wordpress-importer' ).'</p>';
1149
+ echo '<p>'.__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer' ).'</p>';
1150
+ wp_import_upload_form( 'admin.php?import=wordpress&amp;step=1' );
1151
+ echo '</div>';
1152
+ }
1153
+
1154
+ /**
1155
+ * Decide if the given meta key maps to information we will want to import
1156
+ *
1157
+ * @param string $key The meta key to check
1158
+ * @return string|bool The key if we do want to import, false if not
1159
+ */
1160
+ function is_valid_meta_key( $key ) {
1161
+ // skip attachment metadata since we'll regenerate it from scratch
1162
+ // skip _edit_lock as not relevant for import
1163
+ if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) )
1164
+ return false;
1165
+ return $key;
1166
+ }
1167
+
1168
+ /**
1169
+ * Decide whether or not the importer is allowed to create users.
1170
+ * Default is true, can be filtered via import_allow_create_users
1171
+ *
1172
+ * @return bool True if creating users is allowed
1173
+ */
1174
+ function allow_create_users() {
1175
+ return apply_filters( 'import_allow_create_users', true );
1176
+ }
1177
+
1178
+ /**
1179
+ * Decide whether or not the importer should attempt to download attachment files.
1180
+ * Default is true, can be filtered via import_allow_fetch_attachments. The choice
1181
+ * made at the import options screen must also be true, false here hides that checkbox.
1182
+ *
1183
+ * @return bool True if downloading attachments is allowed
1184
+ */
1185
+ function allow_fetch_attachments() {
1186
+ return apply_filters( 'import_allow_fetch_attachments', true );
1187
+ }
1188
+
1189
+ /**
1190
+ * Decide what the maximum file size for downloaded attachments is.
1191
+ * Default is 0 (unlimited), can be filtered via import_attachment_size_limit
1192
+ *
1193
+ * @return int Maximum attachment file size to import
1194
+ */
1195
+ function max_attachment_size() {
1196
+ return apply_filters( 'import_attachment_size_limit', 0 );
1197
+ }
1198
+
1199
+ /**
1200
+ * Added to http_request_timeout filter to force timeout at 60 seconds during import
1201
+ * @return int 60
1202
+ */
1203
+ function bump_request_timeout( $val ) {
1204
+ return 60;
1205
+ }
1206
+
1207
+ // return the difference in length between two strings
1208
+ function cmpr_strlen( $a, $b ) {
1209
+ return strlen($b) - strlen($a);
1210
+ }
1211
+ }
1212
+
1213
+ } // class_exists( 'WP_Importer' )
1214
+
1215
+
readme.md ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Support Themes and Plugins
2
+ - [x] Screenr
3
+ - [x] Screenr Plus
4
+ - [x] Boston
5
+ - [x] Boston Pro
6
+ - [x] OnePress
7
+ - [x] OnePress Plus
8
+ - [ ] Coupon WP
9
+ - [ ] Techone
10
+ - [ ] Codilight
11
+ - [ ] Inspirin
12
+ - [ ] Amzola
13
+ - [ ] Wentasi
14
+ - [ ] Laverde
15
+ - [ ] Bloom
16
+ - [ ] Beandot
17
+ - [ ] LightBog
18
+
19
+
20
+
21
+
22
+
readme.txt ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Plugin Name ===
2
+ Contributors: famethemes, shrimp2t
3
+ Donate link: https://www.famethemes.com/
4
+ Tags: import, demo data, oneclick, famethemes
5
+ Requires at least: 4.5
6
+ Tested up to: 4.6
7
+ Stable tag: 4.6
8
+ License: GPLv2 or later
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
+
11
+ FameThemes Demo importer
12
+
13
+ == Description ==
14
+
15
+ Import demo content for FameThemes’s themes with just one click.
16
+
17
+ Working with themes:
18
+
19
+ - [Screenr](https://wordpress.org/themes/screenr/)
20
+ - [Boston](https://wordpress.org/themes/boston/).
21
+ - [OnePress](https://wordpress.org/themes/onepress/)
22
+
23
+ == Installation ==
24
+
25
+ 1. Upload `famethemes-demo-importer` folder to the `/wp-content/plugins/` directory
26
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
27
+ 3. Appereance -> (Theme Name) -> Select tab One Click Demo Import
28
+
29
+
30
+
31
+ == Changelog ==
32
+ = 1.0.1
33
+ * Improve and fix bug.
34
+
35
+ = 1.0.0 =
36
+ * Release
37
+