Created
June 28, 2022 08:23
-
-
Save rumur/1c3e4a115eab366f8bac23f11393717c to your computer and use it in GitHub Desktop.
Block as Plugin Example
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
{ | |
"$schema": "https://json.schemastore.org/block.json", | |
"apiVersion": 2, | |
"version": "1.0.0", | |
"title": "Core Query Block with Menu Order Extender.", | |
"name": "namespace/extensions", | |
"editorScript": "file:index.js" | |
} |
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
/** | |
* WordPress dependencies | |
*/ | |
import { __ } from '@wordpress/i18n'; | |
import { addFilter } from '@wordpress/hooks'; | |
import { select } from '@wordpress/data'; | |
import { createHigherOrderComponent } from '@wordpress/compose'; | |
import { useLayoutEffect } from '@wordpress/element'; | |
const ORDER_OPTIONS = [ | |
{ | |
/* translators: label for ordering posts by menu_order in ascending order */ | |
label: __( 'Order 0 → 9', 'text-domain' ), | |
value: 'menu_order/asc', | |
}, | |
{ | |
/* translators: label for ordering posts by menu_order in descending order */ | |
label: __( 'Order 9 → 1', 'text-domain' ), | |
value: 'menu_order/desc', | |
}, | |
]; | |
const createOption = ( { label, value } ) => { | |
const option = document.createElement( 'option' ); | |
option.value = value; | |
option.innerText = label; | |
return option; | |
}; | |
const toggleOptions = ( { hasSupport = false } ) => { | |
const parentSelector = document.querySelector( | |
'.block-editor-block-inspector select option[value="date/desc"]' | |
)?.parentElement; | |
if ( ! parentSelector ) { | |
return; | |
} | |
const extraOrders = parentSelector.querySelectorAll( | |
'option[value^="menu_order"]' | |
); | |
if ( hasSupport && ! extraOrders.length ) { | |
ORDER_OPTIONS.forEach( value => { | |
parentSelector.appendChild( createOption( value ) ); | |
} ); | |
} else { | |
extraOrders.forEach( option => { | |
parentSelector.removeChild( option ); | |
} ); | |
} | |
}; | |
const withExtendedOrder = createHigherOrderComponent( | |
BlockEdit => props => { | |
const { name, isSelected, attributes, setAttributes } = props; | |
if ( 'core/query' === name && isSelected ) { | |
const { query = {} } = attributes; | |
const { postType, orderBy } = query; | |
const { getPostType } = select( 'core' ); | |
const postTypeObject = getPostType( postType ); | |
const hasSupport = postTypeObject?.supports?.[ 'page-attributes' ]; | |
useLayoutEffect( () => { | |
toggleOptions( { hasSupport } ); | |
// Reset the order for those who don't support it, | |
// otherwise rest throws 400. | |
if ( 'menu_order' === orderBy && ! hasSupport ) { | |
setAttributes( { | |
query: { ...query, orderBy: 'date' }, | |
} ); | |
} | |
}, [ isSelected, hasSupport ] ); | |
} | |
return <BlockEdit key="edit" { ...props } />; | |
}, | |
'withExtendedOrder' | |
); | |
addFilter( 'editor.BlockEdit', 'namespace/query', withExtendedOrder ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment