fictioneer/single-fcn_collection.php

155 lines
5.1 KiB
PHP
Raw Normal View History

2023-01-21 01:31:34 +01:00
<?php
/**
* Custom Post Type: Collection
*
* Shows the content of a collection with a statistics block and several optional
* partials for customization. It's paginated using the "Blog pages show at most"
* (posts_per_page) option under Settings > Reading. Collections can hold stories,
* chapters, recommendations, posts, and even other collections.
*
* @package WordPress
* @subpackage Fictioneer
* @since 4.0.0
2023-01-21 01:31:34 +01:00
* @see partials/_collection-header.php
* @see partials/_collection-statistics.php
*/
2023-08-17 16:13:12 +02:00
// Header
get_header( null, array( 'type' => 'fcn_collection' ) );
?>
2023-01-21 01:31:34 +01:00
<main id="main" class="main collection">
<?php do_action( 'fictioneer_main', 'collection' ); ?>
2023-01-21 01:31:34 +01:00
<div class="main__wrapper">
2023-01-21 01:31:34 +01:00
<?php do_action( 'fictioneer_main_wrapper' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
// Setup
$title = fictioneer_get_safe_title( $post->ID, 'single-collection' );
2023-01-21 01:31:34 +01:00
$this_breadcrumb = [$title, get_the_permalink()];
2023-06-16 02:07:03 +02:00
$current_page = max( 1, get_query_var( 'pg', 1 ) ?: 1 ); // Paged not available
2024-10-28 02:19:27 +01:00
$featured_list = get_post_meta( $post->ID, 'fictioneer_collection_items', true );
$featured_list = is_array( $featured_list ) ? $featured_list : [];
2023-01-21 01:31:34 +01:00
2024-10-28 02:19:27 +01:00
// Query posts (if any)
if ( ! empty( $featured_list ) ) {
global $wpdb;
// Filter out hidden posts
$placeholders = implode( ',', array_fill( 0, count( $featured_list ), '%d' ) );
$sql =
"SELECT p.ID
FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm_story_hidden
ON (p.ID = pm_story_hidden.post_id AND pm_story_hidden.meta_key = 'fictioneer_story_hidden')
LEFT JOIN {$wpdb->postmeta} pm_chapter_hidden
ON (p.ID = pm_chapter_hidden.post_id AND pm_chapter_hidden.meta_key = 'fictioneer_chapter_hidden')
WHERE p.ID IN ($placeholders)
AND p.post_status = 'publish'
AND (pm_story_hidden.meta_value IS NULL OR pm_story_hidden.meta_value = '' OR pm_story_hidden.meta_value = '0')
AND (pm_chapter_hidden.meta_value IS NULL OR pm_chapter_hidden.meta_value = '' OR pm_chapter_hidden.meta_value = '0')";
$featured_list = $wpdb->get_col( $wpdb->prepare( $sql, ...$featured_list ) );
2023-01-21 01:31:34 +01:00
}
2024-10-28 02:19:27 +01:00
// Query posts
2023-01-21 01:31:34 +01:00
$query_args = array (
'fictioneer_query_name' => 'collection_featured',
2023-01-21 01:31:34 +01:00
'post_type' => 'any',
'post__in' => $featured_list ?: [0], // Must not be empty!
2023-01-21 01:31:34 +01:00
'ignore_sticky_posts' => 1,
'orderby' => 'modified',
'order' => 'DESC',
'paged' => $current_page,
2023-06-16 02:07:03 +02:00
'posts_per_page' => get_option( 'posts_per_page', 8 ),
2024-10-28 02:19:27 +01:00
'update_post_meta_cache' => true,
'update_post_term_cache' => true
2023-01-21 01:31:34 +01:00
);
$featured_query = new WP_Query( $query_args );
// Prime author cache
2023-08-06 21:33:37 +02:00
if ( ! empty( $featured_query->posts ) && function_exists( 'update_post_author_caches' ) ) {
update_post_author_caches( $featured_query->posts );
}
2023-01-21 01:31:34 +01:00
// Arguments for hooks and templates/etc.
$hook_args = array(
'collection' => $post,
'collection_id' => $post->ID,
'title' => $title,
'current_page' => $current_page,
'max_pages' => $featured_query->max_num_pages,
'featured_list' => $featured_list,
'featured_query' => $featured_query
);
?>
2024-08-12 02:42:45 +02:00
<article id="collection-<?php the_ID(); ?>" class="collection__article">
2023-01-21 01:31:34 +01:00
<?php
// Render article header
get_template_part( 'partials/_collection-header', null, $hook_args );
// Hook after header
do_action( 'fictioneer_collection_after_header', $hook_args );
?>
<?php if ( get_the_content() ) : ?>
<section class="collection__content content-section">
<?php the_content(); ?>
</section>
<?php endif; ?>
<?php do_action( 'fictioneer_collection_after_content', $hook_args ); ?>
<footer class="collection__footer"><?php do_action( 'fictioneer_collection_footer', $hook_args ); ?></footer>
</article>
<?php endwhile; ?>
2023-01-21 01:31:34 +01:00
</div>
2024-09-07 16:57:34 +02:00
<?php do_action( 'fictioneer_main_end', 'collection' ); ?>
2023-01-21 01:31:34 +01:00
</main>
<?php
// Footer arguments
$footer_args = array(
'post_type' => 'fcn_collection',
2023-12-02 00:00:13 +01:00
'post_id' => $post->ID,
2023-01-21 01:31:34 +01:00
'breadcrumbs' => array(
[fcntr( 'frontpage' ), get_home_url()]
)
);
// Add recommendation list breadcrumb (if set)
$collections_page_id = intval( get_option( 'fictioneer_collections_page', -1 ) ?: -1 );
2023-01-21 01:31:34 +01:00
if ( $collections_page_id > 0 ) {
$collections_page_title = trim( get_the_title( $collections_page_id ) );
2023-01-21 01:31:34 +01:00
$collections_page_title = empty( $collections_page_title ) ? __( 'Stories', 'fictioneer' ) : $collections_page_title;
$footer_args['breadcrumbs'][] = array(
$collections_page_title,
fictioneer_get_assigned_page_link( 'fictioneer_collections_page' )
2023-01-21 01:31:34 +01:00
);
}
// Add current breadcrumb
$footer_args['breadcrumbs'][] = $this_breadcrumb;
// Get footer with breadcrumbs
get_footer( null, $footer_args );
?>