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 3 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
9 changes: 3 additions & 6 deletions wp-modules/editor/css/src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
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.


/* TODO: prevent pre-publish pattern from opening. */
/* Hide the 'Save Draft' button in the pattern editor (appears after pattern is edited), as the save theme button handles it.*/
button.components-button.editor-post-save-draft.is-tertiary {
button.components-button.editor-post-save-draft.is-tertiary,
[aria-label="Save draft"] {
display: none;
}

Expand Down
11 changes: 1 addition & 10 deletions wp-modules/editor/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ 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,
)
Expand All @@ -60,16 +61,6 @@ function register_pattern_post_type() {
)
);

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 @@ -15,7 +15,7 @@ import { patternManager } from '../../globals';
import usePostData from '../../hooks/usePostData';

export default function PatternManagerMetaControls() {
const { postMeta } = usePostData();
const { postMeta, title } = usePostData();
const [ patternNames, setPatternNames ] = useState(
patternManager.patternNames.filter( ( name ) => {
return name !== postMeta.name;
Expand All @@ -31,6 +31,7 @@ export default function PatternManagerMetaControls() {
<div>
<TitlePanel
postMeta={ postMeta }
title={ title }
handleChange={ updatePostMeta }
patternNames={ patternNames }
/>
Expand Down
21 changes: 15 additions & 6 deletions wp-modules/editor/js/src/components/SidebarPanels/TitlePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import convertToSlug from '../../utils/convertToSlug';

import type { BaseSidebarProps } from './types';
import type { Pattern, PostMeta } from '../../types';
import usePatternData from '../../hooks/usePatternData';

function isTitleTaken(
patternTitle: string,
Expand All @@ -20,12 +21,19 @@ function isTitleTaken(
return patternNames.includes( newSlug ) && newSlug !== currentSlug;
}

type Props = BaseSidebarProps & {
patternNames: Array< Pattern[ 'name' ] >;
title: string;
};

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

return (
Expand All @@ -39,11 +47,12 @@ 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,
kienstra marked this conversation as resolved.
Show resolved Hide resolved
} );
handleChange( 'name', convertToSlug( newTitle ) );

if ( ! newTitle ) {
lockPostSaving();
Expand Down
3 changes: 3 additions & 0 deletions wp-modules/editor/js/src/hooks/usePostData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export default function usePostData() {
( select: SelectQuery ) => ( {
postMeta:
select( 'core/editor' ).getEditedPostAttribute( 'meta' ),
title: select( 'core/editor' ).getEditedPostAttribute(
'title'
),
} ),
[]
),
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
121 changes: 65 additions & 56 deletions wp-modules/editor/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ function get_pattern_content_from_file( $post ) {
return;
}

if ( ! $post->post_title ) {
if ( ! $post->post_name ) {
return;
}

$post->post_content = get_pattern_by_name( $post->post_title )['content'] ?? '';
$pattern = get_pattern_by_name( $post->post_name );
if ( ! $pattern ) {
return;
}

$post->post_content = $pattern['content'];
$post->post_title = $pattern['title'];
}
add_action( 'the_post', __NAMESPACE__ . '\get_pattern_content_from_file' );

Expand All @@ -45,17 +51,19 @@ function save_pattern_to_file( WP_Post $post ) {
return;
}

$pattern = get_pattern_by_name( $post->post_title );
if ( ! $pattern ) {
return;
}
$pattern = get_pattern_by_name( $post->post_name );
Copy link
Contributor Author

@kienstra kienstra Feb 27, 2023

Choose a reason for hiding this comment

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

If it's alright, this now stores the pattern name in $post->post_name, not $post->post_title.

This is so we can store the pattern title in the $post->post_title.

post-new.php needs to accept a post with a $post->post_title, so it can have a title like 'My New Pattern 5'.

Copy link
Contributor

Choose a reason for hiding this comment

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

The reason I moved to store it in post title was this:
Screen Shot 2023-02-13 at 4 24 52 PM

This might cause errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting, is it appending numbers to the end, like my-new-pattern-1-4 instead of my-new-pattern-1?

This should prevent that, but maybe it isn't:

function short_circuit_unique_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh cool! I hadn't noticed that. Should be all good then. Nice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh wow! Good to hear!


update_pattern(
array_merge(
$pattern,
$pattern ? $pattern : [],
[
'content' => $post->post_content,
]
'title' => $post->post_title,
'name' => $post->post_name,
],
$post->post_title
? [ 'title' => $post->post_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.

Sometimes $post->title will be empty, even when there's a Title in the editor. I don't know why.

If it is, this shouldn't save an empty title to the pattern PHP file. That'll cause a PHP notice.

: []
)
);

Expand Down Expand Up @@ -89,23 +97,23 @@ function save_metadata_to_pattern_file( $override, $post_id, $meta_key, $meta_va
return $override;
}

$pattern_name = $post->post_title;
$pattern = get_pattern_by_name( $pattern_name );
if ( ! $pattern ) {
return $override;
}

// Only update the pattern if a registered meta key is being updated here (no need for core keys like _edit_lock).
$registered_meta_keys = array_keys( get_registered_meta_keys( 'post', get_pattern_post_type() ) );
if ( ! in_array( $meta_key, $registered_meta_keys, true ) ) {
return $override;
}

$pattern_name = $post->post_name;
$pattern = get_pattern_by_name( $pattern_name );
if ( ! $pattern ) {
return $override;
}

if ( 'name' === $meta_key ) {
wp_update_post(
[
'ID' => $post_id,
'post_title' => $meta_value,
'ID' => $post_id,
'post_name' => $meta_value,
]
);

Expand Down Expand Up @@ -144,9 +152,7 @@ function get_metadata_from_pattern_file( $override, $post_id, $meta_key, $is_sin
return $override;
}

$pattern_name = $post->post_title;

$pattern = get_pattern_by_name( $pattern_name );
$pattern = get_pattern_by_name( $post->post_name );
if ( ! $pattern ) {
return $override;
}
Expand Down Expand Up @@ -175,24 +181,7 @@ function redirect_pattern_actions() {
$new_post = wp_insert_post(
[
'post_type' => get_pattern_post_type(),
'post_title' => sanitize_text_field( filter_input( INPUT_GET, 'name' ) ),
'post_status' => 'publish',
]
);

wp_safe_redirect(
get_edit_post_link( $new_post, 'direct_link' )
);
}

if ( 'create-new' === filter_input( INPUT_GET, 'action' ) ) {
Copy link
Contributor Author

@kienstra kienstra Feb 27, 2023

Choose a reason for hiding this comment

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

Now that new posts go to post-new.php, we don't need this hack that created a new post and redirected to post.php.

$new_pattern = get_new_pattern( get_theme_patterns() );

update_pattern( $new_pattern );
$new_post = wp_insert_post(
[
'post_type' => get_pattern_post_type(),
'post_title' => $new_pattern['name'],
'post_name' => sanitize_text_field( filter_input( INPUT_GET, 'name' ) ),
'post_status' => 'publish',
]
);
Expand All @@ -219,7 +208,8 @@ function redirect_pattern_actions() {
$new_post = wp_insert_post(
[
'post_type' => get_pattern_post_type(),
'post_title' => $new_pattern['name'],
'post_title' => $new_pattern['title'],
'post_name' => $new_pattern['name'],
'post_status' => 'publish',
'post_content' => '',
]
Expand Down Expand Up @@ -250,24 +240,6 @@ function add_active_theme_to_heartbeat( $response, $data, $screen_id ) {
}
add_filter( 'heartbeat_received', __NAMESPACE__ . '\add_active_theme_to_heartbeat', 10, 3 );

/**
* Filters the fields used in post revisions.
*
* If the revision is for a pattern post,
* don't restore the title of the revision,
* as the title is where the pattern name is stored.
*
* @param array $fields The fields to filter.
* @param WP_Post|array $post The post to filter for.
* @return array The filtered fields.
*/
function ignore_title_field_in_revisions( $fields, $post ) {
return isset( $post->post_parent ) && get_pattern_post_type() === get_post_type( $post->post_parent )
? array_diff_key( $fields, [ 'post_title' => null ] )
: $fields;
}
add_filter( '_wp_post_revision_fields', __NAMESPACE__ . '\ignore_title_field_in_revisions', 10, 2 );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not needed anymore, as the title is stored in ->post_title


/**
* Deletes all pm_pattern posts.
*/
Expand All @@ -283,3 +255,40 @@ function delete_pattern_posts() {
}
}
add_action( 'after_switch_theme', __NAMESPACE__ . '\delete_pattern_posts' );

/**
* Gets the title for a new pattern post.
*
* @param string $post_title The post title.
* @param WP_Post $post The post.
* @return string
*/
function get_default_title( $post_title, $post ) {
return isset( $post->post_type ) && get_pattern_post_type() === $post->post_type
? get_new_pattern( get_theme_patterns() )['title']
: $post_title;
}
add_filter( 'default_title', __NAMESPACE__ . '\get_default_title', 10, 2 );
Copy link
Contributor Author

@kienstra kienstra Feb 27, 2023

Choose a reason for hiding this comment

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

This is what allows using post-new.php.

We can set the title for new posts on post-new.php:

Screenshot 2023-02-27 at 12 07 00 AM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because a pattern in post-new.php will not have been published yet (saved as a .php file), there's nowhere else I could find to save the title.


/**
* Overrides the longer ->post_name that WP gives a pattern.
*
* The ->post_name is where we store the pattern name.
* So it needs to be in sync with the ->post_title.
* It can't be something like foo-4 if the title is 'Foo'.
*
* @param string $slug The post slug.
* @param int $post_ID Post ID.
* @param string $post_status The post status.
* @param string $post_type Post type.
* @param int $post_parent Post parent ID
* @param string $original_slug The original post slug.
* @return string The simpler pattern name.
*/
function get_simpler_pattern_name( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
return get_pattern_post_type() === $post_type
? sanitize_title( $original_slug ) // TODO: port convertToSlug() to PHP and make this like convertToSlug( $original_slug ).
: $slug;
}

add_filter( 'wp_unique_post_slug', __NAMESPACE__ . '\get_simpler_pattern_name', 10, 6 );
Loading