Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.
Merged
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
29 changes: 29 additions & 0 deletions includes/class-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,33 @@ public static function replace_child_block_by_path( array $existing_child_blocks

return $existing_child_blocks;
}

/**
* Get a block attribute value from the current block context or the current AJAX render request.
*
* @param string $attribute_name The attribute name.
* @param WP_Block|null $wp_block The current WordPress block object. Defaults to null if not provided.
* @return mixed|null The attribute value, null if the attribute is not found.
*/
public static function get_block_attribute( string $attribute_name, WP_Block|null $wp_block = null ): mixed {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! empty( $_REQUEST['block'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$decoded_block = json_decode( sanitize_text_field( wp_unslash( $_REQUEST['block'] ) ), true );

if ( ! empty( $decoded_block[ $attribute_name ] ) ) {
return $decoded_block[ $attribute_name ];
}
}

if (
! empty( $wp_block ) &&
isset( $wp_block->attributes ) &&
! empty( $wp_block->attributes[ $attribute_name ] )
) {
return $wp_block->attributes[ $attribute_name ];
}

return null;
}
}
5 changes: 4 additions & 1 deletion includes/scripts/admin/admin-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ class AdminBlock {

/**
* Set a block attribute.
* Re-render the block.
*
* @param {string} name - The name of the attribute.
* @param {any} value - The value of the attribute.
* @returns {void}
*/
setAttribute(name, value) {
let attributes = {};
let attributes = {
_forceRender: Date.now()
};

attributes[name] = value;

Expand Down