Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin: Document the project structure and conventions used in the lib folder for PHP code #39603

Merged
merged 8 commits into from
Mar 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Gutenberg PHP

## File structure

Gutenberg adds features to WordPress Core using PHP hooks and filters. Some
features, once considered stable and useful, are merged into Core during a Core
release. Some features remain in the plugin forever or are removed.

To make it easier for contributors to know which features need to be merged to
Core and which features can be deleted, Gutenberg uses the following file
structure for its PHP code:

- `lib/experimental` - Experimental features that exist only in the plugin. They
are not ready to be merged to Core.
- `lib/stable` - Stable features that exist only in the plugin. They could one
day be merged to Core, but not yet.
- `lib/compat/wordpress-X.Y` - Stable features that are intended to be merged to
Copy link
Member

Choose a reason for hiding this comment

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

Not suggesting we do it right away, but do you think it'd be helpful for folks if expanded the compat topic at some stage? For example rationale, preferred naming conventions when extending classes, things to test, and so on.

There are also plenty of lessons to draw from PRs such as #38883 and #38671

Or maybe it's just too fuzzy an area to pin down. 🌪️

Copy link
Member Author

@noisysocks noisysocks Mar 21, 2022

Choose a reason for hiding this comment

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

Yes absolutely. This should be a living document, like all our docs. I think we're still figuring a lot of that out, so it's hard to pen down right now. If you have any suggestions on things we can add now then please go ahead!

Core in the future X.Y release, or that were previously merged to Core in the
X.Y release and remain in the plugin for backwards compatibility when running
the plugin on older versions of WordPress.

## Best practices

### Prefer the `wp` prefix

For features that may be merged to Core, it's best to use a `wp_` prefix for functions or a `WP_` prefix for classes.

This applies to both experimental and stable features.

Using the `wp_` prefix avoids us having to rename functions and classes from `gutenberg_` to `wp_` if the feature is merged to Core.

Functions that are intended solely for the plugin, e.g., plugin infrastructure, should use the `gutenberg_` prefix.

Comment on lines +24 to +33
Copy link
Contributor

@ajlende ajlende Mar 22, 2022

Choose a reason for hiding this comment

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

Any thoughts on naming conventions for when we're updating a function from core?

For example, this change from https://github.com/WordPress/gutenberg/pull/38681/files?w=1#diff-089523f9c993d53f6f02926d28f7a80394d93740e7d16e47e17244547b2b8f5e. The core action is removed and the plugin action is added to replace it.

My intention with that change was that the gutenberg_ version of the function in the plugin would replace the wp_ version when porting to core. If we're reserving that prefix for things that should never go into core, we need another convention.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we really have a good strategy for this kind of stuff. We have a similar nightmare with WP_Theme_JSON needing Gutenberg_Theme_JSON and WP_Theme_JSON_5_9 😅

Using gutenberg_ seems fine to me, though it will get tricky when the new gutenberg_ function makes it into WP 6.0 and we want to make more changes in 6.1 We won't be able to tell whether the wp_ function that exists is the 5.9 version or the 6.0 version.

#### Feature that might be merged to Core

```php
function wp_get_navigation( $slug ) { ... }
```

#### Plugin infrastructure that will never be merged to Core

```php
function gutenberg_get_navigation( $slug ) { ... }
```

### Group PHP code by _feature_
Copy link
Contributor

Choose a reason for hiding this comment

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

I actually think the lib/compat folder should group its code by "core" php file. Meaning if the change needs to happen in edit-form-blocks.php in 5.9, I'd put it in lib/compat/5.9/edit-form-blocks.php and for new files, try to come up with the best file name that will suite core merge. In practice, this is also very close to "group by feature".

The main goal is to simplify the work for the person back porting.

Copy link
Member Author

Choose a reason for hiding this comment

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

The downside of that though is that you can't move features in and out of "experimental" status e.g. using git mv lib/experimental/cool-new-feature.php lib/compat/wordpress-6.0/cool-new-feature.php. Not sure if we do this in practice, mind you 😀


Developers should organize PHP into files or folders by _feature_, not by _component_.

When defining a function that will be hooked, developers should call `add_action` and `add_filter` immediately after the function declaration.

These two practices make it easier for PHP code to start in one folder (e.g., `lib/experimental`) and eventually move to another using a simple `git mv`.

#### Good

```php
// lib/experimental/navigation.php

function wp_get_navigation( $slug ) { ... }

function wp_register_navigation_cpt() { ... }

add_action( 'init', 'wp_register_navigation_cpt' );
```

#### Not so good

```php
// lib/experimental/functions.php

function wp_get_navigation( $slug ) { ... }

// lib/experimental/post-types.php

function wp_register_navigation_cpt() { ... }

// lib/experimental/init.php
add_action( 'init', 'wp_register_navigation_cpt' );
```

### Wrap functions and classes with `! function_exists` and `! class_exists`

Developers should take care to not define functions and classes that are already defined.

When writing new functions and classes, it's good practice to use `! function_exists` and `! class_exists`.

If Core has defined a symbol once and then Gutenberg defines it a second time, fatal errors will occur.

Wrapping functions and classes avoids such errors if the feature is merged to Core.

#### Good

```php
// lib/experimental/navigation/navigation.php

if ( ! function_exists( 'wp_get_navigation' ) ) {
function wp_get_navigation( $slug ) { ... }
}

// lib/experimental/navigation/class-wp-navigation.php

if ( class_exists( 'WP_Navigation' ) ) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, TIL you can call return at root level in a PHP file (i.e. not inside a function) to skip execution 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

PHP is pretty lawless.

}

class WP_Navigation { ... }
```

#### Not so good

```php
// lib/experimental/navigation/navigation.php

function wp_get_navigation( $slug ) { ... }

// lib/experimental/navigation/class-gutenberg-navigation.php

class WP_Navigation { ... }
```

Furthermore, a quick codebase search will also help you know if your new method is unique.

### Note how your feature should look when merged to Core

Developers should write a brief note about how their feature should be merged to Core, for example, which Core file or function should be patched.
Copy link
Contributor

Choose a reason for hiding this comment

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

This will help so much with backports to Core ⭐


Notes can be included in the doc comment.

This helps future developers know what to do when merging Gutenberg features into Core.

#### Good

```php
/**
* Returns a navigation object for the given slug.
*
* Should live in `wp-includes/navigation.php` when merged to Core.
*
* @param string $slug
*
* @return WP_Navigation
*/
function wp_get_navigation( $slug ) { ... }
```