Add Story Actions shortcode

This commit is contained in:
Tetrakern 2024-04-15 01:52:01 +02:00
parent b3f76ef062
commit f23a06714b
3 changed files with 114 additions and 11 deletions

View File

@ -815,8 +815,12 @@ if ( ! function_exists( 'fictioneer_get_story_buttons' ) ) {
* @since 5.0.0
* @since 5.9.4 - Removed output buffer.
*
* @param array $args['story_data'] Collection of story data.
* @param int $args['story_id'] The story post ID.
* @param array $args['story_data'] Collection of story data.
* @param int $args['story_id'] The story post ID.
* @param bool|null $args['follow'] Optional. Whether to show the Follow button if enabled. Default true.
* @param bool|null $args['reminder'] Optional. Whether to show the Reminder button if enabled. Default true.
* @param bool|null $args['subscribe'] Optional. Whether to show the Subscribe button if enabled. Default true.
* @param bool|null $args['download'] Optional. Whether to show the Download button if enabled. Default true.
*
* @return string HTML for the story buttons.
*/
@ -827,14 +831,18 @@ if ( ! function_exists( 'fictioneer_get_story_buttons' ) ) {
$story_id = $args['story_id'];
$ebook_upload = get_post_meta( $story_id, 'fictioneer_story_ebook_upload_one', true ); // Attachment ID
$subscribe_buttons = fictioneer_get_subscribe_options();
$follow = $args['follow'] ?? 1;
$reminder = $args['reminder'] ?? 1;
$subscribe = $args['subscribe'] ?? 1;
$download = $args['download'] ?? 1;
$output = [];
// Flags
$show_epub_download = $story_data['chapter_count'] > 0 && get_post_meta( $story_id, 'fictioneer_story_epub_preface', true ) && get_option( 'fictioneer_enable_epubs' ) && ! get_post_meta( $story_id, 'fictioneer_story_no_epub', true );
$show_epub_download = $story_data['chapter_count'] > 0 && get_post_meta( $story_id, 'fictioneer_story_epub_preface', true ) && get_option( 'fictioneer_enable_epubs' ) && ! get_post_meta( $story_id, 'fictioneer_story_no_epub', true ) && $download;
$show_login = get_option( 'fictioneer_enable_oauth' ) && ! is_user_logged_in();
// Subscribe
if ( ! empty( $subscribe_buttons ) ) {
if ( $subscribe && ! empty( $subscribe_buttons ) ) {
$output['subscribe'] = sprintf(
'<div class="toggle-last-clicked subscribe-menu-toggle button _secondary popup-menu-toggle _popup-right-if-last" tabindex="0" role="button" aria-label="%s"><div><i class="fa-solid fa-bell"></i> %s</div><div class="popup-menu _bottom _center">%s</div></div>',
fcntr( 'subscribe', true ),
@ -852,7 +860,7 @@ if ( ! function_exists( 'fictioneer_get_story_buttons' ) ) {
esc_attr__( 'Download ePUB', 'fictioneer' ),
__( 'ePUB', 'fictioneer' )
);
} elseif ( wp_get_attachment_url( $ebook_upload ) ) {
} elseif ( $download && wp_get_attachment_url( $ebook_upload ) ) {
$output['ebook'] = sprintf(
'<a href="%s" class="button _secondary" rel="noreferrer noopener nofollow" aria-label="%s" download><i class="fa-solid fa-cloud-download-alt"></i><span class="span-epub hide-below-640">%s</span></a>',
esc_url( wp_get_attachment_url( $ebook_upload ) ),
@ -862,7 +870,7 @@ if ( ! function_exists( 'fictioneer_get_story_buttons' ) ) {
}
// Reminder
if ( get_option( 'fictioneer_enable_reminders' ) ) {
if ( $reminder && get_option( 'fictioneer_enable_reminders' ) ) {
$output['reminder'] = sprintf(
'<button class="button _secondary button-read-later hide-if-logged-out" data-story-id="%d"><i class="fa-solid fa-clock"></i><span class="span-follow hide-below-480">%s</span></button>',
$story_id,
@ -879,7 +887,7 @@ if ( ! function_exists( 'fictioneer_get_story_buttons' ) ) {
}
// Follow
if ( get_option( 'fictioneer_enable_follows' ) ) {
if ( $follow && get_option( 'fictioneer_enable_follows' ) ) {
$output['follow'] = sprintf(
'<button class="button _secondary button-follow-story hide-if-logged-out" data-story-id="%d"><i class="fa-solid fa-star"></i><span class="span-follow hide-below-400">%s</span></button>',
$story_id,

View File

@ -1623,3 +1623,97 @@ function fictioneer_shortcode_story_section( $attr ) {
return $html;
}
add_shortcode( 'fictioneer_story_section', 'fictioneer_shortcode_story_section' );
// =============================================================================
// STORY ACTIONS SHORTCODE
// =============================================================================
/**
* Shortcode to show story actions
*
* @since 5.14.0
*
* @param string $attr['story_id'] The ID of the story.
* @param string|null $attr['class'] Optional. Additional CSS classes, separated by whitespace.
* @param string|null $attr['follow'] Optional. Whether to show the Follow button if enabled. Default true.
* @param string|null $attr['reminder'] Optional. Whether to show the Reminder button if enabled. Default true.
* @param string|null $attr['subscribe'] Optional. Whether to show the Subscribe button if enabled. Default true.
* @param string|null $attr['download'] Optional. Whether to show the Download button if enabled. Default true.
*
* @return string The captured shortcode HTML.
*/
function fictioneer_shortcode_story_actions( $attr ) {
global $post;
// Setup
$story_id = absint( $attr['story_id'] ?? 0 );
$post = get_post( $story_id );
$story_data = fictioneer_get_story_data( $story_id );
$classes = wp_strip_all_tags( $attr['class'] ?? '' );
$follow = filter_var( $attr['follow'] ?? 1, FILTER_VALIDATE_BOOLEAN );
$reminder = filter_var( $attr['reminder'] ?? 1, FILTER_VALIDATE_BOOLEAN );
$subscribe = filter_var( $attr['subscribe'] ?? 1, FILTER_VALIDATE_BOOLEAN );
$download = filter_var( $attr['download'] ?? 1, FILTER_VALIDATE_BOOLEAN );
$hook_args = array(
'story_id' => $story_id,
'story_data' => $story_data,
'follow' => $follow,
'reminder' => $reminder,
'subscribe' => $subscribe,
'download' => $download
);
// Abort if...
if ( ! $story_data ) {
return;
}
if ( ! is_page_template( 'singular-story.php' ) ) {
return fictioneer_notice(
__( 'The [fictioneer_story_section] shortcode requires the "Story Page" template.' ),
'warning',
false
);
}
// Transient?
if ( FICTIONEER_SHORTCODE_TRANSIENTS_ENABLED ) {
$base = serialize( $attr ) . $classes;
$transient_key = "fictioneer_shortcode_story_actions_html_" . md5( $base );
$transient = get_transient( $transient_key );
if ( ! empty( $transient ) ) {
return $transient;
}
}
// Buffer
ob_start();
// Setup post data
setup_postdata( $post );
// Start HTML ---> ?>
<section class="story__after-summary <?php echo esc_attr( $classes ); ?>">
<?php get_template_part( 'partials/_share-buttons' ); ?>
<div class="story__actions"><?php echo fictioneer_get_story_buttons( $hook_args ); ?></div>
</section>
<?php // <--- End HTML
// Store buffer
$html = fictioneer_minify_html( ob_get_clean() );
// Reset post data
wp_reset_postdata();
// Cache in Transient
if ( FICTIONEER_SHORTCODE_TRANSIENTS_ENABLED ) {
set_transient( $transient_key, $html, FICTIONEER_SHORTCODE_TRANSIENT_EXPIRATION );
}
// Return minified buffer
return $html;
}
add_shortcode( 'fictioneer_story_actions', 'fictioneer_shortcode_story_actions' );

View File

@ -18,10 +18,11 @@
defined( 'ABSPATH' ) OR exit;
// Setup
$post_type = get_post_type();
$post_id = get_the_ID();
$feed = fictioneer_get_rss_link();
$show_feed = get_post_type() !== 'fcn_story' ||
( get_post_type() === 'fcn_story' && get_post_meta( $post_id, 'fictioneer_story_status', true ) !== 'Oneshot' );
$feed = fictioneer_get_rss_link( $post_type );
$show_feed = $post_type !== 'fcn_story' ||
( $post_type === 'fcn_story' && get_post_meta( $post_id, 'fictioneer_story_status', true ) !== 'Oneshot' );
?>
@ -37,7 +38,7 @@ $show_feed = get_post_type() !== 'fcn_story' ||
<?php $feed_url = urlencode( $feed ); ?>
<?php if ( get_post_type() === 'fcn_story' && ! get_post_meta( $post_id, 'fictioneer_story_hidden', true ) ) : ?>
<?php if ( $post_type === 'fcn_story' && ! get_post_meta( $post_id, 'fictioneer_story_hidden', true ) ) : ?>
<a
href="<?php echo $feed; ?>"
class="rss-link tooltipped media-buttons__item"