Skip to content

Commit

Permalink
register_block_metadata_collection: Silence path validation notices (…
Browse files Browse the repository at this point in the history
  • Loading branch information
mreishus authored and matticbot committed Dec 4, 2024
1 parent d971cad commit fec43e7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This is an alpha version! The changes listed here are not final.
- AI Assistant: Add feature flag for AI feedback feature
- External media: Add logic to persist session in cookie
- Jetpack AI: remove dead and deprecated code
- register_block_metadata_collection: Silence path validation notices
- Testing: Remove old instructions.
- Updated package dependencies.

Expand Down
30 changes: 30 additions & 0 deletions class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,32 @@ public static function site_supports_next_default_size() {
return false;
}

/**
* Temporarily bypasses _doing_it_wrong() notices for block metadata collection registration.
*
* WordPress 6.7 introduced block metadata collections (with strict path validation).
* Any sites using symlinks for plugins will fail the validation which causes the metadata
* collection to not be registered. However, the blocks will still fall back to the regular
* registration and no functionality is affected.
* While this validation is being discussed in WordPress Core (#62140),
* this method allows registration to proceed by temporarily disabling
* the relevant notice.
*
* @since 14.2-a.0
*
* @param bool $trigger Whether to trigger the error.
* @param string $function The function that was called.
* @param string $message A message explaining what was done incorrectly.
* @param string $version The version of WordPress where the message was added.
* @return bool Whether to trigger the error.
*/
public static function bypass_block_metadata_doing_it_wrong( $trigger, $function, $message, $version ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( $function === 'WP_Block_Metadata_Registry::register_collection' ) {
return false;
}
return $trigger;
}

/**
* Register block metadata collection for Jetpack blocks.
* This allows for more efficient block metadata loading by avoiding
Expand All @@ -1351,11 +1377,15 @@ public static function site_supports_next_default_size() {
public static function register_block_metadata_collection() {
$meta_file_path = JETPACK__PLUGIN_DIR . '_inc/blocks/blocks-manifest.php';
if ( function_exists( 'wp_register_block_metadata_collection' ) && file_exists( $meta_file_path ) ) {
add_filter( 'doing_it_wrong_trigger_error', array( __CLASS__, 'bypass_block_metadata_doing_it_wrong' ), 10, 4 );

// @phan-suppress-next-line PhanUndeclaredFunction -- New in WP 6.7. We're checking if it exists first.
wp_register_block_metadata_collection(
JETPACK__PLUGIN_DIR . '_inc/blocks/',
$meta_file_path
);

remove_filter( 'doing_it_wrong_trigger_error', array( __CLASS__, 'bypass_block_metadata_doing_it_wrong' ), 10 );
}
}
}
Expand Down

0 comments on commit fec43e7

Please sign in to comment.