Add story-wide note displayed in all chapters

This commit is contained in:
Tetrakern 2024-02-22 23:57:56 +01:00
parent 8e671b37e1
commit a03e396b90
3 changed files with 57 additions and 1 deletions

View File

@ -408,6 +408,7 @@ Fires between the top actions sections and chapter header (title and authors) in
* $next_index (int|boolean) Index of next chapter or false if outside bounds.
**Hooked actions:**
* `fictioneer_chapter_global_note( $args )` Story-wide note if provided. Priority 5.
* `fictioneer_chapter_foreword( $args )` Chapter foreword if provided. Priority 10.
* `fictioneer_chapter_warnings( $args )` Chapter warnings if provided. Priority 20.

View File

@ -1980,6 +1980,16 @@ function fictioneer_render_story_data_metabox( $post ) {
);
}
// Global note
$output['fictioneer_story_global_note'] = fictioneer_get_metabox_editor(
$post,
'fictioneer_story_global_note',
array(
'label' => _x( 'Global Note', 'Story global note meta field label.', 'fictioneer' ),
'description' => __( 'Displayed in a box above all chapters; include "[!password]" to hide in protected chapters. Limited HTML allowed.', 'fictioneer' )
)
);
// Password note
$output['fictioneer_story_password_note'] = fictioneer_get_metabox_editor(
$post,
@ -2314,6 +2324,12 @@ function fictioneer_save_story_metaboxes( $post_id ) {
}
}
// Global note
if ( isset( $_POST['fictioneer_story_global_note'] ) ) {
$fields['fictioneer_story_global_note'] =
fictioneer_sanitize_editor( $_POST['fictioneer_story_global_note'] );
}
// Password note
if ( isset( $_POST['fictioneer_story_password_note'] ) ) {
$fields['fictioneer_story_password_note'] =

View File

@ -83,6 +83,45 @@ function fictioneer_chapters_list( $args ) {
}
add_action( 'fictioneer_chapters_after_content', 'fictioneer_chapters_list', 30 );
// =============================================================================
// CHAPTER GLOBAL NOTE
// =============================================================================
/**
* Outputs the HTML for the chapter foreword section
*
* @since 5.11.1
*
* @param WP_Post|null $args['story_post'] The story post object.
*/
function fictioneer_chapter_global_note( $args ) {
// Guard
if ( ! ( $args['story_post'] ?? 0 ) ) {
return;
}
// Setup
$note = fictioneer_get_content_field( 'fictioneer_story_global_note', $args['story_post']->ID );
// Abort conditions
if (
empty( $note ) ||
( strpos( $note, '[!password]' ) !== false && post_password_required() )
) {
return;
}
$note = str_replace( '[!password]', '', $note );
// Start HTML ---> ?>
<section id="chapter-global-note" class="chapter__global-note infobox polygon clearfix chapter-note-hideable"><?php
echo trim( $note );
?></section>
<?php // <--- End HTML
}
add_action( 'fictioneer_chapter_before_header', 'fictioneer_chapter_global_note', 5 );
// =============================================================================
// CHAPTER FOREWORD
// =============================================================================
@ -101,7 +140,7 @@ function fictioneer_chapter_foreword( $args ) {
// Abort conditions
if ( empty( $foreword ) || post_password_required() ) {
return '';
return;
}
// Start HTML ---> ?>