Engine
1 month ago
Integrations
1 month ago
Validator
1 year ago
class-bootstrap.php
2 months ago
class-container.php
7 months ago
class-email-css-inliner.php
7 months ago
class-email-editor-container.php
3 months ago
class-package.php
1 year ago
exceptions.php
1 year ago
class-email-css-inliner.php
93 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Email CSS Inliner class file. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\EmailEditor |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types = 1 ); |
| 9 | namespace Automattic\WooCommerce\EmailEditor; |
| 10 | |
| 11 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\Css_Inliner; |
| 12 | |
| 13 | /** |
| 14 | * Class for inlining CSS in HTML emails. |
| 15 | */ |
| 16 | class Email_Css_Inliner implements Css_Inliner { |
| 17 | |
| 18 | /** |
| 19 | * The CSS inliner instance. |
| 20 | * |
| 21 | * Runtime type: Pelago\Emogrifier\CssInliner | Automattic\WooCommerce\Vendor\Pelago\Emogrifier\CssInliner |
| 22 | * Both classes extend AbstractHtmlProcessor and implement: |
| 23 | * - static fromHtml(string $html): static |
| 24 | * - inlineCss(string $css = ''): self |
| 25 | * - render(): string |
| 26 | * |
| 27 | * @var object|null |
| 28 | */ |
| 29 | private $inliner; |
| 30 | |
| 31 | /** |
| 32 | * Creates a new instance from HTML content. |
| 33 | * |
| 34 | * @param string $unprocessed_html The HTML content to process. |
| 35 | * @return self |
| 36 | */ |
| 37 | public function from_html( string $unprocessed_html ): self { |
| 38 | $inliner_class = $this->get_inliner_class(); |
| 39 | $that = new self(); |
| 40 | $that->inliner = $inliner_class::fromHtml( $unprocessed_html ); |
| 41 | return $that; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Inlines the provided CSS. |
| 46 | * |
| 47 | * @param string $css The CSS to inline. |
| 48 | * @return self |
| 49 | * @throws \LogicException If from_html() was not called first. |
| 50 | */ |
| 51 | public function inline_css( string $css = '' ): self { |
| 52 | if ( ! isset( $this->inliner ) ) { |
| 53 | throw new \LogicException( 'You must call from_html before calling inline_css' ); |
| 54 | } |
| 55 | /** Ignore PHPStan analysis for dynamic inliner method call. @phpstan-ignore-next-line */ |
| 56 | $this->inliner->inlineCss( $css ); |
| 57 | return $this; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Renders the HTML with inlined CSS. |
| 62 | * |
| 63 | * @return string The processed HTML. |
| 64 | * @throws \LogicException If from_html() was not called first. |
| 65 | */ |
| 66 | public function render(): string { |
| 67 | if ( ! isset( $this->inliner ) ) { |
| 68 | throw new \LogicException( 'You must call from_html before calling render' ); |
| 69 | } |
| 70 | /** Ignore PHPStan analysis for dynamic inliner method call. @phpstan-ignore-next-line */ |
| 71 | return $this->inliner->render(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get the inliner class. |
| 76 | * |
| 77 | * Returns the fully qualified class name for the available CSS inliner. |
| 78 | * Runtime return type: 'Pelago\Emogrifier\CssInliner' | 'Automattic\WooCommerce\Vendor\Pelago\Emogrifier\CssInliner' |
| 79 | * |
| 80 | * @return string Fully qualified class name |
| 81 | * @throws \Exception If the inliner class is not found. |
| 82 | */ |
| 83 | private function get_inliner_class(): string { |
| 84 | if ( class_exists( 'Automattic\WooCommerce\Vendor\Pelago\Emogrifier\CssInliner' ) ) { |
| 85 | return 'Automattic\WooCommerce\Vendor\Pelago\Emogrifier\CssInliner'; |
| 86 | } |
| 87 | if ( class_exists( 'Automattic\WooCommerce\EmailEditorVendor\Pelago\Emogrifier\CssInliner' ) ) { |
| 88 | return 'Automattic\WooCommerce\EmailEditorVendor\Pelago\Emogrifier\CssInliner'; |
| 89 | } |
| 90 | throw new \Exception( 'CssInliner class not found' ); |
| 91 | } |
| 92 | } |
| 93 |