fictioneer/stories.php

182 lines
4.8 KiB
PHP
Raw Normal View History

2023-01-21 01:31:34 +01:00
<?php
/**
* Template Name: Stories
*
* Shows a list of all published stories with a statistics block and hooks
* for customization. It's paginated using the "Blog pages show at most"
* (posts_per_page) option under Settings > Reading.
*
* @package WordPress
* @subpackage Fictioneer
* @since 3.0
2023-09-17 18:21:46 +02:00
* @see fictioneer_clause_sticky_stories()
2023-01-21 01:31:34 +01:00
*/
?>
<?php
// Setup
$post_id = get_the_ID();
$page = get_query_var( 'paged', 1 ) ?: 1; // Main query
2023-12-15 17:51:09 +01:00
$order = array_intersect( [ strtolower( $_GET['order'] ?? 0 ) ], ['desc', 'asc'] );
$order = reset( $order ) ?: 'desc'; // Sanitized
$orderby = array_intersect( [ strtolower( $_GET['orderby'] ?? 0 ) ], fictioneer_allowed_orderby() );
$orderby = reset( $orderby ) ?: 'modified'; // Sanitized
2023-06-08 22:44:43 +02:00
$ago = $_GET['ago'] ?? 0;
$ago = is_numeric( $ago ) ? absint( $ago ) : sanitize_text_field( $ago );
$meta_query_stack = [];
2023-01-21 01:31:34 +01:00
// Prepare query
$query_args = array (
'fictioneer_query_name' => 'stories_list',
'post_type' => 'fcn_story',
'post_status' => 'publish',
'order' => $order,
2023-09-16 13:10:52 +02:00
'orderby' => $orderby,
'paged' => $page,
'posts_per_page' => get_option( 'posts_per_page', 8 ),
'update_post_term_cache' => ! get_option( 'fictioneer_hide_taxonomies_on_story_cards' )
);
// Prepare base meta query part
if ( get_option( 'fictioneer_disable_extended_story_list_meta_queries' ) ) {
$meta_query_stack[] = array(
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
)
);
} else {
$meta_query_stack[] = array(
'relation' => 'OR',
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
),
array(
'key' => 'fictioneer_story_hidden',
'compare' => 'NOT EXISTS'
)
);
}
2023-09-16 13:10:52 +02:00
// Build meta query
$query_args['meta_query'] = [];
if ( count( $meta_query_stack ) > 1 ) {
$query_args['meta_query']['relation'] = 'AND';
}
foreach ( $meta_query_stack as $part ) {
$query_args['meta_query'][] = $part;
}
2024-04-11 11:17:30 +02:00
// Order by words?
if ( $orderby === 'words' ) {
$query_args['orderby'] = 'meta_value_num modified';
$query_args['meta_key'] = 'fictioneer_story_total_word_count';
}
// Order by latest chapter update timestamp?
if ( FICTIONEER_ORDER_STORIES_BY_LATEST_CHAPTER && $orderby === 'modified' ) {
$query_args['orderby'] = 'meta_value modified';
$query_args['meta_key'] = 'fictioneer_chapters_added';
}
// Append date query (if any)
$query_args = fictioneer_append_date_query( $query_args, $ago, $orderby );
2023-06-08 22:44:43 +02:00
// Filter query arguments
$query_args = apply_filters( 'fictioneer_filter_stories_query_args', $query_args, $post_id );
// Query stories
$list_of_stories = new WP_Query( $query_args );
2023-01-21 01:31:34 +01:00
// Prime author cache
if ( function_exists( 'update_post_author_caches' ) ) {
update_post_author_caches( $list_of_stories->posts );
}
2023-01-21 01:31:34 +01:00
?>
<?php get_header(); ?>
<main id="main" class="main singular stories">
2023-07-30 17:00:40 +02:00
<div class="observer main-observer"></div>
2023-01-21 01:31:34 +01:00
<?php do_action( 'fictioneer_main' ); ?>
2023-01-21 01:31:34 +01:00
<div class="main__background polygon polygon--main background-texture"></div>
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 = trim( get_the_title() );
$breadcrumb_name = empty( $title ) ? __( 'Stories', 'fictioneer' ) : $title;
2023-12-02 00:00:13 +01:00
$this_breadcrumb = [ $breadcrumb_name, get_the_permalink() ];
2023-01-21 01:31:34 +01:00
// Arguments for hooks and templates/etc.
$hook_args = array(
'current_page' => $page,
2023-12-02 00:00:13 +01:00
'post_id' => $post->ID,
2023-01-21 01:31:34 +01:00
'stories' => $list_of_stories,
'queried_type' => 'fcn_story',
'query_args' => $query_args,
'order' => $order,
2023-06-08 22:44:43 +02:00
'orderby' => $orderby,
'ago' => $ago
2023-01-21 01:31:34 +01:00
);
?>
<article id="singular-<?php echo $post_id; ?>" class="singular__article stories__article padding-top padding-left padding-right padding-bottom">
2023-01-21 01:31:34 +01:00
<?php if ( ! empty( $title ) ) : ?>
<header class="singular__header stories__header">
<h1 class="singular__title stories__title"><?php echo $title; ?></h1>
</header>
<?php endif; ?>
<?php if ( get_the_content() ) : ?>
<section class="singular__content stories__content content-section">
<?php the_content(); ?>
</section>
<?php endif; ?>
<?php
// Renders statistics and list of stories
do_action( 'fictioneer_stories_after_content', $hook_args );
?>
</article>
<?php endwhile; ?>
2023-01-21 01:31:34 +01:00
</div>
2023-01-21 01:31:34 +01:00
</main>
<?php
// Footer arguments
$footer_args = array(
'post_type' => 'page',
'post_id' => $post_id,
2023-01-21 01:31:34 +01:00
'current_page' => $page,
'stories' => $list_of_stories,
'breadcrumbs' => array(
[fcntr( 'frontpage' ), get_home_url()]
)
);
// Add current breadcrumb
$footer_args['breadcrumbs'][] = $this_breadcrumb;
// Get footer with breadcrumbs
get_footer( null, $footer_args );
?>