Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.66% |
26 / 29 |
|
100.00% |
3 / 3 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\Extensions\Markdown\register_block | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| Automattic\Jetpack\Extensions\Markdown\render_email | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| Automattic\Jetpack\Extensions\Markdown\make_content_email_safe | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Markdown Block. |
| 4 | * |
| 5 | * @since 6.8.0 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Markdown; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 0 ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Registers the block for use in Gutenberg |
| 20 | * This is done via an action so that we can disable |
| 21 | * registration if we need to. |
| 22 | */ |
| 23 | function register_block() { |
| 24 | Blocks::jetpack_register_block( |
| 25 | __DIR__, |
| 26 | array( |
| 27 | 'render_email_callback' => __NAMESPACE__ . '\render_email', |
| 28 | ) |
| 29 | ); |
| 30 | } |
| 31 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 32 | |
| 33 | /** |
| 34 | * Render the block for an email. |
| 35 | * |
| 36 | * @param string $block_content The saved block markup. |
| 37 | * @param array $parsed_block The parsed block. |
| 38 | * @param object $rendering_context The email rendering context. |
| 39 | * @return string|null Rendered markup, or null to let the email engine render the block. |
| 40 | */ |
| 41 | function render_email( $block_content, array $parsed_block, $rendering_context ) { |
| 42 | if ( ! class_exists( '\Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Fallback' ) ) { |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | $renderer = new \Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Fallback(); |
| 47 | |
| 48 | return $renderer->render( make_content_email_safe( (string) $block_content ), $parsed_block, $rendering_context ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Let a table or a code fence wrap, so it can't widen the whole email. |
| 53 | * |
| 54 | * Markdown saves rendered HTML, so these arrive without the block classes the email engine |
| 55 | * styles, and one unbreakable word in a cell or line sets the width of the whole message. |
| 56 | * |
| 57 | * @param string $html Block markup. |
| 58 | * @return string |
| 59 | */ |
| 60 | function make_content_email_safe( $html ) { |
| 61 | // Fixed layout splits the width into equal columns, so no column count can overflow a phone. |
| 62 | // It needs the width: without one, the table lays out as if the layout were auto. |
| 63 | $styles = array( |
| 64 | 'TABLE' => 'table-layout: fixed; width: 100%;', |
| 65 | 'TD' => 'word-break: break-word;', |
| 66 | 'TH' => 'word-break: break-word;', |
| 67 | 'PRE' => 'white-space: pre-wrap; word-break: break-word;', |
| 68 | ); |
| 69 | |
| 70 | $processor = new \WP_HTML_Tag_Processor( $html ); |
| 71 | |
| 72 | while ( $processor->next_tag() ) { |
| 73 | $tag = (string) $processor->get_tag(); |
| 74 | |
| 75 | if ( ! isset( $styles[ $tag ] ) ) { |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | $existing = rtrim( trim( (string) $processor->get_attribute( 'style' ) ), ';' ); |
| 80 | $processor->set_attribute( 'style', '' === $existing ? $styles[ $tag ] : $existing . '; ' . $styles[ $tag ] ); |
| 81 | |
| 82 | // Outlook's Word engine may ignore a table's CSS width, but it reads this attribute. |
| 83 | if ( 'TABLE' === $tag ) { |
| 84 | $processor->set_attribute( 'width', '100%' ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return $processor->get_updated_html(); |
| 89 | } |