diff --git a/includes/Map/HrefLang.php b/includes/Map/HrefLang.php index 2f1d2f87..de9f09e7 100644 --- a/includes/Map/HrefLang.php +++ b/includes/Map/HrefLang.php @@ -11,7 +11,7 @@ class HrefLang { /** - * @var array + * @var string[] */ protected $map = []; @@ -19,22 +19,32 @@ class HrefLang { * @param MslsBlogCollection $blogs */ public function __construct( MslsBlogCollection $blogs ) { - $map = []; - foreach ( $blogs->get_objects() as $blog ) { - $map[ $blog->get_alpha2() ][] = $blog->get_language(); - } - - foreach ( $map as $alpha2 => $languages ) { + foreach ( self::map_languages_from( $blogs ) as $alpha2 => $languages ) { if ( 1 == count( $languages ) ) { - $this->map[ $languages[0] ] = $alpha2; + $this->map[ current( $languages) ][ key( $languages ) ] = $alpha2; } else { - foreach ( $languages as $language ) { - $this->map[ $language ] = $this->get_hreflang( $language ); + foreach ( $languages as $blog_id => $language ) { + $this->map[ $language ][ $blog_id ] = $this->get_hreflang( $language ); } } } } + /** + * @param MslsBlogCollection $blogs + * + * @return array + */ + public static function map_languages_from( MslsBlogCollection $blogs ): array { + $map = []; + + foreach ( $blogs->get_objects() as $blog ) { + $map[ $blog->get_alpha2() ][ $blog->userblog_id ] = $blog->get_language(); + } + + return $map; + } + /** * @param string $language * @@ -51,19 +61,20 @@ protected function get_hreflang( string $language ): string { * * @return string */ - public function get( string $language ): string { + public function get( string $language, int $blog_id ): string { if ( ! has_filter( 'msls_head_hreflang' ) ) { - return $this->map[ $language ] ?? $language; + return $this->map[ $language ][ $blog_id ] ?? $language; } /** * Overrides the hreflang value * * @param string $language + * @param int $blog_id * * @since 0.9.9 */ - return (string) apply_filters( 'msls_head_hreflang', $language ); + return (string) apply_filters( 'msls_head_hreflang', $language, $blog_id ); } } \ No newline at end of file diff --git a/includes/MslsOutput.php b/includes/MslsOutput.php index 6bebd59a..7b6065b8 100644 --- a/includes/MslsOutput.php +++ b/includes/MslsOutput.php @@ -118,7 +118,7 @@ public function get_alternate_links() { $default = sprintf( $format, 'x-default', $url, esc_attr( $description ) ); } - $arr[] = sprintf( $format, $hreflang->get( $blog->get_language() ), $url, esc_attr( $description ) ); + $arr[] = sprintf( $format, $hreflang->get( $blog->get_language(), $blog->userblog_id ), $url, esc_attr( $description ) ); } if ( 1 === count( $arr ) ) { diff --git a/tests/Map/test-hreflang.php b/tests/Map/test-hreflang.php index b9026bdf..52ce1a4f 100644 --- a/tests/Map/test-hreflang.php +++ b/tests/Map/test-hreflang.php @@ -22,12 +22,14 @@ public function get_sut() { 'en_GB' => 'en', ]; + $i = 1; foreach ( $map as $locale => $alpha2 ) { $blog = \Mockery::mock( MslsBlog::class ); $blog->shouldReceive( [ - 'get_alpha2' => $alpha2, 'get_language' => $locale, + 'get_alpha2' => $alpha2, ] ); + $blog->userblog_id = $i++; $blogs[] = $blog; } @@ -41,22 +43,26 @@ public function get_sut() { public function test_get() { $obj = $this->get_sut(); - $this->assertEquals( 'de-DE', $obj->get( 'de_DE' ) ); - $this->assertEquals( 'de-DE', $obj->get( 'de_DE_formal' ) ); - $this->assertEquals( 'fr', $obj->get( 'fr_FR' ) ); - $this->assertEquals( 'es', $obj->get( 'es_ES' ) ); - $this->assertEquals( 'cat', $obj->get( 'cat' ) ); - $this->assertEquals( 'en-GB', $obj->get( 'en_GB' ) ); - $this->assertEquals( 'en-US', $obj->get( 'en_US' ) ); + Functions\expect( 'has_filter' )->with( 'msls_head_hreflang' )->times( 8 )->andReturn( false ); + + $this->assertEquals( 'de-DE', $obj->get( 'de_DE', 1 ) ); + $this->assertEquals( 'de-DE', $obj->get( 'de_DE_formal', 2 ) ); + $this->assertEquals( 'fr', $obj->get( 'fr_FR', 3 ) ); + $this->assertEquals( 'es', $obj->get( 'es_ES', 4 ) ); + $this->assertEquals( 'cat', $obj->get( 'cat', 5 ) ); + $this->assertEquals( 'en-US', $obj->get( 'en_US', 6 ) ); + $this->assertEquals( 'en-GB', $obj->get( 'en_GB', 7 ) ); + + $this->assertEquals( 'en_US', $obj->get( 'en_US', 8 ) ); } public function test_get_has_filter() { $obj = $this->get_sut(); - Functions\when( 'has_filter' )->justReturn( true ); - Filters\expectApplied('msls_head_hreflang')->once()->with( 'en_US')->andReturn( 'en-US' ); + Functions\expect( 'has_filter' )->with( 'msls_head_hreflang' )->once()->andReturn( true ); + Filters\expectApplied('msls_head_hreflang')->with( 'en_US')->once()->andReturn( 'en-US' ); - $this->assertEquals( 'en-US', $obj->get( 'en_US' ) ); + $this->assertEquals( 'en-US', $obj->get( 'en_US', 8 ) ); } }