PluginProbe
Redirection / 5.3.1
Redirection v5.3.1
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / importer.php

importer.php in Redirection 5.3.1, at models/importer.php

481 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Red_Plugin_Importer {
4 public static function get_plugins() {
5 $results = array();
6
7 $importers = array(
8 'wp-simple-redirect',
9 'seo-redirection',
10 'safe-redirect-manager',
11 'wordpress-old-slugs',
12 'rank-math',
13 'quick-redirects',
14 'pretty-links',
15 );
16
17 foreach ( $importers as $importer ) {
18 $importer = self::get_importer( $importer );
19 $results[] = $importer->get_data();
20 }
21
22 return array_values( array_filter( $results ) );
23 }
24
25 public static function get_importer( $id ) {
26 if ( $id === 'wp-simple-redirect' ) {
27 return new Red_Simple301_Importer();
28 }
29
30 if ( $id === 'seo-redirection' ) {
31 return new Red_SeoRedirection_Importer();
32 }
33
34 if ( $id === 'safe-redirect-manager' ) {
35 return new Red_SafeRedirectManager_Importer();
36 }
37
38 if ( $id === 'wordpress-old-slugs' ) {
39 return new Red_WordPressOldSlug_Importer();
40 }
41
42 if ( $id === 'rank-math' ) {
43 return new Red_RankMath_Importer();
44 }
45
46 if ( $id === 'quick-redirects' ) {
47 return new Red_QuickRedirect_Importer();
48 }
49
50 if ( $id === 'pretty-links' ) {
51 return new Red_PrettyLinks_Importer();
52 }
53
54 return false;
55 }
56
57 public static function import( $plugin, $group_id ) {
58 $importer = self::get_importer( $plugin );
59 if ( $importer ) {
60 return $importer->import_plugin( $group_id );
61 }
62
63 return 0;
64 }
65 }
66
67 class Red_PrettyLinks_Importer extends Red_Plugin_Importer {
68 public function import_plugin( $group_id ) {
69 global $wpdb;
70
71 $count = 0;
72 $redirects = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}prli_links" );
73
74 foreach ( $redirects as $redirect ) {
75 $created = $this->create_for_item( $group_id, $redirect );
76
77 if ( $created ) {
78 $count++;
79 }
80 }
81
82 return $count;
83 }
84
85 private function create_for_item( $group_id, $link ) {
86 $item = array(
87 'url' => '/' . $link->slug,
88 'action_data' => array( 'url' => $link->url ),
89 'regex' => false,
90 'group_id' => $group_id,
91 'match_type' => 'url',
92 'action_type' => 'url',
93 'title' => $link->name,
94 'action_code' => $link->redirect_type,
95 );
96
97 return Red_Item::create( $item );
98 }
99
100 public function get_data() {
101 $data = get_option( 'prli_db_version' );
102
103 if ( $data ) {
104 global $wpdb;
105
106 return [
107 'id' => 'pretty-links',
108 'name' => 'PrettyLinks',
109 'total' => $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}prli_links" ),
110 ];
111 }
112
113 return false;
114 }
115 }
116
117 class Red_QuickRedirect_Importer extends Red_Plugin_Importer {
118 public function import_plugin( $group_id ) {
119 $redirects = get_option( 'quickppr_redirects' );
120 $count = 0;
121
122 foreach ( $redirects as $source => $target ) {
123 $item = $this->create_for_item( $group_id, $source, $target );
124
125 if ( $item ) {
126 $count++;
127 }
128 }
129
130 return $count;
131 }
132
133 private function create_for_item( $group_id, $source, $target ) {
134 $item = array(
135 'url' => $source,
136 'action_data' => array( 'url' => $target ),
137 'regex' => false,
138 'group_id' => $group_id,
139 'match_type' => 'url',
140 'action_type' => 'url',
141 'action_code' => 301,
142 );
143
144 return Red_Item::create( $item );
145 }
146
147 public function get_data() {
148 $data = get_option( 'quickppr_redirects' );
149
150 if ( $data ) {
151 return array(
152 'id' => 'quick-redirects',
153 'name' => 'Quick Page/Post Redirects',
154 'total' => count( $data ),
155 );
156 }
157
158 return false;
159 }
160 }
161
162 class Red_RankMath_Importer extends Red_Plugin_Importer {
163 public function import_plugin( $group_id ) {
164 global $wpdb;
165
166 $count = 0;
167 $redirects = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}rank_math_redirections" );
168
169 foreach ( $redirects as $redirect ) {
170 $created = $this->create_for_item( $group_id, $redirect );
171 $count += $created;
172 }
173
174 return $count;
175 }
176
177 private function create_for_item( $group_id, $redirect ) {
178 // phpcs:ignore
179 $sources = unserialize( $redirect->sources );
180 $items = [];
181
182 foreach ( $sources as $source ) {
183 $url = $source['pattern'];
184 if ( substr( $url, 0, 1 ) !== '/' ) {
185 $url = '/' . $url;
186 }
187
188 $data = array(
189 'url' => $url,
190 'action_data' => array( 'url' => str_replace( '\\\\', '\\', $redirect->url_to ) ),
191 'regex' => $source['comparison'] === 'regex' ? true : false,
192 'group_id' => $group_id,
193 'match_type' => 'url',
194 'action_type' => 'url',
195 'action_code' => $redirect->header_code,
196 );
197
198 $items[] = Red_Item::create( $data );
199 }
200
201 return count( $items );
202 }
203
204 public function get_data() {
205 global $wpdb;
206
207 if ( defined( 'REDIRECTION_TESTS' ) && REDIRECTION_TESTS ) {
208 return 0;
209 }
210
211 if ( ! function_exists( 'is_plugin_active' ) ) {
212 require_once ABSPATH . 'wp-admin/includes/plugin.php';
213 }
214
215 $total = 0;
216 if ( is_plugin_active( 'seo-by-rank-math/rank-math.php' ) ) {
217 $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}rank_math_redirections" );
218 }
219
220 if ( $total ) {
221 return array(
222 'id' => 'rank-math',
223 'name' => 'RankMath',
224 'total' => intval( $total, 10 ),
225 );
226 }
227
228 return 0;
229 }
230 }
231
232 class Red_Simple301_Importer extends Red_Plugin_Importer {
233 public function import_plugin( $group_id ) {
234 $redirects = get_option( '301_redirects' );
235 $count = 0;
236
237 foreach ( $redirects as $source => $target ) {
238 $item = $this->create_for_item( $group_id, $source, $target );
239
240 if ( $item ) {
241 $count++;
242 }
243 }
244
245 return $count;
246 }
247
248 private function create_for_item( $group_id, $source, $target ) {
249 $item = array(
250 'url' => str_replace( '*', '(.*?)', $source ),
251 'action_data' => array( 'url' => str_replace( '*', '$1', trim( $target ) ) ),
252 'regex' => strpos( $source, '*' ) === false ? false : true,
253 'group_id' => $group_id,
254 'match_type' => 'url',
255 'action_type' => 'url',
256 'action_code' => 301,
257 );
258
259 return Red_Item::create( $item );
260 }
261
262 public function get_data() {
263 $data = get_option( '301_redirects' );
264
265 if ( $data ) {
266 return array(
267 'id' => 'wp-simple-redirect',
268 'name' => 'Simple 301 Redirects',
269 'total' => count( $data ),
270 );
271 }
272
273 return false;
274 }
275 }
276
277 class Red_WordPressOldSlug_Importer extends Red_Plugin_Importer {
278 public function import_plugin( $group_id ) {
279 global $wpdb;
280
281 $count = 0;
282 $redirects = $wpdb->get_results(
283 "SELECT {$wpdb->prefix}postmeta.* FROM {$wpdb->prefix}postmeta INNER JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id " .
284 "WHERE {$wpdb->prefix}postmeta.meta_key = '_wp_old_slug' AND {$wpdb->prefix}postmeta.meta_value != '' AND {$wpdb->prefix}posts.post_status='publish' AND {$wpdb->prefix}posts.post_type IN ('page', 'post')"
285 );
286
287 foreach ( $redirects as $redirect ) {
288 $item = $this->create_for_item( $group_id, $redirect );
289
290 if ( $item ) {
291 $count++;
292 }
293 }
294
295 return $count;
296 }
297
298 private function create_for_item( $group_id, $redirect ) {
299 $new = get_permalink( $redirect->post_id );
300 if ( is_wp_error( $new ) ) {
301 return false;
302 }
303
304 $new_path = wp_parse_url( $new, PHP_URL_PATH );
305 $old = rtrim( dirname( $new_path ), '/' ) . '/' . rtrim( $redirect->meta_value, '/' ) . '/';
306 $old = str_replace( '\\', '', $old );
307 $old = str_replace( '//', '/', $old );
308
309 $data = array(
310 'url' => $old,
311 'action_data' => array( 'url' => $new ),
312 'regex' => false,
313 'group_id' => $group_id,
314 'match_type' => 'url',
315 'action_type' => 'url',
316 'action_code' => 301,
317 );
318
319 return Red_Item::create( $data );
320 }
321
322 public function get_data() {
323 global $wpdb;
324
325 $total = $wpdb->get_var(
326 "SELECT COUNT(*) FROM {$wpdb->prefix}postmeta INNER JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id WHERE {$wpdb->prefix}postmeta.meta_key = '_wp_old_slug' AND {$wpdb->prefix}postmeta.meta_value != '' AND {$wpdb->prefix}posts.post_status='publish' AND {$wpdb->prefix}posts.post_type IN ('page', 'post')"
327 );
328
329 if ( $total ) {
330 return array(
331 'id' => 'wordpress-old-slugs',
332 'name' => __( 'Default WordPress "old slugs"', 'redirection' ),
333 'total' => intval( $total, 10 ),
334 );
335 }
336
337 return false;
338 }
339 }
340
341 class Red_SeoRedirection_Importer extends Red_Plugin_Importer {
342 public function import_plugin( $group_id ) {
343 global $wpdb;
344
345 if ( defined( 'REDIRECTION_TESTS' ) && REDIRECTION_TESTS ) {
346 return 0;
347 }
348
349 $count = 0;
350 $redirects = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}WP_SEO_Redirection" );
351
352 foreach ( $redirects as $redirect ) {
353 $item = $this->create_for_item( $group_id, $redirect );
354
355 if ( $item ) {
356 $count++;
357 }
358 }
359
360 return $count;
361 }
362
363 private function create_for_item( $group_id, $seo ) {
364 if ( intval( $seo->enabled, 10 ) === 0 ) {
365 return false;
366 }
367
368 $data = array(
369 'url' => $seo->regex ? $seo->regex : $seo->redirect_from,
370 'action_data' => array( 'url' => $seo->redirect_to ),
371 'regex' => $seo->regex ? true : false,
372 'group_id' => $group_id,
373 'match_type' => 'url',
374 'action_type' => 'url',
375 'action_code' => intval( $seo->redirect_type, 10 ),
376 );
377
378 return Red_Item::create( $data );
379 }
380
381 public function get_data() {
382 global $wpdb;
383
384 $plugins = get_option( 'active_plugins', array() );
385 $found = false;
386
387 foreach ( $plugins as $plugin ) {
388 if ( strpos( $plugin, 'seo-redirection.php' ) !== false ) {
389 $found = true;
390 break;
391 }
392 }
393
394 if ( $found ) {
395 $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}WP_SEO_Redirection" );
396
397 return array(
398 'id' => 'seo-redirection',
399 'name' => 'SEO Redirection',
400 'total' => $total,
401 );
402 }
403
404 return false;
405 }
406 }
407
408 class Red_SafeRedirectManager_Importer extends Red_Plugin_Importer {
409 public function import_plugin( $group_id ) {
410 global $wpdb;
411
412 $count = 0;
413 $redirects = $wpdb->get_results(
414 "SELECT {$wpdb->prefix}postmeta.* FROM {$wpdb->prefix}postmeta INNER JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id WHERE {$wpdb->prefix}postmeta.meta_key LIKE '_redirect_rule_%' AND {$wpdb->prefix}posts.post_status='publish'"
415 );
416
417 // Group them by post ID
418 $by_post = array();
419 foreach ( $redirects as $redirect ) {
420 if ( ! isset( $by_post[ $redirect->post_id ] ) ) {
421 $by_post[ $redirect->post_id ] = array();
422 }
423
424 $by_post[ $redirect->post_id ][ str_replace( '_redirect_rule_', '', $redirect->meta_key ) ] = $redirect->meta_value;
425 }
426
427 // Now go through the redirects
428 foreach ( $by_post as $post ) {
429 $item = $this->create_for_item( $group_id, $post );
430
431 if ( $item ) {
432 $count++;
433 }
434 }
435
436 return $count;
437 }
438
439 private function create_for_item( $group_id, $post ) {
440 $regex = false;
441 $source = $post['from'];
442
443 if ( strpos( $post['from'], '*' ) !== false ) {
444 $regex = true;
445 $source = str_replace( '*', '.*', $source );
446 } elseif ( isset( $post['from_regex'] ) && $post['from_regex'] === '1' ) {
447 $regex = true;
448 }
449
450 $data = array(
451 'url' => $source,
452 'action_data' => array( 'url' => $post['to'] ),
453 'regex' => $regex,
454 'group_id' => $group_id,
455 'match_type' => 'url',
456 'action_type' => 'url',
457 'action_code' => intval( $post['status_code'], 10 ),
458 );
459
460 return Red_Item::create( $data );
461 }
462
463 public function get_data() {
464 global $wpdb;
465
466 $total = $wpdb->get_var(
467 "SELECT COUNT(*) FROM {$wpdb->prefix}postmeta INNER JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id WHERE {$wpdb->prefix}postmeta.meta_key = '_redirect_rule_from' AND {$wpdb->prefix}posts.post_status='publish'"
468 );
469
470 if ( $total ) {
471 return array(
472 'id' => 'safe-redirect-manager',
473 'name' => 'Safe Redirect Manager',
474 'total' => intval( $total, 10 ),
475 );
476 }
477
478 return false;
479 }
480 }
481