WC_Email::apply_inline_styleprivateWC 10.2.0

Apply inline styles to dynamic content using Emogrifier library (if supported).

Method of the class: WC_Email{}

Returns

String.

Usage

// private - for code of main (parent) class only
$result = $this->apply_inline_style( $content );
$content(string|null) (required)
Content that will receive inline styles.

Changelog

Since 10.2.0 Introduced.

WC_Email::apply_inline_style() code WC 10.8.1

private function apply_inline_style( $content ) {
	$css  = '';
	$css .= $this->get_must_use_css_styles();
	$css .= "\n";

	ob_start();
	wc_get_template( 'emails/email-styles.php' );
	$css .= ob_get_clean();

	/**
	 * Provides an opportunity to filter the CSS styles included in e-mails.
	 *
	 * @since 2.3.0
	 *
	 * @param string    $css   CSS code.
	 * @param \WC_Email $email E-mail instance.
	 */
	$css = apply_filters( 'woocommerce_email_styles', $css, $this );

	$css_inliner_class = CssInliner::class;

	if ( $this->supports_emogrifier() && class_exists( $css_inliner_class ) ) {
		try {
			$css_inliner = CssInliner::fromHtml( $content )->inlineCss( $css );

			/**
			 * Action hook fired when an email content has been processed by Emogrifier CssInliner instance.
			 *
			 * @since 4.1.0
			 *
			 * @param CssInliner $css_inliner CssInliner instance.
			 * @param WC_Email $email WC_Email instance.
			 */
			do_action( 'woocommerce_emogrifier', $css_inliner, $this );

			$dom_document = $css_inliner->getDomDocument();

			// When the email is rendered in the block editor, we don't want to remove the elements with display: none.
			// The main reason is using preview text in the email body which is hidden by default.
			if ( ! $this->block_email_editor_enabled ) {
				HtmlPruner::fromDomDocument( $dom_document )->removeElementsWithDisplayNone();
			}
			$content = CssToAttributeConverter::fromDomDocument( $dom_document )
				->convertCssToVisualAttributes()
				->render();
		} catch ( Exception $e ) {
			$logger = wc_get_logger();
			$logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) );
		}
	} else {
		$content = '<style type="text/css">' . $css . '</style>' . $content;
	}

	return $content;
}