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

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