Skip to content

Commit

Permalink
Merge pull request #32 from Emilia-Capital/jdv/rilwis-changes
Browse files Browse the repository at this point in the history
Add support for Slim SEO - 2
  • Loading branch information
jdevalk authored Jul 2, 2024
2 parents 428f5b0 + 37a881b commit 3d1d71c
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function register_hooks() {
\add_filter( 'get_the_terms', [ $this, 'filter_get_the_terms' ], 10, 3 );
\add_filter( 'wpseo_exclude_from_sitemap_by_term_ids', [ $this, 'exclude_tags_from_yoast_sitemap' ] );
\add_filter( 'wp_sitemaps_taxonomies_query_args', [ $this, 'exclude_tags_from_core_sitemap' ], 10, 2 );
\add_filter( 'slim_seo_taxonomy_query_args', [ $this, 'exclude_tags_from_slim_seo_sitemap' ] );
}

/**
Expand Down Expand Up @@ -110,6 +111,23 @@ public function exclude_tags_from_core_sitemap( $args, $taxonomy ) {
return $args;
}

/**
* Excludes tags with fewer than the minimum number of posts from the Slim SEO sitemap.
*
* @param array $args The arguments for the sitemap query.
*
* @return array The modified arguments for the sitemap query.
*/
public function exclude_tags_from_slim_seo_sitemap( $args ) {
if ( ! isset( $args['exclude'] ) ) {
$args['exclude'] = [];
}

// exclude terms with too few posts.
$args['exclude'] = \array_merge( $args['exclude'], $this->get_tag_ids_with_fewer_than_min_posts() );
return $args;
}

/**
* Fetches the IDs of all tags with fewer than the given minimum post count.
*
Expand All @@ -123,13 +141,13 @@ public function get_tag_ids_with_fewer_than_min_posts() {
];

// Fetch all tag IDs.
$tag_ids = get_terms( $args );
$tag_ids = \get_terms( $args );

// Filter tag IDs based on post count.
$filtered_tag_ids = array_filter(
$filtered_tag_ids = \array_filter(
$tag_ids,
function ( $tag_id ) {
$tag = get_term( $tag_id, 'post_tag' );
$tag = \get_term( $tag_id, 'post_tag' );
return ( $tag->count < \FewerTags\Plugin::$min_posts_count );
}
);
Expand Down Expand Up @@ -157,9 +175,9 @@ public function exclude_tags_from_yoast_sitemap( $excluded_term_ids ) {

$tags = get_terms( $args );

if ( ! is_wp_error( $tags ) ) {
if ( ! \is_wp_error( $tags ) ) {
foreach ( $tags as $tag_id ) {
$term = get_term( $tag_id );
$term = \get_term( $tag_id );
if ( $term->count < \FewerTags\Plugin::$min_posts_count ) {
$excluded_term_ids[] = $tag_id;
}
Expand Down

0 comments on commit 3d1d71c

Please sign in to comment.