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

After switching themes, delete all pm_pattern posts #62

Merged
merged 7 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions wp-modules/pattern-data-handlers/pattern-data-handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PatternManager\PatternDataHandlers;

use WP_Query;
use function PatternManager\PatternPostType\get_pattern_post_type;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
Expand Down Expand Up @@ -158,7 +159,7 @@ function get_theme_patterns_with_editor_links() {
if ( $pattern ) {
$query = new WP_Query(
[
'post_type' => 'pm_pattern',
'post_type' => get_pattern_post_type(),
'post_title' => $pattern['name'],
'posts_per_page' => 1,
]
Expand All @@ -169,7 +170,7 @@ function get_theme_patterns_with_editor_links() {
? get_edit_post_link( $post, 'localized_data' )
: add_query_arg(
[
'post_type' => 'pm_pattern',
'post_type' => get_pattern_post_type(),
'action' => 'edit-pattern',
'name' => $pattern['name'],
],
Expand Down
57 changes: 45 additions & 12 deletions wp-modules/pattern-post-type/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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(
Expand All @@ -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.
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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',
]
Expand All @@ -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',
]
Expand Down Expand Up @@ -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' => '',
Expand All @@ -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() {
Copy link
Contributor Author

@kienstra kienstra Feb 20, 2023

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.

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 );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The 2nd true argument bypasses putting the post in the trash.

}

$post_ids = get_pm_post_ids();
}
}
add_action( 'after_switch_theme', __NAMESPACE__ . '\delete_pattern_posts' );
35 changes: 18 additions & 17 deletions wp-modules/pattern-post-type/pattern-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prevents a PHP error from get_pattern_post_type() not being defined.


/**
* 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,
Expand All @@ -50,7 +51,7 @@ function pattern_post_type() {
);

register_post_meta(
'pm_pattern',
$post_type_key,
'type',
array(
'show_in_rest' => true,
Expand All @@ -60,7 +61,7 @@ function pattern_post_type() {
);

register_post_meta(
'pm_pattern',
$post_type_key,
'title',
array(
'show_in_rest' => true,
Expand All @@ -70,7 +71,7 @@ function pattern_post_type() {
);

register_post_meta(
'pm_pattern',
$post_type_key,
'name',
array(
'show_in_rest' => true,
Expand All @@ -80,7 +81,7 @@ function pattern_post_type() {
);

register_post_meta(
'pm_pattern',
$post_type_key,
'description',
array(
'show_in_rest' => true,
Expand All @@ -90,7 +91,7 @@ function pattern_post_type() {
);

register_post_meta(
'pm_pattern',
$post_type_key,
'inserter',
array(
'show_in_rest' => true,
Expand All @@ -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(
Expand All @@ -123,7 +124,7 @@ function pattern_post_type() {
);

register_post_meta(
'pm_pattern',
$post_type_key,
'postTypes',
array(
'show_in_rest' => array(
Expand All @@ -140,7 +141,7 @@ function pattern_post_type() {
);

register_post_meta(
'pm_pattern',
$post_type_key,
'categories',
array(
'show_in_rest' => array(
Expand All @@ -157,7 +158,7 @@ function pattern_post_type() {
);

register_post_meta(
'pm_pattern',
$post_type_key,
'keywords',
array(
'show_in_rest' => array(
Expand All @@ -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' );
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions wp-modules/pattern-post-type/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@

namespace PatternManager\PatternPostType;

/**
* Gets the pattern post type.
*
* @return string
*/
function get_pattern_post_type(): string {
return 'pm_pattern';
}

/**
* Gets the new pattern number.
*
Expand Down