1313 * @since 5.6.0
1414 *
1515 * @access private
16+ *
17+ * @phpstan-type ApplyCallback callable( WP_Block_Type, array<string, mixed> ): array<string, mixed>
18+ * @phpstan-type RegisterCallback callable( WP_Block_Type ): void
1619 */
1720#[AllowDynamicProperties]
1821class WP_Block_Supports {
@@ -22,14 +25,20 @@ class WP_Block_Supports {
2225 *
2326 * @since 5.6.0
2427 * @var array
28+ * @phpstan-var array<string, array{
29+ * name: string,
30+ * apply?: ApplyCallback,
31+ * register_attribute?: RegisterCallback,
32+ * }>
2533 */
2634 private $ block_supports = array ();
2735
2836 /**
2937 * Tracks the current block to be rendered.
3038 *
3139 * @since 5.6.0
32- * @var array
40+ * @var array|null
41+ * @phpstan-var array<string, mixed>|null
3342 */
3443 public static $ block_to_render = null ;
3544
@@ -62,6 +71,8 @@ public static function get_instance() {
6271 * Initializes the block supports. It registers the block supports block attributes.
6372 *
6473 * @since 5.6.0
74+ *
75+ * @return void
6576 */
6677 public static function init () {
6778 $ instance = self ::get_instance ();
@@ -77,6 +88,13 @@ public static function init() {
7788 *
7889 * @param string $block_support_name Block support name.
7990 * @param array $block_support_config Array containing the properties of the block support.
91+ *
92+ * @phpstan-param array{
93+ * apply?: ApplyCallback,
94+ * register_attribute?: RegisterCallback,
95+ * } $block_support_config
96+ *
97+ * @return void
8098 */
8199 public function register ( $ block_support_name , $ block_support_config ) {
82100 $ this ->block_supports [ $ block_support_name ] = array_merge (
@@ -94,12 +112,16 @@ public function register( $block_support_name, $block_support_config ) {
94112 * @return string[] Array of HTML attribute values keyed by their name.
95113 */
96114 public function apply_block_supports () {
115+ if ( ! is_array ( self ::$ block_to_render ) ) {
116+ return array ();
117+ }
118+
97119 $ block_type = WP_Block_Type_Registry::get_instance ()->get_registered (
98120 self ::$ block_to_render ['blockName ' ]
99121 );
100122
101123 // If no render_callback, assume styles have been previously handled.
102- if ( ! $ block_type || empty ( $ block_type ) ) {
124+ if ( ! $ block_type ) {
103125 return array ();
104126 }
105127
@@ -121,7 +143,11 @@ public function apply_block_supports() {
121143
122144 if ( ! empty ( $ new_attributes ) ) {
123145 foreach ( $ new_attributes as $ attribute_name => $ attribute_value ) {
124- if ( empty ( $ output [ $ attribute_name ] ) ) {
146+ if ( ! is_scalar ( $ attribute_value ) || is_bool ( $ attribute_value ) ) {
147+ continue ;
148+ }
149+ $ attribute_value = (string ) $ attribute_value ;
150+ if ( ! array_key_exists ( $ attribute_name , $ output ) || '' === $ output [ $ attribute_name ] ) {
125151 $ output [ $ attribute_name ] = $ attribute_value ;
126152 } else {
127153 $ output [ $ attribute_name ] .= " $ attribute_value " ;
@@ -137,6 +163,8 @@ public function apply_block_supports() {
137163 * Registers the block attributes required by the different block supports.
138164 *
139165 * @since 5.6.0
166+ *
167+ * @return void
140168 */
141169 private function register_attributes () {
142170 $ block_registry = WP_Block_Type_Registry::get_instance ();
@@ -196,7 +224,7 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) {
196224 (array ) preg_split ( '/\s+/ ' , $ extra_attribute , -1 , PREG_SPLIT_NO_EMPTY ),
197225 (array ) preg_split ( '/\s+/ ' , $ new_attribute , -1 , PREG_SPLIT_NO_EMPTY )
198226 );
199- $ classes = array_unique ( array_filter ( $ classes ) );
227+ $ classes = array_unique ( $ classes );
200228 return implode ( ' ' , $ classes );
201229 },
202230 'id ' => static function ( $ new_attribute , $ extra_attribute ) {
@@ -207,12 +235,13 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) {
207235 },
208236 );
209237
238+ // Accept strings and numbers (cast to string); reject other types (bool, null, array, object).
210239 $ attributes = array ();
211240 foreach ( $ attribute_merge_callbacks as $ attribute_name => $ merge_callback ) {
212241 $ new_attribute = $ new_attributes [ $ attribute_name ] ?? '' ;
213242 $ extra_attribute = $ extra_attributes [ $ attribute_name ] ?? '' ;
214- $ new_attribute = is_string ( $ new_attribute ) ? $ new_attribute : '' ;
215- $ extra_attribute = is_string ( $ extra_attribute ) ? $ extra_attribute : '' ;
243+ $ new_attribute = is_scalar ( $ new_attribute ) && ! is_bool ( $ new_attribute ) ? ( string ) $ new_attribute : '' ;
244+ $ extra_attribute = is_scalar ( $ extra_attribute ) && ! is_bool ( $ extra_attribute ) ? ( string ) $ extra_attribute : '' ;
216245
217246 if ( '' === $ new_attribute && '' === $ extra_attribute ) {
218247 continue ;
@@ -222,8 +251,8 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) {
222251 }
223252
224253 foreach ( $ extra_attributes as $ attribute_name => $ value ) {
225- if ( ! isset ( $ attribute_merge_callbacks [ $ attribute_name ] ) ) {
226- $ attributes [ $ attribute_name ] = $ value ;
254+ if ( ! isset ( $ attribute_merge_callbacks [ $ attribute_name ] ) && is_scalar ( $ value ) && ! is_bool ( $ value ) ) {
255+ $ attributes [ $ attribute_name ] = ( string ) $ value ;
227256 }
228257 }
229258
0 commit comments