Skip to content

Navigation: Namespace PHP functions#18589

Closed
obenland wants to merge 1 commit intomasterfrom
update/namespace
Closed

Navigation: Namespace PHP functions#18589
obenland wants to merge 1 commit intomasterfrom
update/namespace

Conversation

@obenland
Copy link
Copy Markdown
Member

Description

Adds gutenberg_ prefix to navigation block functions.
These functions will get merged to Core verbatim, so let's make sure functions like build_css_colors() don't clash.

For some reason Gutenberg doesn't recognize registration callback function names that don't start with register_block_?!

How has this been tested?

Loaded a navigation block in editor and frontend on a local install and made sure it doesn't cause errors.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.

@obenland obenland added [Type] Task Issues or PRs that have been broken down into an individual action to take [Feature] List View Menu item in the top toolbar to select blocks from a list of links. [Block] Navigation Affects the Navigation Block labels Nov 18, 2019
@obenland obenland self-assigned this Nov 18, 2019
@obenland obenland requested a review from aduth November 18, 2019 19:03
*/
function register_block_core_navigation_menu() {

function register_block_core_navigation() {
Copy link
Copy Markdown
Member Author

@obenland obenland Nov 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this can't be prefixed with gutenberg_?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are prefixed, but not in the source. The prefix is added at build time:

https://github.com/WordPress/gutenberg/blob/6fc0b70/webpack.config.js#L129-L139

You can see this in the build output:

https://plugins.trac.wordpress.org/browser/gutenberg/trunk/build/block-library/blocks/navigation-menu.php

It's done this way because core copies these files directly from Gutenberg:

https://github.com/WordPress/wordpress-develop/blob/4ebaf2b/tools/webpack/packages.js#L96-L108

Since Gutenberg needs to replace / augment the implementation of core blocks, and in order to avoid redeclaring the function, a prefix is added automatically.

As far as what I was suggesting with my comments in the previous pull request, we should write the source as though we expect these functions to exist one day in core, and as such consider whether build_css_colors is named specific enough given that it's used only by this one block (and not a generalized utility for building CSS colors).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know that Gutenberg would do that. In that case the prefix should probably change to something like block_{blockname}_*

Copy link
Copy Markdown
Contributor

@draganescu draganescu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works OK after the refactor of the function names. Will need to be rebased if #18607 is merged in first.

@getdave
Copy link
Copy Markdown
Contributor

getdave commented Jan 8, 2020

Is this just waiting on something to merge?

@draganescu
Copy link
Copy Markdown
Contributor

draganescu commented Jan 8, 2020

Is this just waiting on something to merge?
A rebase and a failing test. Will try to do the rebase myself.

@draganescu draganescu self-requested a review January 8, 2020 12:13
Copy link
Copy Markdown
Contributor

@draganescu draganescu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@getdave @obenland I basically copied these changes to the new location of the navigation index.php file as this PR got too far behind for a normal rebase. Please take it for a spin (I tested and things still work as expected) so it doesn't end up as me reviewing my code :D

Copy link
Copy Markdown
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have been clearer in my comment at #18589 (comment), but: We do not want any Gutenberg-specific prefixes here, because this code will be used verbatim as the core implementation. But, as such, for functions which are being added to the global PHP scope, they should be named in such a way that they are unique and sensible in the context of WordPress, because they will effectively become part of the WordPress public API.

Some prior art:

https://github.com/WordPress/WordPress/blob/cef48881f00c61e0630fff16c7b28ecb43ca309d/wp-includes/blocks/categories.php#L68

So for something like build_css_colors, naming it in a way which makes it clearer that it's intended to be used specifically as a utility function for the navigation block (if it can't otherwise be made into a generic utility). Something like get_navigation_block_css_colors.

It could also be something worth considering to mark as an @access private internal function. I'm not really sure the guidelines around this, but considering that these functions don't really have value outside their use by the block itself, it would seem to make sense to me.

https://make.wordpress.org/core/handbook/best-practices/inline-documentation-standards/php/#phpdoc-tags

@apeatling
Copy link
Copy Markdown
Contributor

@obenland Plans for this PR?

@obenland
Copy link
Copy Markdown
Member Author

That's a good question. In an ideal world I'd introduce a namespace Editor; declaration and be done with it.

they should be named in such a way that they are unique and sensible in the context of WordPress, because they will effectively become part of the WordPress public API.

That tells me we should prefix them with wp_, although I believe that would counter-act the paradigm of keeping Gutenberg CMS agnostic?

Is the solution to introduce a new editor_ namespace?

@aduth
Copy link
Copy Markdown
Member

aduth commented Jan 31, 2020

That tells me we should prefix them with wp_, although I believe that would counter-act the paradigm of keeping Gutenberg CMS agnostic?

Is the solution to introduce a new editor_ namespace?

For context, I'm not really familiar if one of the original goals with this block was that it not depend on anything with how WordPress deals with menus, etc. But at the point a block includes a PHP file, it seems to me there's a pretty high expectation about where that block is going to be able to run (namely, WordPress). And if it doesn't require any WordPress functions, is it even dynamic? And if it's not dynamic, then why does the PHP file exist at all (vs. saving static markup from the block's save JavaScript)? In any case, inside a PHP file I'm not sure we really need to act as if WordPress doesn't exist, or that its functions are unavailable to us. Even if used outside WordPress, it would still be correct to refer to these block implementations as being part of the "WordPress block library", so at least on the point of naming, I don't think it's an inaccurate prefix to use.

To me, the agnosticism is more about removing biases about how we tend to think of content, avoiding unnecessary dependencies, and maximizing reusability. I also think that as long as we treat block types as individual units, these decisions and compromises can be made on a block-by-block basis. Maybe it's fine this block depends on WordPress, and another implementation of a Navigation block could work entirely differently.

Back to the original question:

So it would be okay enough I think. There might be better alternatives. Even in the context of WordPress core code, I tend to see the wp_ used inconsistently, perhaps when better names exist.

Consider as well the name and structure of the Navigation block registration function:

function register_block_core_navigation() {

There could be something of a pattern here around [verb]_block_[block_namespace]_[block_slug]_[extra]

So something like:

  • build_block_core_navigation_markup
  • build_block_core_navigation_css_font_sizes
  • build_block_core_navigation_color_attributes

Maybe not the most expressive, but they at least align to a consistent pattern to follow.

The prior art is a bit all over the place, to be honest.

function core_social_link_get_icon( $site ) {

function build_dropdown_script_block_core_categories( $dropdown_id ) {

@aduth
Copy link
Copy Markdown
Member

aduth commented Feb 7, 2020

Appears that this was handled separately in #20039.

@aduth aduth closed this Feb 7, 2020
@aduth aduth deleted the update/namespace branch February 7, 2020 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Navigation Affects the Navigation Block [Feature] List View Menu item in the top toolbar to select blocks from a list of links. [Type] Task Issues or PRs that have been broken down into an individual action to take

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants