Skip to content

Commit

Permalink
Use filter to get menus
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoodnz committed Sep 7, 2023
1 parent 8f03c79 commit 14a0e95
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 119 deletions.
122 changes: 11 additions & 111 deletions mu-plugins/blocks/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ function init() {
'enqueue_block_assets',
function() use ( $editor_script_handle ) {
if ( is_admin() && wp_should_load_block_editor_scripts_and_styles() ) {
wp_localize_script(
$editor_script_handle,
'wporgLocalNavigationMenus',
apply_filters( 'wporg_block_navigation_menus', array() )
);
wp_enqueue_script( $editor_script_handle );
}
}
Expand Down Expand Up @@ -125,116 +130,11 @@ function get_menu_content( $menu_slug ) {
* slug does not match a navigation list.
*/
function get_menu_items( $menu_slug ) {
switch ( $menu_slug ) {
case 'about-details':
return array(
array(
'label' => __( 'Domains', 'wporg' ),
'url' => '/about/domains/',
),
array(
'label' => __( 'License', 'wporg' ),
'url' => '/about/license/',
),
array(
'label' => __( 'Accessibility', 'wporg' ),
'url' => '/about/accessibility/',
),
array(
'label' => __( 'Privacy Policy', 'wporg' ),
'url' => '/about/privacy/',
),
array(
'label' => __( 'Statistics', 'wporg' ),
'url' => '/about/stats/',
),
);
break;
case 'about-technology':
return array(
array(
'label' => __( 'Requirements', 'wporg' ),
'url' => '/about/requirements/',
),
array(
'label' => __( 'Features', 'wporg' ),
'url' => '/about/features/',
),
array(
'label' => __( 'Security', 'wporg' ),
'url' => '/about/security/',
),
array(
'label' => __( 'Roadmap', 'wporg' ),
'url' => '/about/roadmap/',
),
array(
'label' => __( 'History', 'wporg' ),
'url' => '/about/history/',
),
);
case 'about-people':
return array(
array(
'label' => __( 'Philosophy', 'wporg' ),
'url' => '/about/philosophy/',
),
array(
'label' => __( 'Etiquette', 'wporg' ),
'url' => '/about/etiquette/',
),
array(
'label' => __( 'Swag', 'wporg' ),
'url' => 'https://mercantile.wordpress.org/',
),
array(
'label' => __( 'Logos', 'wporg' ),
'url' => '/about/logos/',
),
array(
'label' => __( 'People of WordPress', 'wporg' ),
'url' => 'https://wordpress.org/news/category/community/',
),
);
case 'download':
return array(
array(
'label' => __( 'Releases', 'wporg' ),
'url' => '/download/releases/',
),
array(
'label' => __( 'Nightly', 'wporg' ),
'url' => '/download/beta-nightly/',
),
array(
'label' => __( 'Counter', 'wporg' ),
'url' => '/download/counter/',
),
array(
'label' => __( 'Source', 'wporg' ),
'url' => '/download/source/',
),
);
// Local navigation for wordpress.org/documentation
case 'documentation':
return array(
array(
'label' => __( 'WordPress Overview', 'wporg' ),
'url' => '/overview/',
),
array(
'label' => __( 'Technical Guides', 'wporg' ),
'url' => '/technical-guides/',
),
array(
'label' => __( 'Support Guides', 'wporg' ),
'url' => '/support-guides/',
),
array(
'label' => __( 'Customization', 'wporg' ),
'url' => '/customization/',
),
);
$menus = apply_filters( 'wporg_block_navigation_menus', array() );

if ( empty( $menus ) ) {
return false;
}
return false;

return $menus[ $menu_slug ];
}
20 changes: 12 additions & 8 deletions mu-plugins/blocks/navigation/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,29 @@ addFilter( 'blocks.registerBlockType', 'wporg/navigation-menu-slug', addNavigati
*/
const withNavigationMenuSlug = ( BlockEdit ) => ( props ) => {
const { name, attributes, setAttributes } = props;

if ( name !== BLOCK_TYPE ) {
return <BlockEdit { ...props } />;
}

const menus = window.wporgLocalNavigationMenus || [];
const options = Object.keys( menus ).map( ( value ) => {
// Convert hyphenated slug value to a title case label.
const label = value
.replace( /-/g, ' ' )
.replace( /\w\S*/g, ( txt ) => txt.charAt( 0 ).toUpperCase() + txt.slice( 1 ).toLowerCase() );

return { label, value };
} );

return (
<>
<InspectorControls group="list">
<PanelBody className={ attributes.menuSlug ? 'wporg-nav-hide-next-panel' : '' }>
<SelectControl
label={ __( 'Dynamic Menu', 'wporg' ) }
value={ attributes.menuSlug }
options={ [
{ label: __( 'Custom menu', 'wporg' ), value: '' },
{ label: __( 'About — Details', 'wporg' ), value: 'about-details' },
{ label: __( 'About — People', 'wporg' ), value: 'about-people' },
{ label: __( 'About — Technology', 'wporg' ), value: 'about-technology' },
{ label: __( 'Download', 'wporg' ), value: 'download' },
{ label: __( 'Documentation', 'wporg' ), value: 'documentation' },
] }
options={ [ { label: __( 'Custom menu', 'wporg' ), value: '' }, ...options ] }
onChange={ ( newValue ) => setAttributes( { menuSlug: newValue } ) }
__nextHasNoMarginBottom
/>
Expand Down

0 comments on commit 14a0e95

Please sign in to comment.