From 0ad747188013f9a653d34384b98f030cc218e711 Mon Sep 17 00:00:00 2001 From: Jake Spurlock Date: Mon, 1 Jun 2020 18:47:41 +0000 Subject: [PATCH] Canonical: Add the ability to disable `redirect_guess_404_permalink()`. This also adds a few more filters to make adding redirects easier. Notably: 1. `do_redirect_guess_404_permalink` 2. `pre_redirect_guess_404_permalink` 3. `strict_redirect_guess_404_permalink` Fixes: #16557. Props: msafi, nacin, simonwheatley, westi, mboynes, joostdevalk, Lex_Robinson, MikeSchinkel, haukep, paulschreiber, DrewAPicture, ravenswd, esemlabel, stevegibson12, martychc23, DrLightman, jivanpal, neonkowy, laternastudio, apedog, audrasjb, davidbaumwald, Confridin, donmhico, ryotsun. git-svn-id: https://develop.svn.wordpress.org/trunk@47878 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/canonical.php | 46 ++++++++++++++++++++++++++++++- tests/phpunit/tests/canonical.php | 45 ++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index f1f41816f0..f4a4337fd2 100644 --- a/src/wp-includes/canonical.php +++ b/src/wp-includes/canonical.php @@ -835,8 +835,52 @@ function strip_fragment_from_url( $url ) { function redirect_guess_404_permalink() { global $wpdb; + /** + * Filters whether to do redirect guess of 404 requests. + * + * Returning a false value from the filter will disable redirect guess 404 permalink. + * and return early. + * + * @since 5.5.0 + * + * @param bool $do_redirect_guess Whether to do redirect guess 404 permalink. Default true. + */ + if ( false === apply_filters( 'do_redirect_guess_404_permalink', true ) ) { + return false; + } + + /** + * Filters whether to short-circuit redirect guess of 404 requests. + * + * Return a non-false value from the filter will short-circuit the handling and return early. + * + * @since 5.5.0 + * + * @param bool $pre Whether to short-circuit redirect guess 404 permalink. Default null. + */ + $pre = apply_filters( 'pre_redirect_guess_404_permalink', null ); + if ( null !== $pre ) { + return $pre; + } + if ( get_query_var( 'name' ) ) { - $where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' ); + + /** + * Filters whether to do a strict or loose guess. + * + * Returning true value from the filter will guess redirect only exact post_name matches. + * + * @since 5.5.0 + * + * @param bool $strict_guess Whether to do a strict/exact guess. Default false. + */ + $strict_guess = apply_filters( 'strict_redirect_guess_404_permalink', false ); + + if ( $strict_guess ) { + $where = $wpdb->prepare( 'post_name = %s', get_query_var( 'name' ) ); + } else { + $where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' ); + } // If any of post_type, year, monthnum, or day are set, use them to refine the query. if ( get_query_var( 'post_type' ) ) { diff --git a/tests/phpunit/tests/canonical.php b/tests/phpunit/tests/canonical.php index 41d377fa70..e345500a61 100644 --- a/tests/phpunit/tests/canonical.php +++ b/tests/phpunit/tests/canonical.php @@ -230,6 +230,51 @@ class Tests_Canonical extends WP_Canonical_UnitTestCase { ); } + /** + * @ticket 16557 + */ + public function test_do_redirect_guess_404_permalink() { + // Test disable do_redirect_guess_404_permalink(). + add_filter( 'do_redirect_guess_404_permalink', '__return_false' ); + $this->go_to( '/child-page-1' ); + $this->assertFalse( redirect_guess_404_permalink() ); + } + + /** + * @ticket 16557 + */ + public function test_pre_redirect_guess_404_permalink() { + // Test short-circuit filter. + add_filter( + 'pre_redirect_guess_404_permalink', + function() { + return 'wp'; + } + ); + $this->go_to( '/child-page-1' ); + $this->assertEquals( 'wp', redirect_guess_404_permalink() ); + } + + /** + * @ticket 16557 + */ + public function test_strict_redirect_guess_404_permalink() { + $post = self::factory()->post->create( + array( + 'post_title' => 'strict-redirect-guess-404-permalink', + ) + ); + + $this->go_to( 'strict-redirect' ); + + // Test default 'non-strict' redirect guess. + $this->assertEquals( get_permalink( $post ), redirect_guess_404_permalink() ); + + // Test 'strict' redirect guess. + add_filter( 'strict_redirect_guess_404_permalink', '__return_true' ); + $this->assertFalse( redirect_guess_404_permalink() ); + } + /** * @ticket 43745 */