fictioneer/partials/_latest-posts.php

149 lines
5.2 KiB
PHP
Raw Permalink Normal View History

2023-01-21 01:31:34 +01:00
<?php
/**
* Partial: Latest Post
*
* This template part renders the HTML for the [fictioneer_latest_post] shortcode.
*
* @package WordPress
* @subpackage Fictioneer
* @since 4.0.0
2023-01-21 01:31:34 +01:00
*
2024-10-06 23:17:22 +02:00
* @internal $args['uid'] Unique ID of the shortcode. Pattern: shortcode-id-{id}.
* @internal $args['type'] Type argument passed from shortcode. Default 'default'.
* @internal $args['author'] The author provided by the shortcode. Default false.
* @internal $args['count'] The number of posts provided by the shortcode. Default 1.
* @internal $args['post_ids'] Array of post IDs. Default empty.
2025-01-12 17:44:38 +01:00
* @internal $args['post_status'] Queried post status. Default 'publish'.
2023-09-16 23:41:30 +02:00
* @internal $args['author_ids'] Array of author IDs. Default empty.
* @internal $args['excluded_authors'] Array of author IDs to exclude. Default empty.
* @internal $args['excluded_cats'] Array of category IDs to exclude. Default empty.
* @internal $args['excluded_tags'] Array of tag IDs to exclude. Default empty.
* @internal $args['ignore_protected'] Whether to ignore protected posts. Default false.
2024-09-20 23:41:37 +02:00
* @internal $args['only_protected'] Whether to query only protected posts. Default false.
* @internal $args['taxonomies'] Array of taxonomy arrays. Default empty.
* @internal $args['relation'] Relationship between taxonomies.
* @internal $args['classes'] Array of additional CSS classes. Default empty.
2023-01-21 01:31:34 +01:00
*/
// No direct access!
defined( 'ABSPATH' ) OR exit;
2023-01-21 01:31:34 +01:00
// Prepare query
2023-03-08 01:07:50 +01:00
$query_args = array(
2023-09-15 14:00:44 +02:00
'fictioneer_query_name' => 'latest_posts',
2023-01-21 01:31:34 +01:00
'post_type' => 'post',
'post_status' => $args['post_status'],
'post__in' => $args['post_ids'], // May be empty!
2023-01-21 01:31:34 +01:00
'order' => 'DESC',
2023-09-16 03:29:38 +02:00
'orderby' => 'date',
2023-01-21 01:31:34 +01:00
'posts_per_page' => $args['count'],
'ignore_sticky_posts' => 1,
'no_found_rows' => true, // Improve performance
'update_post_term_cache' => false // Improve performance
2023-01-21 01:31:34 +01:00
);
2023-09-16 03:29:38 +02:00
// Author?
2023-09-17 01:28:14 +02:00
if ( ! empty( $args['author'] ) ) {
2023-09-16 03:29:38 +02:00
$query_args['author_name'] = $args['author'];
}
2023-03-08 11:36:26 +01:00
// Author IDs?
if ( ! empty( $args['author_ids'] ) ) {
$query_args['author__in'] = $args['author_ids'];
}
2023-03-08 01:07:50 +01:00
// Taxonomies?
2023-03-08 11:36:26 +01:00
if ( ! empty( $args['taxonomies'] ) ) {
2023-03-08 23:24:41 +01:00
$query_args['tax_query'] = fictioneer_get_shortcode_tax_query( $args );
2023-03-08 01:07:50 +01:00
}
2023-03-10 17:23:29 +01:00
// Excluded tags?
if ( ! empty( $args['excluded_tags'] ) ) {
$query_args['tag__not_in'] = $args['excluded_tags'];
}
// Excluded categories?
if ( ! empty( $args['excluded_cats'] ) ) {
$query_args['category__not_in'] = $args['excluded_cats'];
}
// Excluded authors?
if ( ! empty( $args['excluded_authors'] ) ) {
$query_args['author__not_in'] = $args['excluded_authors'];
}
// Ignore protected?
if ( $args['ignore_protected'] ) {
$query_args['has_password'] = false;
}
2024-09-20 23:41:37 +02:00
// Only protected?
if ( $args['only_protected'] ) {
$query_args['has_password'] = true;
}
2023-03-08 01:07:50 +01:00
// Apply filters
2023-03-10 15:20:48 +01:00
$query_args = apply_filters( 'fictioneer_filter_shortcode_latest_posts_query_args', $query_args, $args );
2023-03-08 01:07:50 +01:00
2023-01-21 01:31:34 +01:00
// Query post
$latest_entries = fictioneer_shortcode_query( $query_args );
2023-01-21 01:31:34 +01:00
// Classes
$args['classes'] .= ' _' . $args['post_status'];
2023-01-21 01:31:34 +01:00
?>
2023-09-15 22:42:34 +02:00
<section class="latest-posts <?php echo $args['classes']; ?>">
2023-01-21 01:31:34 +01:00
<?php if ( $latest_entries->have_posts() ) : ?>
<?php while ( $latest_entries->have_posts() ) : $latest_entries->the_post(); ?>
<?php
// Setup
$title = fictioneer_get_safe_title( $post->ID, 'shortcode-latest-posts' );
$label = esc_attr( sprintf( _x( 'Continue reading %s', 'Read more link aria label', 'fictioneer' ), $title ) );
2023-01-21 01:31:34 +01:00
if (
! get_option( 'fictioneer_show_full_post_content' ) &&
! strpos( $post->post_content, '<!--more-->' )
) {
2024-02-28 16:19:26 +01:00
$content = '<p>' . fictioneer_get_excerpt() . '</p><div class="more-link-wrapper"><a class="more-link" href="' . get_permalink() . '" title="' . $label . '" aria-label="' . $label . '">' . fcntr( 'read_more' ) . '</a></div>';
} else {
$content = apply_filters( 'the_content', get_the_content( fcntr( 'read_more' ) ) );
2023-01-21 01:31:34 +01:00
}
?>
<article id="post-<?php the_ID(); ?>" class="post">
<header class="post__header">
2024-08-01 19:26:09 +02:00
<h2 class="post__title h1"><a href="<?php the_permalink(); ?>"><?php echo $title; ?></a></h2>
2023-12-02 00:00:13 +01:00
<div class="post__meta layout-links"><?php echo fictioneer_get_post_meta_items( array( 'no_cat' => true ) ); ?></div>
2023-01-21 01:31:34 +01:00
</header>
<section class="post__main content-section">
<div class="post__excerpt"><?php echo $content; ?></div>
</section>
<?php if ( has_action( 'fictioneer_latest_posts_footers_left' ) || has_action( 'fictioneer_latest_posts_footers_right' ) ) : ?>
<footer class="post__footer">
2023-08-30 13:51:49 +02:00
<div class="post__footer-box post__footer-left">
2023-12-02 00:00:13 +01:00
<?php do_action( 'fictioneer_latest_posts_footers_left', $post->ID ); ?>
2023-01-21 01:31:34 +01:00
</div>
2023-08-30 13:51:49 +02:00
<div class="post__footer-box post__footer-right">
2023-12-02 00:00:13 +01:00
<?php do_action( 'fictioneer_latest_posts_footers_right', $post->ID ); ?>
2023-01-21 01:31:34 +01:00
</div>
</footer>
<?php endif; ?>
</article>
<?php endwhile; ?>
2023-11-30 17:52:19 +01:00
<?php else : ?>
2023-01-21 01:31:34 +01:00
2023-09-16 14:25:26 +02:00
<div class="no-results"><?php _e( 'Nothing to show.', 'fictioneer' ); ?></div>
2023-01-21 01:31:34 +01:00
<?php endif; wp_reset_postdata(); ?>
</section>