diff --git a/README.md b/README.md index 716174a..142e71f 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/readme.txt b/readme.txt index d8e72f6..469398e 100644 --- a/readme.txt +++ b/readme.txt @@ -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 diff --git a/src/Output/ContentRenderer.php b/src/Output/ContentRenderer.php index 2f79b16..67f6f84 100644 --- a/src/Output/ContentRenderer.php +++ b/src/Output/ContentRenderer.php @@ -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 ) { diff --git a/tests/frontmatter-terms.php b/tests/frontmatter-terms.php new file mode 100644 index 0000000..1098bcd --- /dev/null +++ b/tests/frontmatter-terms.php @@ -0,0 +1,110 @@ +post_type = 'product'; + } + $kept = new WP_Term( 'Kept & 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 ) ); +}; diff --git a/tests/run.php b/tests/run.php index 8de5a0b..b8f5508 100644 --- a/tests/run.php +++ b/tests/run.php @@ -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 ) { diff --git a/tests/term-integration.php b/tests/term-integration.php index 56a03cf..ef1a113 100644 --- a/tests/term-integration.php +++ b/tests/term-integration.php @@ -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' ); }; @@ -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 ); @@ -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 );