Skip to content

Commit

Permalink
Merge pull request #29 from jasonyingling/add-combine-duplicate-footn…
Browse files Browse the repository at this point in the history
…otes

Add option to combine duplicate footnotes
  • Loading branch information
jasonyingling authored Nov 26, 2024
2 parents 17edef3 + cce5635 commit 8d89cdf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
11 changes: 9 additions & 2 deletions easy-footnotes-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
$hide_easy_footnote_after_posts = isset( $_POST['hide_easy_footnote_after_posts'] ) ? true : false;
$show_easy_footnote_on_front = isset( $_POST['show_easy_footnote_on_front'] ) ? true : false;
$reset_footnotes = isset( $_POST['reset_footnotes'] ) ? true : false;
$combine_duplicate_footnotes = isset( $_POST['combine_duplicate_footnotes'] ) ? true : false;

$updateOptions = array(
'footnoteLabel' => sanitize_text_field( $easyFootnoteLabel ),
'useLabel' => $easyFootnoteCheck,
'hide_easy_footnote_after_posts' => $hide_easy_footnote_after_posts,
'show_easy_footnote_on_front' => $show_easy_footnote_on_front,
'reset_footnotes' => $reset_footnotes,
'combine_duplicate_footnotes' => $combine_duplicate_footnotes,
);

update_option( 'easy_footnotes_options', $updateOptions );
Expand All @@ -41,6 +43,7 @@
$hide_easy_footnote_after_posts = isset( $footnoteOptions['hide_easy_footnote_after_posts'] ) ? $footnoteOptions['hide_easy_footnote_after_posts'] : false;
$show_easy_footnote_on_front = isset( $footnoteOptions['show_easy_footnote_on_front'] ) ? $footnoteOptions['show_easy_footnote_on_front'] : false;
$reset_footnotes = isset( $footnoteOptions['reset_footnotes'] ) ? $footnoteOptions['reset_footnotes'] : false;
$combine_duplicate_footnotes = isset( $footnoteOptions['combine_duplicate_footnotes'] ) ? $footnoteOptions['combine_duplicate_footnotes'] : false;
}
?>

Expand All @@ -58,10 +61,14 @@

<p><?php esc_html_e( 'Hide Footnotes after post content: ', 'easy-footnotes' ); ?><input type="checkbox" name="hide_easy_footnote_after_posts" <?php checked( $hide_easy_footnote_after_posts ); ?> size="20"></p>

<p><?php esc_html_e( 'Reset Footnotes to avoid duplication after the content: ', 'easy-footnotes' ); ?><input type="checkbox" name="reset_footnotes" <?php checked( $reset_footnotes ); ?> size="20"></p>

<p id="easy_footnote_on_front"><?php esc_html_e( 'Show Footnotes on Front Page: ', 'easy-footnotes' ); ?><input type="checkbox" name="show_easy_footnote_on_front" <?php checked( $show_easy_footnote_on_front ); ?> size="20"></p>

<p><?php esc_html_e( 'Combine duplicate footnotes: ', 'easy-footnotes' ); ?><input type="checkbox" name="combine_duplicate_footnotes" <?php checked( $combine_duplicate_footnotes ); ?> size="20"></p>

<p><?php esc_html_e( 'Reset Footnotes to avoid duplication after the content: ', 'easy-footnotes' ); ?><input type="checkbox" name="reset_footnotes" <?php checked( $reset_footnotes ); ?> size="20">
<br>
<span><?php esc_html_e( 'If you have "Combine duplicate footnotes" turned on, you should leave this setting off. ', 'easy-footnotes' ); ?><span>
</p>

<p class="submit">
<input type="submit" name="Submit" value="<?php esc_attr_e( 'Update Options', 'easy-footnotes' ); ?>" />
Expand Down
29 changes: 16 additions & 13 deletions easy-footnotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin URI: https://jasonyingling.me/easy-footnotes-wordpress/
* Description: Easily add footnotes to your posts with a simple shortcode.
* Text Domain: easy-footnotes
* Version: 1.1.10
* Version: 1.1.11
* Author: Jason Yingling
* Author URI: https://jasonyingling.me
* License: GPL2
Expand Down Expand Up @@ -47,7 +47,7 @@ class easyFootnotes {

private $footnoteSettings;

private $version = '1.1.10';
private $version = '1.1.11';

/**
* Constructing the initial plugin options, shortcodes, and hooks.
Expand All @@ -59,6 +59,7 @@ public function __construct() {
'hide_easy_footnote_after_posts' => false,
'show_easy_footnote_on_front' => false,
'reset_footnotes' => false,
'combine_duplicate_footnotes' => false,
);

add_option( 'easy_footnotes_options', $this->footnoteSettings );
Expand All @@ -68,7 +69,7 @@ public function __construct() {
add_filter( 'the_content', array( $this, 'easy_footnote_after_content' ), 20 );

$this->footnoteOptions = get_option( 'easy_footnotes_options' );
if ( isset( $this->footnoteOptions['reset_footnotes'] ) && $this->footnoteOptions['reset_footnotes'] ) {
if ( isset( $this->footnoteOptions['reset_footnotes'] ) && $this->footnoteOptions['reset_footnotes'] || ( ! isset( $this->footnoteOptions[ 'combine_duplicate_footnotes' ] ) || $this->footnoteOptions[ 'combine_duplicate_footnotes' ] === false ) ) {
add_filter( 'the_content', array( $this, 'easy_footnote_reset' ), 999 );
}

Expand Down Expand Up @@ -142,7 +143,7 @@ public function easy_footnote_shortcode( $atts, $content = null ) {
$this->usedFootnoteNumbers[] = $footnote_number; // Track custom number
$this->footnoteLookup[$content_id] = $footnote_number;
$this->footnotes[$footnote_number] = $content;
} elseif ( isset( $this->footnoteLookup[$content_id] ) ) {
} elseif ( isset( $this->footnoteLookup[$content_id] ) && isset( $this->footnoteOptions[ 'combine_duplicate_footnotes' ] ) && $this->footnoteOptions[ 'combine_duplicate_footnotes' ] === true ) {
// Use existing footnote number for duplicate content
$footnote_number = $this->footnoteLookup[$content_id];
} else {
Expand Down Expand Up @@ -216,16 +217,18 @@ public function easy_footnote_after_content( $content ) {
ksort($footnotesInsert);

foreach ( $footnotesInsert as $count => $footnote ) {
// If the footnote is already in the lookup, use its number
if ( isset( $this->footnoteLookup[$footnote] ) ) {
$count = $this->footnoteLookup[$footnote];
} else {
// Skip custom numbers that were already used
while ( in_array( $footnote_number, $this->usedFootnoteNumbers ) ) {
$footnote_number++;

if ( isset( $this->footnoteOptions[ 'combine_duplicate_footnotes' ] ) && $this->footnoteOptions[ 'combine_duplicate_footnotes' ] === true ) {
// If the footnote is already in the lookup, use its number
if ( isset( $this->footnoteLookup[$footnote] ) ) {
$count = $this->footnoteLookup[$footnote];
} else {
// Skip custom numbers that were already used
while ( in_array( $footnote_number, $this->usedFootnoteNumbers ) ) {
$footnote_number++;
}
}
}

}
// Generate back-to-top link and the footnote item
$footnoteCopy .= '<li class="easy-footnote-single"><span id="easy-footnote-bottom-' . esc_attr( $count ) . '-' . $post_id . '" class="easy-footnote-margin-adjust"></span>' . wp_kses_post( $footnote ) . '<a class="easy-footnote-to-top" href="' . esc_url( '#easy-footnote-' . $count . '-' . $post_id ) . '"></a></li>';
}
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Donate link: http://jasonyingling.me
Tags: footnotes, read, blogging, hover, tooltips, editing, endnotes, Formatting, writing, bibliography, notes, reference
Requires at least: 3.0.1
Tested up to: 6.7
Stable tag: 1.1.10
Stable tag: 1.1.11
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down

0 comments on commit 8d89cdf

Please sign in to comment.