Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions opengraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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';
}


Expand Down
57 changes: 57 additions & 0 deletions tests/phpunit/tests/class-test-opengraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,63 @@ 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'] );
Comment thread
pfefferle marked this conversation as resolved.

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 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.
*
Expand Down
Loading