Make fictioneer_get_card_list pluggable

This commit is contained in:
Tetrakern 2023-08-05 19:13:48 +02:00
parent 9574272fe8
commit 759465d5b3

View File

@ -4,84 +4,86 @@
// GET CARD LIST
// =============================================================================
/**
* Returns the query and HTML list items for a post type.
*
* @since Fictioneer 5.0
*
* @param string $type Either story, chapter, collection, recommendation, or post.
* @param array $query_args Optional. Query arguments merged with the defaults.
* @param string $empty Optional. What to show as empty result. Defaults to 'No results'.
* @param array $card_args Optional. Card partial arguments merged with the defaults.
*
* @return array|boolean The query results ('query') and the cards as list items ('html').
* False for impermissible parameters.
*/
if ( ! function_exists( 'fictioneer_get_card_list' ) ) {
/**
* Returns the query and HTML list items for a post type.
*
* @since Fictioneer 5.0
*
* @param string $type Either story, chapter, collection, recommendation, or post.
* @param array $query_args Optional. Query arguments merged with the defaults.
* @param string $empty Optional. What to show as empty result. Defaults to 'No results'.
* @param array $card_args Optional. Card partial arguments merged with the defaults.
*
* @return array|boolean The query results ('query') and the cards as list items ('html').
* False for impermissible parameters.
*/
function fictioneer_get_card_list( $type, $query_args = [], $empty = '', $card_args = [] ) {
// Setup
$html = '';
$empty = $empty === '' ? __( 'No results.', 'fictioneer' ) : $empty;
$query = false;
$allowed_types = ['fcn_story', 'fcn_chapter', 'fcn_collection', 'fcn_recommendation', 'post'];
$post_type = in_array( $type, ['story', 'chapter', 'collection', 'recommendation'] ) ? "fcn_$type" : $type;
$page = isset( $query_args['paged'] ) ? $query_args['paged'] : 1;
$is_empty = false;
function fictioneer_get_card_list( $type, $query_args = [], $empty = '', $card_args = [] ) {
// Setup
$html = '';
$empty = $empty === '' ? __( 'No results.', 'fictioneer' ) : $empty;
$query = false;
$allowed_types = ['fcn_story', 'fcn_chapter', 'fcn_collection', 'fcn_recommendation', 'post'];
$post_type = in_array( $type, ['story', 'chapter', 'collection', 'recommendation'] ) ? "fcn_$type" : $type;
$page = isset( $query_args['paged'] ) ? $query_args['paged'] : 1;
$is_empty = false;
// Validations
if ( ! in_array( $post_type, $allowed_types ) ) return false;
// Validations
if ( ! in_array( $post_type, $allowed_types ) ) return false;
// Default query arguments
$the_query_args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'orderby' => 'modified',
'order' => 'DESC',
'posts_per_page' => get_option( 'posts_per_page' )
);
// Default query arguments
$the_query_args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'orderby' => 'modified',
'order' => 'DESC',
'posts_per_page' => get_option( 'posts_per_page' )
);
// Default card arguments
$the_card_args = array(
'cache' => fictioneer_caching_active() && ! fictioneer_private_caching_active()
);
// Default card arguments
$the_card_args = array(
'cache' => fictioneer_caching_active() && ! fictioneer_private_caching_active()
);
// Merge with optional arguments
$the_query_args = array_merge( $the_query_args, $query_args );
$the_card_args = array_merge( $the_card_args, $card_args );
// Merge with optional arguments
$the_query_args = array_merge( $the_query_args, $query_args );
$the_card_args = array_merge( $the_card_args, $card_args );
// Query (but not if 'post__in' is set and empty)
if ( ! ( isset( $query_args['post__in'] ) && empty( $the_query_args['post__in'] ) ) ) {
$query = new WP_Query( $the_query_args );
// Query (but not if 'post__in' is set and empty)
if ( ! ( isset( $query_args['post__in'] ) && empty( $the_query_args['post__in'] ) ) ) {
$query = new WP_Query( $the_query_args );
// Prime author cache
if ( ! empty( $query->posts ) && function_exists( 'update_post_author_caches' ) ) {
update_post_author_caches( $query->posts );
}
}
// Buffer HTML output
ob_start();
// Loop results
if ( $query && $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
get_template_part( 'partials/_card-' . str_replace( 'fcn_', '', $post_type ), null, $the_card_args );
// Prime author cache
if ( ! empty( $query->posts ) && function_exists( 'update_post_author_caches' ) ) {
update_post_author_caches( $query->posts );
}
}
wp_reset_postdata();
} elseif ( $empty ) {
$is_empty = true;
// Start HTML ---> ?>
<li class="no-results"><?php echo $page > 1 ? __( 'No more results.', 'fictioneer' ) : $empty; ?></li>
<?php // <--- End HTML
// Buffer HTML output
ob_start();
// Loop results
if ( $query && $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
get_template_part( 'partials/_card-' . str_replace( 'fcn_', '', $post_type ), null, $the_card_args );
}
wp_reset_postdata();
} elseif ( $empty ) {
$is_empty = true;
// Start HTML ---> ?>
<li class="no-results"><?php echo $page > 1 ? __( 'No more results.', 'fictioneer' ) : $empty; ?></li>
<?php // <--- End HTML
}
// Get buffered HTML
$html = ob_get_clean();
// Return results
return ['query' => $query, 'html' => $html, 'page' => $page, 'empty' => $is_empty];
}
// Get buffered HTML
$html = ob_get_clean();
// Return results
return ['query' => $query, 'html' => $html, 'page' => $page, 'empty' => $is_empty];
}
// =============================================================================