Use CssInliner class instead of Emogrifier class

With the upgrade of the Emogrifier package we can't use the Emogrifier
class, since it was removed in v4 (we need to use at least v5 for
compatibility with PHP 8); so we now use the CssInliner class.

Note that this is a potential breaking change for consumers of the
woocommerce_emogrifier hook, which was getting an Emogrifier object
as parameter (now it gets a CssInliner object, which is partially
compatible).
This commit is contained in:
Nestor Soriano 2022-03-18 12:52:43 +01:00
parent 0f2b717462
commit fbc77160db
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
2 changed files with 15 additions and 9 deletions

View File

@ -5,6 +5,10 @@
* @package WooCommerce\Emails
*/
use Pelago\Emogrifier\CssInliner;
use Pelago\Emogrifier\HtmlProcessor\CssToAttributeConverter;
use Pelago\Emogrifier\HtmlProcessor\HtmlPruner;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
@ -559,18 +563,20 @@ class WC_Email extends WC_Settings_API {
wc_get_template( 'emails/email-styles.php' );
$css = apply_filters( 'woocommerce_email_styles', ob_get_clean(), $this );
$emogrifier_class = 'Pelago\\Emogrifier';
$css_inliner_class = CssInliner::class;
if ( $this->supports_emogrifier() && class_exists( $emogrifier_class ) ) {
if ( $this->supports_emogrifier() && class_exists( $css_inliner_class ) ) {
try {
$emogrifier = new $emogrifier_class( $content, $css );
$css_inliner = CssInliner::fromHtml( $content )->inlineCss( $css );
do_action( 'woocommerce_emogrifier', $emogrifier, $this );
do_action( 'woocommerce_emogrifier', $css_inliner, $this );
$content = $emogrifier->emogrify();
$html_prune = \Pelago\Emogrifier\HtmlProcessor\HtmlPruner::fromHtml( $content );
$html_prune->removeElementsWithDisplayNone();
$content = $html_prune->render();
$domDocument = $css_inliner->getDomDocument();
HtmlPruner::fromDomDocument( $domDocument )->removeElementsWithDisplayNone();
$content = CssToAttributeConverter::fromDomDocument( $domDocument )
->convertCssToVisualAttributes()
->render();
} catch ( Exception $e ) {
$logger = wc_get_logger();
$logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) );

View File

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body style="padding: 0;"><p class="text" style='color: #3c3c3c; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif;'>Hello World!</p></body>
<body style="padding: 0;"><p class="text" style='color: #3c3c3c; font-family: "Helvetica Neue",Helvetica,Roboto,Arial,sans-serif;'>Hello World!</p></body>
</html>