MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Postprocessors

Variables_Postprocessor::postprocess()publicWC 1.0

Postprocess the HTML.

Method of the class: Variables_Postprocessor{}

No Hooks.

Return

String.

Usage

$Variables_Postprocessor = new Variables_Postprocessor();
$Variables_Postprocessor->postprocess( $html ): string;
$html(string) (required)
HTML to postprocess.

Variables_Postprocessor::postprocess() code WC 9.8.1

public function postprocess( string $html ): string {
	$variables    = $this->theme_controller->get_variables_values_map();
	$replacements = array();

	foreach ( $variables as $name => $value ) {
		$var_pattern                  = '/' . preg_quote( 'var(' . $name . ')', '/' ) . '/i';
		$replacements[ $var_pattern ] = $value;
	}

	// Pattern to match style attributes and their values.
	$callback = function ( $matches ) use ( $replacements ) {
		// For each match, replace CSS variables with their values.
		$style = $matches[1];
		$style = preg_replace( array_keys( $replacements ), array_values( $replacements ), $style );
		return 'style="' . esc_attr( $style ) . '"';
	};

	// We want to replace the CSS variables only in the style attributes to avoid replacing the actual content.
	$style_pattern     = '/style="(.*?)"/i';
	$style_pattern_alt = "/style='(.*?)'/i";
	$html              = (string) preg_replace_callback( $style_pattern, $callback, $html );
	$html              = (string) preg_replace_callback( $style_pattern_alt, $callback, $html );

	return $html;
}