PluginProbe
Redirection / 3.6
Redirection v3.6
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 3.6, at models/monitor.php

146 lines 4.3 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
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
80 Red_Item::create( $data );
81 }
82
83 /**
84 * Changed if permalinks are different and the before wasn't the site url (we don't want to redirect the site URL)
85 */
86 public function has_permalink_changed( $result, $before, $after ) {
87 // Check it's not redirecting from the root
88 if ( $this->get_site_path() === $before || $before === '/' ) {
89 return false;
90 }
91
92 // Are the URLs the same?
93 if ( $before === $after ) {
94 return false;
95 }
96
97 return true;
98 }
99
100 private function get_site_path() {
101 $path = wp_parse_url( get_site_url(), PHP_URL_PATH );
102
103 if ( $path ) {
104 return rtrim( $path, '/' ) . '/';
105 }
106
107 return '/';
108 }
109
110 public function check_for_modified_slug( $post_id, $before ) {
111 $after = wp_parse_url( get_permalink( $post_id ), PHP_URL_PATH );
112 $before = wp_parse_url( esc_url( $before ), PHP_URL_PATH );
113
114 if ( apply_filters( 'redirection_permalink_changed', false, $before, $after ) ) {
115 do_action( 'redirection_remove_existing', $after, $post_id );
116
117 $data = array(
118 'url' => $before,
119 'action_data' => array( 'url' => $after ),
120 'match_type' => 'url',
121 'action_type' => 'url',
122 'action_code' => 301,
123 'group_id' => $this->monitor_group_id,
124 );
125
126 // Create a new redirect for this post
127 $new_item = Red_Item::create( $data );
128
129 if ( ! is_wp_error( $new_item ) ) {
130 do_action( 'redirection_monitor_created', $new_item, $before, $post_id );
131
132 if ( ! empty( $this->associated ) ) {
133 // Create an associated redirect for this post
134 $data['url'] = trailingslashit( $data['url'] ) . ltrim( $this->associated, '/' );
135 $data['action_data'] = array( 'url' => trailingslashit( $data['action_data']['url'] ) . ltrim( $this->associated, '/' ) );
136 Red_Item::create( $data );
137 }
138 }
139
140 return true;
141 }
142
143 return false;
144 }
145 }
146