This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
After switching themes, delete all pm_pattern
posts
#62
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b991d35
After switching themes, delete all pm_pattern posts
kienstra 2715663
Add PHP DocBlocks summaries
kienstra d20337b
Extract 'pm_pattern' into a function
kienstra 81f1563
Add a return type to get_pattern_post_type()
kienstra 10bb02c
Call the new util function in pattern-data-handlers.php
kienstra d96d1ff
Fix a PHP error
kienstra bc87840
Prevent confusion between names
kienstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
namespace PatternManager\PatternPostType; | ||
|
||
use WP_Post; | ||
use WP_Query; | ||
use function PatternManager\PatternDataHandlers\get_pattern_by_name; | ||
use function PatternManager\PatternDataHandlers\get_theme_patterns; | ||
use function PatternManager\PatternDataHandlers\delete_pattern; | ||
|
@@ -22,7 +23,7 @@ | |
* @param WP_Post $post The post to possibly add content to. | ||
*/ | ||
function get_pattern_content_from_file( $post ) { | ||
if ( 'pm_pattern' !== $post->post_type ) { | ||
if ( get_pattern_post_type() !== $post->post_type ) { | ||
return; | ||
} | ||
|
||
|
@@ -41,7 +42,7 @@ function get_pattern_content_from_file( $post ) { | |
* @param WP_Post $post The post. | ||
*/ | ||
function save_pattern_to_file( WP_Post $post ) { | ||
if ( 'pm_pattern' !== $post->post_type ) { | ||
if ( get_pattern_post_type() !== $post->post_type ) { | ||
return; | ||
} | ||
|
||
|
@@ -60,7 +61,7 @@ function save_pattern_to_file( WP_Post $post ) { | |
); | ||
|
||
// Prevent an infinite loop. | ||
remove_action( 'rest_after_insert_pm_pattern', __NAMESPACE__ . '\save_pattern_to_file' ); | ||
remove_action( 'rest_after_insert_' . get_pattern_post_type(), __NAMESPACE__ . '\save_pattern_to_file' ); | ||
|
||
// Removes the post content, as it should be saved in the pattern .php file. | ||
wp_update_post( | ||
|
@@ -70,9 +71,9 @@ function save_pattern_to_file( WP_Post $post ) { | |
] | ||
); | ||
|
||
add_action( 'rest_after_insert_pm_pattern', __NAMESPACE__ . '\save_pattern_to_file' ); | ||
add_action( 'rest_after_insert_' . get_pattern_post_type(), __NAMESPACE__ . '\save_pattern_to_file' ); | ||
} | ||
add_action( 'rest_after_insert_pm_pattern', __NAMESPACE__ . '\save_pattern_to_file' ); | ||
add_action( 'rest_after_insert_' . get_pattern_post_type(), __NAMESPACE__ . '\save_pattern_to_file' ); | ||
|
||
/** | ||
* Saves a meta value to the pattern file, instead of the DB. | ||
|
@@ -86,7 +87,7 @@ function save_pattern_to_file( WP_Post $post ) { | |
*/ | ||
function save_metadata_to_pattern_file( $override, $post_id, $meta_key, $meta_value, $previous_value ) { | ||
$post = get_post( $post_id ); | ||
if ( 'pm_pattern' !== $post->post_type ) { | ||
if ( get_pattern_post_type() !== $post->post_type ) { | ||
return $override; | ||
} | ||
|
||
|
@@ -97,7 +98,7 @@ function save_metadata_to_pattern_file( $override, $post_id, $meta_key, $meta_va | |
} | ||
|
||
// 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', 'pm_pattern' ) ); | ||
$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; | ||
} | ||
|
@@ -141,7 +142,7 @@ function get_metadata_from_pattern_file( $override, $post_id, $meta_key, $is_sin | |
return $override; | ||
} | ||
|
||
if ( 'pm_pattern' !== $post->post_type ) { | ||
if ( get_pattern_post_type() !== $post->post_type ) { | ||
return $override; | ||
} | ||
|
||
|
@@ -164,7 +165,7 @@ function get_metadata_from_pattern_file( $override, $post_id, $meta_key, $is_sin | |
* Redirects for pattern actions. | ||
*/ | ||
function redirect_pattern_actions() { | ||
if ( 'pm_pattern' !== filter_input( INPUT_GET, 'post_type' ) ) { | ||
if ( get_pattern_post_type() !== filter_input( INPUT_GET, 'post_type' ) ) { | ||
return; | ||
} | ||
|
||
|
@@ -177,7 +178,7 @@ function redirect_pattern_actions() { | |
remove_action( 'rest_after_insert_pm_pattern', __NAMESPACE__ . '\save_pattern_to_file' ); | ||
$new_post = wp_insert_post( | ||
[ | ||
'post_type' => 'pm_pattern', | ||
'post_type' => get_pattern_post_type(), | ||
'post_title' => sanitize_text_field( filter_input( INPUT_GET, 'name' ) ), | ||
'post_status' => 'publish', | ||
] | ||
|
@@ -195,7 +196,7 @@ function redirect_pattern_actions() { | |
update_pattern( $new_pattern ); | ||
$new_post = wp_insert_post( | ||
[ | ||
'post_type' => 'pm_pattern', | ||
'post_type' => get_pattern_post_type(), | ||
'post_title' => $new_pattern['name'], | ||
'post_status' => 'publish', | ||
] | ||
|
@@ -224,7 +225,7 @@ function redirect_pattern_actions() { | |
remove_action( 'rest_after_insert_pm_pattern', __NAMESPACE__ . '\save_pattern_to_file' ); | ||
$new_post = wp_insert_post( | ||
[ | ||
'post_type' => 'pm_pattern', | ||
'post_type' => get_pattern_post_type(), | ||
'post_title' => $new_pattern['name'], | ||
'post_status' => 'publish', | ||
'post_content' => '', | ||
|
@@ -238,3 +239,35 @@ function redirect_pattern_actions() { | |
} | ||
} | ||
add_action( 'admin_init', __NAMESPACE__ . '\redirect_pattern_actions' ); | ||
|
||
/** | ||
* Gets the pm_pattern post IDs. | ||
* | ||
* @return int[] | ||
*/ | ||
function get_pm_post_ids() { | ||
return ( new WP_Query( | ||
[ | ||
'post_type' => get_pattern_post_type(), | ||
'post_status' => 'any', | ||
'fields' => 'ids', | ||
'posts_per_page' => 10, | ||
] | ||
) )->posts; | ||
} | ||
|
||
/** | ||
* Deletes all pm_pattern posts. | ||
*/ | ||
function delete_pattern_posts() { | ||
$post_ids = get_pm_post_ids(); | ||
|
||
while ( ! empty( $post_ids ) ) { | ||
foreach ( $post_ids as $post_id ) { | ||
wp_delete_post( $post_id, true ); | ||
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. The 2nd |
||
} | ||
|
||
$post_ids = get_pm_post_ids(); | ||
} | ||
} | ||
add_action( 'after_switch_theme', __NAMESPACE__ . '\delete_pattern_posts' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,22 +19,23 @@ | |
exit; | ||
} | ||
|
||
require_once module_dir_path( __FILE__ ) . 'model.php'; | ||
require_once module_dir_path( __FILE__ ) . 'utils.php'; | ||
require_once module_dir_path( __FILE__ ) . 'model.php'; | ||
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. Prevents a PHP error from |
||
|
||
/** | ||
* Create a custom post type to be used for our default post. | ||
*/ | ||
function pattern_post_type() { | ||
$labels = array( | ||
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' ), | ||
); | ||
|
||
register_post_type( | ||
'pm_pattern', | ||
$post_type_key, | ||
array( | ||
'public' => false, | ||
'has_archive' => false, | ||
|
@@ -50,7 +51,7 @@ function pattern_post_type() { | |
); | ||
|
||
register_post_meta( | ||
'pm_pattern', | ||
$post_type_key, | ||
'type', | ||
array( | ||
'show_in_rest' => true, | ||
|
@@ -60,7 +61,7 @@ function pattern_post_type() { | |
); | ||
|
||
register_post_meta( | ||
'pm_pattern', | ||
$post_type_key, | ||
'title', | ||
array( | ||
'show_in_rest' => true, | ||
|
@@ -70,7 +71,7 @@ function pattern_post_type() { | |
); | ||
|
||
register_post_meta( | ||
'pm_pattern', | ||
$post_type_key, | ||
'name', | ||
array( | ||
'show_in_rest' => true, | ||
|
@@ -80,7 +81,7 @@ function pattern_post_type() { | |
); | ||
|
||
register_post_meta( | ||
'pm_pattern', | ||
$post_type_key, | ||
'description', | ||
array( | ||
'show_in_rest' => true, | ||
|
@@ -90,7 +91,7 @@ function pattern_post_type() { | |
); | ||
|
||
register_post_meta( | ||
'pm_pattern', | ||
$post_type_key, | ||
'inserter', | ||
array( | ||
'show_in_rest' => true, | ||
|
@@ -106,7 +107,7 @@ function pattern_post_type() { | |
* @see https://make.wordpress.org/core/2019/10/03/wp-5-3-supports-object-and-array-meta-types-in-the-rest-api/ | ||
*/ | ||
register_post_meta( | ||
'pm_pattern', | ||
$post_type_key, | ||
'blockTypes', | ||
array( | ||
'show_in_rest' => array( | ||
|
@@ -123,7 +124,7 @@ function pattern_post_type() { | |
); | ||
|
||
register_post_meta( | ||
'pm_pattern', | ||
$post_type_key, | ||
'postTypes', | ||
array( | ||
'show_in_rest' => array( | ||
|
@@ -140,7 +141,7 @@ function pattern_post_type() { | |
); | ||
|
||
register_post_meta( | ||
'pm_pattern', | ||
$post_type_key, | ||
'categories', | ||
array( | ||
'show_in_rest' => array( | ||
|
@@ -157,7 +158,7 @@ function pattern_post_type() { | |
); | ||
|
||
register_post_meta( | ||
'pm_pattern', | ||
$post_type_key, | ||
'keywords', | ||
array( | ||
'show_in_rest' => array( | ||
|
@@ -173,15 +174,15 @@ function pattern_post_type() { | |
) | ||
); | ||
} | ||
add_action( 'init', __NAMESPACE__ . '\pattern_post_type' ); | ||
add_action( 'init', __NAMESPACE__ . '\register_pattern_post_type' ); | ||
|
||
/** | ||
* Disable auto-save for this post type. | ||
* | ||
*/ | ||
function disable_autosave() { | ||
global $post_type; | ||
if ( 'pm_pattern' === $post_type ) { | ||
if ( get_pattern_post_type() === $post_type ) { | ||
wp_dequeue_script( 'autosave' ); | ||
} | ||
} | ||
|
@@ -238,7 +239,7 @@ function do_the_content_things( $content ) { | |
* Add style and metaboxes to pm_pattern posts when editing. | ||
*/ | ||
function enqueue_meta_fields_in_editor() { | ||
if ( 'pm_pattern' !== get_post_type() ) { | ||
if ( get_pattern_post_type() !== get_post_type() ) { | ||
return; | ||
} | ||
|
||
|
@@ -284,7 +285,7 @@ function enqueue_meta_fields_in_editor() { | |
function register_block_patterns() { | ||
$current_screen = get_current_screen(); | ||
|
||
if ( 'pm_pattern' !== $current_screen->post_type ) { | ||
if ( get_pattern_post_type() !== $current_screen->post_type ) { | ||
return; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is a separate function so we don't have to have a single query with a
'posts_per_page'
that might not get all of the posts.We can keep calling this in the
while
loop until there aren't any more posts.