Skip to content

Commit

Permalink
Move count function to a separate function.
Browse files Browse the repository at this point in the history
  • Loading branch information
kebbet committed Sep 22, 2021
1 parent d7434e8 commit 1413c9e
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions src/shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,45 @@

defined( 'ABSPATH' ) or exit;

/**
* 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 ) {
$note_number = counter();
$sup_content = display( $note_number, $content );

return $sup_content;
}

/**
* Which footnote we are working with. (Count them)
*
* @since 20210922.1
*
* @return int
*/
function counter( ) {
global $counter;

$find_max = intval( 0 );
$target = \kebbet\shortcode\footnotes\helpers\get_post_scope_id();

// Which footnote is this?
if ( isset( $counter[$target] ) ) { // False if first item.
$find_max = max( $counter[$target] );
}

// Update counter.
$number = $find_max + intval( 1 );
$counter[$target][] = $number;

return intval( $number );
}

/**
* Return markup for the `sup`-element.
*
Expand Down Expand Up @@ -47,34 +86,3 @@ function display( $number, $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;
}

0 comments on commit 1413c9e

Please sign in to comment.