Skip to content

Commit 53dbffa

Browse files
authored
Fix custom styles for emails in preview and in sent messages (#7595)
1 parent dfc51fa commit 53dbffa

File tree

6 files changed

+82
-20
lines changed

6 files changed

+82
-20
lines changed

assets/blocks/email-editor.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ export default function handleEmailBlocksEditor() {
2020
* Update the blocks to remove extra settings when used in email editor.
2121
*
2222
* @param {Object} settings Block settings.
23+
* @param {string} name Block name.
2324
*/
24-
function removeIrrelevantSettings( settings ) {
25+
function removeIrrelevantSettings( settings, name ) {
2526
const supports = { ...( settings.supports ? settings.supports : {} ) };
2627

2728
// Remove font family setting.
@@ -48,6 +49,20 @@ export default function handleEmailBlocksEditor() {
4849
} );
4950
}
5051

52+
// Alignment is not supported for buttons block in emails.
53+
if ( name === 'core/buttons' ) {
54+
if ( has( supports, 'layout' ) ) {
55+
supports.layout = false;
56+
}
57+
}
58+
59+
// Alingment is not supported for image block in emails.
60+
if ( name === 'core/image' ) {
61+
if ( has( supports, 'align' ) ) {
62+
supports.align = false;
63+
}
64+
}
65+
5166
return {
5267
...settings,
5368
supports,

assets/css/email-notifications/email-editor-style.scss

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
.wp-block-post-content *,
66
.block-editor-block-list__layout * {
77
font-family: -apple-system, "SF Pro Text", BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif !important;
8-
color: #101517;
98
}
109
.wp-element-button,
1110
.editor-styles-wrapper .wp-block-button__link {

assets/css/email-notifications/email-style.scss

+26-9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ $MOBILE_HEADER_SIZE: 32px;
3535
float: right;
3636
}
3737

38+
.wp-block-image img {
39+
height: auto;
40+
box-sizing: border-box;
41+
}
42+
3843
.has-global-padding{
3944
padding: 0px;
4045
}
@@ -44,7 +49,19 @@ $MOBILE_HEADER_SIZE: 32px;
4449
}
4550

4651
.has-border-color, .has-border {
47-
border-style: solid;
52+
border-style: solid;
53+
}
54+
55+
.has-text-align-center{
56+
text-align:center;
57+
}
58+
59+
.has-text-align-left{
60+
text-align:left;
61+
}
62+
63+
.has-text-align-right{
64+
text-align:right;
4865
}
4966

5067
.wp-element-button, .editor-styles-wrapper .wp-block-button__link {
@@ -80,21 +97,21 @@ figure {
8097
/* MOBILE VERSION */
8198
@media (max-width: 800px) {
8299

83-
.email-notification__header {
84-
min-height: $MOBILE_HEADER_SIZE !important; // It is important when there is no logo available.
85-
}
100+
.email-notification__header {
101+
min-height: $MOBILE_HEADER_SIZE !important; // It is important when there is no logo available.
102+
}
86103

87-
.email-notification__header .wp-block-site-title {
88-
line-height: $MOBILE_HEADER_SIZE !important;
89-
}
104+
.email-notification__header .wp-block-site-title {
105+
line-height: $MOBILE_HEADER_SIZE !important;
106+
}
90107

91108
.wp-block-site-title {
92109
font-size: 15px !important;
93110
}
94111

95112
.wp-block-site-logo img{
96-
max-height: $MOBILE_HEADER_SIZE !important;
97-
}
113+
max-height: $MOBILE_HEADER_SIZE !important;
114+
}
98115

99116
.wp-block-buttons {
100117
margin-bottom: 0 !important;

changelog/fix-email-styles

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Fix custom styles for emails in preview and in sent messages

includes/internal/emails/class-email-post-type.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,40 @@ private function __construct() {}
5656
* @internal
5757
*/
5858
public function init(): void {
59+
add_filter( 'block_editor_settings_all', array( $this, 'disable_unsupported_features' ), 10, 2 );
5960
add_action( 'init', [ $this, 'register_post_type' ] );
6061
add_action( 'load-edit.php', [ $this, 'maybe_redirect_to_listing' ] );
6162
add_action( 'map_meta_cap', [ $this, 'remove_cap_of_deleting_email' ], 10, 4 );
6263

6364
add_filter( 'is_post_type_viewable', [ $this, 'enable_email_template_editor' ], 10, 2 );
6465
}
6566

67+
/**
68+
* Disable the default color palette and gradients in the block editor for the Email post type.
69+
*
70+
* @internal
71+
*
72+
* @since $$next-version$$
73+
*
74+
* @param array $editor_settings The editor settings.
75+
* @param object $editor_context The editor context.
76+
* @return array
77+
*/
78+
public function disable_unsupported_features( $editor_settings, $editor_context ) {
79+
if ( empty( $editor_context->post ) || self::POST_TYPE !== $editor_context->post->post_type ) {
80+
return $editor_settings;
81+
}
82+
83+
// Remove the default styles that might affect the email appearance.
84+
$editor_settings['styles'] = array();
85+
86+
// Disable the default color palette and gradients.
87+
$editor_settings['__experimentalFeatures']['color']['defaultPalette'] = false;
88+
$editor_settings['__experimentalFeatures']['color']['defaultGradients'] = false;
89+
90+
return $editor_settings;
91+
}
92+
6693
/**
6794
* Set sensei_email as viewable only on the admin interface.
6895
*
@@ -171,4 +198,3 @@ public function maybe_redirect_to_listing(): void {
171198
exit;
172199
}
173200
}
174-

includes/internal/emails/class-email-sender.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,17 @@ public function get_email_body( WP_Post $email_post, array $placeholders = [] ):
229229
*
230230
* @internal
231231
*
232-
* @param string $string The string.
232+
* @param string $content Content to replace the placeholders in.
233233
* @param array $placeholders The placeholders.
234234
*
235235
* @return string
236236
*/
237-
private function replace_placeholders( string $string, array $placeholders ): string {
237+
private function replace_placeholders( string $content, array $placeholders ): string {
238238
foreach ( $placeholders as $placeholder => $value ) {
239-
$string = str_replace( '[' . $placeholder . ']', $value, $string );
239+
$content = str_replace( '[' . $placeholder . ']', $value, $content );
240240
}
241241

242-
return $string;
242+
return $content;
243243
}
244244

245245
/**
@@ -250,14 +250,15 @@ private function replace_placeholders( string $string, array $placeholders ): st
250250
* @return string
251251
*/
252252
private function load_email_styles(): string {
253-
$css_dist_path = Sensei()->assets->dist_path( 'css/email-notifications/email-style.css' );
253+
$styles = wp_get_global_stylesheet();
254254

255+
$css_dist_path = Sensei()->assets->dist_path( 'css/email-notifications/email-style.css' );
255256
if ( file_exists( $css_dist_path ) ) {
256257
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file usage.
257-
return file_get_contents( $css_dist_path );
258+
$styles .= file_get_contents( $css_dist_path );
258259
}
259260

260-
return '';
261+
return $styles;
261262
}
262263

263264
/**
@@ -322,7 +323,7 @@ private function add_base_url_for_images( $content ) {
322323
*
323324
* @return array Headers.
324325
*/
325-
private function get_email_headers():array {
326+
private function get_email_headers(): array {
326327
$settings = $this->settings->get_settings();
327328
$headers = [
328329
'Content-Type: text/html; charset=UTF-8',

0 commit comments

Comments
 (0)