| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Monitor { |
| 4 |
private $monitor_group_id; |
| 5 |
|
| 6 |
function __construct( $options ) { |
| 7 |
if ( isset( $options['monitor_post'] ) && $options['monitor_post'] > 0 ) { |
| 8 |
$this->monitor_group_id = intval( $options['monitor_post'], 10 ); |
| 9 |
|
| 10 |
// Only monitor if permalinks enabled |
| 11 |
if ( get_option( 'permalink_structure' ) ) { |
| 12 |
add_action( 'post_updated', array( $this, 'post_updated' ), 11, 3 ); |
| 13 |
add_action( 'edit_form_advanced', array( $this, 'insert_old_post' ) ); |
| 14 |
add_action( 'edit_page_form', array( $this, 'insert_old_post' ) ); |
| 15 |
add_filter( 'redirection_remove_existing', array( $this, 'remove_existing_redirect' ) ); |
| 16 |
add_filter( 'redirection_permalink_changed', array( $this, 'has_permalink_changed' ), 10, 3 ); |
| 17 |
} |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
public function remove_existing_redirect( $url ) { |
| 22 |
Red_Item::disable_where_matches( $url ); |
| 23 |
} |
| 24 |
|
| 25 |
public function insert_old_post() { |
| 26 |
$url = parse_url( get_permalink(), PHP_URL_PATH ); |
| 27 |
|
| 28 |
?> |
| 29 |
<input type="hidden" name="redirection_slug" value="<?php echo esc_attr( $url ) ?>"/> |
| 30 |
<?php |
| 31 |
} |
| 32 |
|
| 33 |
public function can_monitor_post( $post, $post_before, $form_data ) { |
| 34 |
// Check this is the for the expected post |
| 35 |
if ( ! isset( $form_data['ID'] ) || ! isset( $post->ID ) || $form_data['ID'] !== $post->ID ) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
// Don't do anything if we're not published |
| 40 |
if ( $post->post_status !== 'publish' || $post_before->post_status !== 'publish' ) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
// Hierarchical post? Do nothing |
| 45 |
if ( is_post_type_hierarchical( $post->post_type ) ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
// Old Redirection slug not defined? Do nothing |
| 50 |
if ( ! isset( $form_data['redirection_slug'] ) ) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Called when a post has been updated - check if the slug has changed |
| 59 |
*/ |
| 60 |
public function post_updated( $post_id, $post, $post_before ) { |
| 61 |
if ( $this->can_monitor_post( $post, $post_before, $_POST ) ) { |
| 62 |
$this->check_for_modified_slug( $post_id, $_POST['redirection_slug'] ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Changed if permalinks are different and the before wasn't the site url (we don't want to redirect the site URL) |
| 68 |
*/ |
| 69 |
public function has_permalink_changed( $result, $before, $after ) { |
| 70 |
// Check it's not redirecting from the root |
| 71 |
if ( $this->get_site_path() === $before || $before === '/' ) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
// Are the URLs the same? |
| 76 |
if ( $before === $after ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
private function get_site_path() { |
| 84 |
$path = parse_url( get_site_url(), PHP_URL_PATH ); |
| 85 |
|
| 86 |
if ( $path ) { |
| 87 |
return rtrim( $path, '/' ).'/'; |
| 88 |
} |
| 89 |
|
| 90 |
return '/'; |
| 91 |
} |
| 92 |
|
| 93 |
public function check_for_modified_slug( $post_id, $before ) { |
| 94 |
$after = parse_url( get_permalink( $post_id ), PHP_URL_PATH ); |
| 95 |
$before = esc_url( $before ); |
| 96 |
|
| 97 |
if ( apply_filters( 'redirection_permalink_changed', false, $before, $after ) ) { |
| 98 |
do_action( 'redirection_remove_existing', $after, $post_id ); |
| 99 |
|
| 100 |
// Create a new redirect for this post |
| 101 |
Red_Item::create( array( |
| 102 |
'url' => $before, |
| 103 |
'action_data' => $after, |
| 104 |
'match_type' => 'url', |
| 105 |
'action_type' => 'url', |
| 106 |
'group_id' => $this->monitor_group_id, |
| 107 |
) ); |
| 108 |
|
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
return false; |
| 113 |
} |
| 114 |
} |
| 115 |
|