-
Notifications
You must be signed in to change notification settings - Fork 16
Feature/delete update bulk redirects #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,9 +70,10 @@ static function maybe_do_redirect() { | |
| * | ||
| * @param string $from_url URL or path that should be redirected; should have leading slash if path. | ||
| * @param int|string $redirect_to The post ID or URL to redirect to. | ||
| * @return bool|WP_Error Error if invalid redirect URL specified or if the URI already has a rule; false if not is_admin, true otherwise. | ||
| * @param bool $mode add|update if "update", we will update existing redirects for from_url, otherwise we raise an error on an existing redirect | ||
| * @return bool|WP_Error Error if invalid redirect URL specified or if the URI already has a rule and we aren't updating or if the update failed; false if something is very wrong (not admin, not CLI), true otherwise. | ||
| */ | ||
| static function insert_legacy_redirect( $from_url, $redirect_to ) { | ||
| static function insert_legacy_redirect( $from_url, $redirect_to, $mode='add' ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the param might be better as a bool like
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree. I think I was just being clever and passing the mode in directly. |
||
|
|
||
| if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) && ! is_admin() && ! apply_filters( 'wpcom_legacy_redirector_allow_insert', false ) ) { | ||
| // never run on the front end | ||
|
|
@@ -86,7 +87,7 @@ static function insert_legacy_redirect( $from_url, $redirect_to ) { | |
|
|
||
| $from_url_hash = self::get_url_hash( $from_url ); | ||
|
|
||
| if ( false !== self::get_redirect_uri( $from_url ) ) { | ||
| if ( false !== self::get_redirect_uri( $from_url ) && ( 'add' === $mode ) ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of checking for a duplicate via
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe -- the lookup below is not strict (ie -- doesn't care about trailing slashes) whereas this check has so far been strict about trailing slashes. Separate enhancement? |
||
| return new WP_Error( 'duplicate-redirect-uri', 'A redirect for this URI already exists' ); | ||
| } | ||
|
|
||
|
|
@@ -104,13 +105,58 @@ static function insert_legacy_redirect( $from_url, $redirect_to ) { | |
| return new WP_Error( 'invalid-redirect-url', 'Invalid redirect_to param; should be a post_id or a URL' ); | ||
| } | ||
|
|
||
| wp_insert_post( $args ); | ||
| switch ( $mode ) { | ||
| case 'update': | ||
| $args[ 'ID' ] = self::get_redirect_post_id( $from_url, false ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can simplify most of this code by just using
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yah! I forgot that insert falls back to update if there's an ID arg. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
| if ( 0 === wp_update_post( $args ) ) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return new WP_Error( 'failed-redirect-update', 'Redirect update failed for unknown reason' ); | ||
| } | ||
| break; | ||
| case 'add': | ||
| default: | ||
| wp_insert_post( $args ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similarly to how the code is checking whether the I know that it hasn't been doing that prior this update, so feel free to leave that for a separate enhancement. |
||
| break; | ||
| } | ||
|
|
||
| wp_cache_delete( $from_url_hash, self::CACHE_GROUP ); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $from_url URL or path for the redirect we'd like to delete. | ||
| * @return bool|WP_Error WP_Error if something is amiss with the delete; true otherwise. | ||
| */ | ||
|
|
||
| static function delete_legacy_redirect( $from_url ) { | ||
| if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) && ! is_admin() ) { | ||
| // never run on the front end | ||
| return false; | ||
| } | ||
|
|
||
| $from_url = self::normalise_url( $from_url ); | ||
|
|
||
| if ( is_wp_error( $from_url ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| $post_id = self::get_redirect_post_id( $from_url, false); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: spacing. |
||
|
|
||
| //sanity check that we are deleting only the correct post_type | ||
| if ( get_post_type( $post_id ) !== self::POST_TYPE ) { | ||
| return new WP_Error( 'failed-redirect-delete', 'Redirect delete attempted to delete a post of incorrect type.' ); | ||
| } | ||
|
|
||
| if ( 0 !== $post_id ) { | ||
| wp_delete_post( $post_id, true ); | ||
| }else{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: spacing. |
||
| return false; | ||
| } | ||
|
|
||
| wp_cache_delete( self::get_url_hash( $from_url ), self::CACHE_GROUP ); | ||
| return true; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function does return true anytime it attempted to delete the post and flush cache. But it does not mean the post has been deleted. |
||
| } | ||
|
|
||
| static function get_redirect_uri( $url ) { | ||
|
|
||
| $url = self::normalise_url( $url ); | ||
|
|
@@ -144,23 +190,33 @@ static function get_redirect_uri( $url ) { | |
| return false; | ||
| } | ||
|
|
||
| static function get_redirect_post_id( $url ) { | ||
| static function get_redirect_post_id( $url, $strict = true ) { | ||
| global $wpdb; | ||
|
|
||
| $url_hash = self::get_url_hash( $url ); | ||
|
|
||
| $redirect_post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s AND post_name = %s LIMIT 1", self::POST_TYPE, $url_hash ) ); | ||
|
|
||
| if ( ! $redirect_post_id ) | ||
| if ( !$redirect_post_id && !$strict ) { | ||
| // if we have a trailing slash, try without. If we don't try with | ||
| if ( substr( $url, -1 ) === '/' ) { | ||
| $url = rtrim( $url, '/' ); | ||
| }else{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: spacing. |
||
| $url = $url . '/'; | ||
| } | ||
|
|
||
| $url_hash = self::get_url_hash( $url ); | ||
|
|
||
| $redirect_post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s AND post_name = %s LIMIT 1", self::POST_TYPE, $url_hash ) ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just call the function again with
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup that's cleaner. |
||
| } | ||
|
|
||
| if ( !$redirect_post_id ) { | ||
| $redirect_post_id = 0; | ||
| } | ||
|
|
||
| return $redirect_post_id; | ||
| } | ||
|
|
||
| private static function get_url_hash( $url ) { | ||
| return md5( $url ); | ||
| } | ||
|
|
||
| /** | ||
| * Takes a request URL and "normalises" it, stripping common elements | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: spacing.