Make WordPress Core


Ignore:
Timestamp:
01/29/2025 09:14:53 PM (3 weeks ago)
Author:
flixos90
Message:

Editor: Relax restrictions around registration of block metadata collections.

This changeset allows for block metadata collections to be registered for almost any source, such as MU plugins, themes, or custom directories with e.g. symlinked plugins or symlinked themes. Prior to the change, block metadata collections could only be registered for plugins and WordPress Core.

There are still safeguards in place to prevent registration of collections in locations that would cause conflicts. For example, it is not possible to register a collection for the entire wp-content/plugins directory or the entire wp-content/themes directory, since such a collection would conflict with any specific plugin's or theme's collection. In case developers would like to enable this safeguard for their own custom directories, they can use the new wp_allowed_block_metadata_collection_roots filter.

Reviewed by jorbin.
Merges [59730] to the 6.7 branch.

Props assassinateur, bowedk, desrosj, dougwollison, flixos90, glynnquelch, gziolo, jorbin, mreishus, swissspidy.
Fixes #62140.

Location:
branches/6.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.7

  • branches/6.7/tests/phpunit/tests/blocks/wpBlockMetadataRegistry.php

    r59132 r59731  
    8181    }
    8282
    83     public function test_register_collection_with_non_existent_path() {
    84         $non_existent_path = '/path/that/does/not/exist';
     83    /**
     84     * @ticket 62140
     85     */
     86    public function test_register_collection_with_valid_muplugin_path() {
     87        $plugin_path = WPMU_PLUGIN_DIR . '/my-plugin/blocks';
     88        $result      = WP_Block_Metadata_Registry::register_collection( $plugin_path, $this->temp_manifest_file );
     89        $this->assertTrue( $result, 'Valid must-use plugin path should be registered successfully' );
     90    }
     91
     92    /**
     93     * @ticket 62140
     94     */
     95    public function test_register_collection_with_invalid_muplugin_path() {
     96        $invalid_plugin_path = WPMU_PLUGIN_DIR;
    8597
    8698        $this->setExpectedIncorrectUsage( 'WP_Block_Metadata_Registry::register_collection' );
    8799
    88         $result = WP_Block_Metadata_Registry::register_collection( $non_existent_path, $this->temp_manifest_file );
    89         $this->assertFalse( $result, 'Non-existent path should not be registered' );
     100        $result = WP_Block_Metadata_Registry::register_collection( $invalid_plugin_path, $this->temp_manifest_file );
     101        $this->assertFalse( $result, 'Invalid must-use plugin path should not be registered' );
     102    }
     103
     104    /**
     105     * @ticket 62140
     106     */
     107    public function test_register_collection_with_valid_theme_path() {
     108        $theme_path = WP_CONTENT_DIR . '/themes/my-theme/blocks';
     109        $result     = WP_Block_Metadata_Registry::register_collection( $theme_path, $this->temp_manifest_file );
     110        $this->assertTrue( $result, 'Valid theme path should be registered successfully' );
     111    }
     112
     113    /**
     114     * @ticket 62140
     115     */
     116    public function test_register_collection_with_invalid_theme_path() {
     117        $invalid_theme_path = WP_CONTENT_DIR . '/themes';
     118
     119        $this->setExpectedIncorrectUsage( 'WP_Block_Metadata_Registry::register_collection' );
     120
     121        $result = WP_Block_Metadata_Registry::register_collection( $invalid_theme_path, $this->temp_manifest_file );
     122        $this->assertFalse( $result, 'Invalid theme path should not be registered' );
     123    }
     124
     125    /**
     126     * @ticket 62140
     127     */
     128    public function test_register_collection_with_arbitrary_path() {
     129        $arbitrary_path = '/var/arbitrary/path';
     130        $result         = WP_Block_Metadata_Registry::register_collection( $arbitrary_path, $this->temp_manifest_file );
     131        $this->assertTrue( $result, 'Arbitrary path should be registered successfully' );
     132    }
     133
     134    /**
     135     * @ticket 62140
     136     */
     137    public function test_register_collection_with_arbitrary_path_and_collection_roots_filter() {
     138        $arbitrary_path = '/var/arbitrary/path';
     139        add_filter(
     140            'wp_allowed_block_metadata_collection_roots',
     141            static function ( $paths ) use ( $arbitrary_path ) {
     142                $paths[] = $arbitrary_path;
     143                return $paths;
     144            }
     145        );
     146
     147        $this->setExpectedIncorrectUsage( 'WP_Block_Metadata_Registry::register_collection' );
     148
     149        $result = WP_Block_Metadata_Registry::register_collection( $arbitrary_path, $this->temp_manifest_file );
     150        $this->assertFalse( $result, 'Arbitrary path should not be registered if it matches a collection root' );
     151
     152        $result = WP_Block_Metadata_Registry::register_collection( dirname( $arbitrary_path ), $this->temp_manifest_file );
     153        $this->assertFalse( $result, 'Arbitrary path should not be registered if it is a parent directory of a collection root' );
     154
     155        $result = WP_Block_Metadata_Registry::register_collection( $arbitrary_path . '/my-plugin/blocks', $this->temp_manifest_file );
     156        $this->assertTrue( $result, 'Arbitrary path should be registered successfully if it is within a collection root' );
     157    }
     158
     159    /**
     160     * @ticket 62140
     161     */
     162    public function test_register_collection_with_wp_content_parent_directory_path() {
     163        $invalid_path = dirname( WP_CONTENT_DIR );
     164
     165        $this->setExpectedIncorrectUsage( 'WP_Block_Metadata_Registry::register_collection' );
     166
     167        $result = WP_Block_Metadata_Registry::register_collection( $invalid_path, $this->temp_manifest_file );
     168        $this->assertFalse( $result, 'Invalid path (parent directory of "wp-content") should not be registered' );
     169    }
     170
     171    /**
     172     * @ticket 62140
     173     */
     174    public function test_register_collection_with_wp_includes_parent_directory_path() {
     175        $invalid_path = ABSPATH;
     176
     177        $this->setExpectedIncorrectUsage( 'WP_Block_Metadata_Registry::register_collection' );
     178
     179        $result = WP_Block_Metadata_Registry::register_collection( $invalid_path, $this->temp_manifest_file );
     180        $this->assertFalse( $result, 'Invalid path (parent directory of "wp-includes") should not be registered' );
     181    }
     182
     183    public function test_register_collection_with_non_existent_manifest() {
     184        $non_existent_manifest = '/path/that/does/not/exist/block-manifest.php';
     185
     186        $this->setExpectedIncorrectUsage( 'WP_Block_Metadata_Registry::register_collection' );
     187
     188        $result = WP_Block_Metadata_Registry::register_collection( '/var/arbitrary/path', $non_existent_manifest );
     189        $this->assertFalse( $result, 'Non-existent manifest should not be registered' );
    90190    }
    91191}
Note: See TracChangeset for help on using the changeset viewer.