PluginProbe
Redirection / 3.2
Redirection v3.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 3.2, at models/importer.php

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