| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Monitor { |
| 4 |
private $monitor_group_id; |
| 5 |
|
| 6 |
function __construct( $options ) { |
| 7 |
if ( $options['monitor_post'] > 0 ) { |
| 8 |
$this->monitor_group_id = intval( $options['monitor_post'] ); |
| 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 |
} |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
public function insert_old_post() { |
| 20 |
global $post; |
| 21 |
|
| 22 |
$url = parse_url( get_permalink() ); |
| 23 |
$url = $url['path']; |
| 24 |
?> |
| 25 |
<input type="hidden" name="redirection_slug" value="<?php echo esc_attr( $url ) ?>"/> |
| 26 |
<?php |
| 27 |
} |
| 28 |
|
| 29 |
public function post_updated( $post_id, $post, $post_before ) { |
| 30 |
if ( $post->post_status !== 'publish' || is_post_type_hierarchical( $post->post_type ) ) |
| 31 |
return; |
| 32 |
|
| 33 |
if ( isset( $_POST['redirection_slug'] ) ) { |
| 34 |
$after = parse_url( get_permalink( $post_id ) ); |
| 35 |
$after = $after['path']; |
| 36 |
$before = esc_url( $_POST['redirection_slug'] ); |
| 37 |
$site = parse_url( get_site_url() ); |
| 38 |
|
| 39 |
if ( in_array( $post->post_status, array( 'publish', 'static' ) ) && $before !== $after && $before !== '/' && ( !isset( $site['path'] ) || ( isset( $site['path'] ) && $before !== $site['path'].'/' ) ) ) { |
| 40 |
Red_Item::create( array( |
| 41 |
'source' => $before, |
| 42 |
'target' => $after, |
| 43 |
'match' => 'url', |
| 44 |
'red_action' => 'url', |
| 45 |
'group_id' => $this->monitor_group_id |
| 46 |
) ); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|