mailpoet
/
vendor
/
woocommerce
/
email-editor
/
src
/
Integrations
/
Utils
/
class-table-wrapper-helper.php
class-dom-document-helper.php
2 weeks ago
class-html-processing-helper.php
2 weeks ago
class-social-links-helper.php
11 months ago
class-styles-helper.php
6 months ago
class-table-wrapper-helper.php
11 months ago
index.php
11 months ago
class-table-wrapper-helper.php
76 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Integrations\Utils; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | class Table_Wrapper_Helper { |
| 6 | private const DEFAULT_TABLE_ATTRS = array( |
| 7 | 'border' => '0', |
| 8 | 'cellpadding' => '0', |
| 9 | 'cellspacing' => '0', |
| 10 | 'role' => 'presentation', |
| 11 | ); |
| 12 | public static function render_table_cell( |
| 13 | string $content, |
| 14 | array $cell_attrs = array() |
| 15 | ): string { |
| 16 | $cell_attr_string = self::build_attributes_string( $cell_attrs ); |
| 17 | return sprintf( |
| 18 | '<td%1$s>%2$s</td>', |
| 19 | $cell_attr_string ? ' ' . $cell_attr_string : '', |
| 20 | $content |
| 21 | ); |
| 22 | } |
| 23 | public static function render_outlook_table_cell( |
| 24 | string $content, |
| 25 | array $cell_attrs = array() |
| 26 | ): string { |
| 27 | $content_with_outlook_conditional = '<![endif]-->' . $content . '<!--[if mso | IE]>'; |
| 28 | return '<!--[if mso | IE]>' . self::render_table_cell( $content_with_outlook_conditional, $cell_attrs ) . '<![endif]-->'; |
| 29 | } |
| 30 | public static function render_table_wrapper( |
| 31 | string $content, |
| 32 | array $table_attrs = array(), |
| 33 | array $cell_attrs = array(), |
| 34 | array $row_attrs = array(), |
| 35 | bool $render_cell = true |
| 36 | ): string { |
| 37 | $merged_table_attrs = array_merge( self::DEFAULT_TABLE_ATTRS, $table_attrs ); |
| 38 | $table_attr_string = self::build_attributes_string( $merged_table_attrs ); |
| 39 | $row_attr_string = self::build_attributes_string( $row_attrs ); |
| 40 | if ( $render_cell ) { |
| 41 | $content = self::render_table_cell( $content, $cell_attrs ); |
| 42 | } |
| 43 | return sprintf( |
| 44 | '<table%2$s> |
| 45 | <tbody> |
| 46 | <tr%3$s> |
| 47 | %1$s |
| 48 | </tr> |
| 49 | </tbody> |
| 50 | </table>', |
| 51 | $content, |
| 52 | $table_attr_string ? ' ' . $table_attr_string : '', |
| 53 | $row_attr_string ? ' ' . $row_attr_string : '' |
| 54 | ); |
| 55 | } |
| 56 | public static function render_outlook_table_wrapper( |
| 57 | string $content, |
| 58 | array $table_attrs = array(), |
| 59 | array $cell_attrs = array(), |
| 60 | array $row_attrs = array(), |
| 61 | bool $render_cell = true |
| 62 | ): string { |
| 63 | $content_with_outlook_conditional = '<![endif]-->' . $content . '<!--[if mso | IE]>'; |
| 64 | return '<!--[if mso | IE]>' . self::render_table_wrapper( $content_with_outlook_conditional, $table_attrs, $cell_attrs, $row_attrs, $render_cell ) . '<![endif]-->'; |
| 65 | } |
| 66 | private static function build_attributes_string( array $attributes ): string { |
| 67 | $attr_parts = array(); |
| 68 | foreach ( $attributes as $key => $value ) { |
| 69 | if ( '' !== $value ) { |
| 70 | $attr_parts[] = sprintf( '%s="%s"', $key, esc_attr( $value ) ); |
| 71 | } |
| 72 | } |
| 73 | return implode( ' ', $attr_parts ); |
| 74 | } |
| 75 | } |
| 76 |