get( MCI_Footnotes_Settings::C_INT_EXPERT_LOOKUP_THE_TITLE_PRIORITY_LEVEL ) ); $l_int_the_content_priority = intval( MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_INT_EXPERT_LOOKUP_THE_CONTENT_PRIORITY_LEVEL ) ); $l_int_the_excerpt_priority = intval( MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_INT_EXPERT_LOOKUP_THE_EXCERPT_PRIORITY_LEVEL ) ); $l_int_widget_title_priority = intval( MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_INT_EXPERT_LOOKUP_WIDGET_TITLE_PRIORITY_LEVEL ) ); $l_int_widget_text_priority = intval( MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_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', 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 ( MCI_Footnotes_Convert::to_bool( MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_EXPERT_LOOKUP_THE_TITLE ) ) ) { add_filter( 'the_title', array( $this, 'footnotes_in_title' ), $l_int_the_title_priority ); } // Configurable priority level for reference container relative positioning; default 98. if ( MCI_Footnotes_Convert::to_bool( MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_EXPERT_LOOKUP_THE_CONTENT ) ) ) { add_filter( 'the_content', array( $this, 'footnotes_in_content' ), $l_int_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 * @date 2021-01-05T1402+0100 * * 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' ), $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 * @date 2021-01-18T2038+0100 */ add_filter( 'pum_popup_content', array( $this, 'footnotes_in_content' ), $l_int_the_content_priority ); } if ( MCI_Footnotes_Convert::to_bool( MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_EXPERT_LOOKUP_THE_EXCERPT ) ) ) { add_filter( 'the_excerpt', array( $this, 'footnotes_in_excerpt' ), $l_int_the_excerpt_priority ); add_filter( 'get_the_excerpt', array( $this, 'footnotes_in_excerpt' ), $l_int_the_excerpt_priority ); } if ( MCI_Footnotes_Convert::to_bool( MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_EXPERT_LOOKUP_WIDGET_TITLE ) ) ) { add_filter( 'widget_title', array( $this, 'footnotes_in_widget_title' ), $l_int_widget_title_priority ); } if ( MCI_Footnotes_Convert::to_bool( MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_EXPERT_LOOKUP_WIDGET_TEXT ) ) ) { add_filter( 'widget_text', array( $this, 'footnotes_in_widget_text' ), $l_int_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 * @date 2020-11-08T1839+0100 * @accountable @pewgeuges */ // 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 * * @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 ( MCI_Footnotes::$a_bool_alternative_tooltips_enabled ) { // Start internal script. ?> get( MCI_Footnotes_Settings::C_STR_REFERENCE_CONTAINER_POSITION ) ) { echo $this->reference_container(); } // Get setting for love and share this plugin. $l_str_love_me_index = MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_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', MCI_Footnotes_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, MCI_Footnotes_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, MCI_Footnotes_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', MCI_Footnotes_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 = sprintf( '%s', $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( $p_str_content ) { // 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( $p_str_content ) { // phpcs:disable WordPress.PHP.YodaConditions.NotYoda // Appends the reference container if set to "post_end". return $this->exec( $p_str_content, 'post_end' === MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_REFERENCE_CONTAINER_POSITION ) ); // phpcs:enable WordPress.PHP.YodaConditions.NotYoda } /** * Replaces existing excerpt with a new one generated on the basis of the post. * * @since 1.5.0 * @param string $p_str_excerpt Excerpt content. * @return string $p_str_excerpt Excerpt as-is. * The input was already the processed excerpt, no more footnotes to search. * @since 2.6.2 Debug No option. * @since 2.6.3 Debug Yes option, the setting becomes fully effective. */ public function footnotes_in_excerpt( $p_str_excerpt ) { if ( MCI_Footnotes_Convert::to_bool( MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_FOOTNOTES_IN_EXCERPT ) ) ) { return $this->generate_excerpt_with_footnotes( $p_str_excerpt ); } else { return $this->generate_excerpt( $p_str_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 $p_str_content The post. * @return string $p_str_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( $p_str_content ) { // Discard existing excerpt and start on the basis of the post. $p_str_content = get_the_content( get_the_id() ); // Get 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. * * - 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 * @param string $p_str_content The post. * @return string $p_str_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( $p_str_content ) { // Discard existing excerpt and start on the basis of the post. $p_str_content = get_the_content( get_the_id() ); // Get 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 = '@' . mt_rand( 100000000, 2147483647 ) . '@'; $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_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( $p_str_content ) { // 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( $p_str_content ) { // phpcs:disable WordPress.PHP.YodaConditions.NotYoda // Appends the reference container if set to "post_end". return $this->exec( $p_str_content, 'post_end' === MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_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 $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. * @return string */ public function exec( $p_str_content, $p_bool_output_references = false, $p_bool_hide_footnotes_text = false ) { // Process content. $p_str_content = $this->search( $p_str_content, $p_bool_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 * @date 2020-12-13T2057+0100 * * - 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 * @date 2020-12-18T1434+0100 */ // Append the reference container or insert at shortcode. $l_str_reference_container_position_shortcode = MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_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, $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, MCI_Footnotes_Config::C_STR_NO_LOVE_SLUG ) ) { self::$a_bool_allow_love_me = false; $p_str_content = str_replace( MCI_Footnotes_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. * * - 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( $p_str_content ) { // Get footnotes start and end tag short codes. $l_str_starting_tag = MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_FOOTNOTES_SHORT_CODE_START ); $l_str_ending_tag = MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_FOOTNOTES_SHORT_CODE_END ); if ( 'userdefined' === $l_str_starting_tag || 'userdefined' === $l_str_ending_tag ) { $l_str_starting_tag = MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_FOOTNOTES_SHORT_CODE_START_USER_DEFINED ); $l_str_ending_tag = MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_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 * @param string $p_str_content Any content to be searched for footnotes. * @param bool $p_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( $p_str_content, $p_bool_hide_footnotes_text ) { // Get delimiter shortcodes and unify them. $p_str_content = self::unify_delimiters( $p_str_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 * @date 2021-01-07T0824+0100 * 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 ( MCI_Footnotes_Convert::to_bool( MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_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 .= '

“'; $l_str_syntax_error_warning .= $l_str_error_spot_string; $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. * * - 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'. */ $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. * * - 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 */ $l_str_label_issue_solution = MCI_Footnotes_Settings::instance()->get( MCI_Footnotes_Settings::C_STR_FOOTNOTES_LABEL_ISSUE_SOLUTION ); if ( 'move' === $l_str_label_issue_solution ) { $l_str_move_regex = '#(