/
class-import.php
196 lines (161 loc) · 4.88 KB
/
class-import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* @package WPSEO\Admin\Import
*/
/**
* Class WPSEO_Import
*
* Class with functionality to import the Yoast SEO settings
*/
class WPSEO_Import {
/**
* Message about the import
*
* @var string
*/
public $msg = '';
/** @var bool $success If import was a success flag. */
public $success = false;
/**
* @var array
*/
private $file;
/**
* @var string
*/
private $filename;
/**
* @var string
*/
private $old_wpseo_version = null;
/**
* @var string
*/
private $path;
/**
* @var array
*/
private $upload_dir;
/**
* Class constructor
*/
public function __construct() {
if ( ! $this->handle_upload() ) {
return;
}
$this->determine_path();
if ( ! $this->unzip_file() ) {
$this->clean_up();
return;
}
$this->parse_options();
$this->clean_up();
}
/**
* Handle the file upload
*
* @return boolean
*/
private function handle_upload() {
$overrides = array( 'mimes' => array( 'zip' => 'application/zip' ) ); // Explicitly allow zip in multisite.
$this->file = wp_handle_upload( $_FILES['settings_import_file'], $overrides );
if ( is_wp_error( $this->file ) ) {
$this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . $this->file->get_error_message();
return false;
}
if ( is_array( $this->file ) && isset( $this->file['error'] ) ) {
$this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . $this->file['error'];
return false;
}
if ( ! isset( $this->file['file'] ) ) {
$this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . __( 'Upload failed.', 'wordpress-seo' );
return false;
}
return true;
}
/**
* Determine the path to the import file
*/
private function determine_path() {
$this->upload_dir = wp_upload_dir();
if ( ! defined( 'DIRECTORY_SEPARATOR' ) ) {
define( 'DIRECTORY_SEPARATOR', '/' );
}
$this->path = $this->upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'wpseo-import' . DIRECTORY_SEPARATOR;
if ( ! isset( $GLOBALS['wp_filesystem'] ) || ! is_object( $GLOBALS['wp_filesystem'] ) ) {
WP_Filesystem();
}
}
/**
* Unzip the file
*
* @return boolean
*/
private function unzip_file() {
$unzipped = unzip_file( $this->file['file'], $this->path );
if ( is_wp_error( $unzipped ) ) {
$this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . sprintf( __( 'Unzipping failed with error "%s".', 'wordpress-seo' ), $unzipped->get_error_message() );
return false;
}
$this->filename = $this->path . 'settings.ini';
if ( ! is_file( $this->filename ) || ! is_readable( $this->filename ) ) {
$this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . __( 'Unzipping failed - file settings.ini not found.', 'wordpress-seo' );
return false;
}
return true;
}
/**
* Parse the option file
*/
private function parse_options() {
$options = parse_ini_file( $this->filename, true );
if ( is_array( $options ) && $options !== array() ) {
if ( isset( $options['wpseo']['version'] ) && $options['wpseo']['version'] !== '' ) {
$this->old_wpseo_version = $options['wpseo']['version'];
}
foreach ( $options as $name => $opt_group ) {
$this->parse_option_group( $name, $opt_group, $options );
}
$this->msg = __( 'Settings successfully imported.', 'wordpress-seo' );
$this->success = true;
}
else {
$this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . __( 'No settings found in file.', 'wordpress-seo' );
}
}
/**
* Parse the option group and import it
*
* @param string $name Name string.
* @param array $opt_group Option group data.
* @param array $options Options data.
*/
private function parse_option_group( $name, $opt_group, $options ) {
if ( $name === 'wpseo_taxonomy_meta' ) {
$opt_group = json_decode( urldecode( $opt_group['wpseo_taxonomy_meta'] ), true );
}
// Make sure that the imported options are cleaned/converted on import.
$option_instance = WPSEO_Options::get_option_instance( $name );
if ( is_object( $option_instance ) && method_exists( $option_instance, 'import' ) ) {
$option_instance->import( $opt_group, $this->old_wpseo_version, $options );
}
elseif ( WP_DEBUG === true || ( defined( 'WPSEO_DEBUG' ) && WPSEO_DEBUG === true ) ) {
$this->msg = sprintf( __( 'Setting "%s" is no longer used and has been discarded.', 'wordpress-seo' ), $name );
}
}
/**
* Remove the files
*/
private function clean_up() {
if ( file_exists( $this->filename ) && is_writable( $this->filename ) ) {
unlink( $this->filename );
}
if ( ! empty( $this->file['file'] ) && file_exists( $this->file['file'] ) && is_writable( $this->file['file'] ) ) {
unlink( $this->file['file'] );
}
if ( file_exists( $this->path ) && is_writable( $this->path ) ) {
$wp_file = new WP_Filesystem_Direct( $this->path );
$wp_file->rmdir( $this->path, true );
}
}
}