admin管理员组文章数量:1313759
I have a Custom Post Type defined with custom-fields
support. I then registered a meta field.
The first issue is that select( 'core/editor' ).getEditedPostAttribute( 'meta' )._vaenc_actu_roles_scope_roles
always returns undefined
on first call (before being updated by dispatch( 'core/editor' ).editPost
). Also, when I save the Custom post Type the value isn't modified in the database.
The other issue is, when check any of the checkboxes, on first click, isChecked()
is properly called, on next clicks it doesn't happen, but updateMeta()
is triggered. If I fold/unfold the control or uncheck a checkbox, all the checkboxes do get updated by calling isChecked()
:
Here is my code:
class Metaboxes extends Base {
/**
* Setup user.
*/
public function setup() {
add_action('init', [$this, 'init'] );
}
public function init () {
register_post_meta(
'vaenc_actu',
'_vaenc_actu_roles_scope_roles',
[
'show_in_rest' => true,
'single' => true,
'type' => 'array',
'default' => [],
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
]
);
}
}
My javascript is like so:
const {registerPlugin} = wp.plugins;
const {PluginDocumentSettingPanel} = wp.editPost;
const {PanelBody, CheckboxControl} = wpponents;
const {useSelect, select, withSelect, withDispatch} = wp.data;
const {Fragment, useState} = wp.element;
const {__} = wp.i18n;
const {compose} = wppose;
const RolesControl = ( {isChecked, updateMeta} ) => {
return (
<>
<Fragment intialOpen={true}>
<CheckboxControl
label={__( "PRI", "vae" )}
checked={isChecked( 'vae-pri' )}
onChange={( value ) => updateMeta( 'vae-pri', value )}
/>
<CheckboxControl
label={__( "PRC", "vae" )}
checked={isChecked( 'vae-prc' )}
onChange={( value ) => updateMeta( 'vae-prc', value )}
/>
<CheckboxControl
label={__( "Certificateurs", "vae" )}
checked={isChecked( 'vae-certif' )}
onChange={( value ) => updateMeta( 'vae-certif', value )}
/>
</Fragment>
</>
)
}
const RolesField = compose( [
withSelect( () => {
return {
actu_scope_meta: select( 'core/editor' ).getEditedPostAttribute( 'meta' )._vaenc_actu_roles_scope_roles,
};
} ),
withDispatch( ( dispatch ) => ({
isChecked ( prop ) {
let meta = select( 'core/editor' ).getEditedPostAttribute( 'meta' )._vaenc_actu_roles_scope_roles;
if ( meta ) {
return meta.includes( prop );
}
return false
},
updateMeta ( prop, value ) {
let meta = select( 'core/editor' ).getEditedPostAttribute( 'meta' )._vaenc_actu_roles_scope_roles;
if ( !meta ) meta = []
if ( meta.includes( prop ) && !value ) {
meta = meta.filter( arrayItem => arrayItem !== prop );
} else if ( !meta.includes( prop ) ) {
meta.push( prop );
}
dispatch( 'core/editor' ).editPost( {meta: {_vaenc_actu_roles_scope_roles: meta}} );
},
}) ),
] )( RolesControl );
const Render = () => {
const postType = useSelect( select => select( 'core/editor' ).getCurrentPostType() );
if ( 'vaenc_actu' !== postType ) {
return null;
}
return (
<>
<PluginDocumentSettingPanel
name="vae-role-scope-sidebar"
title={__( 'Restrictions', 'vae' )}
>
<RolesField/>
</PluginDocumentSettingPanel>
</>
)
}
registerPlugin( 'vae-role-scope-sidebar', {
render: Render
} )
本文标签: custom post typesGetting meta in editor pluginand event triggering issue
版权声明:本文标题:custom post types - Getting meta in editor plugin, and event triggering issue 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741957070a2407049.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论