PluginProbe
Redirection / 2.4.5
Redirection v2.4.5
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / monitor.php

monitor.php in Redirection 2.4.5, at models/monitor.php

65 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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'] );
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 ?>
26 <input type="hidden" name="redirection_slug" value="<?php echo esc_attr( $url ) ?>"/>
27 <?php
28 }
29
30 public function can_monitor_post( $post, $post_before, $form_data ) {
31 if ( $post->post_status !== 'publish' || $post_before->post_status !== 'publish' ) {
32 return false;
33 }
34
35 if ( is_post_type_hierarchical( $post->post_type ) ) {
36 return false;
37 }
38
39 if ( ! isset( $form_data['redirection_slug'] ) ) {
40 return false;
41 }
42
43 return true;
44 }
45
46 public function post_updated( $post_id, $post, $post_before ) {
47 if ( $this->can_monitor_post( $post, $post_before, $_POST ) ) {
48 $after = parse_url( get_permalink( $post_id ) );
49 $after = $after['path'];
50 $before = esc_url( $_POST['redirection_slug'] );
51 $site = parse_url( get_site_url() );
52
53 if ( $before !== $after && $before !== '/' && ( ! isset( $site['path'] ) || ( isset( $site['path'] ) && $before !== $site['path'].'/' ) ) ) {
54 Red_Item::create( array(
55 'source' => $before,
56 'target' => $after,
57 'match' => 'url',
58 'red_action' => 'url',
59 'group_id' => $this->monitor_group_id,
60 ) );
61 }
62 }
63 }
64 }
65