Skip to content
Open
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions src/Integration/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
25 changes: 24 additions & 1 deletion src/PostTypeSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions tests/woocommerce-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) );
Expand Down
Loading