get( Footnotes_Settings::EXPERT_LOOKUP_THE_TITLE_PRIORITY_LEVEL ) );
$the_content_priority = intval( Footnotes_Settings::instance()->get( Footnotes_Settings::EXPERT_LOOKUP_THE_CONTENT_PRIORITY_LEVEL ) );
$the_excerpt_priority = intval( Footnotes_Settings::instance()->get( Footnotes_Settings::EXPERT_LOOKUP_THE_EXCERPT_PRIORITY_LEVEL ) );
$widget_title_priority = intval( Footnotes_Settings::instance()->get( Footnotes_Settings::EXPERT_LOOKUP_WIDGET_TITLE_PRIORITY_LEVEL ) );
$widget_text_priority = intval( Footnotes_Settings::instance()->get( Footnotes_Settings::EXPERT_LOOKUP_WIDGET_TEXT_PRIORITY_LEVEL ) );
// PHP_INT_MAX can be set by -1.
$the_title_priority = ( -1 === $the_title_priority ) ? PHP_INT_MAX : $the_title_priority;
$the_content_priority = ( -1 === $the_content_priority ) ? PHP_INT_MAX : $the_content_priority;
$the_excerpt_priority = ( -1 === $the_excerpt_priority ) ? PHP_INT_MAX : $the_excerpt_priority;
$widget_title_priority = ( -1 === $widget_title_priority ) ? PHP_INT_MAX : $widget_title_priority;
$widget_text_priority = ( -1 === $widget_text_priority ) ? PHP_INT_MAX : $widget_text_priority;
// Append custom css to the header.
add_filter( 'wp_head', array( $this, 'footnotes_output_head' ), PHP_INT_MAX );
// Append the love and share me slug to the footer.
add_filter( 'wp_footer', array( $this, 'footnotes_output_footer' ), PHP_INT_MAX );
if ( Footnotes_Convert::to_bool( Footnotes_Settings::instance()->get( Footnotes_Settings::EXPERT_LOOKUP_THE_TITLE ) ) ) {
add_filter( 'the_title', array( $this, 'footnotes_in_title' ), $the_title_priority );
}
// Configurable priority level for reference container relative positioning; default 98.
if ( Footnotes_Convert::to_bool( Footnotes_Settings::instance()->get( Footnotes_Settings::EXPERT_LOOKUP_THE_CONTENT ) ) ) {
add_filter( 'the_content', array( $this, 'footnotes_in_content' ), $the_content_priority );
/**
* Hook for category pages.
*
* - Bugfix: Hooks: support footnotes on category pages, thanks to @vitaefit bug report, thanks to @misfist code contribution.
*
* @reporter @vitaefit
* @link https://wordpress.org/support/topic/footnote-doesntwork-on-category-page/
*
* @contributor @misfist
* @link https://wordpress.org/support/topic/footnote-doesntwork-on-category-page/#post-13864859
*
* @since 2.5.0
*
* Category pages can have rich HTML content in a term description with article status.
* For this to happen, WordPress’ built-in partial HTML blocker needs to be disabled.
* @link https://docs.woocommerce.com/document/allow-html-in-term-category-tag-descriptions/
*/
add_filter( 'term_description', array( $this, 'footnotes_in_content' ), $the_content_priority );
/**
* Hook for popup maker popups.
*
* - Bugfix: Hooks: support footnotes in Popup Maker popups, thanks to @squatcher bug report.
*
* @reporter @squatcher
* @link https://wordpress.org/support/topic/footnotes-use-in-popup-maker/
*
* @since 2.5.1
*/
add_filter( 'pum_popup_content', array( $this, 'footnotes_in_content' ), $the_content_priority );
}
/**
* Adds a filter to the excerpt hook.
*
* @since 1.5.0 The hook 'get_the_excerpt' is filtered too.
* @since 1.5.5 The hook 'get_the_excerpt' is removed but not documented in changelog or docblock.
* @since 2.6.2 The hook 'get_the_excerpt' is readded when attempting to debug excerpt handling.
* @since 2.6.6 The hook 'get_the_excerpt' is removed again because it seems to cause issues in some themes.
*/
if ( Footnotes_Convert::to_bool( Footnotes_Settings::instance()->get( Footnotes_Settings::EXPERT_LOOKUP_THE_EXCERPT ) ) ) {
add_filter( 'the_excerpt', array( $this, 'footnotes_in_excerpt' ), $the_excerpt_priority );
}
if ( Footnotes_Convert::to_bool( Footnotes_Settings::instance()->get( Footnotes_Settings::EXPERT_LOOKUP_WIDGET_TITLE ) ) ) {
add_filter( 'widget_title', array( $this, 'footnotes_in_widget_title' ), $widget_title_priority );
}
if ( Footnotes_Convert::to_bool( Footnotes_Settings::instance()->get( Footnotes_Settings::EXPERT_LOOKUP_WIDGET_TEXT ) ) ) {
add_filter( 'widget_text', array( $this, 'footnotes_in_widget_text' ), $widget_text_priority );
}
/**
* The the_post hook.
*
* - Adding: Hooks: support 'the_post' in response to user request for custom post types.
*
* @since 1.5.4
* @accountable @aricura
* @link https://wordpress.org/support/topic/doesnt-work-in-custon-post-types/#post-5339110
*
*
* - Update: Hooks: Default-enable all hooks to prevent footnotes from seeming broken in some parts.
*
* @since 2.0.5
* @accountable @pewgeuges
*
*
* - BUGFIX: Hooks: Default-disable 'the_post', thanks to @spaceling @markcheret @nyamachi @whichgodsaves @spiralofhope2 @mmallett @andreasra @widecast @ymorin007 @tashi1es bug reports.
*
* @reporter @spaceling
* @link https://wordpress.org/support/topic/change-the-position-5/#post-13612697
*
* @reporter @markcheret on behalf of W. Beinert
* @link https://wordpress.org/support/topic/footnotes-now-appear-in-summaries-even-though-this-is-marked-no/
*
* @reporter @nyamachi
* @link https://wordpress.org/support/topic/footnotes-appearing-in-header/
*
* @reporter @whichgodsaves
* @link https://wordpress.org/support/topic/footnotes-appearing-in-header/#post-13622694
*
* @reporter @spiralofhope2
* @link https://wordpress.org/support/topic/2-0-5-broken/
*
* @reporter @mmallett
* @link https://wordpress.org/support/topic/2-0-5-broken/#post-13623208
*
* @reporter @andreasra
* @link https://wordpress.org/support/topic/footnotes-appearing-in-header/#post-13624091
*
* @reporter @widecast
* @link https://wordpress.org/support/topic/2-0-5-broken/#post-13626222
*
* @reporter @ymorin007
* @link https://wordpress.org/support/topic/footnotes-appearing-in-header/#post-13627050
*
* @reporter @markcheret on behalf of L. Smith
* @link https://wordpress.org/support/topic/footnotes-appear-in-random-places-on-academic-website/
*
* @reporter @tashi1es
* @link https://wordpress.org/support/topic/footnotes-appear-in-random-places-on-academic-website/#post-13630495
*
* @since 2.0.7
* @link https://wordpress.org/support/topic/change-the-position-5/page/2/#post-13630114
* @link https://wordpress.org/support/topic/footnotes-appearing-in-header/#post-13630303
* @link https://wordpress.org/support/topic/footnotes-appearing-in-header/page/2/#post-13630799
* @link https://wordpress.org/support/topic/no-footnotes-anymore/#post-13813233
*
* - UPDATE: Hooks: remove 'the_post', the plugin stops supporting this hook.
*
* @since 2.1.0
* @accountable @pewgeuges
*/
// Reset stored footnotes when displaying the header.
self::$footnotes = array();
self::$allow_love_me = true;
}
/**
* Outputs the custom css to the header of the public page.
*
* @since 1.5.0
*
* @since 2.1.1 Bugfix: Reference container: fix start pages by making its display optional, thanks to @dragon013 bug report.
* @since 2.1.1 Bugfix: Tooltips: optional alternative JS implementation with CSS transitions to fix configuration-related outage, thanks to @andreasra feedback.
* @since 2.1.3 raise settings priority to override theme stylesheets
* @since 2.1.4 Bugfix: Tooltips: Styling: fix font size issue by adding font size to settings with legacy as default.
* @since 2.1.4 Bugfix: Reference container: fix layout issues by moving backlink column width to settings.
* @since 2.2.5 Bugfix: Reference container: Label: make bottom border an option, thanks to @markhillyer issue report.
* @since 2.2.5 Bugfix: Reference container: Label: option to select paragraph or heading element, thanks to @markhillyer issue report.
* @since 2.3.0 Bugfix: Reference container: convert top padding to margin and make it a setting, thanks to @hamshe bug report.
* @since 2.5.4 Bugfix: Referrers: optional fixes to vertical alignment, font size and position (static) for in-theme consistency and cross-theme stability, thanks to @tomturowski bug report.
*/
public function footnotes_output_head() {
// Insert start tag without switching out of PHP.
echo "\r\n\r\n";
/**
* Alternative tooltip implementation relying on plain JS and CSS transitions.
*
* - Bugfix: Tooltips: optional alternative JS implementation with CSS transitions to fix configuration-related outage, thanks to @andreasra feedback.
*
* @reporter @andreasra
* @link https://wordpress.org/support/topic/footnotes-appearing-in-header/page/2/#post-13632566
*
* @since 2.1.1
* The script for alternative tooltips is printed formatted, not minified,
* for transparency. It isn’t indented though (the PHP open tag neither).
*/
if ( Footnotes::$alternative_tooltips_enabled ) {
// Start internal script.
?>
get( Footnotes_Settings::REFERENCE_CONTAINER_POSITION ) ) {
echo $this->reference_container();
}
// Get setting for love and share this plugin.
$love_me_index = Footnotes_Settings::instance()->get( Footnotes_Settings::FOOTNOTES_LOVE );
// Check if the admin allows to add a link to the footer.
if ( empty( $love_me_index ) || 'no' === strtolower( $love_me_index ) || ! self::$allow_love_me ) {
return;
}
// Set a hyperlink to the word "footnotes" in the Love slug.
$linked_name = sprintf( '%s', Footnotes_Config::PLUGIN_PUBLIC_NAME );
// Get random love me text.
if ( 'random' === strtolower( $love_me_index ) ) {
$love_me_index = 'text-' . wp_rand( 1, 7 );
}
switch ( $love_me_index ) {
// Options named wrt backcompat, simplest is default.
case 'text-1':
/* Translators: 2: Link to plugin page 1: Love heart symbol */
$love_me_text = sprintf( __( 'I %2$s %1$s', 'footnotes' ), $linked_name, Footnotes_Config::LOVE_SYMBOL );
break;
case 'text-2':
/* Translators: %s: Link to plugin page */
$love_me_text = sprintf( __( 'This website uses the awesome %s plugin.', 'footnotes' ), $linked_name );
break;
case 'text-4':
/* Translators: 1: Link to plugin page 2: Love heart symbol */
$love_me_text = sprintf( '%1$s %2$s', $linked_name, Footnotes_Config::LOVE_SYMBOL );
break;
case 'text-5':
/* Translators: 1: Love heart symbol 2: Link to plugin page */
$love_me_text = sprintf( '%1$s %2$s', Footnotes_Config::LOVE_SYMBOL, $linked_name );
break;
case 'text-6':
/* Translators: %s: Link to plugin page */
$love_me_text = sprintf( __( 'This website uses %s.', 'footnotes' ), $linked_name );
break;
case 'text-7':
/* Translators: %s: Link to plugin page */
$love_me_text = sprintf( __( 'This website uses the %s plugin.', 'footnotes' ), $linked_name );
break;
case 'text-3':
default:
/* Translators: %s: Link to plugin page */
$love_me_text = sprintf( '%s', $linked_name );
break;
}
echo sprintf( '
%s
', $love_me_text );
}
/**
* Replaces footnotes in the post/page title.
*
* @since 1.5.0
* @param string $content Title.
* @return string $content Title with replaced footnotes.
*/
public function footnotes_in_title( $content ) {
// Appends the reference container if set to "post_end".
return $this->exec( $content, false );
}
/**
* Replaces footnotes in the content of the current page/post.
*
* @since 1.5.0
*
* - Adding: Reference container: optionally per section by shortcode, thanks to @grflukas issue report.
*
* @reporter @grflukas
* @link https://wordpress.org/support/topic/multiple-reference-containers-in-single-post/
*
* @since 2.7.0
* @param string $content Page/Post content.
* @return string $content Content with replaced footnotes.
*/
public function footnotes_in_content( $content ) {
$ref_container_position = Footnotes_Settings::instance()->get( Footnotes_Settings::REFERENCE_CONTAINER_POSITION );
$footnote_section_shortcode = Footnotes_Settings::instance()->get( Footnotes_Settings::FOOTNOTE_SECTION_SHORTCODE );
$footnote_section_shortcode_length = strlen( $footnote_section_shortcode );
if ( strpos( $content, $footnote_section_shortcode ) === false ) {
// phpcs:disable WordPress.PHP.YodaConditions.NotYoda
// Appends the reference container if set to "post_end".
return $this->exec( $content, 'post_end' === $ref_container_position );
// phpcs:enable WordPress.PHP.YodaConditions.NotYoda
} else {
$rest_content = $content;
$sections_raw = array();
$sections_processed = array();
do {
$section_end = strpos( $rest_content, $footnote_section_shortcode );
$sections_raw[] = substr( $rest_content, 0, $section_end );
$rest_content = substr( $rest_content, $section_end + $footnote_section_shortcode_length );
} while ( strpos( $rest_content, $footnote_section_shortcode ) !== false );
$sections_raw[] = $rest_content;
foreach ( $sections_raw as $section ) {
$sections_processed[] = self::exec( $section, true );
}
$content = implode( $sections_processed );
return $content;
}
}
/**
* Processes existing excerpt or replaces it with a new one generated on the basis of the post.
*
* @since 1.5.0
* @param string $excerpt Excerpt content.
* @return string $excerpt Processed or new excerpt.
* @since 2.6.2 Debug No option.
* @since 2.6.3 Debug Yes option, the setting becomes fully effective.
*
* - Bugfix: Excerpts: make excerpt handling backward compatible, thanks to @mfessler bug report.
*
* @reporter @mfessler
* @link https://github.com/markcheret/footnotes/issues/65
*
* @since 2.7.0
* The input was already the processed excerpt, no more footnotes to search.
* But issue #65 brought up that manual excerpts can include processable footnotes.
* Default 'manual' is fallback and is backward compatible with the initial setup.
*/
public function footnotes_in_excerpt( $excerpt ) {
$excerpt_mode = Footnotes_Settings::instance()->get( Footnotes_Settings::FOOTNOTES_IN_EXCERPT );
if ( 'yes' === $excerpt_mode ) {
return $this->generate_excerpt_with_footnotes( $excerpt );
} elseif ( 'no' === $excerpt_mode ) {
return $this->generate_excerpt( $excerpt );
} else {
return $this->exec( $excerpt );
}
}
/**
* Generates excerpt on the basis of the post.
*
* - Bugfix: Excerpts: debug the 'No' option by generating excerpts on the basis of the post without footnotes, thanks to @nikelaos @markcheret @martinneumannat bug reports.
*
* @reporter @nikelaos
* @link https://wordpress.org/support/topic/jquery-comes-up-in-feed-content/
* @link https://wordpress.org/support/topic/doesnt-work-with-mailpoet/
*
* @reporter @markcheret
* @link https://wordpress.org/support/topic/footnotes-now-appear-in-summaries-even-though-this-is-marked-no/
*
* @reporter @martinneumannat
* @link https://wordpress.org/support/topic/problem-with-footnotes-in-excerpts-of-the-blog-page/
*
* @since 2.6.2
* @param string $content The post.
* @return string $content An excerpt of the post.
* Applies full WordPress excerpt processing.
* @link https://developer.wordpress.org/reference/functions/wp_trim_excerpt/
* @link https://developer.wordpress.org/reference/functions/wp_trim_words/
*/
public function generate_excerpt( $content ) {
// Discard existing excerpt and start on the basis of the post.
$content = get_the_content( get_the_id() );
// Get footnote delimiter shortcodes and unify them.
$content = self::unify_delimiters( $content );
// Remove footnotes.
$content = preg_replace( '#' . self::$start_tag_regex . '.+?' . self::$end_tag_regex . '#', '', $content );
// Apply WordPress excerpt processing.
$content = strip_shortcodes( $content );
$content = excerpt_remove_blocks( $content );
// Here the footnotes would be processed as part of WordPress content processing.
$content = apply_filters( 'the_content', $content );
// According to Advanced Excerpt, this is some kind of precaution against malformed CDATA in RSS feeds.
$content = str_replace( ']]>', ']]>', $content );
$excerpt_length = (int) _x( '55', 'excerpt_length' );
$excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );
$excerpt_more = apply_filters( 'excerpt_more', ' […]' );
// Function wp_trim_words() calls wp_strip_all_tags() that wrecks the footnotes.
$content = wp_trim_words( $content, $excerpt_length, $excerpt_more );
return $content;
}
/**
* Generates excerpt with footnotes on the basis of the post.
*
* - Bugfix: Excerpts: debug the 'Yes' option by generating excerpts with footnotes on the basis of the posts, thanks to @nikelaos @martinneumannat bug reports.
*
* @reporter @nikelaos
* @link https://wordpress.org/support/topic/jquery-comes-up-in-feed-content/
* @link https://wordpress.org/support/topic/doesnt-work-with-mailpoet/
*
* @reporter @martinneumannat
* @link https://wordpress.org/support/topic/problem-with-footnotes-in-excerpts-of-the-blog-page/
*
* @since 2.6.3
*
* - Bugfix: Process: remove trailing comma after last argument in multiline function calls for PHP < 7.3, thanks to @scroom @copylefter @lagoon24 bug reports.
*
* @reporter @scroom
* @link https://wordpress.org/support/topic/update-crashed-my-website-3/
*
* @reporter @copylefter
* @link https://wordpress.org/support/topic/update-crashed-my-website-3/#post-14259151
*
* @reporter @lagoon24
* @link https://wordpress.org/support/topic/update-crashed-my-website-3/#post-14259396
*
* @since 2.6.4
* @param string $content The post.
* @return string $content An excerpt of the post.
* Does not apply full WordPress excerpt processing.
* @see self::generate_excerpt()
* Uses information and some code from Advanced Excerpt.
* @link https://wordpress.org/plugins/advanced-excerpt/
*/
public function generate_excerpt_with_footnotes( $content ) {
// Discard existing excerpt and start on the basis of the post.
$content = get_the_content( get_the_id() );
// Get footnote delimiter shortcodes and unify them.
$content = self::unify_delimiters( $content );
// Apply WordPress excerpt processing.
$content = strip_shortcodes( $content );
$content = excerpt_remove_blocks( $content );
// But do not process footnotes at this point; do only this.
$content = str_replace( ']]>', ']]>', $content );
// Prepare the excerpt length argument.
$excerpt_length = (int) _x( '55', 'excerpt_length' );
$excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );
// Prepare the Read-on string.
$excerpt_more = apply_filters( 'excerpt_more', ' […]' );
// Safeguard the footnotes.
preg_match_all(
'#' . self::$start_tag_regex . '.+?' . self::$end_tag_regex . '#',
$content,
$saved_footnotes
);
// Prevent the footnotes from altering the excerpt: previously hard-coded '5ED84D6'.
$placeholder = '@' . mt_rand( 100000000, 2147483647 ) . '@';
$content = preg_replace(
'#' . self::$start_tag_regex . '.+?' . self::$end_tag_regex . '#',
$placeholder,
$content
);
// Replace line breaking markup with a separator.
$separator = ' ';
$content = preg_replace( '# #', $separator, $content );
$content = preg_replace( '# #', $separator, $content );
$content = preg_replace( '#<(p|li|div)[^>]*>#', $separator, $content );
$content = preg_replace( '#' . $separator . '#', '', $content, 1 );
$content = preg_replace( '#(p|li|div) *>#', '', $content );
$content = preg_replace( '#[\r\n]#', '', $content );
// To count words like Advanced Excerpt does it.
$tokens = array();
$output = '';
$counter = 0;
// Tokenize into tags and words as in Advanced Excerpt.
preg_match_all( '#(<[^>]+>|[^<>\s]+)\s*#u', $content, $tokens );
// Count words following one option of Advanced Excerpt.
foreach ( $tokens[0] as $token ) {
if ( $counter >= $excerpt_length ) {
break;
}
// If token is not a tag, increment word count.
if ( '<' !== $token[0] ) {
$counter++;
}
// Append the token to the output.
$output .= $token;
}
// Complete unbalanced markup, used by Advanced Excerpt.
$content = force_balance_tags( $output );
// Readd footnotes in excerpt.
$index = 0;
while ( 0 !== preg_match( '#' . $placeholder . '#', $content ) ) {
$content = preg_replace(
'#' . $placeholder . '#',
$saved_footnotes[0][ $index ],
$content,
1
);
$index++;
}
// Append the Read-on string as in wp_trim_words().
$content .= $excerpt_more;
// Process readded footnotes without appending the reference container.
$content = self::exec( $content, false );
return $content;
}
/**
* Replaces footnotes in the widget title.
*
* @since 1.5.0
* @param string $content Widget content.
* @return string $content Content with replaced footnotes.
*/
public function footnotes_in_widget_title( $content ) {
// Appends the reference container if set to "post_end".
return $this->exec( $content, false );
}
/**
* Replaces footnotes in the content of the current widget.
*
* @since 1.5.0
* @param string $content Widget content.
* @return string $content Content with replaced footnotes.
*/
public function footnotes_in_widget_text( $content ) {
// phpcs:disable WordPress.PHP.YodaConditions.NotYoda
// Appends the reference container if set to "post_end".
return $this->exec( $content, 'post_end' === Footnotes_Settings::instance()->get( Footnotes_Settings::REFERENCE_CONTAINER_POSITION ) ? true : false );
// phpcs:enable WordPress.PHP.YodaConditions.NotYoda
}
/**
* Replaces all footnotes that occur in the given content.
*
* @since 1.5.0
* @param string $content Any string that may contain footnotes to be replaced.
* @param bool $output_references Appends the Reference Container to the output if set to true, default true.
* @param bool $hide_footnotes_text Hide footnotes found in the string.
* @return string
*/
public function exec( $content, $output_references = false, $hide_footnotes_text = false ) {
// Process content.
$content = $this->search( $content, $hide_footnotes_text );
/**
* Reference container customized positioning through shortcode.
*
* - Adding: Reference container: support for custom position shortcode, thanks to @hamshe issue report.
*
* @reporter @hamshe
* @link https://wordpress.org/support/topic/reference-container-in-elementor/
*
* @since 2.2.0
*
* - Bugfix: Reference container: delete position shortcode if unused because position may be widget or footer, thanks to @hamshe bug report.
*
* @reporter @hamshe
* @link https://wordpress.org/support/topic/reference-container-in-elementor/#post-13784126
*
* @since 2.2.5
*/
// Append the reference container or insert at shortcode.
$reference_container_position_shortcode = Footnotes_Settings::instance()->get( Footnotes_Settings::REFERENCE_CONTAINER_POSITION_SHORTCODE );
if ( empty( $reference_container_position_shortcode ) ) {
$reference_container_position_shortcode = '[[references]]';
}
if ( $output_references ) {
if ( strpos( $content, $reference_container_position_shortcode ) ) {
$content = str_replace( $reference_container_position_shortcode, $this->reference_container(), $content );
} else {
$content .= $this->reference_container();
}
// Increment the container ID.
self::$reference_container_id++;
}
// Delete position shortcode should any remain.
$content = str_replace( $reference_container_position_shortcode, '', $content );
// Take a look if the LOVE ME slug should NOT be displayed on this page/post, remove the short code if found.
if ( strpos( $content, Footnotes_Config::NO_LOVE_SLUG ) ) {
self::$allow_love_me = false;
$content = str_replace( Footnotes_Config::NO_LOVE_SLUG, '', $content );
}
// Return the content with replaced footnotes and optional reference container appended.
return $content;
}
/**
* Brings the delimiters and unifies their various HTML escapement schemas.
*
* @param string $content TODO.
*
* - Bugfix: Footnote delimiter short codes: fix numbering bug by cross-editor HTML escapement schema unification, thanks to @patrick_here @alifarahani8000 @gova bug reports.
*
* @reporter @patrick_here
* @link https://wordpress.org/support/topic/how-to-add-footnotes-shortcode-in-elementor/
*
* @reporter @alifarahani8000
* @link https://wordpress.org/support/topic/after-version-2-5-10-the-ref-or-tags-are-not-longer-working/
*
* @reporter @gova
* @link https://wordpress.org/support/topic/footnotes-content-number-not-sequential/
*
* @since 2.1.14
* While the Classic Editor (visual mode) escapes both pointy brackets,
* the Block Editor enforces balanced escapement only in code editor mode
* when the opening tag is already escaped. In visual mode, the Block Editor
* does not escape the greater-than sign.
*/
public function unify_delimiters( $content ) {
// Get footnotes start and end tag short codes.
$starting_tag = Footnotes_Settings::instance()->get( Footnotes_Settings::FOOTNOTES_SHORT_CODE_START );
$ending_tag = Footnotes_Settings::instance()->get( Footnotes_Settings::FOOTNOTES_SHORT_CODE_END );
if ( 'userdefined' === $starting_tag || 'userdefined' === $ending_tag ) {
$starting_tag = Footnotes_Settings::instance()->get( Footnotes_Settings::FOOTNOTES_SHORT_CODE_START_USER_DEFINED );
$ending_tag = Footnotes_Settings::instance()->get( Footnotes_Settings::FOOTNOTES_SHORT_CODE_END_USER_DEFINED );
}
// If any footnotes short code is empty, return the content without changes.
if ( empty( $starting_tag ) || empty( $ending_tag ) ) {
return $content;
}
if ( preg_match( '#[&"\'<>]#', $starting_tag . $ending_tag ) ) {
$harmonized_start_tag = '{[(|fnote_stt|)]}';
$harmonized_end_tag = '{[(|fnote_end|)]}';
// Harmonize footnotes without escaping any HTML special characters in delimiter shortcodes.
// The footnote has been added in the Block Editor code editor (doesn’t work in Classic Editor text mode).
$content = str_replace( $starting_tag, $harmonized_start_tag, $content );
$content = str_replace( $ending_tag, $harmonized_end_tag, $content );
// Harmonize footnotes while escaping HTML special characters in delimiter shortcodes.
// The footnote has been added in the Classic Editor visual mode.
$content = str_replace( htmlspecialchars( $starting_tag ), $harmonized_start_tag, $content );
$content = str_replace( htmlspecialchars( $ending_tag ), $harmonized_end_tag, $content );
// Harmonize footnotes while escaping HTML special characters except greater-than sign in delimiter shortcodes.
// The footnote has been added in the Block Editor visual mode.
$content = str_replace( str_replace( '>', '>', htmlspecialchars( $starting_tag ) ), $harmonized_start_tag, $content );
$content = str_replace( str_replace( '>', '>', htmlspecialchars( $ending_tag ) ), $harmonized_end_tag, $content );
// Assign the delimiter shortcodes.
self::$start_tag = $harmonized_start_tag;
self::$end_tag = $harmonized_end_tag;
// Assign the regex-conformant shortcodes.
self::$start_tag_regex = '\{\[\(\|fnote_stt\|\)\]\}';
self::$end_tag_regex = '\{\[\(\|fnote_end\|\)\]\}';
} else {
// Assign the delimiter shortcodes.
self::$start_tag = $starting_tag;
self::$end_tag = $ending_tag;
// Make shortcodes conform to regex syntax.
self::$start_tag_regex = preg_replace( '#([\(\)\{\}\[\]\|\*\.\?\!])#', '\\\\$1', self::$start_tag );
self::$end_tag_regex = preg_replace( '#([\(\)\{\}\[\]\|\*\.\?\!])#', '\\\\$1', self::$end_tag );
}
return $content;
}
/**
* Replaces all footnotes in the given content and appends them to the static property.
*
* @since 1.5.0
* @param string $content Any content to be searched for footnotes.
* @param bool $hide_footnotes_text Hide footnotes found in the string.
* @return string
*
* @since 2.0.0 various.
* @since 2.4.0 Adding: Footnote delimiters: syntax validation for balanced footnote start and end tag short codes.
* @since 2.5.0 Bugfix: Footnote delimiters: Syntax validation: exclude certain cases involving scripts, thanks to @andreasra bug report.
* @since 2.5.0 Bugfix: Footnote delimiters: Syntax validation: complete message with hint about setting, thanks to @andreasra bug report.
* @since 2.5.0 Bugfix: Footnote delimiters: Syntax validation: limit length of quoted string to 300 characters, thanks to @andreasra bug report.
*
* - Bugfix: Footnote delimiter short codes: debug closing pointy brackets in the Block Editor by accounting for unbalanced HTML escapement, thanks to @patrick_here @alifarahani8000 bug reports.
*
* @reporter @patrick_here
* @link https://wordpress.org/support/topic/how-to-add-footnotes-shortcode-in-elementor/
*
* @reporter @alifarahani8000
* @link https://wordpress.org/support/topic/after-version-2-5-10-the-ref-or-tags-are-not-longer-working/
*
* @since 2.5.13
*/
public function search( $content, $hide_footnotes_text ) {
// Get footnote delimiter shortcodes and unify them.
$content = self::unify_delimiters( $content );
/**
* Checks for balanced footnote delimiters; delimiter syntax validation.
*
* - Adding: Footnote delimiters: syntax validation for balanced footnote start and end tag short codes.
*
* @since 2.4.0
*
* - Bugfix: Footnote delimiters: Syntax validation: exclude certain cases involving scripts, thanks to @andreasra bug report.
* - Bugfix: Footnote delimiters: Syntax validation: complete message with hint about setting, thanks to @andreasra bug report.
* - Bugfix: Footnote delimiters: Syntax validation: limit length of quoted string to 300 characters, thanks to @andreasra bug report.
*
* @reporter @andreasra
* @link https://wordpress.org/support/topic/warning-unbalanced-footnote-start-tag-short-code-before/
*
* @since 2.5.0
* If footnotes short codes are unbalanced, and syntax validation is not disabled,
* prepend a warning to the content; displays de facto beneath the post title.
*/
// If enabled.
if ( Footnotes_Convert::to_bool( Footnotes_Settings::instance()->get( Footnotes_Settings::FOOTNOTE_SHORTCODE_SYNTAX_VALIDATION_ENABLE ) ) ) {
// Apply different regex depending on whether start shortcode is double/triple opening parenthesis.
if ( '((' === self::$start_tag || '(((' === self::$start_tag ) {
// This prevents from catching a script containing e.g. a double opening parenthesis.
$validation_regex = '#' . self::$start_tag_regex . '(((?!' . self::$end_tag_regex . ')[^\{\}])*?)(' . self::$start_tag_regex . '|$)#s';
} else {
// Catch all only if the start shortcode is not double/triple opening parenthesis, i.e. is unlikely to occur in scripts.
$validation_regex = '#' . self::$start_tag_regex . '(((?!' . self::$end_tag_regex . ').)*?)(' . self::$start_tag_regex . '|$)#s';
}
// Check syntax and get error locations.
preg_match( $validation_regex, $content, $error_location );
if ( empty( $error_location ) ) {
self::$syntax_error_flag = false;
}
// Prevent generating and inserting the warning multiple times.
if ( self::$syntax_error_flag ) {
// Get plain text string for error location.
$error_spot_string = wp_strip_all_tags( $error_location[1] );
// Limit string length to 300 characters.
if ( strlen( $error_spot_string ) > 300 ) {
$error_spot_string = substr( $error_spot_string, 0, 299 ) . '…';
}
// Compose warning box.
$syntax_error_warning = '
';
$syntax_error_warning .= __( 'WARNING: unbalanced footnote start tag short code found.', 'footnotes' );
$syntax_error_warning .= '
';
// Syntax validation setting in the dashboard under the General settings tab.
/* Translators: 1: General Settings 2: Footnote start and end short codes 3: Check for balanced shortcodes */
$syntax_error_warning .= sprintf( __( 'If this warning is irrelevant, please disable the syntax validation feature in the dashboard under %1$s > %2$s > %3$s.', 'footnotes' ), __( 'General settings', 'footnotes' ), __( 'Footnote start and end short codes', 'footnotes' ), __( 'Check for balanced shortcodes', 'footnotes' ) );
$syntax_error_warning .= '
';
$syntax_error_warning .= __( 'Unbalanced start tag short code found before:', 'footnotes' );
$syntax_error_warning .= '
';
// Prepend the warning box to the content.
$content = $syntax_error_warning . $content;
// Checked, set flag to false to prevent duplicate warning.
self::$syntax_error_flag = false;
return $content;
}
}
/**
* Patch to allow footnotes in input field labels.
*
* - Bugfix: Forms: remove footnotes from input field values, thanks to @bogosavljev bug report.
*
* @reporter @bogosavljev
* @link https://wordpress.org/support/topic/compatibility-issue-with-wpforms/
*
* @since 2.5.11
* When the HTML 'input' element 'value' attribute value
* is derived from 'label', footnotes need to be removed
* in the value of 'value'.
*/
$value_regex = '#(]+?value=["\'][^>]+?)' . self::$start_tag_regex . '[^>]+?' . self::$end_tag_regex . '#';
do {
$content = preg_replace( $value_regex, '$1', $content );
} while ( preg_match( $value_regex, $content ) );
/**
* Optionally moves footnotes outside at the end of the label element.
*
* - Bugfix: Forms: prevent inadvertently toggling input elements with footnotes in their label, by optionally moving footnotes after the end of the label.
*
* @since 2.5.12
* @link https://wordpress.org/support/topic/compatibility-issue-with-wpforms/#post-14212318
*/
$label_issue_solution = Footnotes_Settings::instance()->get( Footnotes_Settings::FOOTNOTES_LABEL_ISSUE_SOLUTION );
if ( 'move' === $label_issue_solution ) {
$move_regex = '#(