Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

On creating a new pattern, don't write to the pattern PHP file until you publish #78

Merged
merged 43 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
863de51
On creating a new pattern, don't write to the pattern .php file until…
kienstra Feb 27, 2023
572f5d8
Fix phpcs error
kienstra Feb 27, 2023
fe5a897
Remove unit tests for deleted function
kienstra Feb 27, 2023
aa868a1
Simplify a call to 1 line
kienstra Feb 27, 2023
3e9e055
Change the snackbar message on publish to 'Pattern created'
kienstra Feb 27, 2023
082e0b5
Disable the post publish panel
kienstra Feb 28, 2023
76e772a
Remove needless PHP filter
kienstra Feb 28, 2023
dc0d756
Merge in main, resolve conflict
kienstra Feb 28, 2023
6f07249
Fix the regression in typing I introduced
kienstra Feb 28, 2023
57736ed
Change get_new_pattern() to get_new_pattern_title()
kienstra Feb 28, 2023
bbe9b1a
Fix an undefined const error
kienstra Feb 28, 2023
85b0d83
Make the return of get_new_pattern_title() a string
kienstra Feb 28, 2023
bd85b9e
Remove a possibly needless filter
kienstra Feb 28, 2023
75c2ac0
Add back in a filter that was needed
kienstra Feb 28, 2023
beae31b
Add a filter back in
kienstra Feb 28, 2023
d0182cb
Rename a function
kienstra Feb 28, 2023
9c8020b
Remove TODO comment that is implemented
kienstra Feb 28, 2023
b30923c
Update a comment
kienstra Feb 28, 2023
d80d8c8
Change any to unknown
kienstra Feb 28, 2023
19b718b
Rename usePostData() to useEditedPostData()
kienstra Feb 28, 2023
5d05982
Add a ts-expect-error, as the editor store isn't typed
kienstra Feb 28, 2023
afcb3b8
Remove setting post_content to '', that's the default
kienstra Feb 28, 2023
620099f
Fix alignment of array
kienstra Feb 28, 2023
76f28b0
Remove needless getAdminUrl() util function
kienstra Feb 28, 2023
1803303
Make the title prop be in alphabetical order
kienstra Feb 28, 2023
e9bf731
Correct the name of an action
kienstra Feb 28, 2023
9afad49
Make the TitlePanel props inline, like other SidebarPanels/ components
kienstra Feb 28, 2023
55acf6e
Remove needless import
kienstra Feb 28, 2023
f65f357
If there's no title, don't write an empty title to the pattern file
kienstra Feb 28, 2023
3b29742
Delegate the post_title to the PHP filter
kienstra Feb 28, 2023
cdfe61c
Merge in main, resolve conflicts
kienstra Feb 28, 2023
41cb2b3
Add a title property
kienstra Feb 28, 2023
2c02efc
Move 'patternNames' into AdditionalSidbarProps
kienstra Feb 28, 2023
78616c7
Remove store import from wordpress/editor
kienstra Mar 1, 2023
1ae006c
Fix an issue where you couldn't publish with meta like Categories
kienstra Mar 2, 2023
a413e10
Ensures the meta keys are registered with the proper type
kienstra Mar 2, 2023
4eeda13
Remove the slug from the defaults
kienstra Mar 2, 2023
2ccd955
Remove 'title' from the defaults this looks for
kienstra Mar 2, 2023
5465c2e
Actually get the meta value
kienstra Mar 2, 2023
dcdcd68
Correct the expected type for 'viewportWidth'
kienstra Mar 2, 2023
c35e0f1
Move meta key and value into a separate array
kienstra Mar 2, 2023
21d1d06
Get the pattern before deleting it
kienstra Mar 2, 2023
4bb8ddc
Add default value for inserter post meta
mike-day Mar 3, 2023
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
6 changes: 2 additions & 4 deletions wp-modules/app/js/src/components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Button } from '@wordpress/components';
/**
* Internal dependencies
*/
import getAdminUrl from '../../utils/getAdminUrl';
import { patternManager } from '../../globals';
import wpeLogoDefaultCropped from '../../../../img/WPE-LOGO-S-Default-Cropped.svg';

export default function Header() {
Expand All @@ -27,9 +27,7 @@ export default function Header() {
<div className="header-container-inner">
<Button
variant="primary"
href={ getAdminUrl( {
action: 'create-new',
} ) }
href={ `${ patternManager.siteUrl }/wp-admin/post-new.php?post_type=pm_pattern` }
>
{ __( 'Create New Pattern', 'pattern-manager' ) }
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import { Button } from '@wordpress/components';
// Hooks
import usePmContext from '../../hooks/usePmContext';

// Utils
import getAdminUrl from '../../utils/getAdminUrl';

// Types
import type { Pattern } from '../../types';
import { patternManager } from '../../globals';

type Props = {
patternData: Pattern;
Expand Down Expand Up @@ -51,10 +49,7 @@ export default function PatternGridActions( { patternData }: Props ) {
__( 'Duplicate %1$s', 'pattern-manager' ),
patternData.title
) }
href={ getAdminUrl( {
action: 'duplicate',
name: patternData.name,
} ) }
href={ `${ patternManager.siteUrl }/wp-admin/admin.php?post_type=pm_pattern&action=duplicate&name=${ patternData.name }` }
>
<Icon
className="item-action-icon"
Expand Down
13 changes: 0 additions & 13 deletions wp-modules/app/js/src/utils/getAdminUrl.ts

This file was deleted.

7 changes: 1 addition & 6 deletions wp-modules/editor/css/src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
display: none !important;
}

/* When the save button is clicked, hide the pre-publish panel that pops open, as we handle saving in PM. */
.editor-post-publish-panel {
display: none !important;
}
Copy link
Contributor Author

@kienstra kienstra Feb 28, 2023

Choose a reason for hiding this comment

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

The pre-publish panel is now disabled in index.js.

Because we're using the editor more like a normal post, with post-new.php, we can't only hide it via CSS anymore.

It'll block publishing.


/* Hide the 'Save Draft' button in the pattern editor (appears after pattern is edited), as the save theme button handles it.*/
/* Hide the 'Save Draft' button in the pattern editor (appears after pattern is published), as the 'Update' button handles it. */
button.components-button.editor-post-save-draft.is-tertiary {
display: none;
}
Expand Down
20 changes: 6 additions & 14 deletions wp-modules/editor/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
function register_pattern_post_type() {
$post_type_key = get_pattern_post_type();
$labels = array(
'name' => __( 'Patterns', 'pattern-manager' ),
'singular_name' => __( 'Pattern', 'pattern-manager' ),
'add_new_item' => __( 'Pattern Editor', 'pattern-manager' ),
'item_updated' => __( 'Pattern saved to your theme directory', 'pattern-manager' ),
'name' => __( 'Patterns', 'pattern-manager' ),
'singular_name' => __( 'Pattern', 'pattern-manager' ),
'add_new_item' => __( 'Pattern Editor', 'pattern-manager' ),
'item_published' => __( 'Pattern created', 'pattern-manager' ),
'item_updated' => __( 'Pattern saved to your theme directory', 'pattern-manager' ),
);

register_post_type(
Expand All @@ -45,21 +46,12 @@ function register_pattern_post_type() {
'supports' => array(
'editor',
'custom-fields',
'title',
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 now stores the pattern title in ->post_title.

),
'labels' => $labels,
)
);

register_post_meta(
$post_type_key,
'title',
array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
)
);

register_post_meta(
$post_type_key,
'name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
import usePatternData from '../../hooks/usePatternData';
import useSave from '../../hooks/useSave';
import { patternManager } from '../../globals';
import usePostData from '../../hooks/usePostData';
import useEditedPostData from '../../hooks/useEditedPostData';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not the saved post, it's the edited post.


export default function PatternManagerMetaControls() {
const { postMeta } = usePostData();
const { postMeta, title } = useEditedPostData();
const [ errorMessage, setErrorMessage ] = useState( '' );
const [ patternNames, setPatternNames ] = useState(
patternManager.patternNames.filter( ( name ) => {
Expand All @@ -37,6 +37,7 @@ export default function PatternManagerMetaControls() {
errorMessage={ errorMessage }
setErrorMessage={ setErrorMessage }
patternNames={ patternNames }
title={ title }
/>
<CategoriesPanel
postMeta={ postMeta }
Expand Down
20 changes: 11 additions & 9 deletions wp-modules/editor/js/src/components/SidebarPanels/TitlePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ function isTitleTaken(
}

export default function TitlePanel( {
postMeta,
handleChange,
errorMessage,
setErrorMessage,
patternNames,
postMeta,
title,
}: BaseSidebarProps &
Pick< AdditionalSidebarProps, 'errorMessage' | 'setErrorMessage' > & {
patternNames: Array< Pattern[ 'name' ] >;
} ) {
const { lockPostSaving, unlockPostSaving } = useDispatch( 'core/editor' );
Pick<
AdditionalSidebarProps,
'errorMessage' | 'patternNames' | 'setErrorMessage' | 'title'
> ) {
const { editPost, lockPostSaving, unlockPostSaving } =
useDispatch( 'core/editor' );

return (
<PluginDocumentSettingPanel
Expand All @@ -42,11 +45,10 @@ export default function TitlePanel( {
'Pattern Title Name Input (used for renaming the pattern)',
'pattern-manager'
) }
value={ postMeta.title }
value={ title }
onChange={ ( newTitle: PostMeta[ 'title' ] ) => {
handleChange( 'title', newTitle, {
name: convertToSlug( newTitle ),
} );
editPost( { title: newTitle } );
handleChange( 'name', convertToSlug( newTitle ) );

if ( ! newTitle ) {
lockPostSaving();
Expand Down
4 changes: 3 additions & 1 deletion wp-modules/editor/js/src/components/SidebarPanels/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import usePatternData from '../../hooks/usePatternData';
import { PostMeta } from '../../types';
import { Pattern, PostMeta } from '../../types';

import type { Dispatch, SetStateAction } from 'react';

Expand All @@ -17,5 +17,7 @@ export type AdditionalSidebarProps = {
categories: ReturnType< typeof usePatternData >[ 'categories' ];
postTypes: ReturnType< typeof usePatternData >[ 'postTypes' ];
errorMessage: string;
patternNames: Array< Pattern[ 'name' ] >;
setErrorMessage: Dispatch< SetStateAction< string > >;
title: string;
};
23 changes: 23 additions & 0 deletions wp-modules/editor/js/src/hooks/useEditedPostData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useSelect } from '@wordpress/data';
import { PostMeta, SelectQuery } from '../types';

type UseEditedPostData = {
postMeta: PostMeta;
title: string;
};

export default function useEditedPostData(): UseEditedPostData {
// @ts-expect-error if @wordpress/editor store is typed, pass it to select() instead of the string.
return {
...useSelect(
( select: SelectQuery ) => ( {
postMeta:
select( 'core/editor' ).getEditedPostAttribute( 'meta' ),
title: select( 'core/editor' ).getEditedPostAttribute(
'title'
),
} ),
[]
),
};
}
14 changes: 0 additions & 14 deletions wp-modules/editor/js/src/hooks/usePostData.ts

This file was deleted.

3 changes: 3 additions & 0 deletions wp-modules/editor/js/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import '../../css/src/index.scss';
import { dispatch } from '@wordpress/data';
import { addAction, addFilter } from '@wordpress/hooks';
import { registerPlugin } from '@wordpress/plugins';
import BackButton from './components/BackButton';
Expand All @@ -22,3 +23,5 @@ addAction(
'pattern-manager/checkActiveTheme',
receiveActiveTheme
);

dispatch( 'core/editor' ).disablePublishSidebar();
2 changes: 1 addition & 1 deletion wp-modules/editor/js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type PostMeta = {
};

export type SelectQuery = ( dataStore: string ) => {
getEditedPostAttribute: ( postAttribute: string ) => PostMeta;
getEditedPostAttribute: ( postAttribute: string ) => unknown;
Copy link
Contributor Author

@kienstra kienstra Feb 28, 2023

Choose a reason for hiding this comment

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

It feels wrong doing this, but now that usePostData() calls getEditedPostAttribute() with both 'meta' and 'title', this can't be PostMeta.

The return of usePostData() is typed.

getEditedPostContent: () => string;
isEditedPostDirty: () => boolean;
getPostTypes: ( {
Expand Down
3 changes: 3 additions & 0 deletions wp-modules/editor/js/src/utils/changeWords.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default function changeWords( translation: string, text: string ) {
if ( text === 'Publish' ) {
return 'Create Pattern';
}
if ( text === 'Update' ) {
return 'Update Pattern';
}
Expand Down
Loading