fictioneer/partials/_latest-updates.php

378 lines
14 KiB
PHP
Raw Normal View History

2023-01-21 01:31:34 +01:00
<?php
/**
* Partial: Latest Updates (+ Simple)
*
* Renders the (buffered) HTML for the [fictioneer_latest_updates] or
* [fictioneer_latest_updates type="simple"] shortcodes. Looks for the
* 'fictioneer_chapters_added' meta property and only updates when the
* items in the chapters list increase. Admittedly, this can be exploited
* by removing and re-adding chapters.
*
* @package WordPress
* @subpackage Fictioneer
* @since 4.3.0
2023-01-21 01:31:34 +01:00
*
* @internal $args['type'] Type argument passed from shortcode. Default 'default'.
* @internal $args['count'] Number of posts provided by the shortcode.
* @internal $args['author'] Author provided by the shortcode.
* @internal $args['order'] Order of posts. Default 'DESC'.
* @internal $args['post_ids'] Array of post IDs. Default empty.
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.
* @internal $args['taxonomies'] Array of taxonomy arrays. Default empty.
* @internal $args['relation'] Relationship between taxonomies.
* @internal $args['simple'] Whether to show the simple variant.
* @internal $args['vertical'] Whether to show the vertical variant.
* @internal $args['seamless'] Whether to render the image seamless. Only with vertical.
* @internal $args['aspect_ratio'] Aspect ratio for the image. Only with vertical.
2023-09-15 22:42:34 +02:00
* @internal $args['classes'] String of additional CSS classes. Default empty.
2023-01-21 01:31:34 +01:00
*/
?>
<?php
// No direct access!
defined( 'ABSPATH' ) OR exit;
2023-01-21 01:31:34 +01:00
// Setup
$card_counter = 0;
// Prepare query
$query_args = array(
2023-09-15 14:00:44 +02:00
'fictioneer_query_name' => 'latest_updates',
2023-03-09 02:03:09 +01:00
'post_type' => 'fcn_story',
'post_status' => 'publish',
'post__in' => $args['post_ids'], // May be empty!
2023-09-16 13:25:38 +02:00
'order' => $args['order'],
2023-01-21 01:31:34 +01:00
'orderby' => 'meta_value',
2023-09-16 13:25:38 +02:00
'meta_key' => 'fictioneer_chapters_added',
'posts_per_page' => $args['count'] + 4, // Account for non-eligible posts!
2024-01-29 23:38:21 +01:00
'no_found_rows' => true // Improve performance
);
// Use extended meta query?
if ( get_option( 'fictioneer_disable_extended_story_list_meta_queries' ) ) {
2024-01-29 23:38:21 +01:00
// Extended syntax necessary due to 'fictioneer_chapters_added'
$query_args['meta_query'] = array(
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
)
);
} else {
$query_args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
),
array(
'key' => 'fictioneer_story_hidden',
'compare' => 'NOT EXISTS'
)
);
}
2023-01-21 01:31:34 +01:00
2023-09-16 13:25:38 +02:00
// Author?
2023-09-17 01:28:14 +02:00
if ( ! empty( $args['author'] ) ) {
2023-09-16 13:25:38 +02:00
$query_args['author_name'] = $args['author'];
}
2023-01-21 01:31:34 +01:00
// Author IDs?
if ( ! empty( $args['author_ids'] ) ) {
$query_args['author__in'] = $args['author_ids'];
}
2023-03-08 23:24:41 +01:00
// Taxonomies?
if ( ! empty( $args['taxonomies'] ) ) {
$query_args['tax_query'] = fictioneer_get_shortcode_tax_query( $args );
}
// 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'] ) {
add_filter( 'posts_where', 'fictioneer_exclude_protected_posts' );
}
// Apply filters
2023-03-10 15:20:48 +01:00
$query_args = apply_filters( 'fictioneer_filter_shortcode_latest_updates_query_args', $query_args, $args );
2023-01-21 01:31:34 +01:00
// Query stories
$entries = fictioneer_shortcode_query( $query_args );
2023-01-21 01:31:34 +01:00
// Remove temporary filters
remove_filter( 'posts_where', 'fictioneer_exclude_protected_posts' );
2023-01-21 01:31:34 +01:00
?>
2023-09-15 22:42:34 +02:00
<section class="small-card-block latest-updates <?php echo $args['classes']; ?>">
2023-01-21 01:31:34 +01:00
<?php if ( $entries->have_posts() ) : ?>
<ul class="grid-columns _collapse-on-mobile">
2023-01-21 01:31:34 +01:00
<?php while ( $entries->have_posts() ) : $entries->the_post(); ?>
<?php
// Setup
$story = fictioneer_get_story_data( $post->ID, false ); // Does not refresh comment count!
2023-01-21 01:31:34 +01:00
$tags = get_option( 'fictioneer_show_tags_on_story_cards' ) ? get_the_tags( $post ) : false;
$grid_or_vertical = $args['vertical'] ? '_vertical' : '_grid';
2023-01-21 01:31:34 +01:00
$chapter_list = [];
2024-02-12 00:27:58 +01:00
$card_classes = [];
2023-01-21 01:31:34 +01:00
// Skip if no chapters
2023-08-28 10:43:04 +02:00
if ( $story['chapter_count'] < 1 ) {
continue;
}
2023-01-21 01:31:34 +01:00
2024-02-12 00:27:58 +01:00
// Extra classes
if ( ! empty( $post->post_password ) ) {
2024-02-12 00:27:58 +01:00
$card_classes[] = '_password';
}
2024-02-12 01:40:38 +01:00
if ( get_theme_mod( 'card_style', 'default' ) !== 'default' ) {
$card_classes[] = '_' . get_theme_mod( 'card_style' );
}
if ( $args['vertical'] && $args['seamless'] ) {
$card_classes[] = '_seamless';
}
2023-01-21 01:31:34 +01:00
// Search for viable chapters...
$search_list = array_reverse( $story['chapter_ids'] );
foreach ( $search_list as $chapter_id ) {
$chapter_post = get_post( $chapter_id );
if ( get_post_meta( $chapter_id, 'fictioneer_chapter_hidden', true ) ) {
2023-08-28 10:43:04 +02:00
continue;
}
$chapter_list[] = $chapter_post;
2023-08-28 10:43:04 +02:00
if ( count( $chapter_list ) > 1 ) {
break; // Max two
}
2023-01-21 01:31:34 +01:00
}
// No viable chapters
2023-08-28 10:43:04 +02:00
if ( count( $chapter_list ) < 1 ) {
continue;
}
2023-01-21 01:31:34 +01:00
// Count actually rendered cards to account for buffer
2023-08-28 10:43:04 +02:00
if ( ++$card_counter > $args['count'] ) {
break;
}
// Card attributes
$attributes = [];
if ( $args['aspect_ratio'] ) {
$attributes['style'] = '--card-image-aspect-ratio: ' . $args['aspect_ratio'];
}
$attributes = apply_filters( 'fictioneer_filter_card_attributes', $attributes, $post, 'shortcode-latest-updates' );
$card_attributes = '';
foreach ( $attributes as $key => $value ) {
$card_attributes .= esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
}
2024-03-16 14:49:51 +01:00
// Thumbnail attributes
$thumbnail_args = array(
'alt' => sprintf( __( '%s Cover', 'fictioneer' ), $story['title'] ),
'class' => 'no-auto-lightbox'
);
2023-01-21 01:31:34 +01:00
?>
<li class="card _small _story-update <?php echo implode( ' ', $card_classes ); ?>" <?php echo $card_attributes; ?>>
2023-01-21 01:31:34 +01:00
<div class="card__body polygon">
<div class="card__main <?php echo $grid_or_vertical; ?> _small">
2023-01-21 01:31:34 +01:00
<?php do_action( 'fictioneer_shortcode_latest_updates_card_body', $post, $story, $args ); ?>
2023-01-21 01:31:34 +01:00
<?php if ( has_post_thumbnail() ) : ?>
2024-03-16 14:49:51 +01:00
<a href="<?php the_post_thumbnail_url( 'full' ); ?>" title="<?php echo esc_attr( sprintf( __( '%s Thumbnail', 'fictioneer' ), $story['title'] ) ); ?>" class="card__image cell-img" <?php echo fictioneer_get_lightbox_attribute(); ?>><?php echo get_the_post_thumbnail( $post, 'snippet', $thumbnail_args ); ?></a>
<?php elseif ( $args['vertical'] ) : ?>
<a href="<?php the_permalink(); ?>" class='card__image cell-img _default'></a>
2023-01-21 01:31:34 +01:00
<?php endif; ?>
2024-01-18 21:44:29 +01:00
<h3 class="card__title _small cell-title"><a href="<?php the_permalink(); ?>" class="truncate _1-1"><?php
if ( ! empty( $post->post_password ) ) {
echo '<i class="fa-solid fa-lock protected-icon"></i> ';
}
echo $story['title'];
?></a></h3>
2023-01-21 01:31:34 +01:00
<div class="card__content _small cell-desc">
<div class="truncate <?php echo count( $chapter_list ) > 1 ? '_1-1' : '_2-2'; ?>">
<?php if ( get_option( 'fictioneer_show_authors' ) ) : ?>
<span class="card__by-author"><?php
printf( _x( 'by %s —', 'Small card: by {Author} —.', 'fictioneer' ), fictioneer_get_author_node() );
?></span>
<?php endif; ?>
2023-01-21 01:31:34 +01:00
<span><?php
$short_description = fictioneer_first_paragraph_as_excerpt(
2023-12-02 00:00:13 +01:00
fictioneer_get_content_field( 'fictioneer_story_short_description', $post->ID )
);
2023-01-21 01:31:34 +01:00
echo strlen( $short_description ) < 230 ? get_the_excerpt() : $short_description;
?></span>
</div>
</div>
<ol class="card__link-list _small cell-list">
<?php foreach ( $chapter_list as $chapter ) : ?>
<?php
// Chapter title
$list_title = get_post_meta( $chapter->ID, 'fictioneer_chapter_list_title', true );
$list_title = trim( wp_strip_all_tags( $list_title ) );
if ( empty( $list_title ) ) {
$chapter_title = fictioneer_get_safe_title( $chapter->ID, 'shortcode-latest-updates' );
} else {
$chapter_title = $list_title;
}
// Extra classes
$list_item_classes = [];
if ( ! empty( $chapter->post_password ) ) {
$list_item_classes[] = '_password';
}
?>
<li class="card__link-list-item <?php echo implode( ' ', $list_item_classes ); ?>">
2023-01-21 01:31:34 +01:00
<div class="card__left text-overflow-ellipsis">
<i class="fa-solid fa-caret-right"></i>
<a href="<?php the_permalink( $chapter->ID ); ?>" class="card__link-list-link"><?php
echo $chapter_title;
2023-01-21 01:31:34 +01:00
?></a>
</div>
<div class="card__right">
<?php
echo fictioneer_shorten_number( fictioneer_get_word_count( $chapter->ID ) );
echo '<span class="separator-dot">&#8196;&bull;&#8196;</span>';
echo get_the_date( FICTIONEER_LATEST_UPDATES_LI_DATE, $chapter->ID )
?>
2023-01-21 01:31:34 +01:00
</div>
</li>
<?php endforeach; ?>
</ol>
<?php if ( ! get_option( 'fictioneer_hide_taxonomies_on_story_cards' ) && ! $args['simple'] ) : ?>
<div class="card__tag-list _small _scrolling cell-tax">
<div class="card__h-scroll">
2023-01-21 01:31:34 +01:00
<?php
if ( $story['has_taxonomies'] || $tags ) {
2023-06-04 01:49:00 +02:00
$output = [];
2023-01-21 01:31:34 +01:00
if ( $story['fandoms'] ) {
foreach ( $story['fandoms'] as $fandom ) {
2023-06-04 01:49:00 +02:00
$output[] = '<a href="' . get_tag_link( $fandom ) . '" class="tag-pill _inline _fandom">' . $fandom->name . '</a>';
2023-01-21 01:31:34 +01:00
}
}
if ( $story['genres'] ) {
foreach ( $story['genres'] as $genre ) {
2023-06-04 01:49:00 +02:00
$output[] = '<a href="' . get_tag_link( $genre ) . '" class="tag-pill _inline _genre">' . $genre->name . '</a>';
2023-01-21 01:31:34 +01:00
}
}
if ( $tags ) {
foreach ( $tags as $tag ) {
2023-06-04 01:49:00 +02:00
$output[] = '<a href="' . get_tag_link( $tag ) . '" class="tag-pill _inline">' . $tag->name . '</a>';
2023-01-21 01:31:34 +01:00
}
}
if ( $story['characters'] ) {
foreach ( $story['characters'] as $character ) {
2023-06-04 01:49:00 +02:00
$output[] = '<a href="' . get_tag_link( $character ) . '" class="tag-pill _inline _character">' . $character->name . '</a>';
2023-01-21 01:31:34 +01:00
}
}
2023-06-04 01:49:00 +02:00
// Implode with three-per-em spaces around a bullet
echo implode( '&#8196;&bull;&#8196;', $output );
2023-01-21 01:31:34 +01:00
} else {
?><span class="card__no-taxonomies"><?php _e( 'No taxonomies specified yet.', 'fictioneer' ); ?></span><?php
}
?>
</div>
</div>
<?php endif; ?>
</div>
<?php if ( ! $args['simple'] ) : ?>
<div class="card__footer _small">
2023-09-10 16:34:15 +02:00
<div class="card__footer-box _left text-overflow-ellipsis"><?php
2023-06-04 22:17:42 +02:00
2023-09-10 16:34:15 +02:00
// Build footer items
$footer_items = [];
2023-01-21 01:31:34 +01:00
2023-09-10 16:34:15 +02:00
$footer_items['chapters'] = '<i class="card-footer-icon fa-solid fa-list" title="' .
esc_attr__( 'Chapters', 'fictioneer' ) . '"></i> ' . $story['chapter_count'];
2023-01-21 01:31:34 +01:00
2023-09-10 16:34:15 +02:00
$footer_items['words'] = '<i class="card-footer-icon fa-solid fa-font" title="' .
esc_attr__( 'Total Words', 'fictioneer' ) . '"></i> ' . $story['word_count_short'];
2023-01-21 01:31:34 +01:00
2023-09-10 16:34:15 +02:00
$footer_items['modified_date'] = '<i class="card-footer-icon fa-regular fa-clock" title="' .
esc_attr__( 'Last Updated', 'fictioneer' ) . '"></i> ' .
get_the_modified_date( FICTIONEER_LATEST_UPDATES_FOOTER_DATE, $post );
2023-06-04 22:17:42 +02:00
2023-09-10 16:34:15 +02:00
$footer_items['status'] = '<i class="card-footer-icon ' . $story['icon'] . '"></i> ' .
fcntr( $story['status'] );
2024-01-28 00:18:25 +01:00
// Filter footer items
2023-09-10 16:34:15 +02:00
$footer_items = apply_filters(
'fictioneer_filter_shortcode_latest_updates_card_footer',
$footer_items,
2023-09-10 18:36:12 +02:00
$post,
2023-09-10 16:34:15 +02:00
$story,
2023-09-10 18:36:12 +02:00
$args
2023-09-10 16:34:15 +02:00
);
// Implode and render footer items
echo implode( ' ', $footer_items );
?></div>
2023-01-21 01:31:34 +01:00
<div class="card__footer-box _right rating-letter-label tooltipped" data-tooltip="<?php echo fcntr( $story['rating'], true ); ?>">
2023-06-04 22:17:42 +02:00
<?php echo fcntr( $story['rating_letter'] ); ?>
2023-01-21 01:31:34 +01:00
</div>
</div>
<?php endif; ?>
</div>
</li>
<?php endwhile; ?>
</ul>
2023-11-30 17:52:19 +01:00
<?php else : ?>
2023-01-21 01:31:34 +01:00
2023-09-16 13:25:38 +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>