|
2 | 2 | /** |
3 | 3 | * Plugin Name: Blockparty Modal |
4 | 4 | * Description: Modal block for WordPress editor. |
5 | | - * Version: 1.0.8 |
| 5 | + * Version: 1.0.9 |
6 | 6 | * Requires at least: 6.8 |
7 | 7 | * Requires PHP: 8.1 |
8 | 8 | * Author: Be API Technical Team |
|
19 | 19 | exit; // Exit if accessed directly. |
20 | 20 | } |
21 | 21 |
|
22 | | -define( 'BLOCKPARTY_MODAL_VERSION', '1.0.8' ); |
| 22 | +define( 'BLOCKPARTY_MODAL_VERSION', '1.0.9' ); |
23 | 23 | define( 'BLOCKPARTY_MODAL_URL', plugin_dir_url( __FILE__ ) ); |
24 | 24 | define( 'BLOCKPARTY_MODAL_DIR', plugin_dir_path( __FILE__ ) ); |
25 | 25 |
|
@@ -47,31 +47,72 @@ function init(): void { |
47 | 47 | add_action( 'init', __NAMESPACE__ . '\\init', 10, 0 ); |
48 | 48 |
|
49 | 49 | /** |
50 | | - * Passes the list of blocks allowed as modal triggers to the block editor settings |
51 | | - * so the "Attached modal" panel is only shown for those blocks. |
| 50 | + * Returns the filtered list of blocks allowed inside the modal block. |
52 | 51 | * |
53 | | - * @param array<array-key, mixed> $settings Block editor settings. |
54 | | - * @param \WP_Block_Editor_Context $_context Block editor context (unused). |
55 | | - * @return array<array-key, mixed> Modified settings. |
| 52 | + * @return string[] Block names. |
56 | 53 | */ |
57 | | -function block_editor_settings_modal_trigger_blocks( array $settings, \WP_Block_Editor_Context $_context ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Required by block_editor_settings_all filter signature. |
| 54 | +function get_modal_inner_allowed_blocks(): array { |
58 | 55 | /** @psalm-suppress MixedAssignment */ |
59 | 56 | $raw = apply_filters( |
60 | | - 'blockparty_modal_trigger_allowed_blocks', |
61 | | - get_default_modal_trigger_allowed_blocks() |
| 57 | + 'blockparty_modal_inner_allowed_blocks', |
| 58 | + get_default_modal_inner_allowed_blocks() |
62 | 59 | ); |
63 | | - $settings['blockpartyModalTriggerAllowedBlocks'] = array_values( |
| 60 | + |
| 61 | + return array_values( |
64 | 62 | array_filter( is_array( $raw ) ? $raw : [], 'is_string' ) |
65 | 63 | ); |
| 64 | +} |
66 | 65 |
|
| 66 | +/** |
| 67 | + * Returns the filtered list of blocks allowed as modal triggers. |
| 68 | + * |
| 69 | + * @return string[] Block names. |
| 70 | + */ |
| 71 | +function get_modal_trigger_allowed_blocks(): array { |
67 | 72 | /** @psalm-suppress MixedAssignment */ |
68 | | - $inner_raw = apply_filters( |
69 | | - 'blockparty_modal_inner_allowed_blocks', |
70 | | - get_default_modal_inner_allowed_blocks() |
| 73 | + $raw = apply_filters( |
| 74 | + 'blockparty_modal_trigger_allowed_blocks', |
| 75 | + get_default_modal_trigger_allowed_blocks() |
71 | 76 | ); |
72 | | - $settings['blockpartyModalInnerAllowedBlocks'] = array_values( |
73 | | - array_filter( is_array( $inner_raw ) ? $inner_raw : [], 'is_string' ) |
| 77 | + |
| 78 | + return array_values( |
| 79 | + array_filter( is_array( $raw ) ? $raw : [], 'is_string' ) |
74 | 80 | ); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Passes allowed-block lists to the block editor script. |
| 85 | + * |
| 86 | + * Custom keys added via block_editor_settings_all are stripped by the editor |
| 87 | + * allowlist in recent WordPress versions, so the lists are localized here. |
| 88 | + */ |
| 89 | +function enqueue_block_editor_data(): void { |
| 90 | + if ( ! wp_script_is( 'blockparty-modal-editor-script', 'registered' ) ) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + wp_localize_script( |
| 95 | + 'blockparty-modal-editor-script', |
| 96 | + 'blockpartyModalEditorSettings', |
| 97 | + [ |
| 98 | + 'innerAllowedBlocks' => get_modal_inner_allowed_blocks(), |
| 99 | + 'triggerAllowedBlocks' => get_modal_trigger_allowed_blocks(), |
| 100 | + ] |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_block_editor_data', 20 ); |
| 105 | + |
| 106 | +/** |
| 107 | + * Passes allowed-block lists to block editor settings (legacy path). |
| 108 | + * |
| 109 | + * @param array<array-key, mixed> $settings Block editor settings. |
| 110 | + * @param \WP_Block_Editor_Context $_context Block editor context (unused). |
| 111 | + * @return array<array-key, mixed> Modified settings. |
| 112 | + */ |
| 113 | +function block_editor_settings_modal_trigger_blocks( array $settings, \WP_Block_Editor_Context $_context ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Required by block_editor_settings_all filter signature. |
| 114 | + $settings['blockpartyModalTriggerAllowedBlocks'] = get_modal_trigger_allowed_blocks(); |
| 115 | + $settings['blockpartyModalInnerAllowedBlocks'] = get_modal_inner_allowed_blocks(); |
75 | 116 |
|
76 | 117 | return $settings; |
77 | 118 | } |
@@ -136,16 +177,8 @@ function render_block_add_modal_trigger( $block_content, array $block ) { |
136 | 177 | return $block_content; |
137 | 178 | } |
138 | 179 |
|
139 | | - $block_name = (string) ( $block['blockName'] ?? '' ); |
140 | | - /** @psalm-suppress MixedAssignment */ |
141 | | - $raw_blocks = apply_filters( |
142 | | - 'blockparty_modal_trigger_allowed_blocks', |
143 | | - get_default_modal_trigger_allowed_blocks() |
144 | | - ); |
145 | | - $allowed_blocks = array_filter( |
146 | | - is_array( $raw_blocks ) ? $raw_blocks : array(), |
147 | | - 'is_string' |
148 | | - ); |
| 180 | + $block_name = (string) ( $block['blockName'] ?? '' ); |
| 181 | + $allowed_blocks = get_modal_trigger_allowed_blocks(); |
149 | 182 |
|
150 | 183 | if ( '' === $block_name || ! in_array( $block_name, $allowed_blocks, true ) ) { |
151 | 184 | return $block_content; |
|
0 commit comments