Optimize templates

Reduce code and avoid some redundant calls of post_password_required().
This commit is contained in:
Tetrakern 2024-05-10 01:26:09 +02:00
parent f91c5c51d7
commit 2e25a6c7c0
10 changed files with 103 additions and 71 deletions

View File

@ -209,6 +209,7 @@ Fires in the second column of top action section in the `single-fcn_chapter.php`
* $chapter_id (int) The chapter ID.
* $chapter_title (string) Safe chapter title.
* $chapter_password (string) Chapter password or empty string.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
* $chapter_ids (array) IDs of visible chapters in the same story or empty array.
* $current_index (int) Current index in the chapters_id array.
* $prev_index (int|boolean) Index of previous chapter or false if outside bounds.
@ -234,6 +235,7 @@ Fires in the first column of the top action section in the `single-fcn_chapter.p
* $chapter_id (int) The chapter ID.
* $chapter_title (string) Safe chapter title.
* $chapter_password (string) Chapter password or empty string.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
* $chapter_ids (array) IDs of visible chapters in the same story or empty array.
* $current_index (int) Current index in the chapters_id array.
* $prev_index (int|boolean) Index of previous chapter or false if outside bounds.
@ -257,6 +259,7 @@ Fires in the third column of the top action section in the `single-fcn_chapter.p
* $chapter_id (int) The chapter ID.
* $chapter_title (string) Safe chapter title.
* $chapter_password (string) Chapter password or empty string.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
* $chapter_ids (array) IDs of visible chapters in the same story or empty array.
* $current_index (int) Current index in the chapters_id array.
* $prev_index (int|boolean) Index of previous chapter or false if outside bounds.
@ -280,6 +283,7 @@ Fires in the second column of the bottom action section in the `single-fcn_chapt
* $chapter_id (int) The chapter ID.
* $chapter_title (string) Safe chapter title.
* $chapter_password (string) Chapter password or empty string.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
* $chapter_ids (array) IDs of visible chapters in the same story or empty array.
* $current_index (int) Current index in the chapters_id array.
* $prev_index (int|boolean) Index of previous chapter or false if outside bounds.
@ -305,6 +309,7 @@ Fires in the first column of the bottom action section in the `single-fcn_chapte
* $chapter_id (int) The chapter ID.
* $chapter_title (string) Safe chapter title.
* $chapter_password (string) Chapter password or empty string.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
* $chapter_ids (array) IDs of visible chapters in the same story or empty array.
* $current_index (int) Current index in the chapters_id array.
* $prev_index (int|boolean) Index of previous chapter or false if outside bounds.
@ -328,6 +333,7 @@ Fires in the third column of the bottom action section in the `single-fcn_chapte
* $chapter_id (int) The chapter ID.
* $chapter_title (string) Safe chapter title.
* $chapter_password (string) Chapter password or empty string.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
* $chapter_ids (array) IDs of visible chapters in the same story or empty array.
* $current_index (int) Current index in the chapters_id array.
* $prev_index (int|boolean) Index of previous chapter or false if outside bounds.
@ -351,6 +357,7 @@ Fires right after the article header (with story, title, and authors) in the `si
* $chapter_id (int) The chapter ID.
* $chapter_title (string) Safe chapter title.
* $chapter_password (string) Chapter password or empty string.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
* $chapter_ids (array) IDs of visible chapters in the same story or empty array.
* $current_index (int) Current index in the chapters_id array.
* $prev_index (int|boolean) Index of previous chapter or false if outside bounds.
@ -368,6 +375,7 @@ Fires right after the content section in the `single-fcn_chapter.php` template,
* $chapter_id (int) The chapter ID.
* $chapter_title (string) Safe chapter title.
* $chapter_password (string) Chapter password or empty string.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
* $chapter_ids (array) IDs of visible chapters in the same story or empty array.
* $current_index (int) Current index in the chapters_id array.
* $prev_index (int|boolean) Index of previous chapter or false if outside bounds.
@ -389,6 +397,7 @@ Fires right after the `<main>` container is closed in the `single-fcn_chapter.ph
* $chapter_id (int) The chapter ID.
* $chapter_title (string) Safe chapter title.
* $chapter_password (string) Chapter password or empty string.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
* $chapter_ids (array) IDs of visible chapters in the same story or empty array.
* $current_index (int) Current index in the chapters_id array.
* $prev_index (int|boolean) Index of previous chapter or false if outside bounds.
@ -411,6 +420,7 @@ Fires between the top actions sections and chapter header (title and authors) in
* $chapter_id (int) The chapter ID.
* $chapter_title (string) Safe chapter title.
* $chapter_password (string) Chapter password or empty string.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
* $chapter_ids (array) IDs of visible chapters in the same story or empty array.
* $current_index (int) Current index in the chapters_id array.
* $prev_index (int|boolean) Index of previous chapter or false if outside bounds.
@ -433,6 +443,7 @@ Fires just before the comments section in the `single-fcn_chapter.php` template
* $chapter_id (int) The chapter ID.
* $chapter_title (string) Safe chapter title.
* $chapter_password (string) Chapter password or empty string.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
* $chapter_ids (array) IDs of visible chapters in the same story or empty array.
* $current_index (int) Current index in the chapters_id array.
* $prev_index (int|boolean) Index of previous chapter or false if outside bounds.
@ -1034,6 +1045,7 @@ Fires right after the `<article>` container in the `single-fcn_story.php` templa
**$args:**
* $story_data (array) Collection of story data.
* $story_id (int) Current story (post) ID.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
**Hooked Actions:**
* `fictioneer_story_comments( $args )` AJAX-loaded list of all comments for story chapters. Priority 10.
@ -1046,6 +1058,7 @@ Fires right after the content section in the `single-fcn_story.php` template, in
**$args:**
* $story_data (array) Collection of story data.
* $story_id (int) Current story (post) ID.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
**Hooked Actions:**
* `fictioneer_story_copyright_notice( $args )` Copyright notice. Priority 10.
@ -1067,6 +1080,7 @@ Fires right after the article header (cover, title, fandom, genres, and characte
**$args:**
* $story_data (array) Collection of story data.
* $story_id (int) Current story (post) ID.
* $password_required (boolean|null) Whether the post is unlocked or not. Unsafe.
---

View File

@ -1591,7 +1591,7 @@ function fictioneer_shortcode_story_section( $attr ) {
$show_pages = filter_var( $attr['pages'] ?? 0, FILTER_VALIDATE_BOOLEAN );
$show_blog = filter_var( $attr['blog'] ?? 0, FILTER_VALIDATE_BOOLEAN );
$show_scheduled = filter_var( $attr['scheduled'] ?? 0, FILTER_VALIDATE_BOOLEAN );
$hook_args = array( 'story_id' => $story_id, 'story_data' => $story_data );
$hook_args = array( 'story_id' => $story_id, 'story_data' => $story_data, 'password_required' => post_password_required() );
// Abort if...
if ( ! $story_data ) {

View File

@ -941,6 +941,7 @@ function fictioneer_add_custom_scripts() {
// Setup
$post_type = get_post_type();
$cache_bust = fictioneer_get_cache_bust();
$password_required = post_password_required();
$strategy = fictioneer_compare_wp_version( '6.3' ) && FICTIONEER_DEFER_SCRIPTS
? array( 'strategy' => 'defer' ) : true; // Defer or load in footer
@ -1035,7 +1036,7 @@ function fictioneer_add_custom_scripts() {
get_option( 'fictioneer_enable_suggestions' ) &&
! is_archive() &&
! is_search() &&
! post_password_required() &&
! $password_required &&
! get_post_meta( get_the_ID(), 'fictioneer_disable_commenting', true ) &&
comments_open()
) {
@ -1049,7 +1050,7 @@ function fictioneer_add_custom_scripts() {
get_option( 'fictioneer_enable_tts' ) &&
! is_archive() &&
! is_search() &&
! post_password_required()
! $password_required
) {
wp_enqueue_script( 'fictioneer-tts-scripts' );
}

View File

@ -92,7 +92,8 @@ add_action( 'fictioneer_chapters_after_content', 'fictioneer_chapters_list', 30
*
* @since 5.11.1
*
* @param WP_Post|null $args['story_post'] The story post object.
* @param WP_Post|null $args['story_post'] The story post object.
* @param bool|null $args['password_required'] Whether the post is unlocked or not.
*/
function fictioneer_chapter_global_note( $args ) {
@ -103,11 +104,12 @@ function fictioneer_chapter_global_note( $args ) {
// Setup
$note = fictioneer_get_content_field( 'fictioneer_story_global_note', $args['story_post']->ID );
$password_required = $args['password_required'] ?? post_password_required();
// Abort conditions
if (
empty( $note ) ||
( strpos( $note, '[!password]' ) !== false && post_password_required() )
( strpos( $note, '[!password]' ) !== false && $password_required )
) {
return;
}
@ -132,17 +134,19 @@ add_action( 'fictioneer_chapter_before_header', 'fictioneer_chapter_global_note'
* @since 5.0.0
* @since 5.11.1 - Show if started with "[!show]".
*
* @param int $args['chapter_id'] The chapter ID.
* @param int $args['chapter_id'] The chapter ID.
* @param bool|null $args['password_required'] Whether the post is unlocked or not.
*/
function fictioneer_chapter_foreword( $args ) {
// Setup
$note = fictioneer_get_content_field( 'fictioneer_chapter_foreword', $args['chapter_id'] );
$password_required = $args['password_required'] ?? post_password_required();
// Abort conditions
if (
empty( $note ) ||
( strpos( $note, '[!show]' ) === false && post_password_required() )
( strpos( $note, '[!show]' ) === false && $password_required )
) {
return;
}
@ -166,16 +170,18 @@ add_action( 'fictioneer_chapter_before_header', 'fictioneer_chapter_foreword', 1
*
* @since 5.0.0
*
* @param int $args['chapter_id'] The chapter ID.
* @param int $args['chapter_id'] The chapter ID.
* @param bool|null $args['password_required'] Whether the post is unlocked or not.
*/
function fictioneer_chapter_warnings( $args ) {
// Setup
$warning = get_post_meta( $args['chapter_id'], 'fictioneer_chapter_warning', true );
$warning_notes = get_post_meta( $args['chapter_id'], 'fictioneer_chapter_warning_notes', true );
$password_required = $args['password_required'] ?? post_password_required();
// Abort conditions
if ( ( ! $warning && ! $warning_notes ) || post_password_required() ) {
if ( ( ! $warning && ! $warning_notes ) || $password_required ) {
return '';
}
@ -453,17 +459,19 @@ add_action( 'fictioneer_chapter_actions_bottom_left', 'fictioneer_chapter_media_
* @since 5.0.0
* @since 5.11.1 - Show if started with "[!show]".
*
* @param int $args['chapter_id'] The chapter ID.
* @param int $args['chapter_id'] The chapter ID.
* @param bool|null $args['password_required'] Whether the post is unlocked or not.
*/
function fictioneer_chapter_afterword( $args ) {
// Setup
$note = fictioneer_get_content_field( 'fictioneer_chapter_afterword', $args['chapter_id'] );
$password_required = $args['password_required'] ?? post_password_required();
// Abort conditions
if (
empty( $note ) ||
( strpos( $note, '[!show]' ) === false && post_password_required() )
( strpos( $note, '[!show]' ) === false && $password_required )
) {
return;
}

View File

@ -335,6 +335,7 @@ function fictioneer_mobile_user_menu() {
$discord_link = get_option( 'fictioneer_discord_invite_link' );
$profile_link = get_edit_profile_url();
$profile_page_id = intval( get_option( 'fictioneer_user_profile_page', -1 ) ?: -1 );
$password_required = post_password_required();
$output = [];
if ( $profile_page_id && $profile_page_id > 0 ) {
@ -408,7 +409,7 @@ function fictioneer_mobile_user_menu() {
! is_search() &&
comments_open() &&
! fictioneer_is_commenting_disabled() &&
! post_password_required()
! $password_required
) {
$output['comment_jump'] = sprintf(
'<a id="mobile-menu-comment-jump" class="comments-toggle" rel="noopener noreferrer nofollow"><i class="fa-solid fa-comments mobile-menu__item-icon"></i> %s</a>',
@ -420,7 +421,7 @@ function fictioneer_mobile_user_menu() {
$post_type === 'fcn_chapter' &&
! is_search() &&
get_option( 'fictioneer_enable_bookmarks' ) &&
! post_password_required()
! $password_required
) {
$output['bookmark_jump'] = sprintf(
'<a id="mobile-menu-bookmark-jump" rel="noopener noreferrer nofollow" hidden><i class="fa-solid fa-bookmark mobile-menu__item-icon"></i> %s</a>',

View File

@ -241,13 +241,14 @@ add_action( 'fictioneer_story_after_content', 'fictioneer_story_tags_and_warning
*
* @since 5.0.0
*
* @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['password_required'] Whether the post is unlocked or not.
*/
function fictioneer_story_actions( $args ) {
// Abort conditions...
if ( post_password_required() ) {
if ( $args['password_required'] ?? post_password_required() ) {
return;
}
@ -269,13 +270,14 @@ add_action( 'fictioneer_story_after_content', 'fictioneer_story_actions', 30 );
*
* @since 5.9.0
*
* @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['password_required'] Whether the post is unlocked or not.
*/
function fictioneer_story_tabs( $args ) {
// Abort conditions...
if ( post_password_required() ) {
if ( $args['password_required'] ?? post_password_required() ) {
return;
}
@ -362,13 +364,14 @@ add_action( 'fictioneer_story_after_content', 'fictioneer_story_tabs', 40 );
*
* @since 5.9.0
*
* @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['password_required'] Whether the post is unlocked or not.
*/
function fictioneer_story_scheduled_chapter( $args ) {
// Abort conditions...
if ( post_password_required() ) {
if ( $args['password_required'] ?? post_password_required() ) {
return;
}
@ -421,13 +424,14 @@ add_action( 'fictioneer_story_after_content', 'fictioneer_story_scheduled_chapte
*
* @since 5.9.0
*
* @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['password_required'] Whether the post is unlocked or not.
*/
function fictioneer_story_pages( $args ) {
// Abort conditions...
if ( post_password_required() ) {
if ( $args['password_required'] ?? post_password_required() ) {
return;
}
@ -478,17 +482,14 @@ add_action( 'fictioneer_story_after_content', 'fictioneer_story_pages', 42 );
*
* @since 5.9.0
*
* @param array $args {
* Array of arguments.
*
* @type array $story_data Pre-compiled array of story data.
* @type int $story_id ID of the story.
* }
* @param array $args['story_data'] Collection of story data.
* @param int $args['story_id'] The story post ID.
* @param bool|null $args['password_required'] Whether the post is unlocked or not.
*/
function fictioneer_story_chapters( $args ) {
// Abort conditions...
if ( post_password_required() ) {
if ( $args['password_required'] ?? post_password_required() ) {
return;
}
@ -745,13 +746,14 @@ add_action( 'fictioneer_story_after_content', 'fictioneer_story_chapters', 43 );
*
* @since 5.9.0
*
* @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['password_required'] Whether the post is unlocked or not.
*/
function fictioneer_story_blog( $args ) {
// Abort conditions...
if ( post_password_required() ) {
if ( $args['password_required'] ?? post_password_required() ) {
return;
}

View File

@ -13,17 +13,18 @@
<?php
// Setup
$password_required = post_password_required();
// Header
$header_args = array(
'type' => 'fcn_chapter'
get_header(
null,
array(
'type' => 'fcn_chapter',
'no_index' => get_post_meta( get_the_ID(), 'fictioneer_chapter_hidden', true ) ? 1 : 0
)
);
if ( get_post_meta( get_the_ID(), 'fictioneer_chapter_hidden', true ) ) {
$header_args['no_index'] = true;
}
get_header( null, $header_args );
?>
<div class="progress">
@ -42,7 +43,7 @@ get_header( null, $header_args );
<?php do_action( 'fictioneer_main_wrapper' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
// Setup
@ -85,6 +86,7 @@ get_header( null, $header_args );
'chapter_id' => $post->ID,
'chapter_title' => $title,
'chapter_password' => $post->post_password,
'password_required' => $password_required,
'chapter_ids' => $chapter_ids,
'indexed_chapter_ids' => $indexed_chapters,
'current_index' => $current_index,
@ -135,7 +137,7 @@ get_header( null, $header_args );
</div>
<?php endif; ?>
<article id="ch-<?php the_ID(); ?>" data-author-id="<?php echo get_the_author_meta( 'ID' ); ?>" class="chapter__article padding-left padding-right <?php echo post_password_required() ? '_password' : ''; ?>" data-age-rating="<?php echo strtolower( $age_rating ); ?>">
<article id="ch-<?php the_ID(); ?>" data-author-id="<?php echo get_the_author_meta( 'ID' ); ?>" class="chapter__article padding-left padding-right <?php echo $password_required ? '_password' : ''; ?>" data-age-rating="<?php echo strtolower( $age_rating ); ?>">
<div class="chapter__actions chapter__actions--top" data-nosnippet>
<div class="chapter__actions-container chapter__actions-left"><?php
@ -162,7 +164,7 @@ get_header( null, $header_args );
// Password note
$password_note = fictioneer_get_content_field( 'fictioneer_chapter_password_note', $post->ID );
if ( $story_post && post_password_required() && empty( $password_note ) ) {
if ( $story_post && $password_required && empty( $password_note ) ) {
$password_note = fictioneer_get_content_field( 'fictioneer_story_password_note', $story_id );
if ( ! empty( $password_note ) && strpos( $password_note, '[!global]' ) !== false ) {
@ -174,15 +176,13 @@ get_header( null, $header_args );
?>
<section id="chapter-content" class="chapter__content content-section"><?php
if ( post_password_required() ) {
if ( $password_note ) {
echo '<div class="chapter__password-note infobox">' . $password_note . '</div>';
}
if ( $password_required && $password_note ) {
echo '<div class="chapter__password-note infobox">' . $password_note . '</div>';
}
echo '<div class="resize-font chapter-formatting chapter-font-color chapter-font-family">';
if ( post_password_required() && get_option( 'fictioneer_show_protected_excerpt' ) ) {
if ( $password_required && get_option( 'fictioneer_show_protected_excerpt' ) ) {
echo '<p class="chapter__forced-excerpt">' . fictioneer_get_forced_excerpt( $post->ID, 512 ) . '</p>';
}
@ -214,13 +214,13 @@ get_header( null, $header_args );
<?php do_action( 'fictioneer_before_comments' ); ?>
<?php if ( comments_open() && ! post_password_required() ) : ?>
<?php if ( comments_open() && ! $password_required ) : ?>
<section class="chapter__comments comment-section padding-left padding-right padding-bottom chapter-comments-hideable">
<?php comments_template(); ?>
</section>
<?php endif; ?>
<?php endwhile; endif; ?>
<?php endwhile; ?>
</div>
@ -228,7 +228,7 @@ get_header( null, $header_args );
<?php
// After chapter main; includes the micro menu, paragraph tools, and suggestion tools
if ( ! post_password_required() ) {
if ( ! $password_required ) {
do_action( 'fictioneer_chapter_after_main', $hook_args );
}
?>

View File

@ -13,6 +13,9 @@
<?php
// Setup
$password_required = post_password_required();
// Header
get_header( null, array( 'type' => 'fcn_recommendation' ) );
@ -52,19 +55,19 @@ get_header( null, array( 'type' => 'fcn_recommendation' ) );
get_template_part( 'partials/_recommendation-header', null, $hook_args );
// Hook after header
if ( ! post_password_required() ) {
if ( ! $password_required ) {
do_action( 'fictioneer_recommendation_after_header', $hook_args );
}
// Thumbnail
if ( has_post_thumbnail() && ! post_password_required() ) {
if ( has_post_thumbnail() && ! $password_required ) {
echo fictioneer_get_recommendation_page_cover( $post );
}
?>
<section class="recommendation__content content-section"><?php the_content(); ?></section>
<?php if ( ! post_password_required() ) : ?>
<?php if ( ! $password_required ) : ?>
<?php do_action( 'fictioneer_recommendation_after_content', $hook_args ); ?>

View File

@ -17,20 +17,19 @@
// Setup
$post_id = $args['post_id'] ?? get_the_ID();
$post = get_post( $post_id );
$can_checkmarks = get_option( 'fictioneer_enable_checkmarks' );
$header_args = array(
'type' => 'fcn_story'
$password_required = post_password_required();
get_header(
null,
array(
'type' => 'fcn_story',
'no_index' => get_post_meta( $post_id, 'fictioneer_story_hidden', true ) ? 1 : 0
)
);
if ( get_post_meta( $post_id, 'fictioneer_story_hidden', true ) ) {
$header_args['no_index'] = true;
}
get_header( null, $header_args );
?>
<main id="main" class="main story <?php if ( ! $can_checkmarks ) echo '_no-checkmarks'; ?>">
<main id="main" class="main story <?php echo get_option( 'fictioneer_enable_checkmarks' ) ? '' : '_no-checkmarks'; ?>">
<div class="observer main-observer"></div>
@ -54,7 +53,8 @@ get_header( null, $header_args );
// Arguments for hooks and templates/etc.
$hook_args = array(
'story_data' => $story,
'story_id' => $story_id
'story_id' => $story_id,
'password_required' => $password_required
);
?>
@ -69,7 +69,7 @@ get_header( null, $header_args );
?>
<section class="story__summary padding-left padding-right"><?php
if ( post_password_required() ) {
if ( $password_required ) {
if ( $password_note ) {
echo '<div class="story__password-note infobox">' . $password_note . '</div>';
}

View File

@ -10,6 +10,9 @@
<?php
// Setup
$password_required = post_password_required();
// Header
get_header();
@ -46,7 +49,7 @@ get_header();
<?php do_action( 'fictioneer_post_after_content', $post->ID ); ?>
<?php if ( ! post_password_required() && ( has_action( 'fictioneer_post_footer_left' ) || has_action( 'fictioneer_post_footer_right' ) ) ) : ?>
<?php if ( ! $password_required && ( has_action( 'fictioneer_post_footer_left' ) || has_action( 'fictioneer_post_footer_right' ) ) ) : ?>
<footer class="post__footer">
<div class="post__footer-box post__footer-left">
<?php do_action( 'fictioneer_post_footer_left', $post->ID ); ?>
@ -61,7 +64,7 @@ get_header();
<?php do_action( 'fictioneer_before_comments' ); ?>
<?php if ( comments_open() && ! post_password_required() ) : ?>
<?php if ( comments_open() && ! $password_required ) : ?>
<section class="post__comments comment-section padding-left padding-right padding-bottom">
<?php comments_template(); ?>
</section>