diff --git a/wp-modules/pattern-data-handlers/pattern-data-handlers.php b/wp-modules/pattern-data-handlers/pattern-data-handlers.php index b2bcd9d34..02cd1ac4b 100644 --- a/wp-modules/pattern-data-handlers/pattern-data-handlers.php +++ b/wp-modules/pattern-data-handlers/pattern-data-handlers.php @@ -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' ) ) { @@ -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, ] @@ -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'], ], diff --git a/wp-modules/pattern-post-type/model.php b/wp-modules/pattern-post-type/model.php index f216c1c3e..51b5d6d78 100644 --- a/wp-modules/pattern-post-type/model.php +++ b/wp-modules/pattern-post-type/model.php @@ -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 ); + } + + $post_ids = get_pm_post_ids(); + } +} +add_action( 'after_switch_theme', __NAMESPACE__ . '\delete_pattern_posts' ); diff --git a/wp-modules/pattern-post-type/pattern-post-type.php b/wp-modules/pattern-post-type/pattern-post-type.php index 49bf60d67..1bc121090 100644 --- a/wp-modules/pattern-post-type/pattern-post-type.php +++ b/wp-modules/pattern-post-type/pattern-post-type.php @@ -19,14 +19,15 @@ 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'; /** * 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' ), @@ -34,7 +35,7 @@ function pattern_post_type() { ); 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,7 +174,7 @@ 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. @@ -181,7 +182,7 @@ function pattern_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; } diff --git a/wp-modules/pattern-post-type/utils.php b/wp-modules/pattern-post-type/utils.php index f04216c23..aa7c44fe9 100644 --- a/wp-modules/pattern-post-type/utils.php +++ b/wp-modules/pattern-post-type/utils.php @@ -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. *