wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



wp_finalize_template_enhancement_output_buffer › WordPress Function

Seit6.9.0
Veraltetn/v
wp_finalize_template_enhancement_output_buffer ( $output, $phase )
Parameter: (2)
  • (string) $output Output buffer.
    Erforderlich: Ja
  • (int) $phase Phase.
    Erforderlich: Ja
Siehe:
Gibt zurück:
  • (string) Finalized output buffer.
Definiert in:
Codex:

Finalizes the template enhancement output buffer.

Checks to see if the output buffer is complete and contains HTML. If so, runs the content through the wp_template_enhancement_output_buffer filter. If not, the original content is returned.


Quellcode

function wp_finalize_template_enhancement_output_buffer( string $output, int $phase ): string {
	// When the output is being cleaned (e.g. pending template is replaced with error page), do not send it through the filter.
	if ( ( $phase & PHP_OUTPUT_HANDLER_CLEAN ) !== 0 ) {
		return $output;
	}

	// Detect if the response is an HTML content type.
	$is_html_content_type = null;
	$html_content_types   = array( 'text/html', 'application/xhtml+xml' );
	foreach ( headers_list() as $header ) {
		$header_parts = preg_split( '/\s*[:;]\s*/', strtolower( $header ) );
		if (
			is_array( $header_parts ) &&
			count( $header_parts ) >= 2 &&
			'content-type' === $header_parts[0]
		) {
			$is_html_content_type = in_array( $header_parts[1], $html_content_types, true );
			break; // PHP only sends the first Content-Type header in the list.
		}
	}
	if ( null === $is_html_content_type ) {
		$is_html_content_type = in_array( ini_get( 'default_mimetype' ), $html_content_types, true );
	}

	// If the content type is not HTML, short-circuit since it is not relevant for enhancement.
	if ( ! $is_html_content_type ) {
		return $output;
	}

	$filtered_output = $output;

	/**
	 * Filters the template enhancement output buffer prior to sending to the client.
	 *
	 * This filter only applies the HTML output of an included template. This filter is a progressive enhancement
	 * intended for applications such as optimizing markup to improve frontend page load performance. Sites must not
	 * depend on this filter applying since they may opt to stream the responses instead. Callbacks for this filter are
	 * highly discouraged from using regular expressions to do any kind of replacement on the output. Use the HTML API
	 * (either `WP_HTML_Tag_Processor` or `WP_HTML_Processor`), or else use {@see DOM\HtmlDocument} as of PHP 8.4 which
	 * fully supports HTML5.
	 *
	 * @since 6.9.0
	 *
	 * @param string $filtered_output HTML template enhancement output buffer.
	 * @param string $output          Original HTML template output buffer.
	 */
	return (string) apply_filters( 'wp_template_enhancement_output_buffer', $filtered_output, $output );
}