class-premium-import-manager.php
9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/**
* @package WPSEO\Premium\Classes
*/
/**
* Class WPSEO_Premium_Import_Manager
*/
class WPSEO_Premium_Import_Manager {
/**
* Indicates whether redirects where imported
*
* @var bool
*/
private $redirects_imported = false;
/**
* Holds the import object
*
* @var object
*/
private $import;
/**
* Constructor.
*/
public function __construct() {
// Handle premium imports.
add_filter( 'wpseo_handle_import', array( $this, 'do_premium_imports' ) );
// Add htaccess import block.
add_action( 'wpseo_import_tab_content', array( $this, 'add_redirect_import_block' ) );
add_action( 'wpseo_import_tab_header', array( $this, 'redirects_import_header' ) );
}
/**
* Do premium imports
*
* @param object|bool $import The import object.
*
* @return object
*/
public function do_premium_imports( $import ) {
if ( ! $import ) {
$import = (object) array(
'msg' => '',
'success' => false,
);
}
$this->import = $import;
$this->htaccess_import();
$this->do_plugin_imports();
return $this->import;
}
/**
* Outputs a tab header for the htaccess import block
*/
public function redirects_import_header() {
/* translators: %s: '.htaccess' file name */
echo '<a class="nav-tab" id="import-htaccess-tab" href="#top#import-htaccess">' . __( 'Import redirects', 'wordpress-seo-premium' ) . '</a>';
}
/**
* Adding the import block for htaccess. Makes it able to import redirects from htaccess
*/
public function add_redirect_import_block() {
// The plugins we have import functions for.
$plugins = array(
'redirection' => __( 'Redirection', 'wordpress-seo-premium' ) . '<br/>',
'safe_redirect_manager' => __( 'Safe Redirect Manager', 'wordpress-seo-premium' ) . '<br/>',
'simple-301-redirects' => __( 'Simple 301 Redirects', 'wordpress-seo-premium' ) . '<br/>',
);
// Display the forms.
require( 'views/import-redirects.php' );
}
/**
* Redirection import success message
*/
private function message_redirect_import_success() {
$this->import->msg .= __( 'Redirects have been imported.', 'wordpress-seo-premium' );
}
/**
* Redirection plugin not found message
*/
private function message_redirection_plugin_not_find() {
$this->import->msg .= __( 'Redirect import failed: Redirection plugin not installed or activated.', 'wordpress-seo-premium' );
}
/**
* Redirection import no redirects found message
*/
private function message_redirect_import_no_redirects() {
$this->import->msg .= __( 'Redirect import failed: No redirects found.', 'wordpress-seo-premium' );
}
/**
* Apache import success message
*/
private function message_htaccess_success() {
/* translators: %s: '.htaccess' file name */
$this->import->msg .= sprintf( __( '%s redirects have been imported.', 'wordpress-seo-premium' ), '<code>.htaccess</code>' );
}
/**
* Apache import no redirects found message
*/
private function message_htaccess_no_redirects() {
/* translators: %s: '.htaccess' file name */
$this->import->msg .= sprintf( __( '%s import failed: No redirects found.', 'wordpress-seo-premium' ), '<code>.htaccess</code>' );
}
/**
* Do .htaccess file import.
*
* @return void
*/
private function htaccess_import() {
$htaccess = stripcslashes( filter_input( INPUT_POST, 'htaccess' ) );
if ( ! $htaccess || '' === $htaccess ) {
return;
}
// Regexpressions.
$regex_patterns = array(
array(
'type' => WPSEO_Redirect::FORMAT_PLAIN,
'pattern' => '`^Redirect ([0-9]{3}) ([^"\s]+) ([a-z/:%&#]+)`im',
),
array(
'type' => WPSEO_Redirect::FORMAT_PLAIN,
'pattern' => '`^Redirect ([0-9]{3}) "([^"]+)" ([a-z/:%&#]+)`im',
),
array(
'type' => WPSEO_Redirect::FORMAT_REGEX,
'pattern' => '`^RedirectMatch ([0-9]{3}) ([^"\s]+) ([^\s]+)`im',
),
array(
'type' => WPSEO_Redirect::FORMAT_REGEX,
'pattern' => '`^RedirectMatch ([0-9]{3}) "?([^"]+)"? ([^\s]+)`im',
),
);
// Loop through patterns.
foreach ( $regex_patterns as $regex ) {
// Get all redirects.
$this->match_redirect_regex( $regex, $htaccess );
}
// Check if we've imported any redirects.
if ( $this->redirects_imported ) {
$this->save_import();
// Display success message.
$this->import->success = true;
$this->message_htaccess_success();
return;
}
// Display fail message.
$this->message_htaccess_no_redirects();
}
/**
* Matches the string (containing redirects) for the given regex
*
* @param string $regex The regular expression to match redirects.
* @param string $htaccess The string of redirects.
*/
private function match_redirect_regex( $regex, $htaccess ) {
preg_match_all( $regex['pattern'], $htaccess, $redirects, PREG_SET_ORDER );
if ( is_array( $redirects ) ) {
foreach ( $redirects as $redirect ) {
$type = trim( $redirect[1] );
$source = trim( $redirect[2] );
$target = trim( $redirect[3] );
if ( '' !== $source && '' !== $target ) {
// Adding the redirect to importer class.
$this->get_redirect_option()->add( new WPSEO_Redirect( $source, $target, $type, $regex['type'] ) );
$this->redirects_imported = true;
}
}
}
}
/**
* Redirect option, used to save and fetch the redirects.
*
* @return WPSEO_Redirect_Option
*/
private function get_redirect_option() {
static $redirect_option;
if ( ! $redirect_option ) {
$redirect_option = new WPSEO_Redirect_Option();
}
return $redirect_option;
}
/**
* Save and export the redirects.
*/
private function save_import() {
$this->get_redirect_option()->save();
$redirect_manager = new WPSEO_Redirect_Manager();
$redirect_manager->export_redirects();
}
/**
* Handle plugin imports
*/
private function do_plugin_imports() {
$wpseo_post = filter_input( INPUT_POST, 'wpseo', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
if ( isset( $wpseo_post['import_plugin'] ) ) {
switch ( $wpseo_post['import_plugin'] ) {
case 'redirection':
$success = $this->redirection_import();
break;
case 'safe_redirect_manager':
$success = $this->safe_redirect_import();
break;
case 'simple-301-redirects':
$success = $this->simple_301_redirects_import();
break;
default:
$success = false;
break;
}
if ( $success ) {
// Add success message.
$this->message_redirect_import_success();
$this->import->success = true;
// Save and export the redirects.
$this->save_import();
return;
}
$this->message_redirect_import_no_redirects();
}
}
/**
* Do redirection(http://wordpress.org/plugins/redirection/) import.
*
* @return bool
*/
private function redirection_import() {
global $wpdb;
// Only do import if Redirections is active.
if ( ! defined( 'REDIRECTION_VERSION' ) ) {
// Add plugin not found message.
$this->message_redirection_plugin_not_find();
return false;
}
// Get redirects.
$items = $wpdb->get_results( "SELECT `url`, `action_data`, `regex`, `action_code` FROM {$wpdb->prefix}redirection_items WHERE `status` = 'enabled' AND `action_type` = 'url'" );
// Loop and add redirect to Yoast SEO Premium.
if ( count( $items ) > 0 ) {
foreach ( $items as $item ) {
$format = WPSEO_Redirect::FORMAT_PLAIN;
if ( 1 === (int) $item->regex ) {
$format = WPSEO_Redirect::FORMAT_REGEX;
}
$this->get_redirect_option()->add( new WPSEO_Redirect( $item->url, $item->action_data, $item->action_code, $format ) );
$this->redirects_imported = true;
}
return true;
}
return false;
}
/**
* Do a safe redirect manager (https://wordpress.org/plugins/safe-redirect-manager/) import.
*
* @return bool
*/
private function safe_redirect_import() {
// Get redirects.
$items = get_transient( '_srm_redirects' );
// Loop and add redirect to Yoast SEO Premium.
if ( count( $items ) > 0 ) {
foreach ( $items as $item ) {
$format = WPSEO_Redirect::FORMAT_PLAIN;
// Special case for safe redirect wildcard system.
if ( substr( $item['redirect_from'], - 1, 1 ) === '*' ) {
$item['redirect_from'] = preg_replace( '/(\*)$/', '.*', $item['redirect_from'] );
$item['enable_regex'] = 1;
}
if ( 1 === (int) $item['enable_regex'] ) {
$format = WPSEO_Redirect::FORMAT_REGEX;
}
// Safe redirect manager has support for 404 and 403 status codes, we don't, so let's bend them to 410's.
// Also, it supports 303's, which we change into 302's.
switch ( $item['status_code'] ) {
case 303:
$status_code = 302;
break;
case 403:
case 404:
$status_code = 410;
break;
default:
$status_code = (int) $item['status_code'];
break;
}
$this->get_redirect_option()->add( new WPSEO_Redirect( $item['redirect_from'], $item['redirect_to'], $status_code, $format ) );
$this->redirects_imported = true;
}
return true;
}
return false;
}
/**
* Do Simple 301 redirects (https://wordpress.org/plugins/simple-301-redirects/) import.
*
* @return bool
*/
private function simple_301_redirects_import() {
// Get redirects.
$redirects = get_option( '301_redirects' );
// Whether to use wildcards.
$wildcard = get_option( '301_redirects_wildcard' );
// Loop and add redirect to Yoast SEO Premium.
if ( count( $redirects ) > 0 ) {
foreach ( $redirects as $origin => $target ) {
$format = WPSEO_Redirect::FORMAT_PLAIN;
// If wildcard redirects had been used, and this is one, flip it.
if ( $wildcard && strpos( $origin, '*' ) !== false ) {
$format = WPSEO_Redirect::FORMAT_REGEX;
$origin = str_replace( '*', '(.*)', $origin );
$target = str_replace( '*', '$1', $target );
}
$this->get_redirect_option()->add( new WPSEO_Redirect( $origin, $target, 301, $format ) );
$this->redirects_imported = true;
}
return true;
}
return false;
}
}