diff --git a/src/Integration/WordPressSeo/Breadcrumbs.php b/src/Integration/WordPressSeo/Breadcrumbs.php index 5124fc4..86eb05e 100644 --- a/src/Integration/WordPressSeo/Breadcrumbs.php +++ b/src/Integration/WordPressSeo/Breadcrumbs.php @@ -8,7 +8,6 @@ use Yoast\WP\SEO\Context\Meta_Tags_Context; use Yoast\WP\SEO\Main; use Yoast\WP\SEO\Models\Indexable; -use Yoast\WP\SEO\Repositories\Indexable_Repository; use Yoast\WP\SEO\Surfaces\Values\Meta; /** @@ -26,7 +25,6 @@ public function __construct( public function registerHooks(): void { - add_filter('wpseo_breadcrumb_indexables', [$this, 'fixHomeBreadcrumbs'], 10, 2); add_filter('wpseo_breadcrumb_indexables', [$this, 'fixTaxonomyBreadcrumbs'], 10, 2); add_filter('wpseo_breadcrumb_indexables', [$this, 'fixPostBreadcrumbs'], 10, 2); } @@ -125,52 +123,6 @@ public function fixTaxonomyBreadcrumbs(array $indexables, Meta_Tags_Context $con return $indexables; } - /** - * Fix Yoast breadcrumbs on home. - * - * @param Indexable[] $indexables - * @return Indexable[] - */ - public function fixHomeBreadcrumbs(array $indexables, Meta_Tags_Context $context): array - { - if (!$this->api->isQueryPageForCustomPostType()) { - return $indexables; - } - - $yoast = $this->getYoast(); - - if ($yoast->helpers->current_page->get_page_type() !== 'Home_Page') { - return $indexables; - } - - /** @var Indexable_Repository $indexableRepository */ - $indexableRepository = $yoast->classes->get(Indexable_Repository::class); - $staticAncestors = []; - - $breadcrumbsHome = $yoast->helpers->options->get('breadcrumbs-home'); - - if ($breadcrumbsHome !== '') { - $frontPageId = $yoast->helpers->current_page->get_front_page_id(); - - $staticAncestor = $frontPageId === 0 - ? $indexableRepository->find_for_home_page() - : $indexableRepository->find_by_id_and_type($frontPageId, 'post'); - - if ( - $staticAncestor instanceof Indexable - && ($frontPageId === 0 || $staticAncestor->post_status !== 'unindexed') - ) { - $staticAncestors[] = $staticAncestor; - } - } - - if (!empty($staticAncestors)) { - array_unshift($indexables, ...$staticAncestors); - } - - return $indexables; - } - private function getYoast(): Main { /** @var Main */ diff --git a/tests/Integration/Integration/WordPressSeoTest.php b/tests/Integration/Integration/WordPressSeoTest.php index a1ab42a..8bdbf55 100644 --- a/tests/Integration/Integration/WordPressSeoTest.php +++ b/tests/Integration/Integration/WordPressSeoTest.php @@ -4,6 +4,11 @@ namespace n5s\PageForCustomPostType\Tests\Integration\Integration; +use n5s\PageForCustomPostType\Core\Api; +use n5s\PageForCustomPostType\Integration\WordPressSeo\Breadcrumbs; +use n5s\PageForCustomPostType\Integration\WordPressSeo\Indexables; +use n5s\PageForCustomPostType\Integration\WordPressSeo\Schema; +use n5s\PageForCustomPostType\Integration\WordPressSeo\WordPressSeo; use n5s\PageForCustomPostType\Tests\Fixtures\TestCase; use PHPUnit\Framework\Attributes\RequiresFunction; use Yoast\WP\SEO\Memoizers\Meta_Tags_Context_Memoizer; @@ -169,4 +174,78 @@ public function testSchemaWebpageTypeUnchangedOnRegularPage(): void $this->assertEquals('WebPage', $type); } + + public function testSchemaWebpageTypeDoesNotDuplicateCollectionPage(): void + { + $this->get($this->getBookHomeUrl()); + + // CollectionPage already present: the filter must return it unchanged. + $type = apply_filters('wpseo_schema_webpage_type', ['WebPage', 'CollectionPage']); + + $this->assertSame(['WebPage', 'CollectionPage'], $type); + } + + public function testIsSupported(): void + { + $seo = new WordPressSeo(new Schema(new Api()), new Breadcrumbs(new Api()), new Indexables(new Api())); + + $this->assertTrue($seo->isSupported()); + } + + public function testRegisterHooksRegistersAllSubIntegrations(): void + { + $schema = new Schema(new Api()); + $breadcrumbs = new Breadcrumbs(new Api()); + $indexables = new Indexables(new Api()); + + (new WordPressSeo($schema, $breadcrumbs, $indexables))->registerHooks(); + + $this->assertNotFalse(has_filter('wpseo_schema_webpage_type', [$schema, 'addCollectionPageType'])); + $this->assertNotFalse(has_filter('wpseo_breadcrumb_indexables', [$breadcrumbs, 'fixPostBreadcrumbs'])); + $this->assertNotFalse(has_filter('wpseo_breadcrumb_indexables', [$breadcrumbs, 'fixTaxonomyBreadcrumbs'])); + $this->assertNotFalse(has_action('wp', [$indexables, 'configurePageDetection'])); + } + + public function testPageDetectionResolvesPfcptPageWithStaticFrontPage(): void + { + // With a static front page, Yoast's Current_Page_Helper would resolve the + // posts page instead of the PFCPT page. Indexables::configurePageDetection + // intercepts show_on_front (only for that helper) so detection falls through + // to the PFCPT page id. + $this->configureStaticFrontPage(); + + $memoizer = \YoastSEO()->classes->get(Meta_Tags_Context_Memoizer::class); + $memoizer->clear(); + + $this->get($this->getBookHomeUrl()); + + $meta = \YoastSEO()->meta->for_current_page(); + + $this->assertSame($this->getBookHomeUrl(), $meta->canonical); + } + + public function testTaxonomyBreadcrumbsSkippedForNonMainTaxonomy(): void + { + // The breadcrumb fix only applies when the current taxonomy is the post + // type's configured main taxonomy. Point books' main taxonomy elsewhere + // and confirm the genre archive is left untouched (no PFCPT crumb). + \YoastSEO()->helpers->options->set('post_types-' . self::BOOK_POST_TYPE . '-maintax', 'category'); + + $genreId = $this->getOrCreateTerm(self::GENRE_TAXONOMY, 'Fantasy'); + foreach ($this->bookIds as $bookId) { + wp_set_object_terms($bookId, $genreId, self::GENRE_TAXONOMY); + } + + $memoizer = \YoastSEO()->classes->get(Meta_Tags_Context_Memoizer::class); + $memoizer->clear(); + + $genre = get_term($genreId, self::GENRE_TAXONOMY); + $this->get(get_term_link($genre)); + + $meta = \YoastSEO()->meta->for_current_page(); + + foreach ($meta->breadcrumbs as $crumb) { + $this->assertNotSame($this->homeForBookId, \is_array($crumb) ? ($crumb['id'] ?? null) : null); + } + } }