class-dom-document-helper.php
1 year ago
class-html-processing-helper.php
4 months ago
class-social-links-helper.php
1 year ago
class-styles-helper.php
4 months ago
class-table-wrapper-helper.php
11 months ago
class-table-wrapper-helper.php
139 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file is part of the WooCommerce Email Editor package |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\EmailEditor |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types = 1 ); |
| 9 | namespace Automattic\WooCommerce\EmailEditor\Integrations\Utils; |
| 10 | |
| 11 | /** |
| 12 | * Helper class for generating table wrappers for email blocks. |
| 13 | */ |
| 14 | class Table_Wrapper_Helper { |
| 15 | |
| 16 | /** |
| 17 | * Default table attributes. |
| 18 | * |
| 19 | * @var array<string, string> |
| 20 | */ |
| 21 | private const DEFAULT_TABLE_ATTRS = array( |
| 22 | 'border' => '0', |
| 23 | 'cellpadding' => '0', |
| 24 | 'cellspacing' => '0', |
| 25 | 'role' => 'presentation', |
| 26 | ); |
| 27 | |
| 28 | /** |
| 29 | * Render a table cell. |
| 30 | * |
| 31 | * @param string $content The content to wrap. |
| 32 | * @param array $cell_attrs Cell attributes. |
| 33 | * @return string The generated table cell HTML. |
| 34 | */ |
| 35 | public static function render_table_cell( |
| 36 | string $content, |
| 37 | array $cell_attrs = array() |
| 38 | ): string { |
| 39 | $cell_attr_string = self::build_attributes_string( $cell_attrs ); |
| 40 | |
| 41 | return sprintf( |
| 42 | '<td%1$s>%2$s</td>', |
| 43 | $cell_attr_string ? ' ' . $cell_attr_string : '', |
| 44 | $content |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Render an Outlook-specific table cell using conditional comments. |
| 50 | * |
| 51 | * @param string $content The content to wrap. |
| 52 | * @param array $cell_attrs Cell attributes. |
| 53 | * @return string The generated table cell HTML with Outlook conditionals. |
| 54 | */ |
| 55 | public static function render_outlook_table_cell( |
| 56 | string $content, |
| 57 | array $cell_attrs = array() |
| 58 | ): string { |
| 59 | $content_with_outlook_conditional = '<![endif]-->' . $content . '<!--[if mso | IE]>'; |
| 60 | return '<!--[if mso | IE]>' . self::render_table_cell( $content_with_outlook_conditional, $cell_attrs ) . '<![endif]-->'; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Render a table wrapper for email blocks. |
| 65 | * |
| 66 | * @param string $content The content to wrap (e.g., '{block_content}'). |
| 67 | * @param array $table_attrs Table attributes to merge with defaults. |
| 68 | * @param array $cell_attrs Cell attributes. |
| 69 | * @param array $row_attrs Row attributes. |
| 70 | * @param bool $render_cell Whether to render the td wrapper (default true). |
| 71 | * @return string The generated table wrapper HTML. |
| 72 | */ |
| 73 | public static function render_table_wrapper( |
| 74 | string $content, |
| 75 | array $table_attrs = array(), |
| 76 | array $cell_attrs = array(), |
| 77 | array $row_attrs = array(), |
| 78 | bool $render_cell = true |
| 79 | ): string { |
| 80 | $merged_table_attrs = array_merge( self::DEFAULT_TABLE_ATTRS, $table_attrs ); |
| 81 | $table_attr_string = self::build_attributes_string( $merged_table_attrs ); |
| 82 | $row_attr_string = self::build_attributes_string( $row_attrs ); |
| 83 | |
| 84 | if ( $render_cell ) { |
| 85 | $content = self::render_table_cell( $content, $cell_attrs ); |
| 86 | } |
| 87 | |
| 88 | return sprintf( |
| 89 | '<table%2$s> |
| 90 | <tbody> |
| 91 | <tr%3$s> |
| 92 | %1$s |
| 93 | </tr> |
| 94 | </tbody> |
| 95 | </table>', |
| 96 | $content, |
| 97 | $table_attr_string ? ' ' . $table_attr_string : '', |
| 98 | $row_attr_string ? ' ' . $row_attr_string : '' |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Render an Outlook-specific table wrapper using conditional comments. |
| 104 | * |
| 105 | * @param string $content The content to wrap (e.g., '{block_content}'). |
| 106 | * @param array $table_attrs Table attributes to merge with defaults. |
| 107 | * @param array $cell_attrs Cell attributes. |
| 108 | * @param array $row_attrs Row attributes. |
| 109 | * @param bool $render_cell Whether to render the td wrapper (default true). |
| 110 | * @return string The generated table wrapper HTML. |
| 111 | */ |
| 112 | public static function render_outlook_table_wrapper( |
| 113 | string $content, |
| 114 | array $table_attrs = array(), |
| 115 | array $cell_attrs = array(), |
| 116 | array $row_attrs = array(), |
| 117 | bool $render_cell = true |
| 118 | ): string { |
| 119 | $content_with_outlook_conditional = '<![endif]-->' . $content . '<!--[if mso | IE]>'; |
| 120 | return '<!--[if mso | IE]>' . self::render_table_wrapper( $content_with_outlook_conditional, $table_attrs, $cell_attrs, $row_attrs, $render_cell ) . '<![endif]-->'; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Build an HTML attributes string from an array. |
| 125 | * |
| 126 | * @param array<string, string> $attributes The attributes array. |
| 127 | * @return string The attributes string. |
| 128 | */ |
| 129 | private static function build_attributes_string( array $attributes ): string { |
| 130 | $attr_parts = array(); |
| 131 | foreach ( $attributes as $key => $value ) { |
| 132 | if ( '' !== $value ) { |
| 133 | $attr_parts[] = sprintf( '%s="%s"', $key, esc_attr( $value ) ); |
| 134 | } |
| 135 | } |
| 136 | return implode( ' ', $attr_parts ); |
| 137 | } |
| 138 | } |
| 139 |