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
55 changes: 54 additions & 1 deletion includes/Admin/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,70 @@ final class Menu {
* Icon is drawn in global admin CSS (`src/js/admin/admin.scss`) via mask so it
* follows the same currentColor / hover / active states as Dashicons.
*
* The top-level entry is open to anyone who can publish posts so authors can
* reach Newsletters. The Settings page is registered first as its own
* `manage_options` submenu under the same slug, so it stays administrator-only.
* This runs on `admin_menu` priority 5, before core adds the Newsletters
* submenu (priority 10), so Settings is first and the parent already exists.
*
* @since 1.0.0
*/
public static function register(): void {
add_menu_page(
__( 'beehiiv Settings', 'beehiiv' ),
__( 'beehiiv', 'beehiiv' ),
'manage_options',
'publish_posts',
Config::PLUGIN_SLUG,
[ SettingsPage::class, 'render' ],
'none',
99
);

// Registered before any other submenu so it replaces the automatic first
// submenu item, which would otherwise inherit the lower top-level capability.
add_submenu_page(
Config::PLUGIN_SLUG,
__( 'beehiiv Settings', 'beehiiv' ),
__( 'Settings', 'beehiiv' ),
'manage_options',
Config::PLUGIN_SLUG,
[ SettingsPage::class, 'render' ]
);
}

/**
* Keep the top-level entry from landing non-administrators on Settings.
*
* Core skips the Settings submenu for users without `manage_options`, then adds
* an automatic first item that points at the top-level slug (the Settings page).
* That item is dropped so the top-level entry opens the first remaining submenu,
* Newsletters. Users who cannot publish posts get no beehiiv menu at all, because
* the top-level capability would deny them the Newsletters page anyway.
*
* Runs on `admin_menu` after core adds the post type submenus (priority 10).
*
* @since x.x.x
*/
public static function prune_for_non_administrators(): void {
global $submenu;

if ( current_user_can( 'manage_options' ) ) {
return;
}

if ( ! current_user_can( 'publish_posts' ) ) {
remove_menu_page( Config::PLUGIN_SLUG );
return;
}

if ( empty( $submenu[ Config::PLUGIN_SLUG ] ) || ! is_array( $submenu[ Config::PLUGIN_SLUG ] ) ) {
return;
}

foreach ( $submenu[ Config::PLUGIN_SLUG ] as $index => $item ) {
if ( Config::PLUGIN_SLUG === $item[2] ) {
unset( $submenu[ Config::PLUGIN_SLUG ][ $index ] );
}
}
}
}
6 changes: 5 additions & 1 deletion includes/Admin/SettingsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public static function init(): void {
*/
public static function render(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
wp_die(
esc_html__( 'Sorry, you are not allowed to access this page.', 'beehiiv' ),
'',
[ 'response' => 403 ]
);
}

require Config::ADMIN_VIEWS_DIR . 'settings-page.php';
Expand Down
8 changes: 8 additions & 0 deletions includes/Editor/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ final class Meta {
*/
public const BEEHIIV_LINKED_POST_TEMPLATE_ID = '_beehiiv_linked_post_template_id';

/**
* Post IDs frozen for automatic Post Roundup blocks at the first send or schedule.
* Server-only; not registered for REST.
*
* @since x.x.x
*/
public const ROUNDUP_SNAPSHOT = '_beehiiv_roundup_snapshot';

/**
* Beehiiv post ID after the newsletter has been created via the API.
*
Expand Down
46 changes: 32 additions & 14 deletions includes/Editor/PostSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
use Beehiiv\API\Resources\Workspace;
use Beehiiv\Config;
use Beehiiv\Connection\Manager;
use Beehiiv\Newsletter\ContentTypes;
use Beehiiv\Newsletter\PublicationResolver;
use Beehiiv\Newsletter\SupportedBlocks;
use Beehiiv\OAuth\Config as OAuthConfig;

defined( 'ABSPATH' ) || exit;

/**
* Registers post meta and enqueues the beehiiv plugin sidebar for the default
* `post` post type.
* Registers post meta and enqueues the beehiiv plugin sidebar for the supported
* content types ({@see ContentTypes}).
*
* @since 1.0.0
*/
Expand All @@ -40,11 +41,6 @@ final class PostSettings {
*/
private const STYLE_HANDLE = 'beehiiv-post-settings';

/**
* Post type that receives beehiiv post meta and editor settings.
*/
private const POST_TYPE = 'post';

/**
* Post meta keys registered for the block editor (REST-visible).
*
Expand Down Expand Up @@ -119,10 +115,30 @@ final class PostSettings {
* @since 1.0.0
*/
public static function register_meta(): void {
add_filter( 'rest_prepare_post', [ self::class, 'prepare_rest_post' ], 10, 3 );
foreach ( ContentTypes::supported() as $post_type ) {
add_filter( 'rest_prepare_' . $post_type, [ self::class, 'prepare_rest_post' ], 10, 3 );
}

add_filter( 'rest_request_before_callbacks', [ self::class, 'drop_readonly_meta_from_request' ], 10, 3 );

foreach ( ContentTypes::supported() as $post_type ) {
self::register_post_type_meta( $post_type );
}
}

/**
* Register every beehiiv meta key for one supported post type.
*
* @param string $post_type Post type name.
* @return void
* @since x.x.x
*/
private static function register_post_type_meta( string $post_type ): void {
foreach ( self::META_KEYS as $key => $config ) {
if ( Meta::SEND_TO_NEWSLETTER === $key ) {
$config['default'] = ContentTypes::send_default( $post_type );
}

$show_in_rest = true;

if ( ! empty( $config['readonly'] ) ) {
Expand All @@ -147,7 +163,7 @@ public static function register_meta(): void {
$args['sanitize_callback'] = $config['sanitize'];
}

register_post_meta( self::POST_TYPE, $key, $args );
register_post_meta( $post_type, $key, $args );
}
}

Expand Down Expand Up @@ -201,7 +217,7 @@ public static function enqueue_canvas_styles(): void {
}

$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( $screen && self::POST_TYPE !== $screen->post_type ) {
if ( $screen && ! ContentTypes::is_supported( $screen->post_type ) ) {
return;
}

Expand All @@ -210,7 +226,7 @@ public static function enqueue_canvas_styles(): void {
}

/**
* Enqueue the compiled editor plugin only on `post` edit screens.
* Enqueue the compiled editor plugin only on supported edit screens.
*
* @since 1.0.0
*/
Expand All @@ -220,7 +236,7 @@ public static function enqueue_assets(): void {
}

$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( $screen && self::POST_TYPE !== $screen->post_type ) {
if ( $screen && ! ContentTypes::is_supported( $screen->post_type ) ) {
return;
}

Expand Down Expand Up @@ -310,7 +326,7 @@ public static function drop_readonly_meta_from_request( $response, $handler, $re
return $response;
}

if ( ! preg_match( '#^/wp/v2/posts(?:/\d+(?:/autosaves)?)?$#', $request->get_route() ) ) {
if ( ! preg_match( ContentTypes::rest_route_pattern(), $request->get_route() ) ) {
return $response;
}

Expand Down Expand Up @@ -349,7 +365,7 @@ public static function prepare_rest_post( $response, $post, $request ) {
return $response;
}

if ( self::POST_TYPE !== $post->post_type || 'edit' !== $request->get_param( 'context' ) ) {
if ( ! ContentTypes::is_supported( $post->post_type ) || 'edit' !== $request->get_param( 'context' ) ) {
return $response;
}

Expand Down Expand Up @@ -412,6 +428,8 @@ private static function get_editor_config(): array {
'defaultPostTemplateId' => trim( (string) ( $settings['post_template_id'] ?? '' ) ),
'requireTemplateChoice' => ! empty( $settings['require_template_choice'] ),
'canPublishPosts' => current_user_can( 'publish_posts' ),
'supportedPostTypes' => ContentTypes::supported(),
'newsletterPostType' => ContentTypes::NEWSLETTER,
'defaultPublicationId' => $default_pub_id,
'publications' => $is_connected ? PublicationResolver::get_editor_publications() : [],
];
Expand Down
74 changes: 60 additions & 14 deletions includes/Newsletter/BlockConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* - `core/separator` — beehiiv `content_break`
* - `core/more` — snippet newsletters only; beehiiv Read More `button` via `convert_more_block()`
* - `beehiiv/advertisement` — beehiiv `advertisement` block via `convert_advertisement_block()`
* - `beehiiv/post-roundup` — newsletters only; `PostRoundup::expand()` makes image, text, button, separator blocks
*
* - `core/group` — `convert_group_block()` (beehiiv `section` block holding the converted inner
* blocks; only for posts whose first sync used sections, see `PostSettingsBuilder`; a Group
Expand Down Expand Up @@ -73,11 +74,13 @@ public static function convert_supported_blocks( WP_Post $post_object, bool $con
}
}

$is_send_newsletter_snippet = (bool) get_post_meta(
$post_object->ID,
Meta::SEND_TO_NEWSLETTER_SNIPPET,
true
);
// Newsletters have no public post to link to, so snippet mode never applies.
$is_send_newsletter_snippet = ! ContentTypes::is_newsletter( $post_object->post_type )
&& (bool) get_post_meta(
$post_object->ID,
Meta::SEND_TO_NEWSLETTER_SNIPPET,
true
);

$converted_blocks = self::convert_blocks(
$wp_blocks,
Expand All @@ -100,16 +103,19 @@ public static function convert_supported_blocks( WP_Post $post_object, bool $con
* @param WP_Post $post_object Post object.
* @param bool $is_send_newsletter_snippet Whether snippet mode is enabled.
* @param bool $convert_groups Whether Group blocks become sections.
* @param bool $is_nested Whether the blocks sit inside a layout block.
* @return array<int, array<string, mixed>>
* @since 1.0.0
*/
private static function convert_blocks(
array $wp_blocks,
WP_Post $post_object,
bool $is_send_newsletter_snippet,
bool $convert_groups = false
bool $convert_groups = false,
bool $is_nested = false
): array {
$beehiiv_blocks = [];
$beehiiv_blocks = [];
$roundup_ordinal = 0;

foreach ( $wp_blocks as $wp_block ) {
if ( empty( $wp_block['blockName'] ) ) {
Expand Down Expand Up @@ -137,6 +143,20 @@ private static function convert_blocks(
continue;
}

if ( PostRoundup::BLOCK === $block_name ) {
$attrs = (array) ( $wp_block['attrs'] ?? [] );
$key = PostRoundup::block_key( $attrs, $is_nested ? null : $roundup_ordinal++ );

// A roundup left in a regular post is ignored.
if ( ContentTypes::is_newsletter( $post_object->post_type ) ) {
$beehiiv_blocks = array_merge(
$beehiiv_blocks,
PostRoundup::expand( $attrs, $post_object->ID, $key )
);
}
continue;
}

if ( 'core/buttons' === $block_name ) {
$button_blocks = self::convert_buttons_block( $wp_block );
$beehiiv_blocks = array_merge( $beehiiv_blocks, $button_blocks );
Expand All @@ -150,7 +170,10 @@ private static function convert_blocks(
}

if ( 'core/columns' === $block_name ) {
$beehiiv_blocks = array_merge( $beehiiv_blocks, self::convert_columns_block_to_blocks( $wp_block ) );
$beehiiv_blocks = array_merge(
$beehiiv_blocks,
self::convert_columns_block_to_blocks( $wp_block, $post_object )
);
continue;
}

Expand Down Expand Up @@ -185,7 +208,8 @@ private static function convert_group_block( array $wp_block, WP_Post $post_obje
$wp_block['innerBlocks'] ?? [],
$post_object,
false,
false
false,
true
);

if ( empty( $inner_blocks ) ) {
Expand Down Expand Up @@ -614,11 +638,12 @@ public static function convert_columns_block( array $wp_block ): array {
* block nested inside a column is not converted to a columns block: its inner
* blocks follow the outer columns block at the top level.
*
* @param array<string, mixed> $wp_block Parsed block.
* @param array<string, mixed> $wp_block Parsed block.
* @param WP_Post|null $post_object Post being converted. Lets a Post Roundup inside a column expand.
* @return array<int, array<string, mixed>>
* @since 1.2.0
*/
public static function convert_columns_block_to_blocks( array $wp_block ): array {
public static function convert_columns_block_to_blocks( array $wp_block, ?WP_Post $post_object = null ): array {
if ( 'core/columns' !== ( $wp_block['blockName'] ?? '' ) ) {
return [];
}
Expand All @@ -640,7 +665,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,
$post_object
);
}

Expand Down Expand Up @@ -690,14 +716,20 @@ public static function convert_columns_block_to_blocks( array $wp_block ): array
* added to it for the caller to emit at the top level. Without it, the nested block
* is omitted.
*
* A Post Roundup expands in place when the post is a newsletter. Its cards are sent
* full width with no separators between cards, because a beehiiv column cannot hold
* another columns block and a divider inside a column is not known to be accepted.
*
* @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 WP_Post|null $post_object Post being converted.
* @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,
?WP_Post $post_object = null
): array {
$beehiiv_blocks = [];

Expand All @@ -714,7 +746,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,
$post_object
);
$nested_blocks = array_merge( $nested_blocks, $nested_converted );
}
Expand All @@ -724,6 +757,19 @@ private static function convert_layout_block_inner_blocks(
continue;
}

if ( PostRoundup::BLOCK === $block_name ) {
if ( $post_object instanceof WP_Post && ContentTypes::is_newsletter( $post_object->post_type ) ) {
$attrs = (array) ( $wp_block['attrs'] ?? [] );
$attrs['layout'] = PostRoundup::LAYOUT_FULL;

$beehiiv_blocks = array_merge(
$beehiiv_blocks,
PostRoundup::expand( $attrs, $post_object->ID, PostRoundup::block_key( $attrs, null ), false )
);
}
continue;
}

if ( 'core/buttons' === $block_name ) {
$beehiiv_blocks = array_merge( $beehiiv_blocks, self::convert_buttons_block( $wp_block ) );
continue;
Expand Down
Loading
Loading