Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit Site: Remove templateIds prop from NavigateToLink #21877

Merged
merged 5 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the bug I missed in #21981. Turns out that $request['some-param'] is an array 😬


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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kinda cosmetic, we just don't need array keys.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's to avoid duplication, but I guess post__in doesn't care.

}
}
$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