mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-14 17:44:25 +00:00
Editor: Introduce get_block_asset_url Utility Function.
This commit introduces a valuable utility function, get_block_asset_url, designed to simplify the retrieval of block asset URLs, such as those for CSS and JavaScript files. This utility eliminates redundancy in both register_block_script_handle and register_block_style_handle. Additionally, `get_block_asset_url` incorporates an early exit mechanism to optimize performance. This update includes comprehensive unit tests, covering various scenarios, including asset registration from core (wp-includes), themes, child themes, plugins, and mu-plugins. Props spacedmonkey, joemcgill, flixos90, gziolo. Fixes #58525. git-svn-id: https://develop.svn.wordpress.org/trunk@56683 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"apiVersion": 2,
|
||||
"title": "Example Theme Block",
|
||||
"name": "block-theme/example-block",
|
||||
"description": "Custom block registered from within a theme",
|
||||
"editorScript": "file:./index.js",
|
||||
"style": "file:./style.css"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
/* Test CSS file - RTL version */
|
||||
@@ -0,0 +1 @@
|
||||
/* Test CSS file */
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
return array(
|
||||
'dependencies' => array( 'wp-element', 'wp-blocks' ),
|
||||
'version' => 'test',
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
/* Test JavaScript file. */
|
||||
@@ -0,0 +1 @@
|
||||
/* Test CSS file - RTL version */
|
||||
@@ -0,0 +1 @@
|
||||
/* Test CSS file */
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
return array(
|
||||
'dependencies' => array( 'wp-element', 'wp-blocks' ),
|
||||
'version' => 'test',
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
/* Test JavaScript file. */
|
||||
116
tests/phpunit/tests/blocks/getBlockAssetUrl.php
Normal file
116
tests/phpunit/tests/blocks/getBlockAssetUrl.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for block asset urls.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Blocks
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @group blocks
|
||||
* @covers ::get_block_asset_url
|
||||
*/
|
||||
class Tests_Get_Block_Asset_Url extends WP_UnitTestCase {
|
||||
/**
|
||||
* Original theme directory.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private $orig_theme_dir;
|
||||
|
||||
public function set_up() {
|
||||
global $wp_theme_directories;
|
||||
|
||||
parent::set_up();
|
||||
|
||||
// Sets up the `wp-content/themes/` directory to ensure consistency when running tests.
|
||||
$this->orig_theme_dir = $wp_theme_directories;
|
||||
$wp_theme_directories = array( WP_CONTENT_DIR . '/themes', realpath( DIR_TESTDATA . '/themedir1' ) );
|
||||
|
||||
wp_clean_themes_cache();
|
||||
unset( $GLOBALS['wp_themes'] );
|
||||
}
|
||||
|
||||
public function tear_down() {
|
||||
global $wp_theme_directories;
|
||||
|
||||
$wp_theme_directories = $this->orig_theme_dir;
|
||||
|
||||
wp_clean_themes_cache();
|
||||
unset( $GLOBALS['wp_themes'] );
|
||||
|
||||
parent::tear_down();
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58525
|
||||
*/
|
||||
public function test_core_block() {
|
||||
$path = ABSPATH . WPINC . '/blocks/file/view.min.js';
|
||||
$url = get_block_asset_url( $path );
|
||||
|
||||
$this->assertStringNotContainsString( ABSPATH . WPINC, 'The return block asset url should not contain include path.' );
|
||||
$this->assertSame( includes_url( '/blocks/file/view.min.js' ), $url, 'The return block asset url should match includes url.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58525
|
||||
*/
|
||||
public function test_parent_theme() {
|
||||
switch_theme( 'block-theme' );
|
||||
|
||||
$path = wp_normalize_path( realpath( DIR_TESTDATA . '/themedir1/block-theme/blocks/example-block/view.js' ) );
|
||||
$url = get_block_asset_url( $path );
|
||||
|
||||
$this->assertSame( get_template_directory_uri() . '/blocks/example-block/view.js', $url );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58525
|
||||
*/
|
||||
public function test_child_theme() {
|
||||
switch_theme( 'block-theme-child' );
|
||||
|
||||
$path = wp_normalize_path( realpath( DIR_TESTDATA . '/themedir1/block-theme-child/blocks/example-block/view.js' ) );
|
||||
$url = get_block_asset_url( $path );
|
||||
|
||||
$this->assertSame( get_stylesheet_directory_uri() . '/blocks/example-block/view.js', $url );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58525
|
||||
*/
|
||||
public function test_plugin() {
|
||||
$path = WP_PLUGIN_DIR . '/test-plugin/blocks/example-block/view.js';
|
||||
$url = get_block_asset_url( $path );
|
||||
|
||||
$this->assertStringNotContainsString( WP_PLUGIN_DIR, $url, 'The return block asset url should not contain plugin path.' );
|
||||
$this->assertSame( plugins_url( 'view.js', $path ), $url, 'The return block asset url should match plugin url.' );
|
||||
$this->assertStringStartsWith( WP_PLUGIN_URL, $url, 'The return block asset url should contain the url that support with the mu plugin url.' );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58525
|
||||
*/
|
||||
public function test_muplugin() {
|
||||
$path = WPMU_PLUGIN_DIR . '/test-plugin/example-block/view.js';
|
||||
$url = get_block_asset_url( $path );
|
||||
|
||||
$this->assertStringNotContainsString( WPMU_PLUGIN_DIR, $url, 'The return block asset url should not contain plugin path.' );
|
||||
$this->assertSame( plugins_url( 'view.js', $path ), $url, 'The return block asset url should match plugin url.' );
|
||||
$this->assertStringStartsWith( WPMU_PLUGIN_URL, $url, 'The return block asset url should contain the url that support with the mu plugin url.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58525
|
||||
*/
|
||||
public function test_empty() {
|
||||
$url = get_block_asset_url( '' );
|
||||
|
||||
$this->assertFalse( $url );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user