From 986ebd436ea4ad42c571cc04f3534268d7e4375c Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 29 Jan 2021 20:36:03 +0000 Subject: [PATCH] Robots: Add `max-image-preview:large` directive by default. This changeset introduces a `wp_robots_max_image_preview_large()` function which is hooked into the `wp_robots` filter to include the `max-image-preview:large` directive for all sites which are configured to be indexed by search engines. The directive allows search engines to display large image previews for the site in search results. Props adamsilverstein, Clorith, flixos90, helen, joostdevalk, tweetythierry, westonruter. Fixes #51511. git-svn-id: https://develop.svn.wordpress.org/trunk@50078 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/default-filters.php | 1 + src/wp-includes/robots-template.php | 22 ++++++++++++++++++++++ tests/phpunit/tests/robots.php | 15 +++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 6babfb4785..1014a37b6d 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -233,6 +233,7 @@ add_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); // Robots filters. add_filter( 'wp_robots', 'wp_robots_noindex' ); +add_filter( 'wp_robots', 'wp_robots_max_image_preview_large' ); // Mark site as no longer fresh. foreach ( array( 'publish_post', 'publish_page', 'wp_ajax_save-widget', 'wp_ajax_widgets-order', 'customize_save_after' ) as $action ) { diff --git a/src/wp-includes/robots-template.php b/src/wp-includes/robots-template.php index 1b9292c975..dc425686c2 100644 --- a/src/wp-includes/robots-template.php +++ b/src/wp-includes/robots-template.php @@ -133,3 +133,25 @@ function wp_robots_sensitive_page( array $robots ) { $robots['noarchive'] = true; return $robots; } + +/** + * Adds 'max-image-preview:large' to the robots meta tag. + * + * This directive tells web robots that large image previews are allowed to be + * displayed, e.g. in search engines, unless the blog is marked as not being public. + * + * Typical usage is as a {@see 'wp_robots'} callback: + * + * add_filter( 'wp_robots', 'wp_robots_max_image_preview_large' ); + * + * @since 5.7.0 + * + * @param array $robots Associative array of robots directives. + * @return array Filtered robots directives. + */ +function wp_robots_max_image_preview_large( array $robots ) { + if ( get_option( 'blog_public' ) ) { + $robots['max-image-preview'] = 'large'; + } + return $robots; +} diff --git a/tests/phpunit/tests/robots.php b/tests/phpunit/tests/robots.php index dc8f3964b8..8eb83281ec 100644 --- a/tests/phpunit/tests/robots.php +++ b/tests/phpunit/tests/robots.php @@ -161,6 +161,21 @@ class Tests_Robots extends WP_UnitTestCase { $this->assertContains( "'noindex, noarchive'", $output ); } + /** + * @ticket 51511 + */ + public function test_wp_robots_max_image_preview_large() { + add_filter( 'wp_robots', 'wp_robots_max_image_preview_large' ); + + update_option( 'blog_public', '1' ); + $output = get_echo( 'wp_robots' ); + $this->assertContains( "'max-image-preview:large'", $output ); + + update_option( 'blog_public', '0' ); + $output = get_echo( 'wp_robots' ); + $this->assertEmpty( $output ); + } + public function add_noindex_directive( array $robots ) { $robots['noindex'] = true; return $robots;