Skip to content

Commit

Permalink
Separates counter from display function for footnote numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
kebbet committed Sep 22, 2021
1 parent 8819513 commit d7434e8
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions src/shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' . $sup_id . ' class="' . esc_attr( $sup_class ) . '">';
$sup_content .= '<a href="' . esc_url( $note_link ) . '"' . $attributes . '>' . esc_attr( $note_number ) . '</a>';
$sup_content .= '<a href="' . esc_url( $note_link ) . '"' . $link_title . '>' . esc_attr( $number ) . '</a>';
$sup_content .= '</sup>';

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;
}

0 comments on commit d7434e8

Please sign in to comment.