PluginProbe
Redirection / 4.2
Redirection v4.2
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 4.2, at models/importer.php

366 lines 8.8 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 );
14
15 foreach ( $importers as $importer ) {
16 $importer = Red_Plugin_Importer::get_importer( $importer );
17 $results[] = $importer->get_data();
18 }
19
20 return array_values( array_filter( $results ) );
21 }
22
23 public static function get_importer( $id ) {
24 if ( $id === 'wp-simple-redirect' ) {
25 return new Red_Simple301_Importer();
26 }
27
28 if ( $id === 'seo-redirection' ) {
29 return new Red_SeoRedirection_Importer();
30 }
31
32 if ( $id === 'safe-redirect-manager' ) {
33 return new Red_SafeRedirectManager_Importer();
34 }
35
36 if ( $id === 'wordpress-old-slugs' ) {
37 return new Red_WordPressOldSlug_Importer();
38 }
39
40 if ( $id === 'rank-math' ) {
41 return new Red_RankMath_Importer();
42 }
43
44 return false;
45 }
46
47 public static function import( $plugin, $group_id ) {
48 $importer = Red_Plugin_Importer::get_importer( $plugin );
49 if ( $importer ) {
50 return $importer->import_plugin( $group_id );
51 }
52
53 return 0;
54 }
55 }
56
57 class Red_RankMath_Importer extends Red_Plugin_Importer {
58 public function import_plugin( $group_id ) {
59 global $wpdb;
60
61 $count = 0;
62 $redirects = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}rank_math_redirections" );
63
64 foreach ( $redirects as $redirect ) {
65 $created = $this->create_for_item( $group_id, $redirect );
66 $count += $created;
67 }
68
69 return $count;
70 }
71
72 private function create_for_item( $group_id, $redirect ) {
73 $sources = unserialize( $redirect->sources );
74
75 foreach ( $sources as $source ) {
76 $url = $source['pattern'];
77 if ( substr( $url, 0, 1 ) !== '/' ) {
78 $url = '/' . $url;
79 }
80
81 $data = array(
82 'url' => $url,
83 'action_data' => array( 'url' => $redirect->url_to ),
84 'regex' => $source['comparison'] === 'regex' ? true : false,
85 'group_id' => $group_id,
86 'match_type' => 'url',
87 'action_type' => 'url',
88 'action_code' => $redirect->header_code,
89 );
90
91 $items[] = Red_Item::create( $data );
92 }
93
94 return count( $items );
95 }
96
97 public function get_data() {
98 global $wpdb;
99
100 if ( defined( 'REDIRECTION_TESTS' ) && REDIRECTION_TESTS ) {
101 return 0;
102 }
103
104 $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}rank_math_redirections" );
105
106 if ( $total ) {
107 return array(
108 'id' => 'rank-math',
109 'name' => 'RankMath',
110 'total' => intval( $total, 10 ),
111 );
112 }
113
114 return 0;
115 }
116 }
117
118 class Red_Simple301_Importer extends Red_Plugin_Importer {
119 public function import_plugin( $group_id ) {
120 $redirects = get_option( '301_redirects' );
121 $count = 0;
122
123 foreach ( $redirects as $source => $target ) {
124 $item = $this->create_for_item( $group_id, $source, $target );
125
126 if ( $item ) {
127 $count++;
128 }
129 }
130
131 return $count;
132 }
133
134 private function create_for_item( $group_id, $source, $target ) {
135 $item = array(
136 'url' => str_replace( '*', '(.*?)', $source ),
137 'action_data' => array( 'url' => str_replace( '*', '$1', trim( $target ) ) ),
138 'regex' => strpos( $source, '*' ) === false ? false : true,
139 'group_id' => $group_id,
140 'match_type' => 'url',
141 'action_type' => 'url',
142 'action_code' => 301,
143 );
144
145 return Red_Item::create( $item );
146 }
147
148 public function get_data() {
149 $data = get_option( '301_redirects' );
150
151 if ( $data ) {
152 return array(
153 'id' => 'wp-simple-redirect',
154 'name' => 'Simple 301 Redirects',
155 'total' => count( $data ),
156 );
157 }
158
159 return false;
160 }
161 }
162
163 class Red_WordPressOldSlug_Importer extends Red_Plugin_Importer {
164 public function import_plugin( $group_id ) {
165 global $wpdb;
166
167 $count = 0;
168 $redirects = $wpdb->get_results(
169 "SELECT {$wpdb->prefix}postmeta.* FROM {$wpdb->prefix}postmeta INNER JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id " .
170 "WHERE {$wpdb->prefix}postmeta.meta_key = '_wp_old_slug' AND {$wpdb->prefix}posts.post_status='publish' AND {$wpdb->prefix}posts.post_type IN ('page', 'post')"
171 );
172
173 foreach ( $redirects as $redirect ) {
174 $item = $this->create_for_item( $group_id, $redirect );
175
176 if ( $item ) {
177 $count++;
178 }
179 }
180
181 return $count;
182 }
183
184 private function create_for_item( $group_id, $redirect ) {
185 $new = get_permalink( $redirect->post_id );
186 if ( is_wp_error( $new ) ) {
187 return false;
188 }
189
190 $new_path = wp_parse_url( $new, PHP_URL_PATH );
191 $old = rtrim( dirname( $new_path ), '/' ) . '/' . rtrim( $redirect->meta_value, '/' ) . '/';
192 $old = str_replace( '\\', '', $old );
193
194 $data = array(
195 'url' => $old,
196 'action_data' => array( 'url' => $new ),
197 'regex' => false,
198 'group_id' => $group_id,
199 'match_type' => 'url',
200 'action_type' => 'url',
201 'action_code' => 301,
202 );
203
204 return Red_Item::create( $data );
205 }
206
207 public function get_data() {
208 global $wpdb;
209
210 $total = $wpdb->get_var(
211 "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}posts.post_status='publish' AND {$wpdb->prefix}posts.post_type IN ('page', 'post')"
212 );
213
214 if ( $total ) {
215 return array(
216 'id' => 'wordpress-old-slugs',
217 'name' => __( 'Default WordPress "old slugs"', 'redirection' ),
218 'total' => intval( $total, 10 ),
219 );
220 }
221
222 return false;
223 }
224 }
225
226 class Red_SeoRedirection_Importer extends Red_Plugin_Importer {
227 public function import_plugin( $group_id ) {
228 global $wpdb;
229
230 if ( defined( 'REDIRECTION_TESTS' ) && REDIRECTION_TESTS ) {
231 return 0;
232 }
233
234 $count = 0;
235 $redirects = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}WP_SEO_Redirection" );
236
237 foreach ( $redirects as $redirect ) {
238 $item = $this->create_for_item( $group_id, $redirect );
239
240 if ( $item ) {
241 $count++;
242 }
243 }
244
245 return $count;
246 }
247
248 private function create_for_item( $group_id, $seo ) {
249 if ( intval( $seo->enabled, 10 ) === 0 ) {
250 return false;
251 }
252
253 $data = array(
254 'url' => $seo->regex ? $seo->regex : $seo->redirect_from,
255 'action_data' => array( 'url' => $seo->redirect_to ),
256 'regex' => $seo->regex ? true : false,
257 'group_id' => $group_id,
258 'match_type' => 'url',
259 'action_type' => 'url',
260 'action_code' => intval( $seo->redirect_type, 10 ),
261 );
262
263 return Red_Item::create( $data );
264 }
265
266 public function get_data() {
267 global $wpdb;
268
269 $plugins = get_option( 'active_plugins', array() );
270 $found = false;
271
272 foreach ( $plugins as $plugin ) {
273 if ( strpos( $plugin, 'seo-redirection.php' ) !== false ) {
274 $found = true;
275 break;
276 }
277 }
278
279 if ( $found ) {
280 $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}WP_SEO_Redirection" );
281
282 return array(
283 'id' => 'seo-redirection',
284 'name' => 'SEO Redirection',
285 'total' => $total,
286 );
287 }
288
289 return false;
290 }
291 }
292
293 class Red_SafeRedirectManager_Importer extends Red_Plugin_Importer {
294 public function import_plugin( $group_id ) {
295 global $wpdb;
296
297 $count = 0;
298 $redirects = $wpdb->get_results(
299 "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'"
300 );
301
302 // Group them by post ID
303 $by_post = array();
304 foreach ( $redirects as $redirect ) {
305 if ( ! isset( $by_post[ $redirect->post_id ] ) ) {
306 $by_post[ $redirect->post_id ] = array();
307 }
308
309 $by_post[ $redirect->post_id ][ str_replace( '_redirect_rule_', '', $redirect->meta_key ) ] = $redirect->meta_value;
310 }
311
312 // Now go through the redirects
313 foreach ( $by_post as $post ) {
314 $item = $this->create_for_item( $group_id, $post );
315
316 if ( $item ) {
317 $count++;
318 }
319 }
320
321 return $count;
322 }
323
324 private function create_for_item( $group_id, $post ) {
325 $regex = false;
326 $source = $post['from'];
327
328 if ( strpos( $post['from'], '*' ) !== false ) {
329 $regex = true;
330 $source = str_replace( '*', '.*', $source );
331 } elseif ( isset( $post['from_regex'] ) && $post['from_regex'] === '1' ) {
332 $regex = true;
333 }
334
335 $data = array(
336 'url' => $source,
337 'action_data' => array( 'url' => $post['to'] ),
338 'regex' => $regex,
339 'group_id' => $group_id,
340 'match_type' => 'url',
341 'action_type' => 'url',
342 'action_code' => intval( $post['status_code'], 10 ),
343 );
344
345 return Red_Item::create( $data );
346 }
347
348 public function get_data() {
349 global $wpdb;
350
351 $total = $wpdb->get_var(
352 "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'"
353 );
354
355 if ( $total ) {
356 return array(
357 'id' => 'safe-redirect-manager',
358 'name' => 'Safe Redirect Manager',
359 'total' => intval( $total, 10 ),
360 );
361 }
362
363 return false;
364 }
365 }
366