Skip to content

Commit

Permalink
Edit Site: Remove templateIds prop from NavigateToLink (#21877)
Browse files Browse the repository at this point in the history
* Edit Site: Remove templateIds prop from NavigateToLink

* Add resolved: true

* No assoc array needed

* Fix logic

* Fix current template retrieval during render
  • Loading branch information
ockham authored May 13, 2020
1 parent c62f32a commit 9699f4f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function get_template_hierachy( $template_type ) {
function gutenberg_override_query_template( $template, $type, array $templates = array() ) {
global $_wp_current_template_id, $_wp_current_template_content;

$current_template = gutenberg_find_template_post_and_parts( basename( $template, '.php' ), $templates );
$current_template = gutenberg_find_template_post_and_parts( $type, $templates );

if ( $current_template ) {
$_wp_current_template_id = $current_template['template_post']->ID;
Expand Down
6 changes: 3 additions & 3 deletions lib/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function filter_rest_wp_template_collection_params( $query_params ) {
function filter_rest_wp_template_query( $args, $request ) {
if ( $request['resolved'] ) {
$template_ids = array( 0 ); // Return nothing by default (the 0 is needed for `post__in`).
$template_types = $request['slug'] ? array( $request['slug'] ) : get_template_types();
$template_types = $request['slug'] ? $request['slug'] : get_template_types();

foreach ( $template_types as $template_type ) {
// Skip 'embed' for now because it is not a regular template type.
Expand All @@ -194,10 +194,10 @@ function filter_rest_wp_template_query( $args, $request ) {

$current_template = gutenberg_find_template_post_and_parts( $template_type );
if ( isset( $current_template ) ) {
$template_ids[ $current_template['template_post']->post_name ] = $current_template['template_post']->ID;
$template_ids[] = $current_template['template_post']->ID;
}
}
$args['post__in'] = array_values( $template_ids );
$args['post__in'] = $template_ids;
}

return $args;
Expand Down
7 changes: 1 addition & 6 deletions packages/edit-site/src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,11 @@ export default function BlockEditor() {
( fillProps ) => (
<NavigateToLink
{ ...fillProps }
templateIds={ settings.templateIds }
activeId={ settings.templateId }
onActiveIdChange={ setActiveTemplateId }
/>
),
[
settings.templateIds,
settings.templateId,
setActiveTemplateId,
]
[ settings.templateId, setActiveTemplateId ]
) }
</__experimentalLinkControl.ViewerFill>
<Sidebar.InspectorFill>
Expand Down
26 changes: 11 additions & 15 deletions packages/edit-site/src/components/navigate-to-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ import { __ } from '@wordpress/i18n';
*/
const { fetch } = window;

export default function NavigateToLink( {
url,
templateIds,
activeId,
onActiveIdChange,
} ) {
export default function NavigateToLink( { url, activeId, onActiveIdChange } ) {
const [ templateId, setTemplateId ] = useState();
useEffect( () => {
const effect = async () => {
Expand All @@ -28,14 +23,15 @@ export default function NavigateToLink( {
if ( success ) {
let newTemplateId = data.ID;
if ( newTemplateId === null ) {
const { getEntityRecord } = select( 'core' );
newTemplateId = templateIds
.map( ( id ) =>
getEntityRecord( 'postType', 'wp_template', id )
)
.find(
( template ) => template.slug === data.post_name
).id;
const { getEntityRecords } = select( 'core' );
newTemplateId = getEntityRecords(
'postType',
'wp_template',
{
resolved: true,
slug: data.post_name,
}
)[ 0 ].id;
}
setTemplateId( newTemplateId );
} else {
Expand All @@ -46,7 +42,7 @@ export default function NavigateToLink( {
}
};
effect();
}, [ url, templateIds ] );
}, [ url ] );
const onClick = useMemo( () => {
if ( ! templateId || templateId === activeId ) {
return null;
Expand Down

0 comments on commit 9699f4f

Please sign in to comment.