register_hooks();
}
/**
* Register WordPress hooks to replace Footnotes in the content of a public page.
*
* @since 1.5.0
* @since 1.5.4 Add support for @see 'the_post' hook.
* @since 2.0.5 Enable all hooks by default.
* @since 2.1.0 Remove @see 'the_post' support.
* @todo Move to {@see General}.
*/
public function register_hooks(): void {
// Get values from settings.
$l_int_the_title_priority = (int) Includes\Settings::instance()->get( \footnotes\includes\Settings::C_INT_EXPERT_LOOKUP_THE_TITLE_PRIORITY_LEVEL );
$l_int_the_content_priority = (int) Includes\Settings::instance()->get( \footnotes\includes\Settings::C_INT_EXPERT_LOOKUP_THE_CONTENT_PRIORITY_LEVEL );
$l_int_the_excerpt_priority = (int) Includes\Settings::instance()->get( \footnotes\includes\Settings::C_INT_EXPERT_LOOKUP_THE_EXCERPT_PRIORITY_LEVEL );
$l_int_widget_title_priority = (int) Includes\Settings::instance()->get( \footnotes\includes\Settings::C_INT_EXPERT_LOOKUP_WIDGET_TITLE_PRIORITY_LEVEL );
$l_int_widget_text_priority = (int) Includes\Settings::instance()->get( \footnotes\includes\Settings::C_INT_EXPERT_LOOKUP_WIDGET_TEXT_PRIORITY_LEVEL );
// PHP_INT_MAX can be set by -1.
$l_int_the_title_priority = ( -1 === $l_int_the_title_priority ) ? PHP_INT_MAX : $l_int_the_title_priority;
$l_int_the_content_priority = ( -1 === $l_int_the_content_priority ) ? PHP_INT_MAX : $l_int_the_content_priority;
$l_int_the_excerpt_priority = ( -1 === $l_int_the_excerpt_priority ) ? PHP_INT_MAX : $l_int_the_excerpt_priority;
$l_int_widget_title_priority = ( -1 === $l_int_widget_title_priority ) ? PHP_INT_MAX : $l_int_widget_title_priority;
$l_int_widget_text_priority = ( -1 === $l_int_widget_text_priority ) ? PHP_INT_MAX : $l_int_widget_text_priority;
// Append custom css to the header.
add_filter(
'wp_head',
fn() => $this->footnotes_output_head(),
PHP_INT_MAX
);
// Append the love and share me slug to the footer.
add_filter(
'wp_footer',
fn() => $this->footnotes_output_footer(),
PHP_INT_MAX
);
if ( Includes\Convert::to_bool( Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_EXPERT_LOOKUP_THE_TITLE ) ) ) {
add_filter(
'the_title',
fn(string $p_str_content): string => $this->footnotes_in_title( $p_str_content ),
$l_int_the_title_priority
);
}
// Configurable priority level for reference container relative positioning; default 98.
if ( Includes\Convert::to_bool( Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_EXPERT_LOOKUP_THE_CONTENT ) ) ) {
add_filter(
'the_content',
fn(string $p_str_content): string => $this->footnotes_in_content( $p_str_content ),
$l_int_the_content_priority
);
/**
* Hook for category pages.
*
* 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/
*
* @since 2.5.0
*/
add_filter(
'term_description',
fn(string $p_str_content): string => $this->footnotes_in_content( $p_str_content ),
$l_int_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',
fn(string $p_str_content): string => $this->footnotes_in_content( $p_str_content ),
$l_int_the_content_priority
);
}
if ( Includes\Convert::to_bool( Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_EXPERT_LOOKUP_THE_EXCERPT ) ) ) {
/**
* Adds a filter to the excerpt hook.
*
* @since 1.5.0 The hook @see 'get_the_excerpt' is filtered too.
* @since 1.5.5 The hook @see 'get_the_excerpt' is removed but not documented in changelog or docblock.
* @since 2.6.2 The hook @see 'get_the_excerpt' is readded when attempting to debug excerpt handling.
* @since 2.6.6 The hook @see 'get_the_excerpt' is removed again because it seems to cause issues in some themes.
*/
add_filter(
'the_excerpt',
fn(string $p_str_excerpt): string => $this->footnotes_in_excerpt( $p_str_excerpt ),
$l_int_the_excerpt_priority
);
}
if ( Includes\Convert::to_bool( Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_EXPERT_LOOKUP_WIDGET_TITLE ) ) ) {
/**
* TODO
*/
add_filter(
'widget_title',
fn(string $p_str_content): string => $this->footnotes_in_widget_title( $p_str_content ),
$l_int_widget_title_priority
);
}
if ( Includes\Convert::to_bool( Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_EXPERT_LOOKUP_WIDGET_TEXT ) ) ) {
/**
* TODO
*/
add_filter(
'widget_text',
fn(string $p_str_content): string => $this->footnotes_in_widget_text( $p_str_content ),
$l_int_widget_text_priority
);
}
// Reset stored footnotes when displaying the header.
self::$a_arr_footnotes = array();
self::$a_bool_allow_love_me = true;
}
/**
* Outputs the custom css to the header of the public page.
*
* @since 1.5.0
* @todo Refactor to enqueue stylesheets properly in {@see General}.
*/
public function footnotes_output_head(): void {
// Insert start tag without switching out of PHP.
echo "\r\n\r\n";
/*
* Alternative tooltip implementation relying on plain JS and CSS transitions.
*
* The script for alternative tooltips is printed formatted, not minified,
* for transparency. It isn’t indented though (the PHP open tag neither).
*/
if ( General::$a_bool_alternative_tooltips_enabled ) {
// Start internal script.
?>
get( \footnotes\includes\Settings::C_STR_REFERENCE_CONTAINER_POSITION ) ) {
echo $this->reference_container();
}
// Get setting for love and share this plugin.
$l_str_love_me_index = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_LOVE );
// Check if the admin allows to add a link to the footer.
if ( empty( $l_str_love_me_index ) || 'no' === strtolower( $l_str_love_me_index ) || ! self::$a_bool_allow_love_me ) {
return;
}
// Set a hyperlink to the word "footnotes" in the Love slug.
$l_str_linked_name = sprintf( '%s', \footnotes\includes\Config::C_STR_PLUGIN_PUBLIC_NAME );
// Get random love me text.
if ( 'random' === strtolower( $l_str_love_me_index ) ) {
$l_str_love_me_index = 'text-' . wp_rand( 1, 7 );
}
switch ( $l_str_love_me_index ) {
// Options named wrt backcompat, simplest is default.
case 'text-1':
/* Translators: 2: Link to plugin page 1: Love heart symbol */
$l_str_love_me_text = sprintf( __( 'I %2$s %1$s', 'footnotes' ), $l_str_linked_name, \footnotes\includes\Config::C_STR_LOVE_SYMBOL );
break;
case 'text-2':
/* Translators: %s: Link to plugin page */
$l_str_love_me_text = sprintf( __( 'This website uses the awesome %s plugin.', 'footnotes' ), $l_str_linked_name );
break;
case 'text-4':
/* Translators: 1: Link to plugin page 2: Love heart symbol */
$l_str_love_me_text = sprintf( '%1$s %2$s', $l_str_linked_name, \footnotes\includes\Config::C_STR_LOVE_SYMBOL );
break;
case 'text-5':
/* Translators: 1: Love heart symbol 2: Link to plugin page */
$l_str_love_me_text = sprintf( '%1$s %2$s', \footnotes\includes\Config::C_STR_LOVE_SYMBOL, $l_str_linked_name );
break;
case 'text-6':
/* Translators: %s: Link to plugin page */
$l_str_love_me_text = sprintf( __( 'This website uses %s.', 'footnotes' ), $l_str_linked_name );
break;
case 'text-7':
/* Translators: %s: Link to plugin page */
$l_str_love_me_text = sprintf( __( 'This website uses the %s plugin.', 'footnotes' ), $l_str_linked_name );
break;
case 'text-3':
default:
/* Translators: %s: Link to plugin page */
$l_str_love_me_text = $l_str_linked_name;
break;
}
echo sprintf( '
%s
', $l_str_love_me_text );
}
/**
* Replaces footnotes in the post/page title.
*
* @since 1.5.0
*
* @param string $p_str_content Title.
* @return string $p_str_content Title with replaced footnotes.
*/
public function footnotes_in_title( string $p_str_content ): string {
// Appends the reference container if set to "post_end".
return $this->exec( $p_str_content, false );
}
/**
* Replaces footnotes in the content of the current page/post.
*
* @since 1.5.0
*
* @param string $p_str_content Page/Post content.
* @return string $p_str_content Content with replaced footnotes.
*/
public function footnotes_in_content( string $p_str_content ): string {
$l_str_ref_container_position = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_REFERENCE_CONTAINER_POSITION );
$l_str_footnote_section_shortcode = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTE_SECTION_SHORTCODE );
$l_int_footnote_section_shortcode_length = strlen( $l_str_footnote_section_shortcode );
if ( !str_contains( $p_str_content, (string) $l_str_footnote_section_shortcode ) ) {
// phpcs:disable WordPress.PHP.YodaConditions.NotYoda
// Appends the reference container if set to "post_end".
return $this->exec( $p_str_content, 'post_end' === $l_str_ref_container_position );
// phpcs:enable WordPress.PHP.YodaConditions.NotYoda
} else {
$l_str_rest_content = $p_str_content;
$l_arr_sections_raw = array();
$l_arr_sections_processed = array();
do {
$l_int_section_end = strpos( $l_str_rest_content, (string) $l_str_footnote_section_shortcode );
$l_arr_sections_raw[] = substr( $l_str_rest_content, 0, $l_int_section_end );
$l_str_rest_content = substr( $l_str_rest_content, $l_int_section_end + $l_int_footnote_section_shortcode_length );
} while ( str_contains( $l_str_rest_content, (string) $l_str_footnote_section_shortcode ) );
$l_arr_sections_raw[] = $l_str_rest_content;
foreach ( $l_arr_sections_raw as $l_str_section ) {
$l_arr_sections_processed[] = self::exec( $l_str_section, true );
}
return implode( $l_arr_sections_processed );
}
}
/**
* Processes existing excerpt or replaces it with a new one generated on the basis of the post.
*
* 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 backwards-compatible with the initial setup.
*
* @since 1.5.0
*
* @param string $p_str_excerpt Excerpt content.
* @return string $p_str_excerpt Processed or new excerpt.
*/
public function footnotes_in_excerpt( string $p_str_excerpt ): string {
$l_str_excerpt_mode = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_IN_EXCERPT );
if ( 'yes' === $l_str_excerpt_mode ) {
return $this->generate_excerpt_with_footnotes( $p_str_excerpt );
} elseif ( 'no' === $l_str_excerpt_mode ) {
return $this->generate_excerpt( $p_str_excerpt );
} else {
return $this->exec( $p_str_excerpt );
}
}
/**
* Generates excerpt on the basis 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/
*
* @since 2.6.2
*
* @param string $p_str_content The post.
* @return string $p_str_content An excerpt of the post.
*/
public function generate_excerpt( string $p_str_content ): string {
// Discard existing excerpt and start on the basis of the post.
$p_str_content = get_the_content( get_the_id() );
// Get footnote delimiter shortcodes and unify them.
$p_str_content = self::unify_delimiters( $p_str_content );
// Remove footnotes.
$p_str_content = preg_replace( '#' . self::$a_str_start_tag_regex . '.+?' . self::$a_str_end_tag_regex . '#', '', $p_str_content );
// Apply WordPress excerpt processing.
$p_str_content = strip_shortcodes( $p_str_content );
$p_str_content = excerpt_remove_blocks( $p_str_content );
// Here the footnotes would be processed as part of WordPress content processing.
$p_str_content = apply_filters( 'the_content', $p_str_content );
// According to Advanced Excerpt, this is some kind of precaution against malformed CDATA in RSS feeds.
$p_str_content = str_replace( ']]>', ']]>', $p_str_content );
$l_int_excerpt_length = (int) _x( '55', 'excerpt_length' );
$l_int_excerpt_length = (int) apply_filters( 'excerpt_length', $l_int_excerpt_length );
$l_str_excerpt_more = apply_filters( 'excerpt_more', ' […]' );
// Function wp_trim_words() calls wp_strip_all_tags() that wrecks the footnotes.
$p_str_content = wp_trim_words( $p_str_content, $l_int_excerpt_length, $l_str_excerpt_more );
return $p_str_content;
}
/**
* Generates excerpt with footnotes on the basis 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/
*
* @since 2.6.3
*
* @param string $p_str_content The post.
* @return string $p_str_content An excerpt of the post.
*/
public function generate_excerpt_with_footnotes( string $p_str_content ): string {
// Discard existing excerpt and start on the basis of the post.
$p_str_content = get_the_content( get_the_id() );
// Get footnote delimiter shortcodes and unify them.
$p_str_content = self::unify_delimiters( $p_str_content );
// Apply WordPress excerpt processing.
$p_str_content = strip_shortcodes( $p_str_content );
$p_str_content = excerpt_remove_blocks( $p_str_content );
// But do not process footnotes at this point; do only this.
$p_str_content = str_replace( ']]>', ']]>', $p_str_content );
// Prepare the excerpt length argument.
$l_int_excerpt_length = (int) _x( '55', 'excerpt_length' );
$l_int_excerpt_length = (int) apply_filters( 'excerpt_length', $l_int_excerpt_length );
// Prepare the Read-on string.
$l_str_excerpt_more = apply_filters( 'excerpt_more', ' […]' );
// Safeguard the footnotes.
preg_match_all(
'#' . self::$a_str_start_tag_regex . '.+?' . self::$a_str_end_tag_regex . '#',
$p_str_content,
$p_arr_saved_footnotes
);
// Prevent the footnotes from altering the excerpt: previously hard-coded '5ED84D6'.
$l_int_placeholder = '@' . wp_rand( 100_000_000, 2_147_483_647 ) . '@';
$p_str_content = preg_replace(
'#' . self::$a_str_start_tag_regex . '.+?' . self::$a_str_end_tag_regex . '#',
$l_int_placeholder,
$p_str_content
);
// Replace line breaking markup with a separator.
$l_str_separator = ' ';
$p_str_content = preg_replace( '# #', $l_str_separator, $p_str_content );
$p_str_content = preg_replace( '# #', $l_str_separator, $p_str_content );
$p_str_content = preg_replace( '#<(p|li|div)[^>]*>#', $l_str_separator, $p_str_content );
$p_str_content = preg_replace( '#' . $l_str_separator . '#', '', $p_str_content, 1 );
$p_str_content = preg_replace( '#(p|li|div) *>#', '', $p_str_content );
$p_str_content = preg_replace( '#[\r\n]#', '', $p_str_content );
// To count words like Advanced Excerpt does it.
$l_arr_tokens = array();
$l_str_output = '';
$l_int_counter = 0;
// Tokenize into tags and words as in Advanced Excerpt.
preg_match_all( '#(<[^>]+>|[^<>\s]+)\s*#u', $p_str_content, $l_arr_tokens );
// Count words following one option of Advanced Excerpt.
foreach ( $l_arr_tokens[0] as $l_str_token ) {
if ( $l_int_counter >= $l_int_excerpt_length ) {
break;
}
// If token is not a tag, increment word count.
if ( '<' !== $l_str_token[0] ) {
$l_int_counter++;
}
// Append the token to the output.
$l_str_output .= $l_str_token;
}
// Complete unbalanced markup, used by Advanced Excerpt.
$p_str_content = force_balance_tags( $l_str_output );
// Readd footnotes in excerpt.
$l_int_index = 0;
while ( 0 !== preg_match( '#' . $l_int_placeholder . '#', $p_str_content ) ) {
$p_str_content = preg_replace(
'#' . $l_int_placeholder . '#',
$p_arr_saved_footnotes[0][ $l_int_index ],
$p_str_content,
1
);
$l_int_index++;
}
// Append the Read-on string as in wp_trim_words().
$p_str_content .= $l_str_excerpt_more;
// Process readded footnotes without appending the reference container.
$p_str_content = self::exec( $p_str_content, false );
return $p_str_content;
}
/**
* Replaces footnotes in the widget title.
*
* @since 1.5.0
*
* @param string $p_str_content Widget content.
* @return string $p_str_content Content with replaced footnotes.
*/
public function footnotes_in_widget_title( string $p_str_content ): string {
// Appends the reference container if set to "post_end".
return $this->exec( $p_str_content, false );
}
/**
* Replaces footnotes in the content of the current widget.
*
* @since 1.5.0
*
* @param string $p_str_content Widget content.
* @return string $p_str_content Content with replaced footnotes.
*/
public function footnotes_in_widget_text( string $p_str_content ): string {
// phpcs:disable WordPress.PHP.YodaConditions.NotYoda
// Appends the reference container if set to "post_end".
return $this->exec( $p_str_content, 'post_end' === Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_REFERENCE_CONTAINER_POSITION ) );
// phpcs:enable WordPress.PHP.YodaConditions.NotYoda
}
/**
* Replaces all footnotes that occur in the given content.
*
* @since 1.5.0
*
* @param string $p_str_content Any string that may contain footnotes to be replaced.
* @param bool $p_bool_output_references Appends the Reference Container to the output if set to true, default true.
* @param bool $p_bool_hide_footnotes_text Hide footnotes found in the string.
*/
public function exec( string $p_str_content, bool $p_bool_output_references = false, bool $p_bool_hide_footnotes_text = false ): string {
// Process content.
$p_str_content = $this->search( $p_str_content, $p_bool_hide_footnotes_text );
/*
* Reference container customized positioning through shortcode.
*/
// Append the reference container or insert at shortcode.
$l_str_reference_container_position_shortcode = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_REFERENCE_CONTAINER_POSITION_SHORTCODE );
if ( empty( $l_str_reference_container_position_shortcode ) ) {
$l_str_reference_container_position_shortcode = '[[references]]';
}
if ( $p_bool_output_references ) {
if ( strpos( $p_str_content, (string) $l_str_reference_container_position_shortcode ) ) {
$p_str_content = str_replace( $l_str_reference_container_position_shortcode, $this->reference_container(), $p_str_content );
} else {
$p_str_content .= $this->reference_container();
}
// Increment the container ID.
self::$a_int_reference_container_id++;
}
// Delete position shortcode should any remain.
$p_str_content = str_replace( $l_str_reference_container_position_shortcode, '', $p_str_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( $p_str_content, \footnotes\includes\Config::C_STR_NO_LOVE_SLUG ) ) {
self::$a_bool_allow_love_me = false;
$p_str_content = str_replace( \footnotes\includes\Config::C_STR_NO_LOVE_SLUG, '', $p_str_content );
}
// Return the content with replaced footnotes and optional reference container appended.
return $p_str_content;
}
/**
* Brings the delimiters and unifies their various HTML escapement schemas.
*
* 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.
*
* @since 2.1.14
*
* @param string $p_str_content The footnote, including delimiters.
*/
public function unify_delimiters( string $p_str_content ): string {
// Get footnotes start and end tag short codes.
$l_str_starting_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_START );
$l_str_ending_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_END );
if ( 'userdefined' === $l_str_starting_tag || 'userdefined' === $l_str_ending_tag ) {
$l_str_starting_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_START_USER_DEFINED );
$l_str_ending_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_END_USER_DEFINED );
}
// If any footnotes short code is empty, return the content without changes.
if ( empty( $l_str_starting_tag ) || empty( $l_str_ending_tag ) ) {
return $p_str_content;
}
if ( preg_match( '#[&"\'<>]#', $l_str_starting_tag . $l_str_ending_tag ) ) {
$l_str_harmonized_start_tag = '{[(|fnote_stt|)]}';
$l_str_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).
$p_str_content = str_replace( $l_str_starting_tag, $l_str_harmonized_start_tag, $p_str_content );
$p_str_content = str_replace( $l_str_ending_tag, $l_str_harmonized_end_tag, $p_str_content );
// Harmonize footnotes while escaping HTML special characters in delimiter shortcodes.
// The footnote has been added in the Classic Editor visual mode.
$p_str_content = str_replace( htmlspecialchars( $l_str_starting_tag ), $l_str_harmonized_start_tag, $p_str_content );
$p_str_content = str_replace( htmlspecialchars( $l_str_ending_tag ), $l_str_harmonized_end_tag, $p_str_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.
$p_str_content = str_replace( str_replace( '>', '>', htmlspecialchars( $l_str_starting_tag ) ), $l_str_harmonized_start_tag, $p_str_content );
$p_str_content = str_replace( str_replace( '>', '>', htmlspecialchars( $l_str_ending_tag ) ), $l_str_harmonized_end_tag, $p_str_content );
// Assign the delimiter shortcodes.
self::$a_str_start_tag = $l_str_harmonized_start_tag;
self::$a_str_end_tag = $l_str_harmonized_end_tag;
// Assign the regex-conformant shortcodes.
self::$a_str_start_tag_regex = '\{\[\(\|fnote_stt\|\)\]\}';
self::$a_str_end_tag_regex = '\{\[\(\|fnote_end\|\)\]\}';
} else {
// Assign the delimiter shortcodes.
self::$a_str_start_tag = $l_str_starting_tag;
self::$a_str_end_tag = $l_str_ending_tag;
// Make shortcodes conform to regex syntax.
self::$a_str_start_tag_regex = preg_replace( '#([\(\)\{\}\[\]\|\*\.\?\!])#', '\\\\$1', self::$a_str_start_tag );
self::$a_str_end_tag_regex = preg_replace( '#([\(\)\{\}\[\]\|\*\.\?\!])#', '\\\\$1', self::$a_str_end_tag );
}
return $p_str_content;
}
/**
* Replaces all footnotes in the given content and appends them to the static property.
*
* @since 1.5.0
* @todo Refactor to parse DOM rather than using RegEx.
* @todo Decompose.
*
* @param string $p_str_content Any content to be parsed for footnotes.
* @param bool $p_bool_hide_footnotes_text Hide footnotes found in the string.
*/
public function search( string $p_str_content, bool $p_bool_hide_footnotes_text ): string {
// Get footnote delimiter shortcodes and unify them.
$p_str_content = self::unify_delimiters( $p_str_content );
/*
* Checks for balanced footnote delimiters; delimiter syntax validation.
*
* 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 ( Includes\Convert::to_bool( Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTE_SHORTCODE_SYNTAX_VALIDATION_ENABLE ) ) ) {
// Apply different regex depending on whether start shortcode is double/triple opening parenthesis.
if ( '((' === self::$a_str_start_tag || '(((' === self::$a_str_start_tag ) {
// This prevents from catching a script containing e.g. a double opening parenthesis.
$l_str_validation_regex = '#' . self::$a_str_start_tag_regex . '(((?!' . self::$a_str_end_tag_regex . ')[^\{\}])*?)(' . self::$a_str_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.
$l_str_validation_regex = '#' . self::$a_str_start_tag_regex . '(((?!' . self::$a_str_end_tag_regex . ').)*?)(' . self::$a_str_start_tag_regex . '|$)#s';
}
// Check syntax and get error locations.
preg_match( $l_str_validation_regex, $p_str_content, $p_arr_error_location );
if ( empty( $p_arr_error_location ) ) {
self::$a_bool_syntax_error_flag = false;
}
// Prevent generating and inserting the warning multiple times.
if ( self::$a_bool_syntax_error_flag ) {
// Get plain text string for error location.
$l_str_error_spot_string = wp_strip_all_tags( $p_arr_error_location[1] );
// Limit string length to 300 characters.
if ( strlen( $l_str_error_spot_string ) > 300 ) {
$l_str_error_spot_string = substr( $l_str_error_spot_string, 0, 299 ) . '…';
}
// Compose warning box.
$l_str_syntax_error_warning = '
';
$l_str_syntax_error_warning .= __( 'WARNING: unbalanced footnote start tag short code found.', 'footnotes' );
$l_str_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 */
$l_str_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' ) );
$l_str_syntax_error_warning .= '
';
$l_str_syntax_error_warning .= __( 'Unbalanced start tag short code found before:', 'footnotes' );
$l_str_syntax_error_warning .= '
';
// Prepend the warning box to the content.
$p_str_content = $l_str_syntax_error_warning . $p_str_content;
// Checked, set flag to false to prevent duplicate warning.
self::$a_bool_syntax_error_flag = false;
return $p_str_content;
}
}
/*
* Patch to allow footnotes in input field labels.
*
* When the HTML 'input' element 'value' attribute value is derived from
* 'label', footnotes need to be removed in the value of 'value'.
*/
$l_str_value_regex = '#(]+?value=["\'][^>]+?)' . self::$a_str_start_tag_regex . '[^>]+?' . self::$a_str_end_tag_regex . '#';
do {
$p_str_content = preg_replace( $l_str_value_regex, '$1', $p_str_content );
} while ( preg_match( $l_str_value_regex, $p_str_content ) );
// Optionally moves footnotes outside at the end of the label element.
$l_str_label_issue_solution = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_LABEL_ISSUE_SOLUTION );
if ( 'move' === $l_str_label_issue_solution ) {
$l_str_move_regex = '#(