MailPoet\EmailEditor\Engine

Theme_Controller::get_stylesheet_for_rendering()publicWC 1.0

Get stylesheet for rendering.

Method of the class: Theme_Controller{}

No Hooks.

Return

String.

Usage

$Theme_Controller = new Theme_Controller();
$Theme_Controller->get_stylesheet_for_rendering( ?WP_Post $post, $template ): string;
?WP_Post $post **
-
Default: null
$template(WP_Block_Template|null)
Template object.
Default: null

Theme_Controller::get_stylesheet_for_rendering() code WC 9.8.1

public function get_stylesheet_for_rendering( ?WP_Post $post = null, $template = null ): string {
	$email_theme_settings = $this->get_settings();

	$css_presets = '';
	// Font family classes.
	foreach ( $email_theme_settings['typography']['fontFamilies']['default'] as $font_family ) {
		$css_presets .= ".has-{$font_family['slug']}-font-family { font-family: {$font_family['fontFamily']}; } \n";
	}
	// Font size classes.
	foreach ( $email_theme_settings['typography']['fontSizes']['default'] as $font_size ) {
		$css_presets .= ".has-{$font_size['slug']}-font-size { font-size: {$font_size['size']}; } \n";
	}
	// Color palette classes.
	$color_definitions = array_merge( $email_theme_settings['color']['palette']['theme'], $email_theme_settings['color']['palette']['default'] );
	foreach ( $color_definitions as $color ) {
		$css_presets .= ".has-{$color['slug']}-color { color: {$color['color']}; } \n";
		$css_presets .= ".has-{$color['slug']}-background-color { background-color: {$color['color']}; } \n";
		$css_presets .= ".has-{$color['slug']}-border-color { border-color: {$color['color']}; } \n";
	}

	// Block specific styles.
	$css_blocks = '';
	$blocks     = $this->get_theme()->get_styles_block_nodes();
	foreach ( $blocks as $block_metadata ) {
		$css_blocks .= $this->get_theme()->get_styles_for_block( $block_metadata );
	}

	// Element specific styles.
	$elements_styles = $this->get_theme()->get_raw_data()['styles']['elements'] ?? array();

	// Because the section styles is not a part of the output the `get_styles_block_nodes` method, we need to get it separately.
	if ( $template && $template->wp_id ) {
		$template_theme    = (array) get_post_meta( $template->wp_id, 'mailpoet_email_theme', true );
		$template_styles   = (array) ( $template_theme['styles'] ?? array() );
		$template_elements = $template_styles['elements'] ?? array();
		$elements_styles   = array_replace_recursive( (array) $elements_styles, (array) $template_elements );
	}

	if ( $post ) {
		$post_theme      = (array) get_post_meta( $post->ID, 'mailpoet_email_theme', true );
		$post_styles     = (array) ( $post_theme['styles'] ?? array() );
		$post_elements   = $post_styles['elements'] ?? array();
		$elements_styles = array_replace_recursive( (array) $elements_styles, (array) $post_elements );
	}

	$css_elements = '';
	foreach ( $elements_styles as $key => $elements_style ) {
		$selector = $key;

		if ( 'button' === $key ) {
			$selector      = '.wp-block-button';
			$css_elements .= wp_style_engine_get_styles( $elements_style, array( 'selector' => '.wp-block-button' ) )['css'];
			// Add color to link element.
			$css_elements .= wp_style_engine_get_styles( array( 'color' => array( 'text' => $elements_style['color']['text'] ?? '' ) ), array( 'selector' => '.wp-block-button a' ) )['css'];
			continue;
		}

		switch ( $key ) {
			case 'heading':
				$selector = 'h1, h2, h3, h4, h5, h6';
				break;
			case 'link':
				$selector = 'a:not(.button-link)';
				break;
		}

		$css_elements .= wp_style_engine_get_styles( $elements_style, array( 'selector' => $selector ) )['css'];
	}

	$result = $css_presets . $css_blocks . $css_elements;
	// Because font-size can by defined by the clamp() function that is not supported in the e-mail clients, we need to replace it to the value.
	// Regular expression to match clamp() function and capture its max value.
	$pattern = '/clamp\([^,]+,\s*[^,]+,\s*([^)]+)\)/';
	// Replace clamp() with its maximum value.
	$result = (string) preg_replace( $pattern, '$1', $result );
	return $result;
}