-
Notifications
You must be signed in to change notification settings - Fork 6
On creating a new pattern, don't write to the pattern PHP file until you publish #78
Changes from 7 commits
863de51
572f5d8
fe5a897
aa868a1
3e9e055
082e0b5
76e772a
dc0d756
6f07249
57736ed
bbe9b1a
85b0d83
bd85b9e
75c2ac0
beae31b
d0182cb
9c8020b
b30923c
d80d8c8
19b718b
5d05982
afcb3b8
620099f
76f28b0
1803303
e9bf731
9afad49
55acf6e
f65f357
3b29742
cdfe61c
41cb2b3
2c02efc
78616c7
1ae006c
a413e10
4eeda13
2ccd955
5465c2e
dcdcd68
c35e0f1
21d1d06
4bb8ddc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
|
@@ -45,6 +46,7 @@ function register_pattern_post_type() { | |
'supports' => array( | ||
'editor', | ||
'custom-fields', | ||
'title', | ||
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. This now stores the pattern title in |
||
), | ||
'labels' => $labels, | ||
) | ||
|
@@ -60,16 +62,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', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import '../../css/src/index.scss'; | ||
import { store as editorStore } from '@wordpress/editor'; | ||
import { dispatch } from '@wordpress/data'; | ||
import { addAction, addFilter } from '@wordpress/hooks'; | ||
import { registerPlugin } from '@wordpress/plugins'; | ||
import BackButton from './components/BackButton'; | ||
|
@@ -22,3 +24,6 @@ addAction( | |
'pattern-manager/checkActiveTheme', | ||
receiveActiveTheme | ||
); | ||
|
||
// @ts-expect-error the @wordpress/editor store isn't typed. | ||
dispatch( editorStore ).disablePublishSidebar(); | ||
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. It'd be better to disable the post publish sidebar with a PHP filter: But there's no good way to filter it because that preference comes from user meta. |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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' ); | ||||
|
||||
|
@@ -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 ); | ||||
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. If it's alright, this now stores the pattern name in This is so we can store the pattern title in the
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. 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. Interesting, is it appending numbers to the end, like This should prevent that, but maybe it isn't: pattern-manager/wp-modules/editor/model.php Line 293 in 78616c7
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. Oh cool! I hadn't noticed that. Should be all good then. Nice 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. 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 ] | ||||
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. Sometimes If it is, this shouldn't save an empty title to the pattern PHP file. That'll cause a PHP notice. |
||||
: [] | ||||
) | ||||
); | ||||
|
||||
|
@@ -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, | ||||
] | ||||
); | ||||
|
||||
|
@@ -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; | ||||
} | ||||
|
@@ -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' ) ) { | ||||
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. Now that new posts go to |
||||
$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', | ||||
] | ||||
); | ||||
|
@@ -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' => '', | ||||
] | ||||
|
@@ -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 ); | ||||
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. Not needed anymore, as the title is stored in |
||||
|
||||
/** | ||||
* Deletes all pm_pattern posts. | ||||
*/ | ||||
|
@@ -283,3 +255,39 @@ 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 ); | ||||
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. 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. Because a pattern in |
||||
|
||||
/** | ||||
* 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 ); |
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.
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.