[wp-trac] [WordPress Trac] #53026: register_block_type_from_metadata fails if path points to a JSON file that is not named "block.json"

WordPress Trac noreply at wordpress.org
Tue Apr 13 14:43:43 UTC 2021


#53026: register_block_type_from_metadata fails if path points to a JSON file that
is not named "block.json"
--------------------------+-----------------------------
 Reporter:  artifacts     |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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
 <?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
 <?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"

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53026>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list