PluginProbe
Redirection / 2.7
Redirection v2.7
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
← All changes | models/monitor.php +33 -64 4.22.7 View file →
@@ -1,28 +1,20 @@
1 1 <?php
2 2
3 3 class Red_Monitor {
4 4 private $monitor_group_id;
5 - private $updated_posts = array();
6 - private $monitor_types = array();
7 5
8 6 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 ) {
7 + if ( isset( $options['monitor_post'] ) && $options['monitor_post'] > 0 ) {
12 8 $this->monitor_group_id = intval( $options['monitor_post'], 10 );
13 - $this->associated = isset( $options['associated_redirect'] ) ? $options['associated_redirect'] : '';
14 9
15 10 // Only monitor if permalinks enabled
16 11 if ( get_option( 'permalink_structure' ) ) {
17 - add_action( 'pre_post_update', array( $this, 'pre_post_update' ), 10, 2 );
18 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' ) );
19 15 add_filter( 'redirection_remove_existing', array( $this, 'remove_existing_redirect' ) );
20 16 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 17 }
26 18 }
27 19 }
28 20
@@ -29,21 +21,34 @@
29 21 public function remove_existing_redirect( $url ) {
30 22 Red_Item::disable_where_matches( $url );
31 23 }
32 24
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 ] ) ) {
25 + public function insert_old_post() {
26 + $url = parse_url( get_permalink(), PHP_URL_PATH );
27 +
28 +?>
29 + <input type="hidden" name="redirection_slug" value="<?php echo esc_attr( $url ) ?>"/>
30 +<?php
31 + }
32 +
33 + public function can_monitor_post( $post, $post_before, $form_data ) {
34 + // Check this is the for the expected post
35 + if ( ! isset( $form_data['ID'] ) || ! isset( $post->ID ) || $form_data['ID'] !== $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' ) {
36 41 return false;
37 42 }
38 43
39 - // Don't do anything if we're not published
40 - if ( $post->post_status !== 'publish' || $post_before->post_status !== 'publish' ) {
44 + // Hierarchical post? Do nothing
45 + if ( is_post_type_hierarchical( $post->post_type ) ) {
41 46 return false;
42 47 }
43 48
44 - $type = get_post_type( $post->ID );
45 - if ( ! in_array( $type, $this->monitor_types ) ) {
49 + // Old Redirection slug not defined? Do nothing
50 + if ( ! isset( $form_data['redirection_slug'] ) ) {
46 51 return false;
47 52 }
48 53
49 54 return true;
@@ -52,36 +57,14 @@
52 57 /**
53 58 * Called when a post has been updated - check if the slug has changed
54 59 */
55 60 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 ] );
61 + if ( $this->can_monitor_post( $post, $post_before, $_POST ) ) {
62 + $this->check_for_modified_slug( $post_id, $_POST['redirection_slug'] );
58 63 }
59 64 }
60 65
61 66 /**
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 67 * Changed if permalinks are different and the before wasn't the site url (we don't want to redirect the site URL)
85 68 */
86 69 public function has_permalink_changed( $result, $before, $after ) {
87 70 // Check it's not redirecting from the root
@@ -97,12 +80,12 @@
97 80 return true;
98 81 }
99 82
100 83 private function get_site_path() {
101 - $path = wp_parse_url( get_site_url(), PHP_URL_PATH );
84 + $path = parse_url( get_site_url(), PHP_URL_PATH );
102 85
103 86 if ( $path ) {
104 - return rtrim( $path, '/' ) . '/';
87 + return rtrim( $path, '/' ).'/';
105 88 }
106 89
107 90 return '/';
108 91 }
@@ -107,36 +90,22 @@
107 90 return '/';
108 91 }
109 92
110 93 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 );
94 + $after = parse_url( get_permalink( $post_id ), PHP_URL_PATH );
95 + $before = esc_url( $before );
113 96
114 97 if ( apply_filters( 'redirection_permalink_changed', false, $before, $after ) ) {
115 98 do_action( 'redirection_remove_existing', $after, $post_id );
116 99
117 - $data = array(
100 + // Create a new redirect for this post
101 + Red_Item::create( array(
118 102 'url' => $before,
119 - 'action_data' => array( 'url' => $after ),
103 + 'action_data' => $after,
120 104 'match_type' => 'url',
121 105 'action_type' => 'url',
122 - 'action_code' => 301,
123 106 '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 - }
107 + ) );
139 108
140 109 return true;
141 110 }
142 111