wpseek.com
Outil de recherche WordPress pour les développeurs et auteurs de thèmes



excerpt_remove_footnotes › WordPress Function

Depuis6.3.0
Dépréciéen/a
› excerpt_remove_footnotes ( $content )
Paramètres:
  • (string) $content HTML which might contain footnote references from the block editor.
    Requis: Oui
Retourne:
  • (string) Given input HTML without the footnote references.
Défini(e) dans:
Codex:

Removes recognized footnote reference markup from a given string.

Example: $content = 'Never1 say never.'; 'Never say never' === excerpt_remove_footnotes( $content );


Source

function excerpt_remove_footnotes( $content ) {
	if ( ! str_contains( $content, 'data-fn' ) ) {
		return $content;
	}

	$processor = new class( $content ) extends WP_HTML_Tag_Processor {
		public function is_whitespace_text_node(): bool {
			if ( self::STATE_TEXT_NODE !== $this->parser_state ) {
				return false;
			}

			$this->subdivide_text_appropriately();
			return self::TEXT_IS_WHITESPACE === $this->text_node_classification;
		}

		public function get_span(): WP_HTML_Span {
			$this->set_bookmark( 'here' );

			return $this->bookmarks['here'];
		}
	};

	$output          = '';
	$at              = 0;
	$reprocess_token = false;

	while ( $reprocess_token || $processor->next_token() ) {
		$reprocess_token = false;

		// Match the <sup data-fn="..." class="..."> opening tag.
		if (
			$processor->is_tag_closer() ||
			'SUP' !== $processor->get_tag() ||
			null === $processor->get_attribute( 'data-fn' ) ||
			null === $processor->get_attribute( 'class' )
		) {
			continue;
		}

		$footnote_opening = $processor->get_span();

		if ( ! $processor->next_token() ) {
			break;
		}

		// Skip any whitespace-only text between the tags.
		if ( '#text' === $processor->get_token_name() ) {
			if ( ! $processor->is_whitespace_text_node() ) {
				$reprocess_token = true;
				continue;
			}

			$processor->next_token();
		}

		// Match the <a href="..." id="..."> opening tag.
		if (
			$processor->is_tag_closer() ||
			'A' !== $processor->get_token_name() ||
			null === $processor->get_attribute( 'href' ) ||
			null === $processor->get_attribute( 'id' )
		) {
			$reprocess_token = true;
			continue;
		}

		// Match an all-digit text node.
		if ( ! $processor->next_token() || '#text' !== $processor->get_token_name() ) {
			$reprocess_token = true;
			continue;
		}

		$footnote_number = $processor->get_modifiable_text();
		$digit_count     = strspn( $footnote_number, '0123456789' );
		if ( 0 === $digit_count || strlen( $footnote_number ) !== $digit_count ) {
			$reprocess_token = true;
			continue;
		}

		// Match the closing </a> tag.
		if (
			! $processor->next_token() ||
			'A' !== $processor->get_token_name() ||
			! $processor->is_tag_closer()
		) {
			$reprocess_token = true;
			continue;
		}

		if ( ! $processor->next_token() ) {
			break;
		}

		// Skip any whitespace-only text between the tags.
		if ( '#text' === $processor->get_token_name() ) {
			if ( ! $processor->is_whitespace_text_node() ) {
				$reprocess_token = true;
				continue;
			}

			$processor->next_token();
		}

		// Match the closing </sup> tag.
		if (
			'SUP' !== $processor->get_token_name() ||
			! $processor->is_tag_closer()
		) {
			$reprocess_token = true;
			continue;
		}

		$footnote_closing = $processor->get_span();

		// Remove the section and continue.
		$output .= substr( $content, $at, $footnote_opening->start - $at );
		$at      = $footnote_closing->start + $footnote_closing->length;
	}

	// No footnotes were found and removed: return the original input.
	if ( 0 === $at ) {
		return $content;
	}

	if ( $at < strlen( $content ) ) {
		$output .= substr( $content, $at );
	}

	return $output;
}