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

373 lines 9.0 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 if ( ! function_exists( 'is_plugin_active' ) ) {
105 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
106 }
107
108 $total = 0;
109 if ( is_plugin_active( 'seo-by-rank-math/rank-math.php' ) ) {
110 $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}rank_math_redirections" );
111 }
112
113 if ( $total ) {
114 return array(
115 'id' => 'rank-math',
116 'name' => 'RankMath',
117 'total' => intval( $total, 10 ),
118 );
119 }
120
121 return 0;
122 }
123 }
124
125 class Red_Simple301_Importer extends Red_Plugin_Importer {
126 public function import_plugin( $group_id ) {
127 $redirects = get_option( '301_redirects' );
128 $count = 0;
129
130 foreach ( $redirects as $source => $target ) {
131 $item = $this->create_for_item( $group_id, $source, $target );
132
133 if ( $item ) {
134 $count++;
135 }
136 }
137
138 return $count;
139 }
140
141 private function create_for_item( $group_id, $source, $target ) {
142 $item = array(
143 'url' => str_replace( '*', '(.*?)', $source ),
144 'action_data' => array( 'url' => str_replace( '*', '$1', trim( $target ) ) ),
145 'regex' => strpos( $source, '*' ) === false ? false : true,
146 'group_id' => $group_id,
147 'match_type' => 'url',
148 'action_type' => 'url',
149 'action_code' => 301,
150 );
151
152 return Red_Item::create( $item );
153 }
154
155 public function get_data() {
156 $data = get_option( '301_redirects' );
157
158 if ( $data ) {
159 return array(
160 'id' => 'wp-simple-redirect',
161 'name' => 'Simple 301 Redirects',
162 'total' => count( $data ),
163 );
164 }
165
166 return false;
167 }
168 }
169
170 class Red_WordPressOldSlug_Importer extends Red_Plugin_Importer {
171 public function import_plugin( $group_id ) {
172 global $wpdb;
173
174 $count = 0;
175 $redirects = $wpdb->get_results(
176 "SELECT {$wpdb->prefix}postmeta.* FROM {$wpdb->prefix}postmeta INNER JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id " .
177 "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')"
178 );
179
180 foreach ( $redirects as $redirect ) {
181 $item = $this->create_for_item( $group_id, $redirect );
182
183 if ( $item ) {
184 $count++;
185 }
186 }
187
188 return $count;
189 }
190
191 private function create_for_item( $group_id, $redirect ) {
192 $new = get_permalink( $redirect->post_id );
193 if ( is_wp_error( $new ) ) {
194 return false;
195 }
196
197 $new_path = wp_parse_url( $new, PHP_URL_PATH );
198 $old = rtrim( dirname( $new_path ), '/' ) . '/' . rtrim( $redirect->meta_value, '/' ) . '/';
199 $old = str_replace( '\\', '', $old );
200
201 $data = array(
202 'url' => $old,
203 'action_data' => array( 'url' => $new ),
204 'regex' => false,
205 'group_id' => $group_id,
206 'match_type' => 'url',
207 'action_type' => 'url',
208 'action_code' => 301,
209 );
210
211 return Red_Item::create( $data );
212 }
213
214 public function get_data() {
215 global $wpdb;
216
217 $total = $wpdb->get_var(
218 "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')"
219 );
220
221 if ( $total ) {
222 return array(
223 'id' => 'wordpress-old-slugs',
224 'name' => __( 'Default WordPress "old slugs"', 'redirection' ),
225 'total' => intval( $total, 10 ),
226 );
227 }
228
229 return false;
230 }
231 }
232
233 class Red_SeoRedirection_Importer extends Red_Plugin_Importer {
234 public function import_plugin( $group_id ) {
235 global $wpdb;
236
237 if ( defined( 'REDIRECTION_TESTS' ) && REDIRECTION_TESTS ) {
238 return 0;
239 }
240
241 $count = 0;
242 $redirects = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}WP_SEO_Redirection" );
243
244 foreach ( $redirects as $redirect ) {
245 $item = $this->create_for_item( $group_id, $redirect );
246
247 if ( $item ) {
248 $count++;
249 }
250 }
251
252 return $count;
253 }
254
255 private function create_for_item( $group_id, $seo ) {
256 if ( intval( $seo->enabled, 10 ) === 0 ) {
257 return false;
258 }
259
260 $data = array(
261 'url' => $seo->regex ? $seo->regex : $seo->redirect_from,
262 'action_data' => array( 'url' => $seo->redirect_to ),
263 'regex' => $seo->regex ? true : false,
264 'group_id' => $group_id,
265 'match_type' => 'url',
266 'action_type' => 'url',
267 'action_code' => intval( $seo->redirect_type, 10 ),
268 );
269
270 return Red_Item::create( $data );
271 }
272
273 public function get_data() {
274 global $wpdb;
275
276 $plugins = get_option( 'active_plugins', array() );
277 $found = false;
278
279 foreach ( $plugins as $plugin ) {
280 if ( strpos( $plugin, 'seo-redirection.php' ) !== false ) {
281 $found = true;
282 break;
283 }
284 }
285
286 if ( $found ) {
287 $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}WP_SEO_Redirection" );
288
289 return array(
290 'id' => 'seo-redirection',
291 'name' => 'SEO Redirection',
292 'total' => $total,
293 );
294 }
295
296 return false;
297 }
298 }
299
300 class Red_SafeRedirectManager_Importer extends Red_Plugin_Importer {
301 public function import_plugin( $group_id ) {
302 global $wpdb;
303
304 $count = 0;
305 $redirects = $wpdb->get_results(
306 "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'"
307 );
308
309 // Group them by post ID
310 $by_post = array();
311 foreach ( $redirects as $redirect ) {
312 if ( ! isset( $by_post[ $redirect->post_id ] ) ) {
313 $by_post[ $redirect->post_id ] = array();
314 }
315
316 $by_post[ $redirect->post_id ][ str_replace( '_redirect_rule_', '', $redirect->meta_key ) ] = $redirect->meta_value;
317 }
318
319 // Now go through the redirects
320 foreach ( $by_post as $post ) {
321 $item = $this->create_for_item( $group_id, $post );
322
323 if ( $item ) {
324 $count++;
325 }
326 }
327
328 return $count;
329 }
330
331 private function create_for_item( $group_id, $post ) {
332 $regex = false;
333 $source = $post['from'];
334
335 if ( strpos( $post['from'], '*' ) !== false ) {
336 $regex = true;
337 $source = str_replace( '*', '.*', $source );
338 } elseif ( isset( $post['from_regex'] ) && $post['from_regex'] === '1' ) {
339 $regex = true;
340 }
341
342 $data = array(
343 'url' => $source,
344 'action_data' => array( 'url' => $post['to'] ),
345 'regex' => $regex,
346 'group_id' => $group_id,
347 'match_type' => 'url',
348 'action_type' => 'url',
349 'action_code' => intval( $post['status_code'], 10 ),
350 );
351
352 return Red_Item::create( $data );
353 }
354
355 public function get_data() {
356 global $wpdb;
357
358 $total = $wpdb->get_var(
359 "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'"
360 );
361
362 if ( $total ) {
363 return array(
364 'id' => 'safe-redirect-manager',
365 'name' => 'Safe Redirect Manager',
366 'total' => intval( $total, 10 ),
367 );
368 }
369
370 return false;
371 }
372 }
373