Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions includes/Editor/NewsletterOnlyHtml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* The "Newsletter only" option on Custom HTML blocks.
*
* @package beehiiv
*/

namespace Beehiiv\Editor;

defined( 'ABSPATH' ) || exit;

/**
* Lets a Custom HTML block go to beehiiv without showing on the WordPress site.
*
* The option is a per-block attribute on `core/html`, off by default. The block is still
* saved and synced to beehiiv as an HTML block; this class only stops it rendering. It
* hooks `render_block`, which runs wherever WordPress renders post content, so the front
* end, the RSS feed, the excerpt and the REST API's rendered content all show nothing for
* a newsletter-only block. The editor shows the block as usual so it stays editable.
*
* @since 1.3.0
*/
final class NewsletterOnlyHtml {

/**
* Block attribute that holds the option.
*/
public const ATTRIBUTE = 'beehiivNewsletterOnly';

/**
* Register hooks.
*
* @return void
* @since 1.3.0
*/
public static function init(): void {
add_filter( 'register_block_type_args', [ self::class, 'register_attribute' ], 10, 2 );
add_filter( 'render_block', [ self::class, 'hide_block' ], 10, 2 );
}

/**
* Add the option to the core/html block type.
*
* @param array<string, mixed> $args Block type arguments.
* @param string $block_name Block name.
* @return array<string, mixed>
* @since 1.3.0
*/
public static function register_attribute( array $args, string $block_name ): array {
if ( 'core/html' !== $block_name ) {
return $args;
}

$attributes = isset( $args['attributes'] ) && is_array( $args['attributes'] ) ? $args['attributes'] : [];

$args['attributes'] = $attributes;
$args['attributes'][ self::ATTRIBUTE ] = [
'type' => 'boolean',
'default' => false,
];

return $args;
}

/**
* Output nothing for a newsletter-only Custom HTML block.
*
* @param string $block_content Rendered block content.
* @param array<string, mixed> $block Parsed block.
* @return string
* @since 1.3.0
*/
public static function hide_block( string $block_content, array $block ): string {
if ( self::is_newsletter_only( $block ) ) {
return '';
}

return $block_content;
}

/**
* Whether a parsed block is a Custom HTML block set to "Newsletter only".
*
* @param array<string, mixed> $block Parsed block.
* @return bool
* @since 1.3.0
*/
public static function is_newsletter_only( array $block ): bool {
return 'core/html' === ( $block['blockName'] ?? '' )
&& ! empty( $block['attrs'][ self::ATTRIBUTE ] );
}
}
62 changes: 54 additions & 8 deletions includes/Newsletter/BlockConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Beehiiv\API\Resources\AdvertisementOpportunities;
use Beehiiv\Editor\Meta;
use Beehiiv\Newsletter\Converters\ColumnsBlockConverter;
use Beehiiv\Newsletter\Converters\HtmlBlockConverter;
use Beehiiv\Newsletter\Converters\GroupBlockConverter;
use Beehiiv\Newsletter\Converters\MediaTextBlockConverter;
use Beehiiv\Newsletter\Converters\TableBlockConverter;
Expand Down Expand Up @@ -154,6 +155,18 @@ private static function convert_blocks(
continue;
}

if ( 'core/media-text' === $block_name ) {
$trailing_blocks = [];
$media_text = self::convert_media_text_block( $wp_block, $trailing_blocks );

if ( ! empty( $media_text ) ) {
$beehiiv_blocks[] = $media_text;
}

$beehiiv_blocks = array_merge( $beehiiv_blocks, $trailing_blocks );
continue;
}

$beehiiv_block = self::convert_block( $wp_block );

if ( ! empty( $beehiiv_block ) ) {
Expand Down Expand Up @@ -235,6 +248,8 @@ private static function convert_block( array $wp_block ): array {
return self::convert_embed_block( $wp_block );
case 'core/media-text':
return self::convert_media_text_block( $wp_block );
case 'core/html':
return HtmlBlockConverter::convert( $wp_block );
case 'beehiiv/advertisement':
return self::convert_advertisement_block( $wp_block );
}
Expand Down Expand Up @@ -631,6 +646,7 @@ public static function convert_columns_block_to_blocks( array $wp_block ): array
$column_blocks = [];
$converted_columns = [];
$nested_blocks = [];
$html_blocks = [];

foreach ( $wp_block['innerBlocks'] ?? [] as $column_block ) {
if ( 'core/column' !== ( $column_block['blockName'] ?? '' ) ) {
Expand All @@ -640,7 +656,8 @@ public static function convert_columns_block_to_blocks( array $wp_block ): array
$column_blocks[] = $column_block;
$converted_columns[] = self::convert_layout_block_inner_blocks(
$column_block['innerBlocks'] ?? [],
$nested_blocks
$nested_blocks,
$html_blocks
);
}

Expand Down Expand Up @@ -678,7 +695,7 @@ public static function convert_columns_block_to_blocks( array $wp_block ): array
ColumnsBlockConverter::resolve_stack_on_mobile( $attrs, $inner_html )
);

return array_values( array_filter( array_merge( [ $columns_block ], $nested_blocks ) ) );
return array_values( array_filter( array_merge( [ $columns_block ], $nested_blocks, $html_blocks ) ) );
}

/**
Expand All @@ -692,12 +709,15 @@ public static function convert_columns_block_to_blocks( array $wp_block ): array
*
* @param array<int, array<string, mixed>> $wp_blocks Parsed inner blocks.
* @param array<int, array<string, mixed>>|null $nested_blocks Receives the inner blocks of nested columns.
* @param array<int, array<string, mixed>>|null $html_blocks Receives the HTML blocks of inner Custom HTML
* blocks, which a column cannot hold.
* @return array<int, array<string, mixed>>
* @since 1.0.0
*/
private static function convert_layout_block_inner_blocks(
array $wp_blocks,
?array &$nested_blocks = null
?array &$nested_blocks = null,
?array &$html_blocks = null
): array {
$beehiiv_blocks = [];

Expand All @@ -714,7 +734,8 @@ private static function convert_layout_block_inner_blocks(
if ( 'core/column' === ( $nested_column['blockName'] ?? '' ) ) {
$nested_converted = self::convert_layout_block_inner_blocks(
$nested_column['innerBlocks'] ?? [],
$nested_blocks
$nested_blocks,
$html_blocks
);
$nested_blocks = array_merge( $nested_blocks, $nested_converted );
}
Expand All @@ -724,6 +745,18 @@ private static function convert_layout_block_inner_blocks(
continue;
}

// A beehiiv column cannot hold an HTML block, so the snippet is handed to the caller
// to emit right after the parent block. Without a caller to receive it, it is omitted.
if ( 'core/html' === $block_name ) {
$html_block = HtmlBlockConverter::convert( $wp_block );

if ( null !== $html_blocks && ! empty( $html_block ) ) {
$html_blocks[] = $html_block;
}

continue;
}

if ( 'core/buttons' === $block_name ) {
$beehiiv_blocks = array_merge( $beehiiv_blocks, self::convert_buttons_block( $wp_block ) );
continue;
Expand Down Expand Up @@ -1110,11 +1143,13 @@ public static function convert_embed_block( array $wp_block ): array {
* entirely. Respects media position, column widths, vertical alignment, and
* mobile stacking.
*
* @param array<string, mixed> $wp_block Parsed block.
* @param array<string, mixed> $wp_block Parsed block.
* @param array<int, array<string, mixed>>|null $trailing_blocks Receives the HTML blocks of inner Custom HTML
* blocks, to emit after the returned block.
* @return array<string, mixed>
* @since 1.0.0
*/
public static function convert_media_text_block( array $wp_block ): array {
public static function convert_media_text_block( array $wp_block, ?array &$trailing_blocks = null ): array {
$attrs = $wp_block['attrs'] ?? [];
$inner_html = (string) ( $wp_block['innerHTML'] ?? '' );
$inner_blocks = $wp_block['innerBlocks'] ?? [];
Expand All @@ -1134,13 +1169,20 @@ public static function convert_media_text_block( array $wp_block ): array {
return [];
}

$content_blocks = self::convert_layout_block_inner_blocks( $inner_blocks );
$html_blocks = [];
$content_nested = null;
$content_blocks = self::convert_layout_block_inner_blocks( $inner_blocks, $content_nested, $html_blocks );

if ( empty( $content_blocks ) && '' !== $inner_html ) {
$content_html = MediaTextBlockConverter::extract_content_inner_html( $inner_html );

if ( '' !== $content_html ) {
$content_blocks = self::convert_layout_block_inner_blocks( parse_blocks( $content_html ) );
$html_blocks = [];
$content_blocks = self::convert_layout_block_inner_blocks(
parse_blocks( $content_html ),
$content_nested,
$html_blocks
);
}
}

Expand Down Expand Up @@ -1174,6 +1216,10 @@ public static function convert_media_text_block( array $wp_block ): array {
return [];
}

if ( null !== $trailing_blocks ) {
$trailing_blocks = $html_blocks;
}

return ColumnsBlockConverter::build_beehiiv_columns_block( $columns, $stack_on_mobile );
}

Expand Down
72 changes: 72 additions & 0 deletions includes/Newsletter/Converters/HtmlBlockConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Maps a core/html block to a beehiiv HTML block.
*
* @package beehiiv
*/

namespace Beehiiv\Newsletter\Converters;

defined( 'ABSPATH' ) || exit;

/**
* Builds a beehiiv `html` block (an HTML Snippet) from a Custom HTML block.
*
* The HTML is sent as written. Only `<script>` and `<iframe>` elements are removed,
* because beehiiv HTML Snippets do not accept them. Nothing is added around the HTML and
* no visibility rule is set, so a snippet follows beehiiv's own default.
*
* @since 1.3.0
*/
final class HtmlBlockConverter {

/**
* Remove `<script>` and `<iframe>` elements from HTML.
*
* Whole elements are removed, together with their content. Any stray opening or
* closing tag left behind is removed as well. Everything else is left untouched.
*
* @param string $html Raw HTML.
* @return string
* @since 1.3.0
*/
public static function strip_unsupported_elements( string $html ): string {
$html = (string) preg_replace( '#<\s*(script|iframe)\b[^>]*>.*?<\s*/\s*\1\s*>#is', '', $html );

return (string) preg_replace( '#<\s*/?\s*(?:script|iframe)\b[^>]*>#i', '', $html );
}

/**
* Whether HTML holds a `<script>` or `<iframe>` element.
*
* @param string $html Raw HTML.
* @return bool
* @since 1.3.0
*/
public static function has_unsupported_elements( string $html ): bool {
return 1 === preg_match( '#<\s*(?:script|iframe)\b#i', $html );
}

/**
* Convert a core/html block to a beehiiv `html` block.
*
* Returns an empty array when nothing is left to send: an empty block, a block with
* only whitespace, or a block that held only script or iframe elements.
*
* @param array<string, mixed> $wp_block Parsed core/html block.
* @return array<string, mixed>
* @since 1.3.0
*/
public static function convert( array $wp_block ): array {
$html = trim( self::strip_unsupported_elements( (string) ( $wp_block['innerHTML'] ?? '' ) ) );

if ( '' === $html ) {
return [];
}

return [
'type' => 'html',
'html' => $html,
];
}
}
4 changes: 3 additions & 1 deletion includes/Newsletter/SupportedBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* `core/group` is supported as a beehiiv section for posts first synced with section
* support; see `BlockConverter::convert_group_block()`. `core/columns` is supported, but a
* Columns block with more than 3 columns is omitted; see
* `BlockConverter::convert_columns_block_to_blocks()`. Other layout blocks are not
* `BlockConverter::convert_columns_block_to_blocks()`. `core/html` is sent as a beehiiv HTML
* block; see `HtmlBlockConverter`. Other layout blocks are not
* supported. They and all of their inner blocks are omitted from the newsletter payload.
*
* @since 1.0.0
Expand All @@ -41,6 +42,7 @@ final class SupportedBlocks {
'core/media-text',
'core/columns',
'core/group',
'core/html',
'core/buttons',
'core/separator',
'beehiiv/advertisement',
Expand Down
1 change: 1 addition & 0 deletions includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static function init(): void {
Frontend\Assets::init();
Admin\Assets::init();
Newsletter\Sender::init();
Editor\NewsletterOnlyHtml::init();
Advertisement\Sync::init();
OAuth\Config::register_hooks();
OAuth\AdminActions::init();
Expand Down
14 changes: 14 additions & 0 deletions src/js/editor/post-settings/css/_newsletter-only-html.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* stylelint-disable selector-class-pattern -- block editor */
.editor-styles-wrapper .beehiiv-newsletter-only-label {
background: #f0f6fc;
border: 1px solid #72aee6;
border-radius: 2px;
color: #0a4b78;
display: inline-block;
font-size: 12px;
font-weight: 600;
line-height: 1.4;
margin: 0 0 8px;
padding: 2px 8px;
}
/* stylelint-enable selector-class-pattern */
1 change: 1 addition & 0 deletions src/js/editor/post-settings/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
@use "css/email-preview";
@use "css/newsletter-wording";
@use "css/omitted-blocks";
@use "css/newsletter-only-html";
@use "css/test-email";
Loading
Loading