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

PoC for decoupling metabox and sidebar #20866

Open
1 of 25 tasks
vraja-pro opened this issue Nov 22, 2023 · 0 comments
Open
1 of 25 tasks

PoC for decoupling metabox and sidebar #20866

vraja-pro opened this issue Nov 22, 2023 · 0 comments
Assignees

Comments

@vraja-pro
Copy link
Contributor

vraja-pro commented Nov 22, 2023

If I remove the add_meta_box from class-metabox.php, I'm left with sidebar, however, I do need to find a away to add the hidden fields since the sidebar is using them. This is done in the method render_hidden_fields.
render_hidden_fields is rendering the hidden fields of a form that is submitted once the post is updated.
In the metabox class it is rendered as part of the metabox and the sidebar is depending on those fields to get data relating to:

  • SEO analysis
  • Readability analysis
  • Inclusive language analysis

Those fields are also simultaneously updated when settings are changed in the metabox or sidebar.
This separation is also done in elementor.php. The render_hidden_fields is rendering a complete form while the metabox render_hidden_fields is rendering only the input fields.
Without the metabox, the hidden fields are not needed. Once the metabox is removed, the data should be from the used fields not the hidden fields. Better to use redux store and not hidden fields.

The ideal solution would be to get the data from the store (get the initial data from localised script) and update the data in the store ( That is done here: wordpress-seo/packages/js/src/helpers/fields/AnalysisFields.js). From the store it should be accessible to the metabox, sidebar and form.
This change might require the changing Meta_Fields_Presenter since it returns the html input fields while will need to construct them on the JS side.
Using this guide:
The meta fields can be registered like so:

add_action( 'admin_init', [ $this, 'register_meta_fields'] );
public function register_meta_fields() {
		register_meta('post', '_yoast_wpseo_focuskw', array(
			'show_in_rest' => true,
			'single' => true,
			'type' => 'string',
		));
}

And then they will appear under meta in the core/editor store:

wp.data.select( 'core/editor' ).getCurrentPost().meta;

Another way to retrieve and update this meta with useEntityProp from :

import { useEntityProp } from "@wordpress/core-data";
const MetaBlockField = () => {
	const [ meta, setMeta ] = useEntityProp( "postType", "post", "meta" );
	const saveValue = ( content ) => {
		console.log( content );
		setMeta( {
			...meta,
			_yoast_wpseo_focuskw: content,
		} );
	};

	return <TextControl
		label="Meta Block Field"
		defaultValue={ meta._yoast_wpseo_focuskw }
		onChange={ content => saveValue( content ) }
	/>;
};

Tried to implement this for focus keyphrase, was able to add it to the store but it won't save. (based on this example).

Was able to fix it by adding shoe_in_rest => true when registering the meta in WPSEO_Meta init method.

JS side is separated with different slots and fills according to location from a LocationProvider.

  • global $post does not work on site editor and neither other WP methods related to current post.
  • The redux store core/edit-site include editedPost.context where we can get post id, and post type

post-edit.js Register the store for the usage of the block editor and the metabox, however it also initialise the tabs for the metabox. Either that should be separated or conditioned.

Files to change when moving away from hidden fields:

  • packages/yoastseo/src/app.js on App.prototype.bindInputEvent which adds event listener to the hidden fields.
  • packages/js/src/initializer/post-scraper.js in initializePostAnalysis for signing value to hidden fields: $( "#yoast_wpseo_focuskw" ).val( focusKeyword );
  • packages/js/src/analysis/PostDataCollector.js under PostDataCollector.prototype.getKeyword
  • packages/js/src/components/contentAnalysis/KeywordInputComponent.js
  • packages/js/src/helpers/fields/AnalysisFields.js under static get keyphraseElement

The hidden fields method should be preserved in case user is using the classic editor. In that casr the store is not available.
I recommend assigning the initial state and updating the data according to 3 different states:

  • Classic editor (hidden fields) (only for metabox)
  • Block editor ( core/editor store ) ( for both metabox and sidebar)
  • Site editor ( core store) (only for sidebar)

I added the hidden fields values to wpseoScriptData.
I added a JS class to fetch initial value for hidden fields from wpseoScriptData and update according to the editor (PostMeta.js).
The following task list is for all the actions that need to be updated, and the classes that are used now and should be removed:

Hidden fields to treat and - classes and path (packages/js/src)

Tasks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants