From 480d4fa06bd9826c6c6ec7da97b98ef73b614256 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Tue, 11 Jun 2024 17:12:09 +0545 Subject: [PATCH] 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..f2e5ee972 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']; + } else if ( 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' );