Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Featured content blocks contains logic that should be located in a filter #679

Open
dingo-d opened this issue Nov 7, 2022 · 0 comments
Open
Labels
bug Something isn't working
Milestone

Comments

@dingo-d
Copy link
Collaborator

dingo-d commented Nov 7, 2022

global $post;
$args = [
'post_type' => $featuredContentPostType,
'posts_per_page' => $featuredContentLayoutTotalItems,
'fields' => 'ids',
'order' => 'ASC',
];
if ($featuredContentTaxonomy) {
$args['tax_query'][0] = [
'taxonomy' => $featuredContentTaxonomy,
'field' => 'id',
];
if ($featuredContentTerms) {
$args['tax_query'][0]['terms'] = array_map(
function ($item) {
return $item['value'];
},
(array)$featuredContentTerms
);
} elseif ($featuredContentUseCurrentTerm && $post instanceof WP_Post && !$featuredContentServerSideRender) {
$currentTerms = get_the_terms($post->ID, strval($featuredContentTaxonomy));
if ($currentTerms) {
$args['tax_query'][0]['terms'] = [$currentTerms[0]->term_id]; // @phpstan-ignore-line
}
} else {
$args['tax_query'][0]['operator'] = 'NOT IN'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
}
}
$excludeList = [];
if ($featuredContentExcludeCurrentPost && $post instanceof WP_Post) {
$excludeList[] = $post->ID;
}
if ($excludeList) {
$args['post__not_in'] = $excludeList;
}
if ($featuredContentPosts) {
$args['post__in'] = array_map(
function ($item) {
return $item['value'];
},
(array)$featuredContentPosts
);
$args['orderby'] = 'post__in';
}
if ($featuredContentRandomOrder) {
$args['orderby'] = 'rand';
}
$mainQuery = new WP_Query($args);
if (!$mainQuery->have_posts()) {
return;
}
wp_reset_postdata();

I understand that this is done in a way it is because you have a standalone block that works, but this should be refactored to use a filter that will return, cache, .etc, the data...

Currently, it mixes presentational and business logic.

@dingo-d dingo-d added the bug Something isn't working label Nov 7, 2022
@dingo-d dingo-d added this to the Minor milestone Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant