Skip to content

Fixes registering block scripts & styles for parent themes#3921

Closed
jack-h2ml wants to merge 6 commits intoWordPress:trunkfrom
jack-h2ml:trunk
Closed

Fixes registering block scripts & styles for parent themes#3921
jack-h2ml wants to merge 6 commits intoWordPress:trunkfrom
jack-h2ml:trunk

Conversation

@jack-h2ml
Copy link
Copy Markdown

Fixes for both the register_block_script_handle() and register_block_style_handle() functions within wp-includes/blocks.js , allowing for child themes to access and utilise blocks defined in parent themes.

Previously these functions could not handle blocks registered in Parent themes, however the above changes fix this by determining whether the given asset path belongs to either of the template or stylesheet directories (Parent and Child themes respectively) as opposed to just checking using get_theme_file_path('') as is currently the case, thus mitigating this issue.

An issue was raised under the WordPress / gutenberg Repo, however as wp-includes/blocks.js is included in the WordPress / wordpress-develop repo I have created the PR here instead. That original ticket can be found here.

Trac ticket: Ticket


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Fixes for both the 
    register_block_script_handle
and 
    register_block_style_handle 

Which allow for Child themes to access and utilise blocks defined in Parent themes. 

Previously these functions could not handle blocks registered in Parent themes, the above changes fix this by determining whether the given asset path belongs to either of the template or stylesheet directories (Parent and Child themes respectively).
@github-actions
Copy link
Copy Markdown

Hi @jack-h2ml! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in this blog post.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

jack-h2ml and others added 5 commits January 27, 2023 09:51
Fixes for PHP formatting issues as raised by PR unit-tests.
Removed errant tab from Line 252
Removed errant whitespace from line 252
Editor: Use a non-persistent object cache instead of transient in `wp…
jack-h2ml added a commit to jack-h2ml/H2ML-FSE-Starter-Framework that referenced this pull request Jan 27, 2023
Implemented a workaround for the following [WP Core PR](WordPress/wordpress-develop#3921)
@jack-h2ml
Copy link
Copy Markdown
Author

jack-h2ml commented Jan 27, 2023

Temporary Workaround

I have created a filter based workaround for the above issue, just incase anyone lands here!

Tested and working on my local dev server [MacOS 13.1, NGINX, PHP 8.0] and SiteGround Hosting [Linux, Apache, PHP 7.4.33] without issue thus far.

I do have some reservations with regards to plugins filtering theme_file_path & possibly with Windows based environments - use with caution and always test on a dev server before adding to production.

Commented

/**
 *
 * If registering a block which is solely defined in the current themes parent,
 * then add the `overrideThemeFilePath` filter to `theme_file_path`, this serves 
 * as a temporary workaround for https://github.com/WordPress/wordpress-develop/pull/3921
 * 
 */

add_action('init', function() {
	//
	// The filter function for overriding `get_theme_file_path()`
	//
	function overrideThemeFilePath($_, $file) {
		$path = get_template_directory() . '/' . $file;
		return $path;
	}
	//
	// Add the filter where appropriate.
	//
	add_filter('block_type_metadata', function($metadata) {
		if(array_key_exists('file', $metadata)) {
			// Normalize the block path
			$blockPathNorm = wp_normalize_path($metadata['file']);
			// Cache the $templatePathNorm and $stylesheetPathNorm to avoid unnecessary additional calls.
			static $templatePathNorm   = '';
			static $stylesheetPathNorm = '';
			if (!$templatePathNorm || !$stylesheetPathNorm) {
				$templatePathNorm   = wp_normalize_path(get_template_directory());
				$stylesheetPathNorm = wp_normalize_path(get_stylesheet_directory());
			}
			// If looking at a block which is only defined in the parent theme then add the filter.
			$isTemplateBlock     = str_starts_with($blockPathNorm, $templatePathNorm);
			$isStylesheetBlock   = str_starts_with($blockPathNorm, $stylesheetPathNorm);
			if($isTemplateBlock && !$isStylesheetBlock) {
				add_filter('theme_file_path', 'overrideThemeFilePath', 10, 2);
			}
		}
		return $metadata;
	}, 10, 1);
	//
	// Ensure the filter is removed as soon as possible
	//
	add_filter('block_type_metadata_settings', function($settings, $metadata) {
		remove_filter('theme_file_path', 'overrideThemeFilePath', 10);
		return $settings;
	}, 10, 2);
}, 10);

Compacted

Note the three add_filter calls are nested within the add_action call, so do not split these lines or add code in between them.

/**
 *
 * If registering a block which is solely defined in the current themes parent,
 * then add the `overrideThemeFilePath` filter to `theme_file_path`, this serves 
 * as a temporary workaround for https://github.com/WordPress/wordpress-develop/pull/3921
 * 
 */

add_action('init',function(){function overrideThemeFilePath($_,$file){$path=get_template_directory().'/'.$file;return $path;}
add_filter('block_type_metadata',function($metadata){if(array_key_exists('file',$metadata)){$blockPathNorm=wp_normalize_path($metadata['file']);static $templatePathNorm='';static $stylesheetPathNorm='';if(!$templatePathNorm||!$stylesheetPathNorm){$templatePathNorm=wp_normalize_path(get_template_directory());$stylesheetPathNorm=wp_normalize_path(get_stylesheet_directory());}$isTemplateBlock=str_starts_with($blockPathNorm,$templatePathNorm);$isStylesheetBlock=str_starts_with($blockPathNorm,$stylesheetPathNorm);if($isTemplateBlock&&!$isStylesheetBlock){
add_filter('theme_file_path','overrideThemeFilePath',10,2);}} return $metadata;},10,1);
add_filter('block_type_metadata_settings',function($settings,$metadata){remove_filter('theme_file_path','overrideThemeFilePath',10);return $settings;},10,2);},10);

@jack-h2ml
Copy link
Copy Markdown
Author

A note on the above workaround, ensure that the workaround is called before you register your blocks or it won't work, so for example call the above init filter with a priority of 1, and then register your blocks in a separate init filter with a priority >= 2.

@audrasjb
Copy link
Copy Markdown
Contributor

audrasjb commented Jul 5, 2023

Closing in favor of #4788

@audrasjb audrasjb closed this Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants