Skip to content

Commit

Permalink
Merge pull request #265 from Automattic/master
Browse files Browse the repository at this point in the history
Alpha release Sep 06
  • Loading branch information
dkoo authored Sep 6, 2023
2 parents a0a161a + 0f37ec4 commit d6287b4
Show file tree
Hide file tree
Showing 4 changed files with 595 additions and 227 deletions.
63 changes: 35 additions & 28 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions includes/theme-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,65 @@ function get_sponsor_posts_for_terms( $terms ) {
return $sponsor_posts->posts;
}

/**
* Get all sponsored terms.
*
* @return array|boolean An associative array keyed by taxonomy name with an array of sponsored term IDs for each, or false if no sponsored terms.
*/
function get_all_sponsored_terms() {
$taxonomies = \get_object_taxonomies( Core::NEWSPACK_SPONSORS_CPT );
if ( empty( $taxonomies ) ) {
return false;
}

$tax_query_args = array_map(
function( $taxonomy ) {
return [
'taxonomy' => $taxonomy,
'operator' => 'EXISTS',
];
},
$taxonomies
);

$tax_query_args['relation'] = 'OR';
$sponsors_with_terms = new \WP_Query(
[
'is_sponsors' => 1,
'fields' => 'ids',
'post_type' => Core::NEWSPACK_SPONSORS_CPT,
'posts_per_page' => 100,
'post_status' => 'publish',
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'tax_query' => $tax_query_args,
]
);

if ( empty( $sponsors_with_terms->posts ) ) {
return false;
}

$sponsored_terms = \get_terms(
[
'taxonomy' => $taxonomies,
'object_ids' => $sponsors_with_terms->posts,
]
);

return array_reduce(
$sponsored_terms,
function( $acc, $term ) {
if ( ! isset( $acc[ $term->taxonomy ] ) ) {
$acc[ $term->taxonomy ] = [];
}

$acc[ $term->taxonomy ][] = $term->term_id;
return $acc;
},
[]
);
}

/**
* Formats a post object into a sponsor object, for ease of theme developer use.
*
Expand Down
Loading

0 comments on commit d6287b4

Please sign in to comment.