| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Monitor { |
| 4 |
private $monitor_group_id; |
| 5 |
private $updated_posts = array(); |
| 6 |
private $monitor_types = array(); |
| 7 |
|
| 8 |
function __construct( $options ) { |
| 9 |
$this->monitor_types = apply_filters( 'redirection_monitor_types', isset( $options['monitor_types'] ) ? $options['monitor_types'] : array() ); |
| 10 |
|
| 11 |
if ( count( $this->monitor_types ) > 0 && $options['monitor_post'] > 0 ) { |
| 12 |
$this->monitor_group_id = intval( $options['monitor_post'], 10 ); |
| 13 |
$this->associated = isset( $options['associated_redirect'] ) ? $options['associated_redirect'] : ''; |
| 14 |
|
| 15 |
// Only monitor if permalinks enabled |
| 16 |
if ( get_option( 'permalink_structure' ) ) { |
| 17 |
add_action( 'pre_post_update', array( $this, 'pre_post_update' ), 10, 2 ); |
| 18 |
add_action( 'post_updated', array( $this, 'post_updated' ), 11, 3 ); |
| 19 |
add_filter( 'redirection_remove_existing', array( $this, 'remove_existing_redirect' ) ); |
| 20 |
add_filter( 'redirection_permalink_changed', array( $this, 'has_permalink_changed' ), 10, 3 ); |
| 21 |
|
| 22 |
if ( in_array( 'trash', $this->monitor_types ) ) { |
| 23 |
add_action( 'wp_trash_post', array( $this, 'post_trashed' ) ); |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public function remove_existing_redirect( $url ) { |
| 30 |
Red_Item::disable_where_matches( $url ); |
| 31 |
} |
| 32 |
|
| 33 |
public function can_monitor_post( $post, $post_before ) { |
| 34 |
// Check this is for the expected post |
| 35 |
if ( ! isset( $post->ID ) || ! isset( $this->updated_posts[ $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 |
$type = get_post_type( $post->ID ); |
| 45 |
if ( ! in_array( $type, $this->monitor_types ) ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Called when a post has been updated - check if the slug has changed |
| 54 |
*/ |
| 55 |
public function post_updated( $post_id, $post, $post_before ) { |
| 56 |
if ( isset( $this->updated_posts[ $post_id ] ) && $this->can_monitor_post( $post, $post_before ) ) { |
| 57 |
$this->check_for_modified_slug( $post_id, $this->updated_posts[ $post_id ] ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Remember the previous post permalink |
| 63 |
*/ |
| 64 |
public function pre_post_update( $post_id, $data ) { |
| 65 |
$this->updated_posts[ $post_id ] = get_permalink( $post_id ); |
| 66 |
} |
| 67 |
|
| 68 |
public function post_trashed( $post_id ) { |
| 69 |
$data = array( |
| 70 |
'url' => wp_parse_url( get_permalink( $post_id ), PHP_URL_PATH ), |
| 71 |
'action_data' => array( 'url' => '/' ), |
| 72 |
'match_type' => 'url', |
| 73 |
'action_type' => 'url', |
| 74 |
'action_code' => 301, |
| 75 |
'group_id' => $this->monitor_group_id, |
| 76 |
'status' => 'disabled', |
| 77 |
); |
| 78 |
|
| 79 |
// Create a new redirect for this post, but only if not draft |
| 80 |
if ( $data['url'] !== '/' ) { |
| 81 |
Red_Item::create( $data ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Changed if permalinks are different and the before wasn't the site url (we don't want to redirect the site URL) |
| 87 |
*/ |
| 88 |
public function has_permalink_changed( $result, $before, $after ) { |
| 89 |
// Check it's not redirecting from the root |
| 90 |
if ( $this->get_site_path() === $before || $before === '/' ) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
// Are the URLs the same? |
| 95 |
if ( $before === $after ) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
private function get_site_path() { |
| 103 |
$path = wp_parse_url( get_site_url(), PHP_URL_PATH ); |
| 104 |
|
| 105 |
if ( $path ) { |
| 106 |
return rtrim( $path, '/' ) . '/'; |
| 107 |
} |
| 108 |
|
| 109 |
return '/'; |
| 110 |
} |
| 111 |
|
| 112 |
public function check_for_modified_slug( $post_id, $before ) { |
| 113 |
$after = wp_parse_url( get_permalink( $post_id ), PHP_URL_PATH ); |
| 114 |
$before = wp_parse_url( esc_url( $before ), PHP_URL_PATH ); |
| 115 |
|
| 116 |
if ( apply_filters( 'redirection_permalink_changed', false, $before, $after ) ) { |
| 117 |
do_action( 'redirection_remove_existing', $after, $post_id ); |
| 118 |
|
| 119 |
$data = array( |
| 120 |
'url' => $before, |
| 121 |
'action_data' => array( 'url' => $after ), |
| 122 |
'match_type' => 'url', |
| 123 |
'action_type' => 'url', |
| 124 |
'action_code' => 301, |
| 125 |
'group_id' => $this->monitor_group_id, |
| 126 |
); |
| 127 |
|
| 128 |
// Create a new redirect for this post |
| 129 |
$new_item = Red_Item::create( $data ); |
| 130 |
|
| 131 |
if ( ! is_wp_error( $new_item ) ) { |
| 132 |
do_action( 'redirection_monitor_created', $new_item, $before, $post_id ); |
| 133 |
|
| 134 |
if ( ! empty( $this->associated ) ) { |
| 135 |
// Create an associated redirect for this post |
| 136 |
$data['url'] = trailingslashit( $data['url'] ) . ltrim( $this->associated, '/' ); |
| 137 |
$data['action_data'] = array( 'url' => trailingslashit( $data['action_data']['url'] ) . ltrim( $this->associated, '/' ) ); |
| 138 |
Red_Item::create( $data ); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
return false; |
| 146 |
} |
| 147 |
} |
| 148 |
|