footnoteSettings = array( 'footnoteLabel' => 'Footnotes', 'useLabel' => false, '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 ); add_shortcode( 'note', array( $this, 'easy_footnote_shortcode' ) ); add_shortcode( 'efn_note', array( $this, 'easy_footnote_shortcode' ) ); add_shortcode( 'efn_reset', array( $this, 'short_code_reset' ) ); 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'] || ( ! isset( $this->footnoteOptions[ 'combine_duplicate_footnotes' ] ) || $this->footnoteOptions[ 'combine_duplicate_footnotes' ] === false ) ) { add_filter( 'the_content', array( $this, 'easy_footnote_reset' ), 999 ); } add_action( 'wp_enqueue_scripts', array( $this, 'register_qtip_scripts' ) ); add_action( 'admin_menu', array( $this, 'easy_footnotes_admin_actions' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'easy_footnotes_admin_scripts' ) ); add_action( 'init', array($this, 'efn_load_textdomain') ); $this->footnoteOptions = get_option( 'easy_footnotes_options' ); } /** * Registering the scripts and styles used by jQuery qTip. */ public function register_qtip_scripts() { wp_register_script( 'imagesloaded', plugins_url( '/assets/qtip/imagesloaded.pkgd.min.js', __FILE__ ), array(), $this->version, true ); wp_register_script( 'qtip', plugins_url( '/assets/qtip/jquery.qtip.min.js', __FILE__ ), array( 'jquery', 'imagesloaded' ), $this->version, true ); wp_register_script( 'qtipcall', plugins_url( '/assets/qtip/jquery.qtipcall.js', __FILE__ ), array( 'jquery', 'qtip' ), $this->version, true ); wp_register_style( 'qtipstyles', plugins_url( '/assets/qtip/jquery.qtip.min.css', __FILE__ ), array(), $this->version, false ); wp_register_style( 'easyfootnotescss', plugins_url( '/assets/easy-footnotes.css', __FILE__ ), array(), $this->version, false ); } /** * Create the Easy Footnotes shortcode. * * @param array $atts Shortcode attributes. * @param string $content Content within the shortcode. */ public function easy_footnote_shortcode( $atts, $content = null ) { if ( isset( $this->footnoteOptions['show_easy_footnote_on_front'] ) && $this->footnoteOptions['show_easy_footnote_on_front'] ) { $efn_show_on_front = is_front_page(); } else { $efn_show_on_front = false; } wp_enqueue_style( 'qtipstyles' ); wp_enqueue_style( 'easyfootnotescss' ); wp_enqueue_script( 'imagesloaded' ); wp_enqueue_script( 'qtip' ); wp_enqueue_script( 'qtipcall' ); wp_enqueue_style( 'dashicons' ); // Accept optional custom number attribute $atts = shortcode_atts( array( 'num' => null ), $atts ); $post_id = get_the_ID(); $content = do_shortcode( $content ); $content_id = md5( preg_replace("/[^A-Za-z0-9 ]/", '', wp_strip_all_tags( html_entity_decode( $content ) ) ) ); if ( isset( $this->footnoteOptions[ 'combine_duplicate_footnotes' ] ) && $this->footnoteOptions[ 'combine_duplicate_footnotes' ] === true ) { /** * Search for existing footnote for removing duplicate * Also add the same numbers to duplicate footnotes */ if (!isset($this->usedFootnoteNumbers) ) { $this->usedFootnoteNumbers = array(); } if (!isset($this->footnoteLookup)) { $this->footnoteLookup = array(); } } // If a custom number is provided, use that number and mark it as used if ( ! empty( $atts['num'] ) ) { $footnote_number = intval( $atts['num'] ); $this->usedFootnoteNumbers[] = $footnote_number; // Track custom number $this->footnoteLookup[$content_id] = $footnote_number; $this->footnotes[$footnote_number] = $content; } elseif ( isset( $this->footnoteLookup[$content_id] ) ) { if ( 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 { $this->footnoteCount++; $footnote_number = $this->footnoteCount; $this->footnotes[$footnote_number] = $content; } } else { // Auto-increment for new footnotes, skipping used numbers do { $this->footnoteCount++; } while ( in_array( $this->footnoteCount, $this->usedFootnoteNumbers ) ); $footnote_number = $this->footnoteCount; $this->footnoteLookup[$content_id] = $footnote_number; $this->usedFootnoteNumbers[] = $footnote_number; // Mark as used $this->footnotes[$footnote_number] = $content; } // Generate the correct footnote link with the correct number $footnoteLink = (is_singular() || $efn_show_on_front) && is_main_query() ? '#easy-footnote-bottom-' . $footnote_number . '-' . $post_id : get_permalink($post_id) . '#easy-footnote-bottom-' . $footnote_number . '-' . $post_id; // Now generate the footnote markup with the correct number and link $footnoteContent = "" . "" . esc_html($footnote_number) . ""; return $footnoteContent; } /** * The content of a particular footnote. * * @param string $content The content from a particular call of the shortcode. */ public function easy_footnote_content( $content ) { $this->footnotes[ $this->footnoteCount ] = $content; return $this->footnotes; } /** * Display the list of footnotes after the post content. * * @param string $content The content of the current post. */ public function easy_footnote_after_content( $content ) { if ( isset( $this->footnoteOptions['hide_easy_footnote_after_posts'] ) && $this->footnoteOptions['hide_easy_footnote_after_posts'] ) { return $content; } if ( isset( $this->footnoteOptions['show_easy_footnote_on_front'] ) && $this->footnoteOptions['show_easy_footnote_on_front'] ) { $efn_show_on_front = is_front_page() || is_home(); } else { $efn_show_on_front = false; } if ( ( is_singular() || $efn_show_on_front ) && is_main_query() ) { $footnotesInsert = $this->footnotes; $footnoteCopy = ''; $efn_output = ''; $useLabel = isset( $this->footnoteOptions['useLabel'] ) ? $this->footnoteOptions['useLabel'] : false; $efLabel = isset($this->footnoteOptions['footnoteLabel'] ) ? $this->footnoteOptions['footnoteLabel'] : __( 'Footnotes', 'easy-footnotes' ); $post_id = get_the_ID(); // Create a new array to track used footnotes and their numbers $footnote_number = 1; // sort footnotes according to numbers ksort($footnotesInsert); foreach ( $footnotesInsert as $count => $footnote ) { 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 .= '