diff --git a/README.md b/README.md index 71dd760..ce42275 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,15 @@ By default a post keeps its markdown copy even when an SEO plugin marks it noind add_filter( 'markdown_alternate_skip_noindexed', '__return_true' ); ``` -The filter receives the post as its second argument. The check reads Yoast's effective robots value, including an empty robots array or filters that remove noindex; stale per-post metadata does not override that value. Without Yoast active, nothing is skipped. During Yoast llms.txt generation, skipped posts keep their HTML URLs instead of links to unavailable Markdown copies. Regenerate llms.txt after enabling the filter to update an existing file. Markdown responses retain their canonical Link header; this opt-in does not add a blanket noindex header. +The filter receives the post as its second argument. The check reads Yoast's effective robots value, including an empty robots array or filters that remove noindex; stale per-post metadata does not override that value. Without Yoast active, nothing is skipped unless an integration marks the post. With WooCommerce active, the cart, checkout and My account pages count as noindexed, as WooCommerce keeps them out of search itself. During Yoast llms.txt generation, skipped posts keep their HTML URLs instead of links to unavailable Markdown copies. Regenerate llms.txt after enabling the filter to update an existing file. Markdown responses retain their canonical Link header; this opt-in does not add a blanket noindex header. + +Mark other posts as noindexed, for example pages another plugin keeps out of search, with `markdown_alternate_is_noindexed`: + +```php +add_filter( 'markdown_alternate_is_noindexed', function( $noindexed, $post ) { + return $noindexed || 'members-area' === $post->post_name; +}, 10, 2 ); +``` ### Category, tag, and other term archives diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 5c6a595..3aa1e1e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -144,6 +144,12 @@ parameters: count: 1 path: src/Integration/WooCommerce.php + - + message: '#^Function wc_get_page_id not found\.$#' + identifier: function.notFound + count: 1 + path: src/Integration/WooCommerce.php + - message: '#^Function wc_get_product not found\.$#' identifier: function.notFound diff --git a/readme.txt b/readme.txt index 3e42601..9b5cd72 100644 --- a/readme.txt +++ b/readme.txt @@ -102,6 +102,10 @@ Yes, as an opt-in. With `add_filter( 'markdown_alternate_skip_noindexed', '__ret == Changelog == += Unreleased = +* Added the `markdown_alternate_is_noindexed` filter, to mark posts noindexed by other means than Yoast SEO +* With `markdown_alternate_skip_noindexed` on, the WooCommerce cart, checkout and My account pages get no Markdown version + = 1.3.0 = * Security: Prevented logged-in, password-protected, and known session-specific Markdown from entering shared caches; invalidated older cached documents and sent private, no-store headers for personalized post and archive responses. * Moved Markdown response handlers to template_redirect priority 15, after default-priority access checks and canonical redirects, while retaining the canonical-redirect exemption for explicit .md URLs. diff --git a/src/Integration/WooCommerce.php b/src/Integration/WooCommerce.php index 64087c9..b02eb7b 100644 --- a/src/Integration/WooCommerce.php +++ b/src/Integration/WooCommerce.php @@ -40,6 +40,7 @@ public function register(): void { add_filter( 'markdown_alternate_term_item', array( $this, 'term_item' ), 10, 3 ); add_filter( 'markdown_alternate_term_frontmatter_content_lines', array( $this, 'term_frontmatter' ), 10, 2 ); add_filter( 'markdown_alternate_term_cache_enabled', array( $this, 'term_cache_enabled' ), 10, 2 ); + add_filter( 'markdown_alternate_is_noindexed', array( $this, 'is_noindexed' ), 10, 2 ); } /** @@ -155,6 +156,29 @@ public function term_cache_enabled( $enabled, $term ): bool { return in_array( $term->taxonomy, $this->product_taxonomies(), true ) ? false : (bool) $enabled; } + /** + * Treat the cart, checkout and My account pages as noindexed. + * + * WooCommerce keeps them out of search itself, through wp_robots while the + * HTML page renders, which is not visible from a Markdown request. They + * depend on the customer and have nothing an agent can use. + * + * @param bool $noindexed Whether the post is treated as noindexed. + * @param mixed $post The post being checked. + * @return bool Whether the post is treated as noindexed. + */ + public function is_noindexed( $noindexed, $post ): bool { + if ( $noindexed || ! $post instanceof WP_Post || 'page' !== $post->post_type ) { + return (bool) $noindexed; + } + foreach ( array( 'cart', 'checkout', 'myaccount' ) as $page ) { + if ( (int) wc_get_page_id( $page ) === (int) $post->ID ) { + return true; + } + } + return false; + } + /** * Support product permalinks without exposing standalone variations. * diff --git a/src/PostTypeSupport.php b/src/PostTypeSupport.php index 6cce73e..df9957f 100644 --- a/src/PostTypeSupport.php +++ b/src/PostTypeSupport.php @@ -65,6 +65,29 @@ public static function should_skip_noindexed( \WP_Post $post ): bool { return self::is_noindexed( $post ); } + /** + * Check whether a post is marked noindex. + * + * Reads Yoast SEO, then lets integrations and sites add posts that are + * kept out of search by other means, such as the WooCommerce cart. + * + * @param \WP_Post $post The post to check. + * @return bool True if the post is marked noindex. + */ + public static function is_noindexed( \WP_Post $post ): bool { + /** + * Filters whether a post is treated as noindexed. + * + * Only consulted when the site opts in with markdown_alternate_skip_noindexed. + * + * @since 1.3.1 + * + * @param bool $noindexed Whether Yoast SEO marks the post noindex. + * @param \WP_Post $post The post being checked. + */ + return (bool) apply_filters( 'markdown_alternate_is_noindexed', self::is_noindexed_by_yoast( $post ), $post ); + } + /** * Check whether Yoast SEO marks a post noindex. * @@ -78,7 +101,7 @@ public static function should_skip_noindexed( \WP_Post $post ): bool { * @param \WP_Post $post The post to check. * @return bool True if Yoast marks the post noindex. */ - public static function is_noindexed( \WP_Post $post ): bool { + private static function is_noindexed_by_yoast( \WP_Post $post ): bool { if ( ! function_exists( 'YoastSEO' ) ) { return false; } diff --git a/tests/run.php b/tests/run.php index b8f5508..6088ed8 100644 --- a/tests/run.php +++ b/tests/run.php @@ -354,6 +354,29 @@ static function ( $skip, $filtered_post ) use ( $post ) { md_alt_assert_same( false, PostTypeSupport::should_skip_noindexed( $post ), 'indexed' ); }; +$md_alt_tests['Integrations can mark posts noindexed that Yoast indexes'] = static function () { + $post = md_alt_test_fixture( 'ordinary' ); + $GLOBALS['md_alt_test_state']['yoast'] = array( 'index' => 'index' ); + add_filter( + 'markdown_alternate_is_noindexed', + static function ( $noindexed, $filtered_post ) use ( $post ) { + md_alt_assert_same( false, $noindexed, 'Yoast answer' ); + md_alt_assert_same( $post, $filtered_post, 'post' ); + return true; + }, + 10, + 2 + ); + md_alt_assert_same( false, PostTypeSupport::should_skip_noindexed( $post ), 'not opted in' ); + add_filter( + 'markdown_alternate_skip_noindexed', + static function () { + return true; + } + ); + md_alt_assert_same( true, PostTypeSupport::should_skip_noindexed( $post ), 'opted in' ); +}; + $md_alt_tests['Disabling cache ignores existing entries and never stores customer output'] = static function () { $post = md_alt_test_fixture( 'ordinary' ); $renderer = new ContentRenderer(); diff --git a/tests/woocommerce-integration.php b/tests/woocommerce-integration.php index 4b3b197..db65171 100644 --- a/tests/woocommerce-integration.php +++ b/tests/woocommerce-integration.php @@ -318,6 +318,31 @@ function md_alt_woo_render( $id ) { $md_alt_response = wp_remote_get( $md_alt_md_url ); md_alt_woo_assert( 404 === wp_remote_retrieve_response_code( $md_alt_response ) && false === strpos( wp_remote_retrieve_body( $md_alt_response ), 'product_type:' ), 'Unpublished products return 404' ); } + // Cart, checkout and My account count as noindexed, so the opt-in leaves them out. + $md_alt_shop_pages = array_filter( + array_map( 'wc_get_page_id', array( 'cart', 'checkout', 'myaccount' ) ), + static function ( $id ) { + return $id > 0 && get_post( $id ); + } + ); + $md_alt_plain_page = wp_insert_post( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'Markdown test page', + ) + ); + $md_alt_woo_ids[] = $md_alt_plain_page; + foreach ( $md_alt_shop_pages as $md_alt_shop_page ) { + md_alt_woo_assert( PostTypeSupport::is_noindexed( get_post( $md_alt_shop_page ) ) && ! PostTypeSupport::should_skip_noindexed( get_post( $md_alt_shop_page ) ), 'Shop page ' . $md_alt_shop_page . ' is noindexed but kept without the opt-in' ); + } + add_filter( 'markdown_alternate_skip_noindexed', '__return_true' ); + foreach ( $md_alt_shop_pages as $md_alt_shop_page ) { + md_alt_woo_assert( PostTypeSupport::should_skip_noindexed( get_post( $md_alt_shop_page ) ), 'Shop page ' . $md_alt_shop_page . ' is skipped with the opt-in' ); + } + md_alt_woo_assert( ! PostTypeSupport::should_skip_noindexed( get_post( $md_alt_plain_page ) ), 'An ordinary page is not skipped with the opt-in' ); + remove_filter( 'markdown_alternate_skip_noindexed', '__return_true' ); + // Term archives: product categories, nested, with catalog visibility and the list limit. $md_alt_parent_term = wp_insert_term( 'Markdown archive parent ' . wp_generate_uuid4(), 'product_cat' ); $md_alt_child_term = wp_insert_term( 'Markdown archive child ' . wp_generate_uuid4(), 'product_cat', array( 'parent' => $md_alt_parent_term['term_id'] ) );