fictioneer/partials/_latest-recommendations.php

267 lines
10 KiB
PHP
Raw Normal View History

2023-01-21 01:31:34 +01:00
<?php
/**
* Partial: Latest Recommendations
*
* Renders the (buffered) HTML for the [fictioneer_latest_recommendations] shortcode.
*
* @package WordPress
* @subpackage Fictioneer
* @since 4.0.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['orderby'] Sorting of posts. Default 'date'.
* @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.s
* @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['vertical'] Whether to show the vertical variant.
2024-05-15 09:57:43 +02:00
* @internal $args['seamless'] Whether to render the image seamless. Default false (Customizer).
* @internal $args['aspect_ratio'] Aspect ratio for the image. Only with vertical.
2024-08-23 23:12:24 +02:00
* @internal $args['terms'] Either inline, pills, none, or false. Default inline.
* @internal $args['max_terms'] Maximum number of shown taxonomies. Default 10.
* @internal $args['lightbox'] Whether the image is opened in the lightbox. Default true.
2024-05-15 09:57:43 +02:00
* @internal $args['thumbnail'] Whether the image is rendered. Default true (Customizer).
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
*/
// No direct access!
defined( 'ABSPATH' ) OR exit;
2023-01-21 01:31:34 +01:00
// Setup
$show_taxonomies = ! get_option( 'fictioneer_hide_taxonomies_on_recommendation_cards' );
// Prepare query
$query_args = array (
2023-09-15 14:00:44 +02:00
'fictioneer_query_name' => 'latest_recommendations',
2023-01-21 01:31:34 +01:00
'post_type' => 'fcn_recommendation',
2023-03-09 02:03:09 +01:00
'post_status' => 'publish',
'post__in' => $args['post_ids'], // May be empty!
'order' => $args['order'],
'orderby' => $args['orderby'],
2023-01-21 01:31:34 +01:00
'posts_per_page' => $args['count'],
'no_found_rows' => true
);
// Author?
2023-09-17 01:28:14 +02:00
if ( ! empty( $args['author'] ) ) {
$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_recommendations_query_args', $query_args, $args );
2023-01-21 01:31:34 +01:00
// Query recommendations
$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-recommendations <?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
2024-05-17 17:46:54 +02:00
$post_id = $post->ID;
$title = fictioneer_get_safe_title( $post_id, 'shortcode-latest-recommendations' );
$one_sentence = get_post_meta( $post_id, 'fictioneer_recommendation_one_sentence', true );
2023-01-21 01:31:34 +01:00
$fandoms = get_the_terms( $post, 'fcn_fandom' );
$characters = get_the_terms( $post, 'fcn_character' );
$genres = get_the_terms( $post, 'fcn_genre' );
$tags = get_option( 'fictioneer_show_tags_on_recommendation_cards' ) ? get_the_tags( $post ) : false;
$grid_or_vertical = $args['vertical'] ? '_vertical' : '_grid';
2024-02-12 00:27:58 +01:00
$card_classes = [];
// Extra classes
2024-02-12 01:40:38 +01:00
if ( get_theme_mod( 'card_style', 'default' ) !== 'default' ) {
$card_classes[] = '_' . get_theme_mod( 'card_style' );
2024-02-12 00:27:58 +01:00
}
2023-01-21 01:31:34 +01:00
if ( $args['vertical'] ) {
$card_classes[] = '_vertical';
}
2024-05-14 18:40:52 +02:00
if ( $args['seamless'] ) {
$card_classes[] = '_seamless';
}
// Truncate factor
$truncate_factor = $args['vertical'] ? '_4-4' : '_3-3';
2023-01-21 01:31:34 +01:00
// Sources
$urls = array_merge(
2024-05-17 17:46:54 +02:00
explode( "\n", get_post_meta( $post_id, 'fictioneer_recommendation_urls', true ) ),
explode( "\n", get_post_meta( $post_id, 'fictioneer_recommendation_support', true ) )
2023-01-21 01:31:34 +01:00
);
// Sanitize
$urls = array_map( 'wp_strip_all_tags', $urls );
// Remove empty nodes
$urls = array_filter( $urls );
// Abort if no links found
2023-08-28 10:43:04 +02:00
if ( empty( $urls ) ) {
continue;
}
2023-01-21 01:31:34 +01:00
// Extract first link
$url = $urls[0];
$tuple = explode( '|', $url );
$tuple = array_map( 'trim', $tuple );
// 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-recommendations' );
$card_attributes = '';
foreach ( $attributes as $key => $value ) {
$card_attributes .= esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
}
2023-01-21 01:31:34 +01:00
?>
2024-05-17 17:46:54 +02:00
<li class="post-<?php echo $post_id; ?> card _small _recommendation _no-footer <?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_recommendations_card_body', $post, $args );
if ( $args['thumbnail'] ) {
fictioneer_render_thumbnail(
array(
2024-05-17 17:46:54 +02:00
'post_id' => $post_id,
'title' => $title,
'classes' => 'card__image cell-img',
'permalink' => get_permalink(),
2024-04-18 16:09:27 +02:00
'lightbox' => $args['lightbox'],
'vertical' => $args['vertical'],
'seamless' => $args['seamless'],
'aspect_ratio' => $args['aspect_ratio']
)
);
}
?>
2023-01-21 01:31:34 +01:00
<h3 class="card__title _small cell-title"><a href="<?php the_permalink(); ?>" class="truncate _1-1"><?php echo $title; ?></a></h3>
2023-01-21 01:31:34 +01:00
<div class="card__content _small cell-meta text-overflow-ellipsis">
<span class="card__by-author"><?php
2023-01-21 01:31:34 +01:00
printf(
_x( 'by %s on', 'Small card: by {Author} on.', 'fictioneer' ),
2024-05-17 17:46:54 +02:00
'<span class="author">' . get_post_meta( $post_id, 'fictioneer_recommendation_author', true ) . '</span>'
2023-01-21 01:31:34 +01:00
);
?></span>
<a href="<?php echo esc_url( $tuple[1] ); ?>" rel="noopener" target="_blank" class="bold-link"><?php
echo $tuple[0];
?></a>
2023-01-21 01:31:34 +01:00
</div>
2024-01-19 14:13:17 +01:00
<div class="card__content _small cell-desc">
<div class="truncate <?php echo $truncate_factor; ?>"><?php
2023-01-21 01:31:34 +01:00
if ( ! empty( $one_sentence ) ) {
echo wp_strip_all_tags( $one_sentence, true );
} else {
echo fictioneer_first_paragraph_as_excerpt( get_the_content() );
}
2024-01-19 14:13:17 +01:00
?></div>
2023-01-21 01:31:34 +01:00
</div>
<?php if ( $show_taxonomies ) : ?>
<div class="card__tag-list _small _scrolling cell-tax">
2024-08-23 23:12:24 +02:00
<div class="card__h-scroll <?php echo $args['terms'] === 'pills' ? '_pills' : ''; ?>">
2023-01-21 01:31:34 +01:00
<?php
if ( $fandoms || $characters || $genres || $tags ) {
2024-08-23 23:12:24 +02:00
$variant = $args['terms'] === 'pills' ? '_pill' : '_inline';
$terms = array_merge(
$fandoms ? fictioneer_get_term_nodes( $fandoms, "{$variant} _fandom" ) : [],
$genres ? fictioneer_get_term_nodes( $genres, "{$variant} _genre" ) : [],
$tags ? fictioneer_get_term_nodes( $tags, "{$variant} _tag" ) : [],
$characters ? fictioneer_get_term_nodes( $characters, "{$variant} _character" ) : []
);
$terms = apply_filters(
'fictioneer_filter_shortcode_latest_recommendations_terms',
$terms, $post, $args, null
);
2024-08-23 16:44:04 +02:00
// Implode with separator
2024-08-23 23:12:24 +02:00
echo implode(
fictioneer_get_bullet_separator( 'latest-recommendations', $args['terms'] === 'pills' ),
array_slice( $terms, 0, $args['max_terms'] )
);
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>
</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 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>