PluginProbe
Redirection / 5.4.1
Redirection v5.4.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 5.4.1, at models/monitor.php

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