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

296 lines 7.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 );
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 $redirects = $wpdb->get_results(
103 "SELECT {$wpdb->prefix}postmeta.* FROM {$wpdb->prefix}postmeta INNER JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id " .
104 "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')"
105 );
106
107 foreach ( $redirects as $redirect ) {
108 $item = $this->create_for_item( $group_id, $redirect );
109
110 if ( $item ) {
111 $count++;
112 }
113 }
114
115 return $count;
116 }
117
118 private function create_for_item( $group_id, $redirect ) {
119 $new = get_permalink( $redirect->post_id );
120 if ( is_wp_error( $new ) ) {
121 return false;
122 }
123
124 $new = wp_parse_url( $new, PHP_URL_PATH );
125 $old = rtrim( dirname( $new ), '/' ) . '/' . rtrim( $redirect->meta_value, '/' ) . '/';
126 $old = str_replace( '\\', '', $old );
127
128 $data = array(
129 'url' => $old,
130 'action_data' => array( 'url' => $new ),
131 'regex' => false,
132 'group_id' => $group_id,
133 'match_type' => 'url',
134 'action_type' => 'url',
135 'action_code' => 301,
136 );
137
138 return Red_Item::create( $data );
139 }
140
141 public function get_data() {
142 global $wpdb;
143
144 $total = $wpdb->get_var(
145 "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')"
146 );
147
148 if ( $total ) {
149 return array(
150 'id' => 'wordpress-old-slugs',
151 'name' => __( 'Default WordPress "old slugs"', 'redirection' ),
152 'total' => intval( $total, 10 ),
153 );
154 }
155
156 return false;
157 }
158 }
159
160 class Red_SeoRedirection_Importer extends Red_Plugin_Importer {
161 public function import_plugin( $group_id ) {
162 global $wpdb;
163
164 $count = 0;
165 $redirects = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}WP_SEO_Redirection" );
166
167 foreach ( $redirects as $redirect ) {
168 $item = $this->create_for_item( $group_id, $redirect );
169
170 if ( $item ) {
171 $count++;
172 }
173 }
174
175 return $count;
176 }
177
178 private function create_for_item( $group_id, $seo ) {
179 if ( intval( $seo->enabled, 10 ) === 0 ) {
180 return false;
181 }
182
183 $data = array(
184 'url' => $seo->regex ? $seo->regex : $seo->redirect_from,
185 'action_data' => array( 'url' => $seo->redirect_to ),
186 'regex' => $seo->regex ? true : false,
187 'group_id' => $group_id,
188 'match_type' => 'url',
189 'action_type' => 'url',
190 'action_code' => intval( $seo->redirect_type, 10 ),
191 );
192
193 return Red_Item::create( $data );
194 }
195
196 public function get_data() {
197 global $wpdb;
198
199 $plugins = get_option( 'active_plugins', array() );
200 $found = false;
201
202 foreach ( $plugins as $plugin ) {
203 if ( strpos( $plugin, 'seo-redirection.php' ) !== false ) {
204 $found = true;
205 break;
206 }
207 }
208
209 if ( $found ) {
210 $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}WP_SEO_Redirection" );
211
212 return array(
213 'id' => 'seo-redirection',
214 'name' => 'SEO Redirection',
215 'total' => $total,
216 );
217 }
218
219 return false;
220 }
221 }
222
223 class Red_SafeRedirectManager_Importer extends Red_Plugin_Importer {
224 public function import_plugin( $group_id ) {
225 global $wpdb;
226
227 $count = 0;
228 $redirects = $wpdb->get_results(
229 "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'"
230 );
231
232 // Group them by post ID
233 $by_post = array();
234 foreach ( $redirects as $redirect ) {
235 if ( ! isset( $by_post[ $redirect->post_id ] ) ) {
236 $by_post[ $redirect->post_id ] = array();
237 }
238
239 $by_post[ $redirect->post_id ][ str_replace( '_redirect_rule_', '', $redirect->meta_key ) ] = $redirect->meta_value;
240 }
241
242 // Now go through the redirects
243 foreach ( $by_post as $post ) {
244 $item = $this->create_for_item( $group_id, $post );
245
246 if ( $item ) {
247 $count++;
248 }
249 }
250
251 return $count;
252 }
253
254 private function create_for_item( $group_id, $post ) {
255 $regex = false;
256 $source = $post['from'];
257
258 if ( strpos( $post['from'], '*' ) !== false ) {
259 $regex = true;
260 $source = str_replace( '*', '.*', $source );
261 } elseif ( isset( $post['from_regex'] ) && $post['from_regex'] === '1' ) {
262 $regex = true;
263 }
264
265 $data = array(
266 'url' => $source,
267 'action_data' => array( 'url' => $post['to'] ),
268 'regex' => $regex,
269 'group_id' => $group_id,
270 'match_type' => 'url',
271 'action_type' => 'url',
272 'action_code' => intval( $post['status_code'], 10 ),
273 );
274
275 return Red_Item::create( $data );
276 }
277
278 public function get_data() {
279 global $wpdb;
280
281 $total = $wpdb->get_var(
282 "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'"
283 );
284
285 if ( $total ) {
286 return array(
287 'id' => 'safe-redirect-manager',
288 'name' => 'Safe Redirect Manager',
289 'total' => intval( $total, 10 ),
290 );
291 }
292
293 return false;
294 }
295 }
296