fictioneer/partials/_card-collection.php

253 lines
8.7 KiB
PHP
Raw Normal View History

2023-01-21 01:31:34 +01:00
<?php
/**
* Partial: Post Collection
*
* @package WordPress
* @subpackage Fictioneer
* @since 4.0
*
2023-06-07 23:23:19 +02:00
* @internal $args['show_type'] Whether to show the post type label. Unsafe.
* @internal $args['cache'] Whether to account for active caching. Unsafe.
* @internal $args['order'] Current order. Default 'desc'. Unsafe.
* @internal $args['orderby'] Current orderby. Default 'modified'. Unsafe.
2023-01-21 01:31:34 +01:00
*/
?>
<?php
// Setup
$list_title = trim( fictioneer_get_field( 'fictioneer_collection_list_title' ) );
$title = empty( $list_title ) ? fictioneer_get_safe_title( get_the_ID() ) : $list_title;
$description = fictioneer_get_content_field( 'fictioneer_collection_description' );
$items = fictioneer_get_field( 'fictioneer_collection_items' );
$items = empty( $items ) ? [] : $items;
$story_count = 0;
$chapter_count = 0;
$word_count = 0;
$processed_ids = [];
// Taxonomies
$tags = false;
$fandoms = false;
$characters = false;
$genres = false;
if (
get_option( 'fictioneer_show_tags_on_collection_cards' ) &&
! get_option( 'fictioneer_hide_taxonomies_on_collection_cards' )
) {
$tags = get_the_tags();
}
if ( ! get_option( 'fictioneer_hide_taxonomies_on_collection_cards' ) ) {
$fandoms = get_the_terms( $post->ID, 'fcn_fandom' );
$characters = get_the_terms( $post->ID, 'fcn_character' );
$genres = get_the_terms( $post->ID, 'fcn_genre' );
}
2023-01-21 01:31:34 +01:00
// Flags
2023-08-07 19:55:24 +02:00
$show_type = $args['show_type'] ?? false;
$show_taxonomies = ! get_option( 'fictioneer_hide_taxonomies_on_collection_cards' ) && ( $fandoms || $characters || $genres || $tags );
2023-01-21 01:31:34 +01:00
// Query featured posts
if ( ! empty( $items ) ) {
$items = new WP_Query(
array(
'post_type' => 'any',
'post_status' => 'publish',
'post__in' => $items,
'ignore_sticky_posts' => true,
'orderby' => 'modified',
'posts_per_page' => -1,
'update_post_meta_cache' => false, // Improve performance
'update_post_term_cache' => false, // Improve performance
'no_found_rows' => true // Improve performance
)
);
$items = $items->posts;
2023-01-21 01:31:34 +01:00
}
// Statistics
if ( ! empty( $items ) ) {
foreach ( $items as $item ) {
2023-01-21 01:31:34 +01:00
// Count by type
switch ( $item->post_type ) {
2023-01-21 01:31:34 +01:00
case 'fcn_chapter':
if ( ! in_array( $item->ID, $processed_ids ) ) {
2023-01-21 01:31:34 +01:00
$chapter_count += 1;
$word_count += get_post_meta( $item->ID, '_word_count', true );
$processed_ids[] = $item->ID;
2023-01-21 01:31:34 +01:00
}
break;
case 'fcn_story':
$story_count += 1;
$chapter_ids = fictioneer_get_field( 'fictioneer_story_chapters', $item->ID );
2023-01-21 01:31:34 +01:00
// Try to rescue an empty description by using one from a story...
if ( empty( $description ) ) {
$description = fictioneer_first_paragraph_as_excerpt(
fictioneer_get_content_field( 'fictioneer_story_short_description', $item->ID )
2023-01-21 01:31:34 +01:00
);
}
// Query eligible chapters
$chapter_query_args = array(
'post_type' => 'fcn_chapter',
'post_status' => 'publish',
'post__in' => $chapter_ids,
'posts_per_page' => -1,
'update_post_term_cache' => false, // Improve performance
'no_found_rows' => true // Improve performance
);
$chapters = new WP_Query( $chapter_query_args );
$chapters = $chapters->posts;
if ( ! empty( $chapters ) ) {
foreach ( $chapters as $chapter ) {
if ( fictioneer_get_field( 'fictioneer_chapter_no_chapter', $chapter->ID ) ) continue;
if ( fictioneer_get_field( 'fictioneer_chapter_hidden', $chapter->ID ) ) continue;
if ( ! in_array( $chapter->ID, $processed_ids ) ) {
$chapter_count += 1;
$word_count += get_post_meta( $chapter->ID, '_word_count', true );
$processed_ids[] = $chapter->ID;
2023-01-21 01:31:34 +01:00
}
}
}
break;
}
}
// Prepare features items
$items = array_slice( $items, 0, 3 );
2023-01-21 01:31:34 +01:00
}
// Required since it's possible to add chapters to collection outside of
// their stories, which would cause the comments to be counted twice.
$comment_args = array(
'post_type' => 'fcn_chapter',
2023-08-08 00:18:53 +02:00
'status' => 1,
'post__in' => $processed_ids,
'count' => true,
'update_comment_meta_cache' => false
);
2023-01-21 01:31:34 +01:00
$comment_count = get_comments( $comment_args );
?>
<li id="collection-card-<?php the_ID(); ?>" class="card">
<div class="card__body polygon">
<div class="card__header _large">
<?php if ( $show_type ) : ?>
<div class="card__label"><?php _ex( 'Collection', 'Collection card label.', 'fictioneer' ); ?></div>
<?php endif; ?>
<h3 class="card__title"><a href="<?php the_permalink(); ?>" class="truncate _1-1"><?php echo $title; ?></a></h3>
2023-01-21 01:31:34 +01:00
</div>
<div class="card__main _grid _large">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_post_thumbnail_url( 'full' ); ?>" title="<?php printf( __( '%s Thumbnail', 'fictioneer' ), $title ) ?>" class="card__image cell-img" <?php echo fictioneer_get_lightbox_attribute(); ?>><?php the_post_thumbnail( 'cover' ); ?></a>
<?php endif; ?>
<div class="card__content cell-desc truncate <?php echo count( $items ) > 2 ? '_3-4' : '_4-4'; ?>"><span><?php echo $description; ?></span></div>
2023-01-21 01:31:34 +01:00
<?php if ( ! empty( $items ) ): ?>
<ol class="card__link-list cell-list">
<?php foreach ( $items as $post_id ) : ?>
<li>
<div class="card__left text-overflow-ellipsis">
<i class="fa-solid fa-caret-right"></i>
<a href="<?php the_permalink( $post_id ); ?>"><?php
$list_title = fictioneer_get_field( 'fictioneer_chapter_list_title', $post_id );
if ( $list_title ) {
echo wp_strip_all_tags( $list_title );
} else {
echo fictioneer_get_safe_title( $post_id );
}
?></a>
</div>
<div class="card__right">
<?php
echo get_post_type_object( get_post_type( $post_id ) )->labels->singular_name;
echo '<span class="separator-dot hide-below-480">&#8196;&bull;&#8196;</span><span class="hide-below-480">';
echo get_the_modified_date( FICTIONEER_CARD_COLLECTION_LI_DATE, $post_id );
echo '</span>';
?>
2023-01-21 01:31:34 +01:00
</div>
</li>
<?php endforeach; ?>
</ol>
<?php endif; ?>
<?php if ( $show_taxonomies ) : ?>
2023-06-04 01:49:00 +02:00
<div class="card__tag-list cell-tax">
2023-01-21 01:31:34 +01:00
<?php
2023-06-04 01:49:00 +02:00
$output = [];
2023-01-21 01:31:34 +01:00
if ( $fandoms ) {
foreach ( $fandoms as $fandom ) {
$output[] = "<a href='" . get_tag_link( $fandom ) . "' class='tag-pill _inline _fandom'>{$fandom->name}</a>";
2023-01-21 01:31:34 +01:00
}
}
if ( $genres ) {
foreach ( $genres as $genre ) {
$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 ) {
$output[] = "<a href='" . get_tag_link( $tag ) . "' class='tag-pill _inline'>{$tag->name}</a>";
2023-01-21 01:31:34 +01:00
}
}
if ( $characters ) {
foreach ( $characters as $character ) {
$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
?>
</div>
<?php endif; ?>
</div>
<div class="card__footer">
<div class="card__left text-overflow-ellipsis">
<i class="fa-solid fa-book" title="<?php esc_attr_e( 'Stories', 'fictioneer' ) ?>"></i>
2023-06-04 22:17:42 +02:00
<?php echo $story_count; ?>
2023-01-21 01:31:34 +01:00
<i class="fa-solid fa-list" title="<?php esc_attr_e( 'Chapters', 'fictioneer' ) ?>"></i>
2023-06-04 22:17:42 +02:00
<?php echo $chapter_count; ?>
2023-01-21 01:31:34 +01:00
<i class="fa-solid fa-font"></i>
2023-06-04 22:17:42 +02:00
<?php echo fictioneer_shorten_number( $word_count ); ?>
2023-01-21 01:31:34 +01:00
<?php if ( ( $args['orderby'] ?? 0 ) === 'date' ) : ?>
<i class="fa-solid fa-clock" title="<?php esc_attr_e( 'Published', 'fictioneer' ) ?>"></i>
<?php echo get_the_date( FICTIONEER_CARD_CHAPTER_FOOTER_DATE ); ?>
<?php else : ?>
<i class="fa-regular fa-clock" title="<?php esc_attr_e( 'Last Updated', 'fictioneer' ) ?>"></i>
<?php the_modified_date( FICTIONEER_CARD_COLLECTION_FOOTER_DATE ); ?>
<?php endif; ?>
2023-01-21 01:31:34 +01:00
<i class="fa-solid fa-message" title="<?php esc_attr_e( 'Comments', 'fictioneer' ) ?>"></i>
2023-08-08 00:18:53 +02:00
<?php echo number_format_i18n( $comment_count ); ?>
2023-01-21 01:31:34 +01:00
</div>
</div>
</div>
</li>