| @@ -1,10 +1,8 @@ | ||
| 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 | 7 | $this->monitor_types = apply_filters( 'redirection_monitor_types', isset( $options['monitor_types'] ) ? $options['monitor_types'] : array() ); |
| 10 | 8 | |
| @@ -13,10 +11,11 @@ | ||
| 13 | 11 | $this->associated = isset( $options['associated_redirect'] ) ? $options['associated_redirect'] : ''; |
| 14 | 12 | |
| 15 | 13 | // Only monitor if permalinks enabled |
| 16 | 14 | if ( get_option( 'permalink_structure' ) ) { |
| 17 | - add_action( 'pre_post_update', array( $this, 'pre_post_update' ), 10, 2 ); | |
| 18 | 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' ) ); | |
| 19 | 18 | add_filter( 'redirection_remove_existing', array( $this, 'remove_existing_redirect' ) ); |
| 20 | 19 | add_filter( 'redirection_permalink_changed', array( $this, 'has_permalink_changed' ), 10, 3 ); |
| 21 | 20 | |
| 22 | 21 | if ( in_array( 'trash', $this->monitor_types ) ) { |
| @@ -29,24 +28,42 @@ | ||
| 29 | 28 | public function remove_existing_redirect( $url ) { |
| 30 | 29 | Red_Item::disable_where_matches( $url ); |
| 31 | 30 | } |
| 32 | 31 | |
| 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 | - } | |
| 32 | + public function insert_old_post() { | |
| 33 | + $url = parse_url( get_permalink(), PHP_URL_PATH ); | |
| 38 | 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 | + | |
| 39 | 46 | // Don't do anything if we're not published |
| 40 | 47 | if ( $post->post_status !== 'publish' || $post_before->post_status !== 'publish' ) { |
| 41 | 48 | return false; |
| 42 | 49 | } |
| 43 | 50 | |
| 51 | + // Hierarchical post? Do nothing | |
| 44 | 52 | $type = get_post_type( $post->ID ); |
| 53 | + if ( is_post_type_hierarchical( $post->post_type ) && $type !== 'page' ) { | |
| 54 | + return false; | |
| 55 | + } | |
| 56 | + | |
| 45 | 57 | if ( ! in_array( $type, $this->monitor_types ) ) { |
| 46 | 58 | return false; |
| 47 | 59 | } |
| 48 | 60 | |
| 61 | + // Old Redirection slug not defined? Do nothing | |
| 62 | + if ( ! isset( $form_data['redirection_slug'] ) ) { | |
| 63 | + return false; | |
| 64 | + } | |
| 65 | + | |
| 49 | 66 | return true; |
| 50 | 67 | } |
| 51 | 68 | |
| 52 | 69 | /** |
| @@ -52,23 +69,16 @@ | ||
| 52 | 69 | /** |
| 53 | 70 | * Called when a post has been updated - check if the slug has changed |
| 54 | 71 | */ |
| 55 | 72 | 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 ] ); | |
| 73 | + if ( $this->can_monitor_post( $post, $post_before, $_POST ) ) { | |
| 74 | + $this->check_for_modified_slug( $post_id, $_POST['redirection_slug'] ); | |
| 58 | 75 | } |
| 59 | 76 | } |
| 60 | 77 | |
| 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 | 78 | public function post_trashed( $post_id ) { |
| 69 | 79 | $data = array( |
| 70 | - 'url' => wp_parse_url( get_permalink( $post_id ), PHP_URL_PATH ), | |
| 80 | + 'url' => parse_url( get_permalink( $post_id ), PHP_URL_PATH ), | |
| 71 | 81 | 'action_data' => array( 'url' => '/' ), |
| 72 | 82 | 'match_type' => 'url', |
| 73 | 83 | 'action_type' => 'url', |
| 74 | 84 | 'action_code' => 301, |
| @@ -97,12 +107,12 @@ | ||
| 97 | 107 | return true; |
| 98 | 108 | } |
| 99 | 109 | |
| 100 | 110 | private function get_site_path() { |
| 101 | - $path = wp_parse_url( get_site_url(), PHP_URL_PATH ); | |
| 111 | + $path = parse_url( get_site_url(), PHP_URL_PATH ); | |
| 102 | 112 | |
| 103 | 113 | if ( $path ) { |
| 104 | - return rtrim( $path, '/' ) . '/'; | |
| 114 | + return rtrim( $path, '/' ).'/'; | |
| 105 | 115 | } |
| 106 | 116 | |
| 107 | 117 | return '/'; |
| 108 | 118 | } |
| @@ -107,10 +117,10 @@ | ||
| 107 | 117 | return '/'; |
| 108 | 118 | } |
| 109 | 119 | |
| 110 | 120 | 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 ); | |
| 121 | + $after = parse_url( get_permalink( $post_id ), PHP_URL_PATH ); | |
| 122 | + $before = esc_url( $before ); | |
| 113 | 123 | |
| 114 | 124 | if ( apply_filters( 'redirection_permalink_changed', false, $before, $after ) ) { |
| 115 | 125 | do_action( 'redirection_remove_existing', $after, $post_id ); |
| 116 | 126 | |
| @@ -123,19 +133,15 @@ | ||
| 123 | 133 | 'group_id' => $this->monitor_group_id, |
| 124 | 134 | ); |
| 125 | 135 | |
| 126 | 136 | // Create a new redirect for this post |
| 127 | - $new_item = Red_Item::create( $data ); | |
| 137 | + Red_Item::create( $data ); | |
| 128 | 138 | |
| 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 | - } | |
| 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'] = array( 'url' => trailingslashit( $data['action_data']['url'] ) . ltrim( $this->associated, '/' ) ); | |
| 143 | + Red_Item::create( $data ); | |
| 138 | 144 | } |
| 139 | 145 | |
| 140 | 146 | return true; |
| 141 | 147 | } |