Created
August 6, 2020 09:32
-
-
Save sudar/3d53c4bc170b81079fef42cdd3e1312f to your computer and use it in GitHub Desktop.
store-post-meta-gutenberg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { domReady } = wp; | |
const { useMemo } = wp.element; | |
const { TextControl } = wp.components; | |
const { select, useSelect, useDispatch } = wp.data; | |
const { PluginDocumentSettingPanel } = wp.editPost; | |
const { registerPlugin } = wp.plugins; | |
const { __ } = wp.i18n; | |
/** | |
* Output the Meta panel for Report pages. | |
*/ | |
function ReportPageMeta() { | |
const { editPost } = useDispatch( 'core/editor' ); | |
const meta = useSelect( ( select ) => | |
select( 'core/editor' ).getEditedPostAttribute( 'meta' ) || {} | |
); | |
const downloadPDFKey = 'download_pdf_link'; | |
const downloadPDFValue = useMemo( | |
() => { | |
if ( meta != null ) { | |
return meta[ downloadPDFKey ] || ''; | |
} | |
}, | |
[ meta ] | |
); | |
return ( | |
<PluginDocumentSettingPanel | |
name="blackstone-byline-panel" | |
title={ __( 'Report page settings', 'fbs' ) } | |
> | |
<TextControl | |
value={ downloadPDFValue } | |
onChange={ ( newValue ) => { | |
editPost( { | |
meta: {...meta, [downloadPDFKey]: newValue }, | |
} ); | |
} } | |
label={ | |
__( 'Enter the full download PDF URL', 'fbs' ) | |
} | |
/> | |
</PluginDocumentSettingPanel> | |
); | |
} | |
domReady( () => { | |
if ( select( 'core/editor' ).getCurrentPostType() !== 'fb_report' ) { | |
return; | |
} | |
registerPlugin( 'plugin-report-page-meta', { render: ReportPageMeta, icon: null } ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment