Skip to content
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
9e29a3a
Add block guidelines management
aswasif007 Mar 6, 2026
2c86cd6
Fix spacing of the dataviews
aswasif007 Mar 9, 2026
90830ba
Remove dataviews sort and filter
aswasif007 Mar 9, 2026
bc1cbd1
Exclude blocks that already have guidelines from add-guideline list
aswasif007 Mar 9, 2026
4785b34
Show confirmation modal when deleting block guideline from table
aswasif007 Mar 9, 2026
7dc2dee
Improve save/delete logic
aswasif007 Mar 9, 2026
54ecac2
Use base-style variable
aswasif007 Mar 9, 2026
644e43c
Fix bootstrapBlockRegistry() placement
aswasif007 Mar 9, 2026
15819cb
Add types
aswasif007 Mar 9, 2026
78abe86
Use hstack wrapper for add-block-guideline btn
aswasif007 Mar 9, 2026
36ab3a5
Do not remove block-key when removing block guideline
aswasif007 Mar 9, 2026
61da70b
Fix issue with empty page after deleting rows
aswasif007 Mar 9, 2026
15ac515
Fix mistake with wrapping up action-text in double quote
aswasif007 Mar 9, 2026
b2f4263
Add . after success notice text
aswasif007 Mar 9, 2026
a0b531d
Remove undefined keys from block entries
aswasif007 Mar 9, 2026
ecc1312
Add clarifying comment for setting empty string to delete a block gui…
aswasif007 Mar 9, 2026
d524f4a
Content Guidelines: Add actions section UI
iamchughmayank Mar 4, 2026
3eac082
Adds simple import and export function
iamchughmayank Mar 4, 2026
110eec0
Content Guidelines: Add manage access and revision screens
iamchughmayank Mar 5, 2026
2db272d
Content Guidelines: Search in revision history screen
iamchughmayank Mar 5, 2026
92bcadc
Content guidelines: Use filterSortAndPaginate from DataViews to imple…
iamchughmayank Mar 6, 2026
e5fee18
Content guidelines: Hide Manage Access action button for MVP
iamchughmayank Mar 6, 2026
056cb56
Improve spacing
aswasif007 Mar 6, 2026
738bd06
Content guidelines: Use DataViews inbuilt filtering
iamchughmayank Mar 6, 2026
c77723f
Refetch content-guidelines after restoring a revision
aswasif007 Mar 6, 2026
ea55601
Improve revision-history page styles
aswasif007 Mar 6, 2026
dda9b5a
Content guidelines: remove manage-access from MVP
iamchughmayank Mar 6, 2026
411d2aa
Content Guidelines: Use Notice as error message component instead of …
iamchughmayank Mar 6, 2026
f4976c5
Content Guidelines: Addresses PR feedback:
iamchughmayank Mar 7, 2026
eb02ba1
Content Guidelines: Align author to the right of revision history table
iamchughmayank Mar 7, 2026
5b4083e
Content guidelines: Rebase with #76187
iamchughmayank Mar 9, 2026
00d2bbb
Content guideliens: Adds confirmation modal to import guidelines
iamchughmayank Mar 9, 2026
f75d9f4
Content guidelines: Updates modal sequence and copy
iamchughmayank Mar 9, 2026
cc31eeb
Content guidelines: Addresses PR feedback to:1. Fix unnecessary try/c…
iamchughmayank Mar 10, 2026
8f0815a
Content guidelines: Remove unnecessary overrides in CSS, where possible
iamchughmayank Mar 10, 2026
03a7d0f
Content guidelines: Use memoisation in filter and pagination to avoid…
iamchughmayank Mar 10, 2026
876c65b
Content guidelines: Revert changes done by incorrect rebase and merge
iamchughmayank Mar 10, 2026
150eb32
Content guidelines: Revert unnecessary changes brought in incorrect r…
iamchughmayank Mar 10, 2026
e834df6
Content guidelines: Handle guidelines import error correctly by resto…
iamchughmayank Mar 10, 2026
9c89dd7
Content guidelines: Pre-emptively add override for wordpress/ui Notic…
iamchughmayank Mar 10, 2026
44271e9
Content guidelines: Update copy for content guidelines import error
iamchughmayank Mar 10, 2026
b4b3cd3
Guidelines: Rename "Content Guidelines" to "Guidelines" in all user-f…
aagam-shah Mar 10, 2026
1241059
Content guidelines: Change the text 'content guidelines' to 'guidelin…
iamchughmayank Mar 10, 2026
b80ce5f
Content Guidelines: Fixes grammatical error in copy
iamchughmayank Mar 10, 2026
58d7408
Content Guidelines: Remove lint override which is not live yet
iamchughmayank Mar 10, 2026
f88ac9e
Content guidelines: Updates copy of import error notice and removes m…
iamchughmayank Mar 10, 2026
dfd8932
Content guidelines: Updates snackbar/toast message copy
iamchughmayank Mar 10, 2026
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ public function prepare_item_for_response( $item, $request ) {
$response->set_data( $data );
}

// Add embeddable author link to get author name in revision history screen
if ( ! empty( $item->post_author ) ) {
$response->add_link(
'author',
rest_url( 'wp/v2/users/' . $item->post_author ),
array( 'embeddable' => true )
);
}

Comment on lines +106 to +114

@andrewserong andrewserong Mar 11, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you want to include the author in the response, I think you can also do that via registering 'author' as one of the supports somewhere around here:

Though if you do that, you'd also want to make sure 'delete_with_user' is set to false so that if the user who created the guidelines is removed, the guidelines themselves are not.

(Not a blocker, just something that could be looked at in a follow-up if need be)

Edit: Oh wait, I don't think that approach will work since this isn't inheriting from the Post controller. Ignore me! 😄

return $response;
}

Expand Down
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

159 changes: 158 additions & 1 deletion routes/content-guidelines/api.ts
Comment thread
iamchughmayank marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { downloadBlob } from '@wordpress/blob';
import { dispatch, select } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
*/
import { STORE_NAME } from './store';
import type { Categories, RestGuidelinesResponse } from './types';
import type {
Categories,
RestGuidelinesResponse,
GuidelinesImportData,
ContentGuidelinesRevision,
} from './types';

const FLAT_CATEGORIES = [ 'site', 'copy', 'images', 'additional' ] as const;

function isValidGuidelinesImport(
data: unknown

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is data unknown at this stage? Can it be given a proper type?

): data is GuidelinesImportData {
return (
!! data &&
typeof data === 'object' &&
'guideline_categories' in data &&
typeof ( data as Record< string, unknown > ).guideline_categories ===
'object' &&
( data as Record< string, unknown > ).guideline_categories !== null
);
}

export async function fetchContentGuidelines(): Promise< RestGuidelinesResponse > {
const { setFromResponse } = dispatch( STORE_NAME ) as {
Expand Down Expand Up @@ -83,3 +106,137 @@ export async function saveContentGuidelines(): Promise< RestGuidelinesResponse >

return response;
}

/**
* Opens file selector, reads the selected file and imports the content guidelines.
* @param file Content Guidelines JSON file
*/
export async function importContentGuidelines( file: File ): Promise< void > {
// @ts-ignore
const { setGuideline, setBlockGuideline } = dispatch( STORE_NAME );
const { createSuccessNotice } = dispatch( noticesStore );

const parsed: unknown = JSON.parse( await file.text() );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should JSON syntax errors be handled explicitly somewhere? I.e. to have a friendlier version of the following error:

Image

(This might slightly depend on the intended audience for this feature. If you're a developer, the granular error here might be okay, but to a general user uploading malformed JSON, it might be awkward)


if ( ! isValidGuidelinesImport( parsed ) ) {
throw new Error( __( 'Invalid file format.' ) );
}

const { guideline_categories: contentGuidelinesCategories } = parsed;

FLAT_CATEGORIES.forEach( ( guidelineCategory ) => {
const guidelines =
contentGuidelinesCategories[ guidelineCategory ]?.guidelines;
if ( typeof guidelines === 'string' ) {
setGuideline( guidelineCategory, guidelines );
}
} );

const blocksData = contentGuidelinesCategories.blocks;
if ( blocksData && typeof blocksData === 'object' ) {
Object.entries( blocksData ).forEach( ( [ blockName, blockData ] ) => {
if ( blockData && typeof blockData.guidelines === 'string' ) {
setBlockGuideline( blockName, blockData.guidelines );
}
} );
}

await saveContentGuidelines();
createSuccessNotice( __( 'Content guidelines imported.' ), {
type: 'snackbar',
} );
}
Comment thread
iamchughmayank marked this conversation as resolved.
Resolved

/**
* Exports the content guidelines as a JSON file.
*/
export function exportContentGuidelines(): void {
const { createSuccessNotice } = dispatch( noticesStore );

const guidelinesStore = select( STORE_NAME ) as unknown as {
getAllGuidelines: () => Categories;
getBlockGuidelines: () => Record< string, string >;
};
const contentGuidelinesCategories = guidelinesStore.getAllGuidelines();
const blockGuidelines = guidelinesStore.getBlockGuidelines();

const data = {
guideline_categories: {
...Object.fromEntries(
FLAT_CATEGORIES.map( ( guidelineCategory ) => [
guidelineCategory,
{
guidelines:
contentGuidelinesCategories[ guidelineCategory ] ??
'',
},
] )
),
blocks: Object.fromEntries(
Object.entries( blockGuidelines ).map(
( [ blockName, guidelines ] ) => [
blockName,
{ guidelines },
]
)
),
},
};

downloadBlob(
'guidelines.json',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just an idea for a follow-up, but would it be worth including the date in the filename? I.e. how are folks going to be using the exported format?

JSON.stringify( data, null, 2 ),
'application/json'
);

createSuccessNotice( __( 'Content guidelines exported.' ), {
type: 'snackbar',
} );
}

export async function fetchContentGuidelinesRevisions( {
guidelinesId,
page = 1,
perPage = 10,
search,
}: {
guidelinesId: number;
page?: number;
perPage?: number;
search?: string;
} ): Promise< {
revisions: ContentGuidelinesRevision[];
total: number;
totalPages: number;
} > {
const params = new URLSearchParams( {
page: String( page ),
per_page: String( perPage ),
_embed: 'author',
...( search ? { search } : {} ),
} );

const response = ( await apiFetch( {
path: `/wp/v2/content-guidelines/${ guidelinesId }/revisions?${ params }`,
parse: false,
} ) ) as Response;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is the as Response here necessary?


const revisions = ( await response.json() ) as ContentGuidelinesRevision[];
const total = parseInt( response.headers.get( 'X-WP-Total' ) ?? '0', 10 );
const totalPages = parseInt(
response.headers.get( 'X-WP-TotalPages' ) ?? '0',
10
);

return { revisions, total, totalPages };
}

export async function restoreContentGuidelinesRevision(
guidelinesId: number,
revisionId: number
): Promise< RestGuidelinesResponse > {
return ( await apiFetch( {
path: `/wp/v2/content-guidelines/${ guidelinesId }/revisions/${ revisionId }/restore`,
method: 'POST',
} ) ) as RestGuidelinesResponse;
}
Loading
Loading