PluginProbe
Redirection / 2.8.1
Redirection v2.8.1
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.8.1, at models/monitor.php

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