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

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Feb 20, 2023

Fixes part of #57 (When theme is changed, delete all pm_pattern posts (these are not needed as we pull from files)

How to test

  1. Go to /wp-admin/admin.php?page=pattern-manager
  2. Click to edit a pattern: Screenshot 2023-02-20 at 1 44 23 PM
  3. wp post list --post_type=pm_pattern
  4. You should see at least 1 pattern, like:
+------+------------------+------------------+---------------------+-------------+
| ID   | post_title       | post_name        | post_date           | post_status |
+------+------------------+------------------+---------------------+-------------+
| 1229 | footer-default-c | footer-default-c | 2023-02-20 19:45:07 | publish     |
|      | ontrast-backgrou | ontrast-backgrou |                     |             |
|      | nd               | nd               |                     |             |
+------+------------------+------------------+---------------------+-------------+
  1. Change the theme
  2. wp post list --post_type=pm_pattern
  3. Expected: There's no pattern:
+----+------------+-----------+-----------+-------------+
| ID | post_title | post_name | post_date | post_status |
+----+------------+-----------+-----------+-------------+
+----+------------+-----------+-----------+-------------+
  1. Expected: The pattern files in the previous theme's patterns/ directory are still there

@@ -238,3 +239,27 @@ function redirect_pattern_actions() {
}
}
add_action( 'admin_init', __NAMESPACE__ . '\redirect_pattern_actions' );

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.


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.

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.

@kienstra kienstra marked this pull request as ready for review February 20, 2023 20:13
@kienstra kienstra mentioned this pull request Feb 20, 2023
11 tasks
@kienstra
Copy link
Contributor Author

Thanks a lot, @johnstonphilip!

@kienstra kienstra merged commit df51131 into main Feb 21, 2023
@kienstra kienstra deleted the update/remove-pm-pattern-posts branch February 21, 2023 17:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants