Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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 packages/wp-build/lib/php-generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function getPhpReplacements( rootDir, baseUrlExpression ) {
export function applyTemplateReplacements( template, replacements ) {
let content = template;
for ( const [ placeholder, value ] of Object.entries( replacements ) ) {
content = content.replaceAll( placeholder, value );
content = content.split( placeholder ).join( value );
}
return content;
}
Expand Down
2 changes: 1 addition & 1 deletion routes/post-edit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"private": true,
"route": {
"path": "/types/$type/edit/$id",
"path": "/types/$type/edit/$",
"page": "site-editor-v2"
},
"dependencies": {
Expand Down
61 changes: 35 additions & 26 deletions routes/post-edit/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,40 @@ import { decodeEntities } from '@wordpress/html-entities';
import { __ } from '@wordpress/i18n';
import { notFound } from '@wordpress/route';

const TEMPLATE_POST_TYPES = [ 'wp_template', 'wp_template_part' ];

type PostEditParams = {
type: string;
id?: string;
'*'?: string;
[ key: string ]: string | undefined;
};
Comment thread
scruffian marked this conversation as resolved.
Resolved

function getPostId( params: PostEditParams ) {
const id = params.id ?? params[ '*' ];
Comment thread
scruffian marked this conversation as resolved.
Resolved

if ( ! id ) {
throw notFound();
}

try {
return decodeURIComponent( id );
} catch {
return id;
}
}

/**
* Route configuration for post edit.
*/
export const route = {
beforeLoad: async ( {
params,
}: {
params: {
type: string;
id: string;
};
} ) => {
const postId = parseInt( params.id, 10 );
beforeLoad: async ( { params }: { params: PostEditParams } ) => {
const postId = getPostId( params );

if ( Number.isNaN( postId ) ) {
if (
! TEMPLATE_POST_TYPES.includes( params.type ) &&
! /^\d+$/.test( postId )
) {
throw notFound();
}

Expand All @@ -42,18 +61,12 @@ export const route = {
throw notFound();
}
},
title: async ( {
params,
}: {
params: {
type: string;
id: string;
};
} ) => {
title: async ( { params }: { params: PostEditParams } ) => {
const postId = getPostId( params );
const post = await resolveSelect( coreStore ).getEntityRecord(
'postType',
params.type,
params.id
postId
);

if ( post?.title?.rendered ) {
Expand All @@ -65,17 +78,13 @@ export const route = {
);
return postType?.labels?.edit_item || __( 'Edit' );
},
async canvas( context: {
params: {
type: string;
id: string;
};
} ) {
async canvas( context: { params: PostEditParams } ) {
const { params } = context;
const postId = getPostId( params );

return {
postType: params.type,
postId: params.id,
postId,
};
},
};
Loading