1, 2, 3 progress

This commit is contained in:
iniznet 2024-10-14 23:28:46 +07:00
parent 3459ed1531
commit 66e0bfdb44
4 changed files with 130 additions and 114 deletions

View File

@ -458,3 +458,74 @@ if ( FICTIONEER_ENABLE_STORY_DATA_META_CACHE ) {
add_action( 'wp_insert_comment', 'fictioneer_increment_story_comment_count' );
add_action( 'delete_comment', 'fictioneer_decrement_story_comment_count' );
}
// =============================================================================
// TOOLTIP FOOTNOTES
// =============================================================================
/**
* Collect 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.
*/
function fictioneer_collect_footnote( $footnote_id, $content )
{
global $fictioneer_footnotes;
// Initialize footnotes array if it doesn't exist
if ( ! is_array( $fictioneer_footnotes ) ) {
$fictioneer_footnotes = [];
}
// Store footnote content with its ID
$fictioneer_footnotes[ $footnote_id ] = $content;
}
/**
* Render collected footnotes at the end of the content
*
* @since 5.25.0
*
* @param string $content The post content.
*
* @return string The post content with appended footnotes.
*/
function fictioneer_append_footnotes_to_content( $content )
{
global $fictioneer_footnotes;
// Only proceed for single posts/pages with footnotes
if ( ! is_singular() || empty( $fictioneer_footnotes ) ) {
return $content;
}
// Allow modifications to the collected footnotes
$fictioneer_footnotes = apply_filters( 'fictioneer_footnotes', $fictioneer_footnotes );
$footnotes_section_title = __( 'Footnotes', 'fictioneer' );
$html = sprintf( '<h3>%s</h3>', esc_html( $footnotes_section_title ) );
// 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>',
$id,
wp_kses_post( $footnote )
);
}
$html .= '</ol>';
// Reset the footnotes array
$fictioneer_footnotes = [];
// Append footnotes to the content
return $content . $html;
}
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

@ -2346,149 +2346,77 @@ if ( ! get_option( 'fictioneer_disable_all_widgets' ) ) {
}
// =============================================================================
// TOOLTIP SHORTCODE
// TOOLTIP AND FOOTNOTE SHORTCODE
// =============================================================================
/**
* Shortcode to add tooltip modal
*
* Shortcode to add a tooltip with an associated footnote
*
* @since 5.25.0
*
* @param string $atts['header'] Optional. Header of the tooltip.
* @param string $atts['content'] Content of the tooltip.
* @param string $content Shortcode content.
*
* @return string The shortcode HTML.
*
* @return string HTML for the tooltip and associated footnote link.
*/
function fictioneer_shortcode_tooltip( $atts, $content = null ) {
// Setup
static $tooltip_count = 0;
$tooltip_count++;
function fictioneer_create_tooltip_with_footnote( $atts, $content = null )
{
// Initialize a static counter for unique tooltip/footnote IDs
static $tooltip_id_counter = 0;
$tooltip_id_counter++;
$attributes = shortcode_atts(
array(
'header' => '',
'content' => '',
),
$atts,
'fcnt'
);
$default_atts = [
'header' => '',
'content' => '',
];
$atts = shortcode_atts( $default_atts, $atts, 'fcnt' );
// Sanitize attributes
$modal_header = trim( wp_kses_post( $attributes['header'] ) );
$modal_content = trim( wp_kses_post( $attributes['content'] ) );
// Sanitize user inputs
$tooltip_header = trim( wp_kses_post( $atts[ 'header' ] ) );
$tooltip_content = trim( wp_kses_post( $atts[ 'content' ] ) );
// Bail if no content
if ( empty( $modal_content ) ) {
if ( empty( $tooltip_content ) ) {
return $content;
}
// Add footnote number to the 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_count
);
$modal_content_with_number = $modal_content . $footnote_link;
// Prepare data attributes
$data = array(
'dialog-header' => $modal_header,
'dialog-content' => $modal_content_with_number,
$tooltip_id_counter
);
// Build attributes
$data_attributes = array_map(
function( $key, $value ) {
return sprintf(
'data-%s="%s"',
esc_attr( $key ),
esc_attr( $value )
);
// Prepare data attributes for the tooltip
$tooltip_data = [
'dialog-header' => $tooltip_header,
'dialog-content' => $tooltip_content . $footnote_link,
];
// Convert data array to HTML attributes
$tooltip_data_attributes = array_map(
function ( $key, $value ) {
return sprintf( 'data-%s="%s"', esc_attr( $key ), esc_attr( $value ) );
},
array_keys( $data ),
$data
array_keys( $tooltip_data ),
$tooltip_data
);
// Filter title
$title = apply_filters( 'fictioneer_tooltip_title', _x( 'Click to see note', 'Tooltip shortcode.', 'fictioneer' ) );
$tooltip_title = _x( 'Click to see note', 'Tooltip shortcode.', 'fictioneer' );
// Build tooltip
// Construct the HTML for the tooltip
$html = sprintf(
'<div><a id="tooltip-%1$d" class="modal-tooltip" title="%2$s" %3$s data-click-action="open-tooltip-modal">%4$s</a>%5$s</div>',
$tooltip_count,
esc_attr( $title ),
implode( ' ', $data_attributes ),
'<span><a id="tooltip-%1$d" class="modal-tooltip" title="%2$s" %3$s data-click-action="open-tooltip-modal">%4$s</a>%5$s</span>',
$tooltip_id_counter,
esc_attr( $tooltip_title ),
implode( ' ', $tooltip_data_attributes ),
$content,
$footnote_link
);
do_action( 'fictioneer_after_tooltip_shortcode', $tooltip_count, $modal_content );
// Trigger action to collect footnote for later rendering
do_action( 'fictioneer_collect_footnote', $tooltip_id_counter, $tooltip_content );
return $html;
}
add_shortcode( 'fcnt', 'fictioneer_shortcode_tooltip' );
/**
* Store footnotes for later use
*
* @since 5.25.0
*
* @param int $tooltip_count Tooltip counter.
* @param string $content Footnote content.
*
* @return void
*/
function fictioneer_store_footnote( $tooltip_count, $content ) {
global $fictioneer_footnotes;
// Initialize footnotes array
if ( ! is_array( $fictioneer_footnotes ) ) {
$fictioneer_footnotes = array();
}
// Store footnote
$fictioneer_footnotes[ $tooltip_count ] = $content;
}
add_action( 'fictioneer_after_tooltip_shortcode', 'fictioneer_store_footnote', 10, 2 );
/**
* Renders the footnotes after the content
*
* @since 5.25.0
*
* @param string $content The post content.
*
* @return string The post content with footnotes.
*/
function fictioneer_render_footnotes( $content ) {
global $fictioneer_footnotes;
// Bail if it's not a singular post or there are no footnotes
if ( ! is_singular() || empty( $fictioneer_footnotes ) ) {
return $content;
}
// Filter footnotes
$fictioneer_footnotes = apply_filters( 'fictioneer_footnotes', $fictioneer_footnotes );
// Filter title
$footnotes_title = apply_filters( 'fictioneer_footnotes_title', __( 'Footnotes', 'fictioneer' ) );
$output = sprintf( '<h3>%s</h3>', esc_html( $footnotes_title ) );
// Build footnotes
$output .= '<ol class="footnotes list">';
foreach ( $fictioneer_footnotes as $key => $note ) {
$output .= sprintf(
'<li id="footnote-%1$d">%2$s <a href="#tooltip-%1$d">↑</a></li>',
$key,
wp_kses_post( $note )
);
}
$output .= '</ol>';
// Clear footnotes
$fictioneer_footnotes = array();
return $content . $output;
}
add_filter( 'the_content', 'fictioneer_render_footnotes', 20 );
add_shortcode( 'fcnt', 'fictioneer_create_tooltip_with_footnote' );

View File

@ -582,6 +582,12 @@ define( 'FICTIONEER_OPTIONS', array(
'sanitize_callback' => 'fictioneer_sanitize_checkbox',
'default' => 0
),
'fictioneer_generate_footnotes_from_tooltips' => array(
'name' => 'fictioneer_generate_footnotes_from_tooltips',
'group' => 'fictioneer-settings-general-group',
'sanitize_callback' => 'fictioneer_sanitize_checkbox',
'default' => 0
),
'fictioneer_show_protected_excerpt' => array(
'name' => 'fictioneer_show_protected_excerpt',
'group' => 'fictioneer-settings-general-group',
@ -1137,6 +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_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

@ -382,6 +382,16 @@ $images = get_template_directory_uri() . '/img/documentation/';
?>
</div>
<div class="fictioneer-card__row">
<?php
fictioneer_settings_label_checkbox(
'fictioneer_generate_footnotes_from_tooltips',
__( 'Generate Footnotes from Tooltips', 'fictioneer' ),
__( 'Show footnotes at the end of the post content based on the tooltips.', 'fictioneer' )
);
?>
</div>
<div class="fictioneer-card__row fictioneer-card__row--inline-input">
<p class="fictioneer-inline-text-input"><?php
printf(