Repository navigation
Guidelines: Add actions for Import, Export and Revisions of guidelines #76155
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
Changes from all commits
9e29a3a
2c86cd6
90830ba
bc1cbd1
4785b34
7dc2dee
54ecac2
644e43c
15819cb
78abe86
36ab3a5
61da70b
15ac515
b2f4263
a0b531d
ecc1312
d524f4a
3eac082
110eec0
2db272d
92bcadc
e5fee18
056cb56
738bd06
c77723f
ea55601
dda9b5a
411d2aa
f4976c5
eb02ba1
5b4083e
00d2bbb
f75d9f4
cc31eeb
8f0815a
03a7d0f
876c65b
150eb32
e834df6
9c89dd7
44271e9
b4b3cd3
1241059
b80ce5f
58d7408
f88ac9e
dfd8932
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
|
iamchughmayank marked this conversation as resolved.
Resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
|
@@ -83,3 +106,164 @@ 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() ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
(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( | ||
| __( | ||
| 'Check that your file contains valid JSON markup and try again.' | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| const guidelinesStore = select( STORE_NAME ) as unknown as { | ||
| getAllGuidelines: () => Categories; | ||
| getBlockGuidelines: () => Record< string, string >; | ||
| }; | ||
| const previousCategories = guidelinesStore.getAllGuidelines(); | ||
| const previousBlocks = { ...guidelinesStore.getBlockGuidelines() }; | ||
|
|
||
| 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 ); | ||
| } | ||
| } ); | ||
| } | ||
|
|
||
| try { | ||
| await saveContentGuidelines(); | ||
| createSuccessNotice( __( 'Guidelines imported.' ), { | ||
| type: 'snackbar', | ||
| } ); | ||
| } catch ( error ) { | ||
| FLAT_CATEGORIES.forEach( ( cat ) => { | ||
| setGuideline( cat, previousCategories[ cat ] ?? '' ); | ||
| } ); | ||
|
|
||
| const currentBlocks = guidelinesStore.getBlockGuidelines(); | ||
| Object.keys( currentBlocks ).forEach( ( blockName ) => | ||
| setBlockGuideline( blockName, '' ) | ||
| ); | ||
| Object.entries( previousBlocks ).forEach( ( [ blockName, value ] ) => | ||
| setBlockGuideline( blockName, value ) | ||
| ); | ||
|
|
||
| throw error; | ||
| } | ||
| } | ||
|
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', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( __( '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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the |
||
|
|
||
| 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; | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 thesupportssomewhere around here:gutenberg/lib/experimental/content-guidelines/class-gutenberg-content-guidelines-post-type.php
Line 98 in 1241059
Though if you do that, you'd also want to make sure
'delete_with_user'is set tofalseso 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! 😄