From b3a582a8bba02a95979b452605bdd41fcb03d49a Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 18 Sep 2026 19:09:32 +0200 Subject: [PATCH 1/2] summary card for fallback images --- opengraph.php | 13 +++++++++---- tests/phpunit/tests/class-test-opengraph.php | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/opengraph.php b/opengraph.php index 35c56ae..70e5749 100644 --- a/opengraph.php +++ b/opengraph.php @@ -660,7 +660,8 @@ function opengraph_default_locale( $locale = '' ) { * * Twitter takes the image from `og:image`, so the card type only depends on * whether a singular post has one: `summary_large_image` if it does, - * `summary` otherwise. + * `summary` otherwise. Fallback images (site icon, logo, header) are not + * the post's own images and look better in a small card. * * @param string $card The current card type. * @param array $metadata The metadata collected so far, including `og:image`. @@ -672,11 +673,15 @@ function twitter_default_card( $card = '', $metadata = array() ) { return $card; } - if ( is_singular() && ! empty( $metadata['og:image'] ) ) { - return 'summary_large_image'; + if ( ! is_singular() || empty( $metadata['og:image'] ) ) { + return 'summary'; } - return 'summary'; + if ( opengraph_fallback_image() === $metadata['og:image'] ) { + return 'summary'; + } + + return 'summary_large_image'; } diff --git a/tests/phpunit/tests/class-test-opengraph.php b/tests/phpunit/tests/class-test-opengraph.php index 5254aa3..faf12ca 100644 --- a/tests/phpunit/tests/class-test-opengraph.php +++ b/tests/phpunit/tests/class-test-opengraph.php @@ -158,6 +158,26 @@ public function test_twitter_card() { $this->assertSame( 'summary_large_image', $metadata['twitter:card'] ); } + /** + * Test the Twitter card stays small for fallback images. + * + * @covers ::twitter_default_card + */ + public function test_twitter_card_fallback_image() { + update_option( 'site_icon', $this->create_image() ); + + $post_id = self::factory()->post->create(); + $metadata = $this->metadata_for( get_permalink( $post_id ) ); + $this->assertSame( array( get_site_icon_url( 512 ) ), $metadata['og:image'] ); + $this->assertSame( 'summary', $metadata['twitter:card'] ); + + set_post_thumbnail( $post_id, $this->create_image() ); + $metadata = $this->metadata_for( get_permalink( $post_id ) ); + $this->assertSame( 'summary_large_image', $metadata['twitter:card'] ); + + delete_option( 'site_icon' ); + } + /** * Test the property filters receive the metadata collected so far. * From 4e24184448e8203c518ae3d86eba2f561f21f35d Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 18 Sep 2026 19:14:42 +0200 Subject: [PATCH 2/2] more fallback tests --- tests/phpunit/tests/class-test-opengraph.php | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/phpunit/tests/class-test-opengraph.php b/tests/phpunit/tests/class-test-opengraph.php index faf12ca..5e1ec79 100644 --- a/tests/phpunit/tests/class-test-opengraph.php +++ b/tests/phpunit/tests/class-test-opengraph.php @@ -178,6 +178,43 @@ public function test_twitter_card_fallback_image() { delete_option( 'site_icon' ); } + /** + * Test the Twitter card stays small for the custom logo. + * + * @covers ::twitter_default_card + */ + public function test_twitter_card_fallback_custom_logo() { + set_theme_mod( 'custom_logo', $this->create_image() ); + + $post_id = self::factory()->post->create(); + $metadata = $this->metadata_for( get_permalink( $post_id ) ); + + remove_theme_mod( 'custom_logo' ); + + $this->assertCount( 1, $metadata['og:image'] ); + $this->assertSame( 'summary', $metadata['twitter:card'] ); + } + + /** + * Test the Twitter card stays small for random header images. + * + * @covers ::twitter_default_card + */ + public function test_twitter_card_fallback_random_header() { + foreach ( array( $this->create_image(), $this->create_image() ) as $header_id ) { + update_post_meta( $header_id, '_wp_attachment_is_custom_header', get_option( 'stylesheet' ) ); + } + set_theme_mod( 'header_image', 'random-uploaded-image' ); + + $post_id = self::factory()->post->create(); + $metadata = $this->metadata_for( get_permalink( $post_id ) ); + + remove_theme_mod( 'header_image' ); + + $this->assertCount( 2, $metadata['og:image'] ); + $this->assertSame( 'summary', $metadata['twitter:card'] ); + } + /** * Test the property filters receive the metadata collected so far. *