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