| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Provides permalink migration facilities |
| 5 |
*/ |
| 6 |
class Red_Permalinks { |
| 7 |
/** |
| 8 |
* List of migrated permalink structures |
| 9 |
* |
| 10 |
* @var string[] |
| 11 |
*/ |
| 12 |
private $permalinks = []; |
| 13 |
|
| 14 |
/** |
| 15 |
* Current permalink structure |
| 16 |
* |
| 17 |
* @var string|null |
| 18 |
*/ |
| 19 |
private $current_permalink = null; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor |
| 23 |
* |
| 24 |
* @param string[] $permalinks List of migrated permalinks. |
| 25 |
*/ |
| 26 |
public function __construct( $permalinks ) { |
| 27 |
$this->permalinks = $permalinks; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Match and migrate any permalinks |
| 32 |
* |
| 33 |
* @param WP_Query $query Query. |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function migrate( WP_Query $query ) { |
| 37 |
global $wp; |
| 38 |
|
| 39 |
if ( count( $this->permalinks ) === 0 ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$this->intercept_permalinks(); |
| 44 |
|
| 45 |
foreach ( $this->permalinks as $old ) { |
| 46 |
// Set the current permalink |
| 47 |
$this->current_permalink = $old; |
| 48 |
|
| 49 |
// Run the WP query again |
| 50 |
$wp->init(); |
| 51 |
$wp->parse_request(); |
| 52 |
|
| 53 |
// Anything matched? |
| 54 |
if ( $wp->matched_rule ) { |
| 55 |
// Perform the post query |
| 56 |
$wp->query_posts(); |
| 57 |
|
| 58 |
// A single post? |
| 59 |
if ( is_single() && count( $query->posts ) > 0 ) { |
| 60 |
// Restore permalinks |
| 61 |
$this->release_permalinks(); |
| 62 |
|
| 63 |
// Get real URL from the post ID |
| 64 |
$url = get_permalink( $query->posts[0]->ID ); |
| 65 |
if ( $url ) { |
| 66 |
wp_safe_redirect( $url, 301, 'redirection' ); |
| 67 |
die(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
$this->release_permalinks(); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Hook the permalink options and return the migrated one |
| 80 |
* |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
private function intercept_permalinks() { |
| 84 |
add_filter( 'pre_option_rewrite_rules', [ $this, 'get_old_rewrite_rules' ] ); |
| 85 |
add_filter( 'pre_option_permalink_structure', [ $this, 'get_old_permalink' ] ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Restore the hooked option |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
private function release_permalinks() { |
| 94 |
remove_filter( 'pre_option_rewrite_rules', [ $this, 'get_old_rewrite_rules' ] ); |
| 95 |
remove_filter( 'pre_option_permalink_structure', [ $this, 'get_old_permalink' ] ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns rewrite rules for the current migrated permalink |
| 100 |
* |
| 101 |
* @param array $rules Current rules. |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
public function get_old_rewrite_rules( $rules ) { |
| 105 |
global $wp_rewrite; |
| 106 |
|
| 107 |
if ( $this->current_permalink ) { |
| 108 |
$wp_rewrite->init(); |
| 109 |
$wp_rewrite->matches = 'matches'; |
| 110 |
return $wp_rewrite->rewrite_rules(); |
| 111 |
} |
| 112 |
|
| 113 |
return $rules; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get the current migrated permalink structure |
| 118 |
* |
| 119 |
* @param string $result Current value. |
| 120 |
* @return string |
| 121 |
*/ |
| 122 |
public function get_old_permalink( $result ) { |
| 123 |
if ( $this->current_permalink ) { |
| 124 |
return $this->current_permalink; |
| 125 |
} |
| 126 |
|
| 127 |
return $result; |
| 128 |
} |
| 129 |
} |
| 130 |
|