From 239bf8f80da8c9406f186d7cd4a397ce9c4cb716 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Tue, 11 Jun 2024 17:12:09 +0545 Subject: [PATCH 1/3] Update scaffold cpt --- src/Scaffold_Command.php | 18 +++++++++++++++++- templates/post_type.mustache | 10 +++++----- templates/post_type_extended.mustache | 25 +++++++++++++++---------- templates/taxonomy_extended.mustache | 8 ++++---- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/Scaffold_Command.php b/src/Scaffold_Command.php index 99734daaa..f841a5867 100644 --- a/src/Scaffold_Command.php +++ b/src/Scaffold_Command.php @@ -178,7 +178,23 @@ private function scaffold( $slug, $assoc_args, $defaults, $subdir, $templates ) if ( ! $control_args['raw'] ) { $vars['machine_name'] = $machine_name; - $vars['output'] = $raw_output; + $vars['output'] = rtrim( $raw_output ); + + $target_slug = ''; + + if ( false !== $control_args['theme'] ) { + $target_slug = $control_args['theme']; + } elseif ( false !== $control_args['plugin'] ) { + $target_slug = $control_args['plugin']; + } + + $target_name = ( $target_slug ) ? $this->generate_machine_name( $target_slug ) : ''; + + if ( empty( $target_name ) ) { + $target_name = $machine_name; + } + + $vars['prefix'] = $target_name; $final_output = self::mustache_render( $extended_template, $vars ); } else { diff --git a/templates/post_type.mustache b/templates/post_type.mustache index 8f731a4a2..7115162a6 100644 --- a/templates/post_type.mustache +++ b/templates/post_type.mustache @@ -1,7 +1,7 @@ register_post_type( '{{slug}}', - [ - 'labels' => [ + array( + 'labels' => array( 'name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ), 'singular_name' => __( '{{label_ucfirst}}', '{{textdomain}}' ), 'all_items' => __( 'All {{label_plural_ucfirst}}', '{{textdomain}}' ), @@ -27,12 +27,12 @@ 'not_found_in_trash' => __( 'No {{label_plural}} found in trash', '{{textdomain}}' ), 'parent_item_colon' => __( 'Parent {{label_ucfirst}}:', '{{textdomain}}' ), 'menu_name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ), - ], + ), 'public' => true, 'hierarchical' => false, 'show_ui' => true, 'show_in_nav_menus' => true, - 'supports' => [ 'title', 'editor' ], + 'supports' => array( 'title', 'editor' ), 'has_archive' => true, 'rewrite' => true, 'query_var' => true, @@ -41,5 +41,5 @@ 'show_in_rest' => true, 'rest_base' => '{{slug}}', 'rest_controller_class' => 'WP_REST_Posts_Controller', - ] + ) ); diff --git a/templates/post_type_extended.mustache b/templates/post_type_extended.mustache index adb4d9dc6..288fa1c9d 100644 --- a/templates/post_type_extended.mustache +++ b/templates/post_type_extended.mustache @@ -1,13 +1,18 @@ '', // Unused. Messages start at index 1. /* translators: %s: post permalink */ 1 => sprintf( __( '{{label_ucfirst}} updated. View {{label}}', '{{textdomain}}' ), esc_url( $permalink ) ), @@ -38,12 +43,12 @@ function {{machine_name}}_updated_messages( $messages ) { 9 => sprintf( __( '{{label_ucfirst}} scheduled for: %1$s. Preview {{label}}', '{{textdomain}}' ), date_i18n( __( 'M j, Y @ G:i', '{{textdomain}}' ), strtotime( $post->post_date ) ), esc_url( $permalink ) ), /* translators: %s: post permalink */ 10 => sprintf( __( '{{label_ucfirst}} draft updated. Preview {{label}}', '{{textdomain}}' ), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ), - ]; + ); return $messages; } -add_filter( 'post_updated_messages', '{{machine_name}}_updated_messages' ); +add_filter( 'post_updated_messages', '{{prefix}}_updated_messages' ); /** * Sets the bulk post updated messages for the `{{machine_name}}` post type. @@ -53,10 +58,10 @@ add_filter( 'post_updated_messages', '{{machine_name}}_updated_messages' ); * @param int[] $bulk_counts Array of item counts for each message, used to build internationalized strings. * @return array Bulk messages for the `{{machine_name}}` post type. */ -function {{machine_name}}_bulk_updated_messages( $bulk_messages, $bulk_counts ) { +function {{prefix}}_bulk_updated_messages( $bulk_messages, $bulk_counts ) { global $post; - $bulk_messages['{{slug}}'] = [ + $bulk_messages['{{slug}}'] = array( /* translators: %s: Number of {{label_plural}}. */ 'updated' => _n( '%s {{label}} updated.', '%s {{label_plural}} updated.', $bulk_counts['updated'], '{{textdomain}}' ), 'locked' => ( 1 === $bulk_counts['locked'] ) ? __( '1 {{label}} not updated, somebody is editing it.', '{{textdomain}}' ) : @@ -68,9 +73,9 @@ function {{machine_name}}_bulk_updated_messages( $bulk_messages, $bulk_counts ) 'trashed' => _n( '%s {{label}} moved to the Trash.', '%s {{label_plural}} moved to the Trash.', $bulk_counts['trashed'], '{{textdomain}}' ), /* translators: %s: Number of {{label_plural}}. */ 'untrashed' => _n( '%s {{label}} restored from the Trash.', '%s {{label_plural}} restored from the Trash.', $bulk_counts['untrashed'], '{{textdomain}}' ), - ]; + ); return $bulk_messages; } -add_filter( 'bulk_post_updated_messages', '{{machine_name}}_bulk_updated_messages', 10, 2 ); +add_filter( 'bulk_post_updated_messages', '{{prefix}}_bulk_updated_messages', 10, 2 ); diff --git a/templates/taxonomy_extended.mustache b/templates/taxonomy_extended.mustache index 9a87cf11c..9c4ad1255 100644 --- a/templates/taxonomy_extended.mustache +++ b/templates/taxonomy_extended.mustache @@ -4,11 +4,11 @@ * Registers the `{{machine_name}}` taxonomy, * for use with {{post_types}}. */ -function {{machine_name}}_init() { +function {{prefix}}_init() { {{output}} } -add_action( 'init', '{{machine_name}}_init' ); +add_action( 'init', '{{prefix}}_init' ); /** * Sets the post updated messages for the `{{machine_name}}` taxonomy. @@ -16,7 +16,7 @@ add_action( 'init', '{{machine_name}}_init' ); * @param array $messages Post updated messages. * @return array Messages for the `{{machine_name}}` taxonomy. */ -function {{machine_name}}_updated_messages( $messages ) { +function {{prefix}}_updated_messages( $messages ) { $messages['{{slug}}'] = [ 0 => '', // Unused. Messages start at index 1. @@ -31,4 +31,4 @@ function {{machine_name}}_updated_messages( $messages ) { return $messages; } -add_filter( 'term_updated_messages', '{{machine_name}}_updated_messages' ); +add_filter( 'term_updated_messages', '{{prefix}}_updated_messages' ); From 429aa80e8a60addd0aff8f7fe9d5f3ebc0b88b16 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 12 Jun 2024 11:06:41 +0545 Subject: [PATCH 2/3] Update taxonomy --- templates/taxonomy.mustache | 88 +++++++++++++++------------- templates/taxonomy_extended.mustache | 9 ++- 2 files changed, 53 insertions(+), 44 deletions(-) diff --git a/templates/taxonomy.mustache b/templates/taxonomy.mustache index 3d9aca111..6f5ea8958 100644 --- a/templates/taxonomy.mustache +++ b/templates/taxonomy.mustache @@ -1,42 +1,46 @@ - register_taxonomy( '{{slug}}', [ {{post_types}} ], [ - 'hierarchical' => false, - 'public' => true, - 'show_in_nav_menus' => true, - 'show_ui' => true, - 'show_admin_column' => false, - 'query_var' => true, - 'rewrite' => true, - 'capabilities' => [ - 'manage_terms' => 'edit_posts', - 'edit_terms' => 'edit_posts', - 'delete_terms' => 'edit_posts', - 'assign_terms' => 'edit_posts', - ], - 'labels' => [ - 'name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ), - 'singular_name' => _x( '{{label_ucfirst}}', 'taxonomy general name', '{{textdomain}}' ), - 'search_items' => __( 'Search {{label_plural_ucfirst}}', '{{textdomain}}' ), - 'popular_items' => __( 'Popular {{label_plural_ucfirst}}', '{{textdomain}}' ), - 'all_items' => __( 'All {{label_plural_ucfirst}}', '{{textdomain}}' ), - 'parent_item' => __( 'Parent {{label_ucfirst}}', '{{textdomain}}' ), - 'parent_item_colon' => __( 'Parent {{label_ucfirst}}:', '{{textdomain}}' ), - 'edit_item' => __( 'Edit {{label_ucfirst}}', '{{textdomain}}' ), - 'update_item' => __( 'Update {{label_ucfirst}}', '{{textdomain}}' ), - 'view_item' => __( 'View {{label_ucfirst}}', '{{textdomain}}' ), - 'add_new_item' => __( 'Add New {{label_ucfirst}}', '{{textdomain}}' ), - 'new_item_name' => __( 'New {{label_ucfirst}}', '{{textdomain}}' ), - 'separate_items_with_commas' => __( 'Separate {{label_plural}} with commas', '{{textdomain}}' ), - 'add_or_remove_items' => __( 'Add or remove {{label_plural}}', '{{textdomain}}' ), - 'choose_from_most_used' => __( 'Choose from the most used {{label_plural}}', '{{textdomain}}' ), - 'not_found' => __( 'No {{label_plural}} found.', '{{textdomain}}' ), - 'no_terms' => __( 'No {{label_plural}}', '{{textdomain}}' ), - 'menu_name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ), - 'items_list_navigation' => __( '{{label_plural_ucfirst}} list navigation', '{{textdomain}}' ), - 'items_list' => __( '{{label_plural_ucfirst}} list', '{{textdomain}}' ), - 'most_used' => _x( 'Most Used', '{{slug}}', '{{textdomain}}' ), - 'back_to_items' => __( '← Back to {{label_plural_ucfirst}}', '{{textdomain}}' ), - ], - 'show_in_rest' => true, - 'rest_base' => '{{slug}}', - 'rest_controller_class' => 'WP_REST_Terms_Controller', - ] ); + register_taxonomy( + '{{slug}}', + array( {{post_types}} ), + array( + 'hierarchical' => false, + 'public' => true, + 'show_in_nav_menus' => true, + 'show_ui' => true, + 'show_admin_column' => false, + 'query_var' => true, + 'rewrite' => true, + 'capabilities' => array( + 'manage_terms' => 'edit_posts', + 'edit_terms' => 'edit_posts', + 'delete_terms' => 'edit_posts', + 'assign_terms' => 'edit_posts', + ), + 'labels' => array( + 'name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ), + 'singular_name' => _x( '{{label_ucfirst}}', 'taxonomy general name', '{{textdomain}}' ), + 'search_items' => __( 'Search {{label_plural_ucfirst}}', '{{textdomain}}' ), + 'popular_items' => __( 'Popular {{label_plural_ucfirst}}', '{{textdomain}}' ), + 'all_items' => __( 'All {{label_plural_ucfirst}}', '{{textdomain}}' ), + 'parent_item' => __( 'Parent {{label_ucfirst}}', '{{textdomain}}' ), + 'parent_item_colon' => __( 'Parent {{label_ucfirst}}:', '{{textdomain}}' ), + 'edit_item' => __( 'Edit {{label_ucfirst}}', '{{textdomain}}' ), + 'update_item' => __( 'Update {{label_ucfirst}}', '{{textdomain}}' ), + 'view_item' => __( 'View {{label_ucfirst}}', '{{textdomain}}' ), + 'add_new_item' => __( 'Add New {{label_ucfirst}}', '{{textdomain}}' ), + 'new_item_name' => __( 'New {{label_ucfirst}}', '{{textdomain}}' ), + 'separate_items_with_commas' => __( 'Separate {{label_plural}} with commas', '{{textdomain}}' ), + 'add_or_remove_items' => __( 'Add or remove {{label_plural}}', '{{textdomain}}' ), + 'choose_from_most_used' => __( 'Choose from the most used {{label_plural}}', '{{textdomain}}' ), + 'not_found' => __( 'No {{label_plural}} found.', '{{textdomain}}' ), + 'no_terms' => __( 'No {{label_plural}}', '{{textdomain}}' ), + 'menu_name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ), + 'items_list_navigation' => __( '{{label_plural_ucfirst}} list navigation', '{{textdomain}}' ), + 'items_list' => __( '{{label_plural_ucfirst}} list', '{{textdomain}}' ), + 'most_used' => _x( 'Most Used', '{{slug}}', '{{textdomain}}' ), + 'back_to_items' => __( '← Back to {{label_plural_ucfirst}}', '{{textdomain}}' ), + ), + 'show_in_rest' => true, + 'rest_base' => '{{slug}}', + 'rest_controller_class' => 'WP_REST_Terms_Controller', + ) + ); diff --git a/templates/taxonomy_extended.mustache b/templates/taxonomy_extended.mustache index 9c4ad1255..fc71a6596 100644 --- a/templates/taxonomy_extended.mustache +++ b/templates/taxonomy_extended.mustache @@ -1,4 +1,9 @@ '', // Unused. Messages start at index 1. 1 => __( '{{label_ucfirst}} added.', '{{textdomain}}' ), 2 => __( '{{label_ucfirst}} deleted.', '{{textdomain}}' ), @@ -26,7 +31,7 @@ function {{prefix}}_updated_messages( $messages ) { 4 => __( '{{label_ucfirst}} not added.', '{{textdomain}}' ), 5 => __( '{{label_ucfirst}} not updated.', '{{textdomain}}' ), 6 => __( '{{label_plural_ucfirst}} deleted.', '{{textdomain}}' ), - ]; + ); return $messages; } From 9330fbea5806ff142fc4d640539e96f8cf9cc3d3 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 12 Jun 2024 11:23:57 +0545 Subject: [PATCH 3/3] Fix test for taxonomy scaffold --- features/scaffold-taxonomy.feature | 2 +- features/scaffold.feature | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/features/scaffold-taxonomy.feature b/features/scaffold-taxonomy.feature index 73a986e44..f6b367a83 100644 --- a/features/scaffold-taxonomy.feature +++ b/features/scaffold-taxonomy.feature @@ -19,7 +19,7 @@ Feature: Scaffold a custom taxonomy """ And STDOUT should contain: """ - $messages['fungus'] = [ + $messages['fungus'] = array( """ And STDOUT should contain: """ diff --git a/features/scaffold.feature b/features/scaffold.feature index 23aee121b..b92b7b480 100644 --- a/features/scaffold.feature +++ b/features/scaffold.feature @@ -116,7 +116,7 @@ Feature: WordPress code scaffolding """ And STDOUT should contain: """ - [ 'prefix-zombie', 'wraith' ] + array( 'prefix-zombie', 'wraith' ) """ And STDOUT should contain: """