Some refinements

Mostly formatting to satisfy my OCD, but I also fixed that footnotes were counted even if they did not exist. The sup element was also always rendered in the dialog.
This commit is contained in:
Tetrakern 2024-10-14 20:01:25 +02:00
parent 9328f155b3
commit 37d6da3d36
10 changed files with 73 additions and 44 deletions

View File

@ -587,8 +587,8 @@ Fires right after opening the articles `<footer>` container in the `single-fc
Fires immediately after the HTML output of the tooltip is created to collect tooltip content for footnotes in the `_setup-shortcodes.php` file.
**$args:**
* $footnote_id (int) Unique identifier for the footnote.
* $content (string) Content of the footnote.
* `footnote_id` (int) Unique identifier for the footnote.
* `content` (string) Content of the footnote.
**Hooked Actions:**
* `fictioneer_collect_footnote( $footnote_id, $content )` Collect a footnote to be stored in a global array for later rendering. Priority 10.

View File

@ -929,6 +929,16 @@ Filters the font array compiled from all valid font folders and Google Fonts lin
---
### `apply_filters( 'fictioneer_filter_footnotes', $footnotes )`
Filters the collection array of footnotes to be rendered.
Filters the font array compiled from all valid font folders and Google Fonts links in both the parent and child theme. Note that the `fictioneer_get_font_data()` function reads the file system, which is potentially slow.
**Parameters:**
* $footnotes (array) Array of footnotes (ID => HTML).
---
### `apply_filters( 'fictioneer_filter_header_image', $header_image_url, $post_id )`
Filters the URL of the header image in the `header.php` template.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -462,16 +462,17 @@ if ( FICTIONEER_ENABLE_STORY_DATA_META_CACHE ) {
// =============================================================================
// TOOLTIP FOOTNOTES
// =============================================================================
/**
* Collect a footnote to be stored for later rendering
* Collects a footnote to be stored for later rendering
*
* @since 5.25.0
*
* @param int $footnote_id Unique identifier for the footnote.
* @param string $content Content of the footnote.
* @param int $footnote_id Unique identifier for the footnote.
* @param string $content Content of the footnote.
*/
function fictioneer_collect_footnote( $footnote_id, $content )
{
function fictioneer_collect_footnote( $footnote_id, $content ) {
global $fictioneer_footnotes;
// Initialize footnotes array if it doesn't exist
@ -484,16 +485,16 @@ function fictioneer_collect_footnote( $footnote_id, $content )
}
/**
* Render collected footnotes at the end of the content
* Renders collected footnotes at the end of the content
*
* @since 5.25.0
*
* @param string $content The post content.
* @param string $content The post content.
*
* @return string The post content with appended footnotes.
*/
function fictioneer_append_footnotes_to_content( $content )
{
function fictioneer_append_footnotes_to_content( $content ) {
global $fictioneer_footnotes;
// Only proceed for single posts/pages with footnotes
@ -502,13 +503,16 @@ function fictioneer_append_footnotes_to_content( $content )
}
// Allow modifications to the collected footnotes
$fictioneer_footnotes = apply_filters( 'fictioneer_footnotes', $fictioneer_footnotes );
$fictioneer_footnotes = apply_filters( 'fictioneer_filter_footnotes', $fictioneer_footnotes );
$footnotes_section_title = __( 'Footnotes', 'fictioneer' );
$html = sprintf( '<h3>%s</h3>', esc_html( $footnotes_section_title ) );
// Generate the HTML for footnotes section
$html = sprintf(
'<h3>%s</h3>',
esc_html( __( 'Footnotes', 'fictioneer' ) )
);
// Generate the HTML for footnotes
$html .= '<ol class="footnotes list">';
foreach ( $fictioneer_footnotes as $id => $footnote ) {
$html .= sprintf(
'<li id="footnote-%1$d">%2$s <a href="#tooltip-%1$d">↑</a></li>',
@ -516,6 +520,7 @@ function fictioneer_append_footnotes_to_content( $content )
wp_kses_post( $footnote )
);
}
$html .= '</ol>';
// Reset the footnotes array
@ -528,4 +533,4 @@ function fictioneer_append_footnotes_to_content( $content )
if ( get_option( 'fictioneer_generate_footnotes_from_tooltips' ) ) {
add_action( 'fictioneer_collect_footnote', 'fictioneer_collect_footnote', 10, 2 );
add_filter( 'the_content', 'fictioneer_append_footnotes_to_content', 20 );
}
}

View File

@ -2354,28 +2354,31 @@ if ( ! get_option( 'fictioneer_disable_all_widgets' ) ) {
*
* @since 5.25.0
*
* @param string $atts['header'] Optional. Header of the tooltip.
* @param string $atts['content'] Content of the tooltip.
* @param boolean $atts['footnote'] Optional. Whether to show the footnote. Default true.
* @param string $content Shortcode content.
* @param string $atts['header'] Optional. Header of the tooltip.
* @param string $atts['content'] Content of the tooltip.
* @param bool $atts['footnote'] Optional. Whether to show the footnote. Default true.
* @param string $content Shortcode content.
*
* @return string HTML for the tooltip and associated footnote link.
*/
function fictioneer_shortcode_tooltip( $atts, $content = null )
{
function fictioneer_shortcode_tooltip( $atts, $content = null ) {
// Initialize a static counter for unique tooltip/footnote IDs
static $tooltip_id_counter = 0;
$tooltip_id_counter++;
$default_atts = [
'header' => '',
// Setup
$default_atts = array(
'header' => '',
'content' => '',
'footnote' => true,
];
);
$atts = shortcode_atts( $default_atts, $atts, 'fcnt' );
$footnote_allowed = get_option( 'fictioneer_generate_footnotes_from_tooltips' ) && $atts['footnote'];
$footnote_link = '';
// Sanitize user inputs
$tooltip_header = trim( wp_kses_post( $atts[ 'header' ] ) );
$tooltip_header = trim( wp_kses_post( $atts[ 'header' ] ) );
$tooltip_content = trim( wp_kses_post( $atts[ 'content' ] ) );
// Bail if no content
@ -2383,17 +2386,23 @@ function fictioneer_shortcode_tooltip( $atts, $content = null )
return $content;
}
// Create a footnote link to be appended to the tooltip content
$footnote_link = sprintf(
'<sup class="tooltip-counter"><a href="#footnote-%1$d">%1$d</a></sup>',
$tooltip_id_counter
);
// Increment counter
$tooltip_id_counter++;
// Prepare footnote if allowed
if ( $footnote_allowed ) {
// Create a footnote link to be appended to the tooltip content
$footnote_link = sprintf(
'<sup class="tooltip-counter"><a href="#footnote-%1$d" class="footnote-link">%1$d</a></sup>',
$tooltip_id_counter
);
}
// Prepare data attributes for the tooltip
$tooltip_data = [
'dialog-header' => $tooltip_header,
$tooltip_data = array(
'dialog-header' => $tooltip_header,
'dialog-content' => $tooltip_content . $footnote_link,
];
);
// Convert data array to HTML attributes
$tooltip_data_attributes = array_map(
@ -2416,12 +2425,11 @@ function fictioneer_shortcode_tooltip( $atts, $content = null )
$footnote_link
);
// Only if allowed
if ( $atts['footnote'] ) {
// Trigger action to collect footnote for later rendering
// Collect footnote if allowed
if ( $footnote_allowed ) {
do_action( 'fictioneer_collect_footnote', $tooltip_id_counter, $tooltip_content );
}
return $html;
}
add_shortcode( 'fcnt', 'fictioneer_shortcode_tooltip' );
add_shortcode( 'fcnt', 'fictioneer_shortcode_tooltip' );

View File

@ -1143,7 +1143,7 @@ function fictioneer_get_option_label( $option ) {
'fictioneer_disable_extended_story_list_meta_queries' => __( 'Disable extended story list meta queries', 'fictioneer' ),
'fictioneer_disable_extended_chapter_list_meta_queries' => __( 'Disable extended chapter list meta queries', 'fictioneer' ),
'fictioneer_count_characters_as_words' => __( 'Count characters instead of words', 'fictioneer' ),
'fictioneer_generate_footnotes_from_tooltips' => __( 'Generate Footnotes from Tooltips', 'fictioneer' ),
'fictioneer_generate_footnotes_from_tooltips' => __( 'Generate footnotes from tooltips', 'fictioneer' ),
'fictioneer_show_protected_excerpt' => __( 'Show excerpt on password-protected posts', 'fictioneer' ),
'fictioneer_hide_large_card_chapter_list' => __( 'Hide chapter list on large story cards', 'fictioneer' ),
'fictioneer_show_story_cards_latest_chapters' => __( 'Show latest chapter on large story cards', 'fictioneer' ),

View File

@ -386,7 +386,7 @@ $images = get_template_directory_uri() . '/img/documentation/';
<?php
fictioneer_settings_label_checkbox(
'fictioneer_generate_footnotes_from_tooltips',
__( 'Generate Footnotes from Tooltips', 'fictioneer' ),
__( 'Generate footnotes from tooltips', 'fictioneer' ),
__( 'Show footnotes at the end of the post content based on the tooltips.', 'fictioneer' )
);
?>

View File

@ -1292,7 +1292,7 @@ const fcn_chapterKeyboardNavigation = event => {
document.addEventListener('keydown', fcn_chapterKeyboardNavigation);
// =============================================================================
// SCROLL TO START OG CHAPTER
// SCROLL TO START OF CHAPTER
// =============================================================================
if (window.location.hash === '#start') {

View File

@ -510,3 +510,9 @@ a.modal-tooltip {
margin: 0 .875rem;
overflow: auto;
}
.footnote-link {
&:hover {
text-decoration: none !important;
}
}