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
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,43 @@ add_filter( 'markdown_alternate_supported_taxonomies', function( $taxonomies ) {
Term archives are not stored in the Markdown transient cache: they change
whenever a post is published in them.

### Exclude categories or tags from frontmatter

Use `markdown_alternate_frontmatter_terms` to remove individual assigned terms
from post, page, or product metadata. It receives an array of `WP_Term` objects,
the taxonomy name, and the post. Return an empty array to omit the field entirely.
For example, exclude category IDs 12 and 34:

```php
add_filter( 'markdown_alternate_frontmatter_terms', function( $terms, $taxonomy, $post ) {
if ( 'category' !== $taxonomy ) {
return $terms;
}
return array_filter( $terms, function( $term ) {
return ! in_array( $term->term_id, array( 12, 34 ), true );
} );
}, 10, 3 );
```

To omit terms that Yoast SEO marks noindex, use its effective robots value through
the plugin's helper instead:

```php
add_filter( 'markdown_alternate_frontmatter_terms', function( $terms ) {
return array_filter( $terms, function( $term ) {
return ! \MarkdownAlternate\TaxonomySupport::is_noindexed( $term );
} );
} );
```

Without Yoast active, that example retains all terms. These filters affect only
post frontmatter; use `markdown_alternate_skip_noindexed_terms` separately to
withhold noindexed term archives. Returning a non-array keeps the original list;
non-term entries in a returned array are ignored. The filter runs only when the
document is regenerated. Delete affected `md_alt_cache_{post_id}` transients or
change `markdown_alternate_cache_version` after changing exclusion rules or term
indexing settings, as described below.

### Extend frontmatter

The rendering filters receive the current `WP_Post` as their second argument.
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Yes, as an opt-in. With `add_filter( 'markdown_alternate_skip_noindexed', '__ret
== Changelog ==

= Unreleased =
* Added a filter to exclude individual categories, tags, or product terms from post frontmatter.
* Added a translation template and loading support for contributed translations.
* Label tax-inclusive displayed product prices and give long product descriptions their own heading.
* Kept author and date archives in HTML instead of redirecting Markdown requests to missing pages
Expand Down
25 changes: 25 additions & 0 deletions src/Output/ContentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,31 @@ private function generate_frontmatter( WP_Post $post ): string {
foreach ( $taxonomies as $taxonomy => $key ) {
$terms = get_the_terms( $post->ID, $taxonomy );
if ( $terms && ! is_wp_error( $terms ) ) {
/**
* Filters the terms included in a post's taxonomy frontmatter.
*
* Return an empty array to omit this taxonomy's field. This does
* not disable term archives or exclude posts from those archives.
* Runs only when the Markdown document is regenerated.
*
* @since 1.3.0
*
* @param mixed $terms Array of WP_Term objects; non-arrays keep the assigned terms.
* @param string $taxonomy Taxonomy name.
* @param WP_Post $post The post being rendered.
*/
$filtered_terms = apply_filters( 'markdown_alternate_frontmatter_terms', $terms, $taxonomy, $post );
if ( is_array( $filtered_terms ) ) {
$terms = array_filter(
$filtered_terms,
static function ( $term ) {
return $term instanceof \WP_Term;
}
);
}
if ( empty( $terms ) ) {
continue;
}
$content_lines[ $key ]['type'] = 'array';
$content_lines[ $key ]['items'] = array();
foreach ( $terms as $term ) {
Expand Down
110 changes: 110 additions & 0 deletions tests/frontmatter-terms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Term exclusion regressions, loaded by tests/run.php.
*
* @package MarkdownAlternate
*/

use MarkdownAlternate\Output\ContentRenderer;

$md_alt_tests['Term exclusions work for categories, tags and product taxonomies'] = static function () {
foreach ( array( 'category', 'post_tag', 'product_cat', 'product_tag', 'product_brand' ) as $taxonomy ) {
$post = md_alt_test_fixture( 'ordinary' );
if ( 0 === strpos( $taxonomy, 'product_' ) ) {
$post->post_type = 'product';
}
$kept = new WP_Term( 'Kept &amp; quoted "term"', 'https://example.com/kept/' );
$excluded = new WP_Term( 'Excluded term', 'https://example.com/excluded/' );
$GLOBALS['md_alt_test_state']['terms'][ $taxonomy ] = array( $excluded, $kept );
add_filter(
'markdown_alternate_frontmatter_taxonomies',
static function () use ( $taxonomy ) {
return array( $taxonomy => 'terms' );
}
);
add_filter(
'markdown_alternate_frontmatter_terms',
static function ( $terms, $filtered_taxonomy, $filtered_post ) use ( $taxonomy, $post, $kept ) {
md_alt_assert_same( $taxonomy, $filtered_taxonomy );
md_alt_assert_same( $post, $filtered_post );
return array_filter(
$terms,
static function ( $term ) use ( $kept ) {
return $kept === $term;
}
);
},
10,
3
);
$output = ( new ContentRenderer() )->render( $post );
md_alt_assert_contains( 'terms:', $output );
md_alt_assert_contains( 'https://example.com/kept', $output );
md_alt_assert_contains( 'Kept & quoted \"term\"', $output );
md_alt_assert_same( false, strpos( $output, 'Excluded term' ), $taxonomy );
md_alt_assert_same( false, strpos( $output, 'https://example.com/excluded' ), $taxonomy );
}
};

$md_alt_tests['Excluding every category omits its field and leaves tags intact'] = static function () {
$post = md_alt_test_fixture( 'escaped' );
add_filter(
'markdown_alternate_frontmatter_terms',
static function ( $terms, $taxonomy ) {
return 'category' === $taxonomy ? array() : $terms;
},
10,
2
);
$output = ( new ContentRenderer() )->render( $post );
md_alt_assert_same( false, strpos( $output, 'categories:' ) );
md_alt_assert_contains( 'tags:', $output );
md_alt_assert_contains( 'tag & name', $output );
};

$md_alt_tests['Invalid term filter returns retain defaults and malformed entries are ignored'] = static function () {
foreach ( array( null, false, new WP_Error(), 'invalid' ) as $invalid ) {
$post = md_alt_test_fixture( 'escaped' );
$renderer = new ContentRenderer();
$expected = $renderer->render( $post );
delete_transient( 'md_alt_cache_' . $post->ID );
add_filter(
'markdown_alternate_frontmatter_terms',
static function () use ( $invalid ) {
return $invalid;
}
);
md_alt_assert_same( $expected, $renderer->render( $post ) );
}
$post = md_alt_test_fixture( 'escaped' );
add_filter(
'markdown_alternate_frontmatter_terms',
static function ( $terms ) {
return array_merge( array( null, new WP_Error(), 'invalid' ), $terms );
}
);
md_alt_assert_same( $expected, ( new ContentRenderer() )->render( $post ) );
};

$md_alt_tests['Changing the cache version applies updated term exclusion rules'] = static function () {
$post = md_alt_test_fixture( 'escaped' );
$renderer = new ContentRenderer();
$original = $renderer->render( $post );
add_filter(
'markdown_alternate_frontmatter_terms',
static function () {
return array();
}
);
md_alt_assert_same( $original, $renderer->render( $post ) );
add_filter(
'markdown_alternate_cache_version',
static function ( $version ) {
return $version . '|term-exclusions-v1';
}
);
$output = $renderer->render( $post );
md_alt_assert_same( false, strpos( $output, 'categories:' ) );
md_alt_assert_same( false, strpos( $output, 'tags:' ) );
md_alt_assert_same( $output, $renderer->render( $post ) );
};
1 change: 1 addition & 0 deletions tests/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ static function () {

require __DIR__ . '/security.php';
require __DIR__ . '/conversion.php';
require __DIR__ . '/frontmatter-terms.php';

$md_alt_failures = 0;
foreach ( $md_alt_tests as $md_alt_name => $md_alt_test ) {
Expand Down
18 changes: 18 additions & 0 deletions tests/term-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ function md_alt_term_assert( $condition, $message ) {
$md_alt_term_home = static function ( $home ) {
return rtrim( $home, '/' ) . '/blog';
};
$md_alt_term_exclusions = static function ( $terms ) {
return array_filter(
$terms,
static function ( $term ) {
return ! TaxonomySupport::is_noindexed( $term );
}
);
};
$md_alt_term_types = static function () {
return array( 'page' );
};
Expand Down Expand Up @@ -165,6 +173,15 @@ function md_alt_term_assert( $condition, $message ) {
$md_alt_post_output = ( new ContentRenderer() )->render( get_post( $md_alt_term_post_id ) );
md_alt_term_assert( false === strpos( $md_alt_post_output, '.md"' ) && false !== strpos( $md_alt_post_output, $md_alt_term_child->slug . '/"' ), 'Post frontmatter links withheld terms to HTML' );
remove_filter( 'markdown_alternate_skip_noindexed_terms', '__return_true' );
add_filter( 'markdown_alternate_frontmatter_terms', $md_alt_term_exclusions );
delete_transient( 'md_alt_cache_' . $md_alt_term_post_id );
$md_alt_post_output = ( new ContentRenderer() )->render( get_post( $md_alt_term_post_id ) );
md_alt_term_assert( false === strpos( $md_alt_post_output, 'categories:' ), 'Noindexed terms can be omitted from frontmatter independently of archive routing' );
$GLOBALS['md_alt_noindexed_terms'] = array();
delete_transient( 'md_alt_cache_' . $md_alt_term_post_id );
$md_alt_post_output = ( new ContentRenderer() )->render( get_post( $md_alt_term_post_id ) );
md_alt_term_assert( false !== strpos( $md_alt_post_output, $md_alt_child_md ), 'Indexed terms remain in frontmatter' );
remove_filter( 'markdown_alternate_frontmatter_terms', $md_alt_term_exclusions );

md_alt_term_assert( false !== strpos( $md_alt_term_renderer->render( $md_alt_term_child ), '[Archive regression post](' ), 'Supported posts appear in archives' );
add_filter( 'markdown_alternate_supported_post_types', $md_alt_term_types, 100 );
Expand All @@ -173,6 +190,7 @@ function md_alt_term_assert( $condition, $message ) {
remove_filter( 'markdown_alternate_supported_post_types', $md_alt_term_types, 100 );
WP_CLI::success( 'Term archive regression tests passed.' );
} finally {
remove_filter( 'markdown_alternate_frontmatter_terms', $md_alt_term_exclusions );
remove_filter( 'option_home', $md_alt_term_home );
remove_filter( 'markdown_alternate_skip_noindexed_terms', '__return_true' );
remove_filter( 'markdown_alternate_supported_post_types', $md_alt_term_types, 100 );
Expand Down
Loading