WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.52k stars 4.21k forks source link

Move `WP_Navigation_Block_Renderer` class to its own file. #59239

Open swissspidy opened 9 months ago

swissspidy commented 9 months ago

Core requires class definitions to be in their own file.

The WP_Navigation_Block_Renderer class should be moved out of packages/block-library/src/navigation/index.php to its own file, packages/block-library/src/navigation/class-wp-navigation-block-renderer.php

Besides: why is the class not prefixed anyway? I thought Gutenberg fixed class and function prefixes during the build process.

talldan commented 9 months ago

I think it's a long (possibly painful) story why it's implemented this way.

@scruffian might be able to tell you more.

scruffian commented 8 months ago

This is what I did at first but then we moved it back: https://github.com/WordPress/gutenberg/pull/57979

See also: https://core.trac.wordpress.org/ticket/59867#comment:4 https://github.com/WordPress/gutenberg/pull/58429

In summary, there are several competing wishes...

On the one hand we want to put code into private functions inside classes so that they can't be used elsewhere in WordPress which would give us a larger surface to maintain with respect to backwards compatibility. Static classes are a good way to achieve this.

On the other hand, at the moment all the PHP rendering code for blocks lives in the block library package, and is back-ported automatically. We are reluctant to start moving block specific code outside of this package.

An ideal solution would allow us to keep all the PHP code inside the block-library package, and also to find a mechanism to keep functions clearly namespaced and not exposed globally for everyone to use. PHP namespaces could be an alternative to a static class.

swissspidy commented 8 months ago

I‘m not suggesting to move this class out of the package, just into a separate file next to index.php. That doesn‘t change anything about the API surface at all.

scruffian commented 8 months ago

I think that would be great but it involves making some changes to the way that the PHP files are built for the block library package. Hopefully someone is able to work on that soon!