From d7434e89f7c7c4fa35e503e90c0f7e0b31089031 Mon Sep 17 00:00:00 2001 From: kebbet Date: Wed, 22 Sep 2021 07:58:56 +0200 Subject: [PATCH] Separates counter from display function for footnote numbers. --- src/shortcode.php | 66 ++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/src/shortcode.php b/src/shortcode.php index 8408617..a807717 100644 --- a/src/shortcode.php +++ b/src/shortcode.php @@ -12,51 +12,69 @@ defined( 'ABSPATH' ) or exit; /** - * Replace content between the shortcode and count number of footnotes. + * Return markup for the `sup`-element. * - * @param array $attributes The attributes. - * @param string $content The content of each shortcode. + * @since 20210922.1 + * + * @param int $number The number that replaces the shortcode content. + * @param string $content The content between shortcode-tags that can be places in a title-attribute. * @return string */ -function replace_content_with_sup( $attributes, $content ) { - - // Count number of notes in each post. - global $counter; - - $attributes = ''; - $note_number = intval( 1 ); +function display( $number, $content ) { + $link_title = ''; + $note_link = \kebbet\shortcode\footnotes\helpers\link_id( $number, true, true ); $sup_class = apply_filters( 'kebbet_shortcode_footnote_note_class', 'footnotes-footnote' ); $sup_id = ''; - $target = \kebbet\shortcode\footnotes\helpers\get_post_scope_id(); - $first_item = ! isset( $counter[$target] ); - - // Which footnote is this? - if ( ! $first_item ) { - $find_max = max( $counter[$target]['ref'] ); - $note_number = $find_max + intval( 1 ); - } - - $counter[$target]['ref'][] = $note_number; - $note_link = \kebbet\shortcode\footnotes\helpers\link_id( $note_number, true, true ); // Add optional title attribute to link element. if ( true === \kebbet\shortcode\footnotes\settings\title_attributes() ) { $content = do_shortcode( $content ); // Render out any shortcode within the contents. $content = \kebbet\shortcode\footnotes\helpers\strip_paragraph( $content ); $content = str_replace( '"', '"', strip_tags( $content ) ); - $attributes = ' title="' . esc_attr( $content ) . '"'; + $link_title = ' title="' . esc_attr( $content ) . '"'; } // Add back links if enabled. if ( true === \kebbet\shortcode\footnotes\settings\back_link() ) { - $source_id = \kebbet\shortcode\footnotes\helpers\link_id( $note_number, false, false ); + $source_id = \kebbet\shortcode\footnotes\helpers\link_id( $number, false, false ); $sup_id = ' id="' . esc_attr( $source_id ) . '"'; } // Build the `sup`-element. $sup_content = ''; - $sup_content .= '' . esc_attr( $note_number ) . ''; + $sup_content .= '' . esc_attr( $number ) . ''; $sup_content .= ''; return $sup_content; } + +/** + * Count number of footnotes and replace content between the shortcode tags. + * + * @param array $attributes The attributes. + * @param string $content The content of each shortcode. + * @return string + */ +function replace_content_with_sup( $attributes, $content ) { + + // Count number of notes in each post. + global $counter; + + $note_number = intval( 1 ); + $target = \kebbet\shortcode\footnotes\helpers\get_post_scope_id(); + $first_item = ! isset( $counter[$target] ); + + // Which footnote is this? + if ( ! $first_item ) { + $find_max = max( $counter[$target] ); + $note_number = $find_max + intval( 1 ); + } + + // Update counter. + $counter[$target][] = $note_number; + + // Markup for the sup-element. + $sup_content = display( $note_number, $content ); + + return $sup_content; +}