Skip to content

Commit

Permalink
Somewhat functional entity creation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Nov 18, 2021
1 parent 39f5ae0 commit 6e8aecd
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,30 +103,64 @@ public function register_routes() {
protected function permissions_check() {
// Verify if the current user has edit_theme_options capability.
// This capability is required to edit/view/delete templates.
if ( ! current_user_can( 'edit_theme_options' ) ) {
return new WP_Error(
'rest_cannot_manage_templates',
__( 'Sorry, you are not allowed to access the templates on this site.', 'gutenberg' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
// if ( ! current_user_can( 'edit_theme_options' ) ) {
// return new WP_Error(
// 'rest_cannot_manage_templates',
// __( 'Sorry, you are not allowed to access the templates on this site.', 'gutenberg' ),
// array(
// 'status' => rest_authorization_required_code(),
// )
// );
// }

return true;
}

public function get_item_permissions_check( $request ) {
return true;
}

public function create_item_permissions_check( $request ) {
return true;
}

public function update_item_permissions_check( $request ) {
return true;
}

public function delete_item_permissions_check( $request ) {
return true;
}

protected function check_update_permission( $post ) {
return true;
}

public function get_items_permissions_check( $request ) {
return true;
}

public function check_read_permission( $post ) {
return true;
}

protected function check_create_permission( $post ) {
return true;
}

protected function check_delete_permission( $post ) {
return true;
}

protected function handle_status_param( $post_status, $post_type ) {
return $post_status;
}

public function filter_response_by_context( $data, $context ) {
$data['id'] = $data['slug'];
return $data;
}

protected function prepare_item_for_database( $request ) {
$changes = parent::prepare_item_for_database( $request );
$changes->post_name = $request['slug'];
return $changes;
}

public function get_item_schema() {
$schema = parent::get_item_schema(); // TODO: Change the autogenerated stub
$schema['properties']['id']['type'] = 'string';
Expand All @@ -136,7 +170,7 @@ public function get_item_schema() {

protected function get_post( $id ) {
$wp_query_args = array(
'post_name__in' => array( $id ),
'name' => $id,
'post_type' => 'wp_navigation',
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ),
'posts_per_page' => 1,
Expand Down
11 changes: 8 additions & 3 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
useCallback,
Platform,
useContext,
} from '@wordpress/element'
} from '@wordpress/element';
import { v4 as uuid } from 'uuid';
import { Disabled } from '@wordpress/components';
import {
Expand Down Expand Up @@ -268,11 +268,16 @@ function Navigation( {
useEffect( () => {
if ( ! oldSlug ) {
const newSlug = area?.area
? `${ area.area }//menu`
: `${ uuid() }//menu`;
? `wp-${ area.area }-menu`
: `wp-${ uuid() }-menu`;
setAttributes( { menuSlug: newSlug } );
}
}, [ oldSlug, area?.area ] );
useEffect( () => {
if ( navigationMenu?.slug ) {
setAttributes( { menuSlug: navigationMenu.slug } );
}
}, [ navigationMenu?.slug ] );

// If the block has inner blocks, but no menu id, this was an older
// navigation block added before the block used a wp_navigation entity.
Expand Down
53 changes: 31 additions & 22 deletions packages/block-library/src/navigation/use-navigation-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
store as coreDataStore,
store as coreStore,
} from '@wordpress/core-data';
import { useState } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';

export default function useNavigationMenu( slug ) {
const dispatch = useDispatch();
const [ created, setCreated ] = useState( false ); // @TODO check with core data
return useSelect(
( select ) => {
const {
Expand All @@ -30,27 +32,6 @@ export default function useNavigationMenu( slug ) {
)
: false;

let navigationMenu = null;

if ( slug ) {
navigationMenu = getEditedEntityRecord( ...navigationMenuSingleArgs );
if ( ! navigationMenu?.id /* && hasResolvedNavigationMenu */ ) {
// This could be a mock resolver or transform into a new way of creating entities in Gutenberg
dispatch(coreStore).receiveEntityRecords(
'postType',
'wp_navigation',
[{
id: slug,
slug: slug,
name: slug,
blocks: [],
content: ""
}]
);
navigationMenu = getEditedEntityRecord( ...navigationMenuSingleArgs );
}
}

const navigationMenuMultipleArgs = [
'postType',
'wp_navigation',
Expand All @@ -60,6 +41,34 @@ export default function useNavigationMenu( slug ) {
...navigationMenuMultipleArgs
);

let navigationMenu = null;

if ( slug ) {
navigationMenu = getEditedEntityRecord(
...navigationMenuSingleArgs
);
if (
! navigationMenu?.id &&
! created /* && hasResolvedNavigationMenu */
) {
setCreated( true );
const record = {
slug: slug,
name: slug,
post_name: slug,
};
dispatch( coreStore )
.saveEntityRecord( 'postType', 'wp_navigation', record )
dispatch( coreStore )
.receiveEntityRecords( 'postType', 'wp_navigation', [
{ ...record, id: slug },
] )
navigationMenu = getEditedEntityRecord(
...navigationMenuSingleArgs
);
}
}

const canSwitchNavigationMenu = slug
? navigationMenus?.length > 1
: navigationMenus?.length > 0;
Expand All @@ -77,6 +86,6 @@ export default function useNavigationMenu( slug ) {
navigationMenus,
};
},
[ slug ]
[ slug, created ]
);
}

0 comments on commit 6e8aecd

Please sign in to comment.