-
Notifications
You must be signed in to change notification settings - Fork 40
Migrate post type meta fields to block editor sidebar with legacy meta box fallback #21
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
3859af2
b088863
4a8f06c
9b065e6
34aa0ee
a956007
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,108 @@ | ||||||
| /** | ||||||
| * Block Editor Meta Fields Sidebar | ||||||
| * | ||||||
| * Registers a PluginDocumentSettingPanel for each field section defined by | ||||||
| * the PHP class. Field definitions are passed via the `starterPluginMetaFields` | ||||||
| * global object that is localised by Starter_Plugin_Post_Type::enqueue_block_editor_assets(). | ||||||
| * | ||||||
| * When the classic editor is active for a post type this script is never | ||||||
| * enqueued, so legacy meta boxes remain in use instead. | ||||||
| * | ||||||
| * @package Starter_Plugin | ||||||
| * @since 1.0.0 | ||||||
| */ | ||||||
| ( function () { | ||||||
| var fieldData = window.starterPluginMetaFields; | ||||||
|
|
||||||
| if ( ! fieldData || ! fieldData.fields || ! fieldData.fields.length ) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| var el = wp.element.createElement; | ||||||
| var Fragment = wp.element.Fragment; | ||||||
| var registerPlugin = wp.plugins.registerPlugin; | ||||||
| var PluginDocumentSettingPanel = wp.editor.PluginDocumentSettingPanel; | ||||||
| var TextControl = wp.components.TextControl; | ||||||
| var useSelect = wp.data.useSelect; | ||||||
| var useDispatch = wp.data.useDispatch; | ||||||
|
|
||||||
| if ( ! PluginDocumentSettingPanel ) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // Group fields by their declared section key. | ||||||
| var fieldsBySection = {}; | ||||||
| fieldData.fields.forEach( function ( field ) { | ||||||
| var section = field.section || 'default'; | ||||||
| if ( ! fieldsBySection[ section ] ) { | ||||||
| fieldsBySection[ section ] = []; | ||||||
| } | ||||||
| fieldsBySection[ section ].push( field ); | ||||||
| } ); | ||||||
|
|
||||||
| var sectionKeys = Object.keys( fieldsBySection ); | ||||||
|
|
||||||
| /** | ||||||
| * Renders one sidebar panel for each field section. | ||||||
| * | ||||||
| * Meta values are read from and written to the block editor's post entity | ||||||
| * through the `core/editor` data store, so they are saved automatically | ||||||
| * when the editor saves the post. | ||||||
| */ | ||||||
| function MetaFieldsPanels() { | ||||||
| var meta = useSelect( function ( select ) { | ||||||
| return select( 'core/editor' ).getEditedPostAttribute( 'meta' ) || {}; | ||||||
| } ); | ||||||
|
|
||||||
| var { editPost } = useDispatch( 'core/editor' ); | ||||||
|
|
||||||
| function handleChange( metaKey, value ) { | ||||||
| var update = {}; | ||||||
| update[ metaKey ] = value; | ||||||
| editPost( { meta: update } ); | ||||||
|
||||||
| editPost( { meta: update } ); | |
| editPost( { meta: Object.assign( {}, meta, update ) } ); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,159 @@ | ||||||||||||||||
| <?php | ||||||||||||||||
| if ( ! defined( 'ABSPATH' ) ) { | ||||||||||||||||
| exit; // Exit if accessed directly. | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Starter Plugin Post Type Meta Box Class (Legacy) | ||||||||||||||||
| * | ||||||||||||||||
| * Provides the classic-editor meta box fallback for post types that are not | ||||||||||||||||
| * using the block editor. When Gutenberg is active this class is not | ||||||||||||||||
| * instantiated. | ||||||||||||||||
|
Comment on lines
+10
to
+11
|
||||||||||||||||
| * using the block editor. When Gutenberg is active this class is not | |
| * instantiated. | |
| * using the block editor. This class is instantiated in the admin for | |
| * supported post types, but its setup routine is a no-op when the block | |
| * editor (Gutenberg) is active for the post type. |
Copilot
AI
Mar 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The nonce field is built into $html but never printed, so the hidden input won’t be present in the meta box and save() will always fail the nonce check. Output a nonce field directly (e.g., via wp_nonce_field(...)) or echo $html before rendering inputs; also remove $html if it’s no longer needed.
| $html = ''; | |
| $html .= '<input type="hidden" name="starter_plugin_' . $this->post_type . '_noonce" id="starter-plugin_' . $this->post_type . '_noonce" value="' . wp_create_nonce( plugin_basename( dirname( Starter_Plugin()->plugin_path ) ) ) . '" />'; | |
| wp_nonce_field( | |
| plugin_basename( dirname( Starter_Plugin()->plugin_path ) ), | |
| 'starter_plugin_' . $this->post_type . '_noonce' | |
| ); |
Check warning on line 147 in classes/class-starter-plugin-post-type-meta-box.php
GitHub Actions / test
WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
Variable variable which could potentially override an imported global variable detected. Global variables defined by a theme/plugin should start with the theme/plugin prefix. Found: "${$f}".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The header comment says data is localized by
Starter_Plugin_Post_Type::enqueue_block_editor_assets(), but the code localizes/enqueues it fromStarter_Plugin_Post_Type_Meta_Fields::enqueue_block_editor_assets(). Update the comment to match the current implementation to avoid confusion.