From 6388364fec8e465fd1aefb57fbf2da0cd911e0aa Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 23 Mar 2021 23:00:03 +0000 Subject: [PATCH] Robots: Remove contradictory directive check in `wp_robots()`. Removes the mutually exclusive directives check in `wp_robots()`, ie allow both `follow` and `nofollow` to be specified and for `archive` and `noarchive` to be specified. This fixes a bug in which WordPress would defer to the most permissive over the least permissive. When contradictory instructions are included, WordPress will defer to the search engine's or archivist's resolution policy: generally this is to observe the least, not most permissive. Props Cybr, flixos90. Fixes #52713. git-svn-id: https://develop.svn.wordpress.org/trunk@50566 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/robots-template.php | 15 +---- tests/phpunit/tests/robots.php | 90 ----------------------------- 2 files changed, 1 insertion(+), 104 deletions(-) diff --git a/src/wp-includes/robots-template.php b/src/wp-includes/robots-template.php index 19c7699790..f504930093 100644 --- a/src/wp-includes/robots-template.php +++ b/src/wp-includes/robots-template.php @@ -15,6 +15,7 @@ * robots meta tag is output if there is at least one relevant directive. * * @since 5.7.0 + * @since 5.7.1 No longer prevents specific directives to occur together. */ function wp_robots() { /** @@ -30,20 +31,6 @@ function wp_robots() { */ $robots = apply_filters( 'wp_robots', array() ); - // Don't allow mutually exclusive directives. - if ( ! empty( $robots['follow'] ) ) { - unset( $robots['nofollow'] ); - } - if ( ! empty( $robots['nofollow'] ) ) { - unset( $robots['follow'] ); - } - if ( ! empty( $robots['archive'] ) ) { - unset( $robots['noarchive'] ); - } - if ( ! empty( $robots['noarchive'] ) ) { - unset( $robots['archive'] ); - } - $robots_strings = array(); foreach ( $robots as $directive => $value ) { if ( is_string( $value ) ) { diff --git a/tests/phpunit/tests/robots.php b/tests/phpunit/tests/robots.php index f826b3bf11..49fbccfe96 100644 --- a/tests/phpunit/tests/robots.php +++ b/tests/phpunit/tests/robots.php @@ -71,56 +71,6 @@ class Tests_Robots extends WP_UnitTestCase { $this->assertContains( "'{$expected_directives_string}'", $output ); } - /** - * @ticket 51511 - */ - public function test_wp_robots_includes_basic_sanitization_follow_nofollow() { - // Only follow or nofollow can be present, with follow taking precedence. - add_filter( 'wp_robots', array( $this, 'add_follow_directive' ) ); - add_filter( 'wp_robots', array( $this, 'add_nofollow_directive' ) ); - $output = get_echo( 'wp_robots' ); - $this->assertContains( "'follow'", $output ); - - // Consider truthyness of the directive value though. - // Here nofollow is true, follow is false. - add_filter( 'wp_robots', array( $this, 'remove_follow_directive' ), 11 ); - add_filter( 'wp_robots', array( $this, 'add_nofollow_directive' ), 11 ); - $output = get_echo( 'wp_robots' ); - $this->assertContains( "'nofollow'", $output ); - - // Consider truthyness of the directive value though. - // Here follow is true, nofollow is false. - add_filter( 'wp_robots', array( $this, 'add_follow_directive' ), 12 ); - add_filter( 'wp_robots', array( $this, 'remove_nofollow_directive' ), 12 ); - $output = get_echo( 'wp_robots' ); - $this->assertContains( "'follow'", $output ); - } - - /** - * @ticket 51511 - */ - public function test_wp_robots_includes_basic_sanitization_archive_noarchive() { - // Only archive or noarchive can be present, with archive taking precedence. - add_filter( 'wp_robots', array( $this, 'add_archive_directive' ) ); - add_filter( 'wp_robots', array( $this, 'add_noarchive_directive' ) ); - $output = get_echo( 'wp_robots' ); - $this->assertContains( "'archive'", $output ); - - // Consider truthyness of the directive value though. - // Here noarchive is true, archive is false. - add_filter( 'wp_robots', array( $this, 'remove_archive_directive' ), 11 ); - add_filter( 'wp_robots', array( $this, 'add_noarchive_directive' ), 11 ); - $output = get_echo( 'wp_robots' ); - $this->assertContains( "'noarchive'", $output ); - - // Consider truthyness of the directive value though. - // Here archive is true, noarchive is false. - add_filter( 'wp_robots', array( $this, 'add_archive_directive' ), 12 ); - add_filter( 'wp_robots', array( $this, 'remove_noarchive_directive' ), 12 ); - $output = get_echo( 'wp_robots' ); - $this->assertContains( "'archive'", $output ); - } - /** * @ticket 51511 */ @@ -207,44 +157,4 @@ class Tests_Robots extends WP_UnitTestCase { $robots['noindex'] = false; return $robots; } - - public function add_follow_directive( array $robots ) { - $robots['follow'] = true; - return $robots; - } - - public function remove_follow_directive( array $robots ) { - $robots['follow'] = false; - return $robots; - } - - public function add_nofollow_directive( array $robots ) { - $robots['nofollow'] = true; - return $robots; - } - - public function remove_nofollow_directive( array $robots ) { - $robots['nofollow'] = false; - return $robots; - } - - public function add_archive_directive( array $robots ) { - $robots['archive'] = true; - return $robots; - } - - public function remove_archive_directive( array $robots ) { - $robots['archive'] = false; - return $robots; - } - - public function add_noarchive_directive( array $robots ) { - $robots['noarchive'] = true; - return $robots; - } - - public function remove_noarchive_directive( array $robots ) { - $robots['noarchive'] = false; - return $robots; - } }