PluginProbe
Gutenberg / 16.8.1
Gutenberg v16.8.1
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / README.md

README.md in Gutenberg 16.8.1, at lib/README.md

182 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 # Gutenberg PHP
2
3 The Gutenberg plugin is continuously enhancing existing features and creating new ones. Some features, once considered stable and useful, are merged into Core (the WordPress source code) during a WordPress release. Others remain in the plugin forever or are eventually removed as the minimum supported WordPress version changes.
4
5 During a WordPress release, new features, bugfixes and other changes are "synced" between the Gutenberg plugin and WordPress Core. Consistent naming and directory structures make this process easier by preventing naming conflicts and compartmentalizing release-specific code.
6
7 The following documentation is intended to act as a guide only. If you're unsure about naming or where to place new PHP files, please don't hesitate to ping other contributors on Github or ask in the #core-editor channel on [](https://make.wordpress.org/chat/WordPress Slack](https://make.wordpress.org/chat/](https://make.wordpress.org/chat/).
8
9 ## File structure
10
11 To make it easier for contributors to identify features that should be merged into Core and those that can be deleted, Gutenberg uses the following file structure for its PHP code:
12
13 - `lib/experimental` - Experimental features that exist only in the plugin. They should not be merged into Core.
14 - `lib/compat/wordpress-X.Y` - Stable features that are intended to be merged into Core in a 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.
15 - `lib/compat/plugin` - Features for backwards compatibility for the plugin consumers. These files don't need to be merged into Core and should have a timeline for when they should be removed from the plugin.
16
17 Files at the root of `/lib` are generally considered to contain "evergreen" code. Such code is both fundamental to the proper functioning of the plugin, and also so often updated that versioning it between WordPress releases is not practical. Changes to these files are merged into Core as required.
18
19 ## Best practices
20
21 There are a few best practices that should be observed when adding new files to the Gutenberg plugin.
22
23 ### Using `gutenberg` suffixes/prefixes vs. `wp` prefixes
24
25 To avoid naming conflicts with WordPress Core and other plugins, the Gutenberg plugin uses the `gutenberg` identifier in many of its PHP classes and function names, e.g., `WP_Theme_JSON_Gutenberg` and `gutenberg_get_block_editor_settings`.
26
27 This is especially so for classes and functions whose functionality is ubiquitous and constantly being updated — so-called "evergreen" code. Anything related to `WP_Theme_JSON_Gutenberg` is a good example of this: this class controls the way Gutenberg processes and outputs global styles and much more, and its methods are called in many places. In every aspect of plugin functionality, we want plugin users to have access to the latest versions of these files, even if they are running an older version of WordPress.
28
29 ```php
30 /**
31 * Returns something useful.
32 *
33 * @since 6.2.0 Updates to something even more useful.
34 * @since 6.3.0 Now more useful than ever.
35 *
36 * @return string Something useful.
37 */
38 function gutenberg_get_something_useful() {
39 // ...
40 }
41 ```
42
43 When porting new functions into Core, the function must be renamed to use the `wp_` prefix for functions or a `WP_` prefix for classes.
44
45 ```php
46 /**
47 * Returns something useful.
48 *
49 * @since 6.2.0 Updates to something even more useful.
50 * @since 6.3.0 Now more useful than ever.
51 *
52 * @return string Something useful.
53 */
54 function wp_get_something_useful() {
55 // ...
56 }
57 ```
58
59 Plugin code that is stable and expected to be merged "as-is" into Core in the near future can use the `wp_` prefix for functions or a `WP_` prefix for classes.
60
61 When doing so, care must be taken to ensure that no duplicate declarations to create functions or classes exist between Gutenberg and WordPress core code. A quick codebase search will also help you know if your new names are unique.
62
63 Wrapping such code in `class_exists()` and `function_exists()` checks should be used to ensure it executes in the plugin up until it is merged to Core, or when running the plugin on older versions of WordPress.
64
65 ```php
66 if ( ! function_exists( 'wp_a_new_and_stable_feature' ) ) {
67 /**
68 * A very new and stable feature.
69 *
70 * @return string Something useful.
71 */
72 function wp_a_new_and_stable_feature() {
73 // ...
74 }
75 }
76 ```
77
78 Or for classes:
79
80 ```php
81 /**
82 * WP_A_Stable_Class class
83 *
84 * @package WordPress
85 * @since 6.3.0
86 */
87 if ( class_exists( 'WP_A_Stable_Class' ) ) {
88 return;
89 }
90
91 /**
92 * A very stable class that does something.
93 *
94 * @since 6.3.0
95 */
96 class WP_A_Stable_Class { ... }
97 ```
98
99 Wrapping code in `class_exists()` and `function_exists()` is usually inappropriate for evergreen code, or any plugin code that we expect to undergo constant change between WordPress releases, because it would prevent the latest versions of the code from being used. For example, the statement `class_exists( 'WP_Theme_JSON' )` would return `true` because the class already exists in Core.
100
101 When to use which prefix is a judgement call, but the general rule is that if you're unsure, use the `gutenberg` prefix because it will less likely give rise to naming conflicts.
102
103 As always, get in touch with your fellow contributors if you're unsure.
104
105 ### Documentation and annotations
106
107 For every class, method and function in the plugin, refer to the [](https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/WordPress PHP documentation standards](https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/](https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/) when documenting your code.
108
109 It's particularly important to observe annotation standards, and `@since` descriptions that specify the target WordPress version, so that all contributors can easily identify what needs to be (or what already has been) merged to Core and when.
110
111 Developers should also write a brief note about _how_ their feature should be merged to Core, for example, which Core file or function should be patched.
112
113 Notes can be included in the doc comment.
114
115 This helps future developers know what to do when merging Gutenberg features into Core.
116
117 ```php
118 /**
119 * Returns a navigation object for the given slug.
120 *
121 * Should live in `wp-includes/navigation.php` when merged to Core.
122 *
123 * @since 6.3.0
124 *
125 * @param string $slug
126 * @return WP_Navigation
127 */
128 function wp_get_navigation( $slug ) { ... }
129 ```
130
131 ### Group PHP code by _feature_
132
133 Developers should organize PHP into files or folders by _feature_, not by _component_.
134
135 When defining a function that will be hooked, developers should call `add_action` and `add_filter` immediately after the function declaration.
136
137 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`.
138
139 #### Good
140
141 ```php
142 // lib/experimental/navigation.php
143
144 function wp_get_navigation( $slug ) { ... }
145
146 function wp_register_navigation_cpt() { ... }
147
148 add_action( 'init', 'wp_register_navigation_cpt' );
149 ```
150
151 #### Not so good
152
153 ```php
154 // lib/experimental/functions.php
155
156 function wp_get_navigation( $slug ) { ... }
157
158 // lib/experimental/post-types.php
159
160 function wp_register_navigation_cpt() { ... }
161
162 // lib/experimental/init.php
163 add_action( 'init', 'wp_register_navigation_cpt' );
164 ```
165
166 ### Requiring files in lib/load.php
167 Should the load order allow it, try to group imports according to WordPress release, then feature. It'll help everyone to quickly recognise the files that belong to specific WordPress releases.
168
169 Existing comments in `lib/load.php` should act as a guide.
170
171 ## When to sync changes to Gutenberg PHP with Core and vice versa
172
173 If you've changed or added PHP files to the Gutenberg plugin, you'll need to confirm whether the changes are to be synced to WordPress Core, and therefore featured in the next release of WordPress.
174
175 The Gutenberg Github pull request in question should be labeled with the `Needs PHP backport` label if the changes are to be synced to Core.
176
177 If so, it is recommended to create a [](https://core.trac.wordpress.org/newticketnew Trac ticket](https://core.trac.wordpress.org/newticket](https://core.trac.wordpress.org/newticket) and submit a pull request to the [](https://github.com/WordPress/wordpress-developWordPress Core Github repository](https://github.com/WordPress/wordpress-develop](https://github.com/WordPress/wordpress-develop) soon after your pull request is merged.
178
179 So too, if you've made changes in WordPress Core to code that also lives in the Gutenberg plugin, these changes will need to be synced (often called "backporting") to Gutenberg. The relevant Gutenberg Github pull request should be labeled with the `Backport from WordPress Core` label.
180
181 If you're unsure, you can always ask for help in the #core-editor channel in [](https://make.wordpress.org/chat/WordPress Slack](https://make.wordpress.org/chat/](https://make.wordpress.org/chat/).
182