WordPress.org

Make WordPress Core

Opened 4 months ago

Last modified 3 months ago

#53026 new defect (bug)

register_block_type_from_metadata fails if path points to a JSON file that is not named "block.json"

Reported by: artifacts Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.5
Component: Editor Keywords: 2nd-opinion
Focuses: Cc:

Description

If a path to a file is given that does not end with "block.json", the function will exit early and the block won't be registered because the path to the json file is not found.

The documentation states that either a directory or a path to a JSON file can be given:

Parameters #Parameters
$file_or_folder
(string) (Required) Path to the JSON file with metadata definition for the block or path to the folder where the block.json file is located.

Example:

register_block_type_from_metadata() is called with $path = path_join(DIR,'block-section.json'):

<?php
$path = path_join(__DIR__,'block-section.json');
register_block_type_from_metadata($path);

in register_block_type_from_metadata this test is performed:

<?php

function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
195             $filename      = 'block.json';
196             $metadata_file = ( substr( $file_or_folder, -strlen( $filename ) ) !== $filename ) ?
197                     trailingslashit( $file_or_folder ) . $filename :
198                     $file_or_folder;
199             if ( ! file_exists( $metadata_file ) ) {
200                     return false;
201             }

wich results in

"ction.json"

"ction.json" is not equal to $filename ("block.json"), so the $metadata_file will be assigned to a wrong path, in this case

"/var/www/html/wp-content/plugins/artifacts-section-block/block-section.json/block.json"

Change History (1)

#1 @desrosj
3 months ago

  • Component changed from General to Editor
  • Keywords 2nd-opinion added
  • Version changed from trunk to 5.5

Welcome to Trac, @artifacts!

Thanks for this report. You are correct and I am able to confirm this behavior. Looks like [48141] introduced the issue in 5.5.

Before a fix can be created, I'd like to get some feedback from some editor contributors that would know more about the background of this.

Specifically, I'd like clarity on whether it is preferred that block files always be named block.json and this was meant to enforce this as a best practice (the docs should be updated to be more clear in this case), or if this is an oversight and any name is permitted for the JSON file (the code needs to be corrected).

Note: See TracTickets for help on using tickets.