Skip to content

Commit d296bb7

Browse files
authored
Merge pull request #341 from ernilambar/update/cpt-scaffold
Update cpt and taxonomy scaffold
2 parents 1a08091 + f1d7f04 commit d296bb7

File tree

7 files changed

+96
-66
lines changed

7 files changed

+96
-66
lines changed

features/scaffold-taxonomy.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Feature: Scaffold a custom taxonomy
1919
"""
2020
And STDOUT should contain:
2121
"""
22-
$messages['fungus'] = [
22+
$messages['fungus'] = array(
2323
"""
2424
And STDOUT should contain:
2525
"""

features/scaffold.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Feature: WordPress code scaffolding
116116
"""
117117
And STDOUT should contain:
118118
"""
119-
[ 'prefix-zombie', 'wraith' ]
119+
array( 'prefix-zombie', 'wraith' )
120120
"""
121121
And STDOUT should contain:
122122
"""

src/Scaffold_Command.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,23 @@ private function scaffold( $slug, $assoc_args, $defaults, $subdir, $templates )
178178

179179
if ( ! $control_args['raw'] ) {
180180
$vars['machine_name'] = $machine_name;
181-
$vars['output'] = $raw_output;
181+
$vars['output'] = rtrim( $raw_output );
182+
183+
$target_slug = '';
184+
185+
if ( false !== $control_args['theme'] ) {
186+
$target_slug = $control_args['theme'];
187+
} elseif ( false !== $control_args['plugin'] ) {
188+
$target_slug = $control_args['plugin'];
189+
}
190+
191+
$target_name = ( $target_slug ) ? $this->generate_machine_name( $target_slug ) : '';
192+
193+
if ( empty( $target_name ) ) {
194+
$target_name = $machine_name;
195+
}
196+
197+
$vars['prefix'] = $target_name;
182198

183199
$final_output = self::mustache_render( $extended_template, $vars );
184200
} else {

templates/post_type.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
register_post_type(
22
'{{slug}}',
3-
[
4-
'labels' => [
3+
array(
4+
'labels' => array(
55
'name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ),
66
'singular_name' => __( '{{label_ucfirst}}', '{{textdomain}}' ),
77
'all_items' => __( 'All {{label_plural_ucfirst}}', '{{textdomain}}' ),
@@ -27,12 +27,12 @@
2727
'not_found_in_trash' => __( 'No {{label_plural}} found in trash', '{{textdomain}}' ),
2828
'parent_item_colon' => __( 'Parent {{label_ucfirst}}:', '{{textdomain}}' ),
2929
'menu_name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ),
30-
],
30+
),
3131
'public' => true,
3232
'hierarchical' => false,
3333
'show_ui' => true,
3434
'show_in_nav_menus' => true,
35-
'supports' => [ 'title', 'editor' ],
35+
'supports' => array( 'title', 'editor' ),
3636
'has_archive' => true,
3737
'rewrite' => true,
3838
'query_var' => true,
@@ -41,5 +41,5 @@
4141
'show_in_rest' => true,
4242
'rest_base' => '{{slug}}',
4343
'rest_controller_class' => 'WP_REST_Posts_Controller',
44-
]
44+
)
4545
);

templates/post_type_extended.mustache

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
<?php
2+
/**
3+
* Custom post type
4+
*
5+
* @package {{prefix}}
6+
*/
27

38
/**
49
* Registers the `{{machine_name}}` post type.
510
*/
6-
function {{machine_name}}_init() {
11+
function {{prefix}}_init() {
712
{{output}}
813
}
914

10-
add_action( 'init', '{{machine_name}}_init' );
15+
add_action( 'init', '{{prefix}}_init' );
1116

1217
/**
1318
* Sets the post updated messages for the `{{machine_name}}` post type.
1419
*
1520
* @param array $messages Post updated messages.
1621
* @return array Messages for the `{{machine_name}}` post type.
1722
*/
18-
function {{machine_name}}_updated_messages( $messages ) {
23+
function {{prefix}}_updated_messages( $messages ) {
1924
global $post;
2025
2126
$permalink = get_permalink( $post );
2227
23-
$messages['{{slug}}'] = [
28+
$messages['{{slug}}'] = array(
2429
0 => '', // Unused. Messages start at index 1.
2530
/* translators: %s: post permalink */
2631
1 => sprintf( __( '{{label_ucfirst}} updated. <a target="_blank" href="%s">View {{label}}</a>', '{{textdomain}}' ), esc_url( $permalink ) ),
@@ -38,12 +43,12 @@ function {{machine_name}}_updated_messages( $messages ) {
3843
9 => sprintf( __( '{{label_ucfirst}} scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview {{label}}</a>', '{{textdomain}}' ), date_i18n( __( 'M j, Y @ G:i', '{{textdomain}}' ), strtotime( $post->post_date ) ), esc_url( $permalink ) ),
3944
/* translators: %s: post permalink */
4045
10 => sprintf( __( '{{label_ucfirst}} draft updated. <a target="_blank" href="%s">Preview {{label}}</a>', '{{textdomain}}' ), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ),
41-
];
46+
);
4247
4348
return $messages;
4449
}
4550

46-
add_filter( 'post_updated_messages', '{{machine_name}}_updated_messages' );
51+
add_filter( 'post_updated_messages', '{{prefix}}_updated_messages' );
4752

4853
/**
4954
* 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' );
5358
* @param int[] $bulk_counts Array of item counts for each message, used to build internationalized strings.
5459
* @return array Bulk messages for the `{{machine_name}}` post type.
5560
*/
56-
function {{machine_name}}_bulk_updated_messages( $bulk_messages, $bulk_counts ) {
61+
function {{prefix}}_bulk_updated_messages( $bulk_messages, $bulk_counts ) {
5762
global $post;
5863
59-
$bulk_messages['{{slug}}'] = [
64+
$bulk_messages['{{slug}}'] = array(
6065
/* translators: %s: Number of {{label_plural}}. */
6166
'updated' => _n( '%s {{label}} updated.', '%s {{label_plural}} updated.', $bulk_counts['updated'], '{{textdomain}}' ),
6267
'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 )
6873
'trashed' => _n( '%s {{label}} moved to the Trash.', '%s {{label_plural}} moved to the Trash.', $bulk_counts['trashed'], '{{textdomain}}' ),
6974
/* translators: %s: Number of {{label_plural}}. */
7075
'untrashed' => _n( '%s {{label}} restored from the Trash.', '%s {{label_plural}} restored from the Trash.', $bulk_counts['untrashed'], '{{textdomain}}' ),
71-
];
76+
);
7277

7378
return $bulk_messages;
7479
}
7580

76-
add_filter( 'bulk_post_updated_messages', '{{machine_name}}_bulk_updated_messages', 10, 2 );
81+
add_filter( 'bulk_post_updated_messages', '{{prefix}}_bulk_updated_messages', 10, 2 );

templates/taxonomy.mustache

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
1-
register_taxonomy( '{{slug}}', [ {{post_types}} ], [
2-
'hierarchical' => false,
3-
'public' => true,
4-
'show_in_nav_menus' => true,
5-
'show_ui' => true,
6-
'show_admin_column' => false,
7-
'query_var' => true,
8-
'rewrite' => true,
9-
'capabilities' => [
10-
'manage_terms' => 'edit_posts',
11-
'edit_terms' => 'edit_posts',
12-
'delete_terms' => 'edit_posts',
13-
'assign_terms' => 'edit_posts',
14-
],
15-
'labels' => [
16-
'name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ),
17-
'singular_name' => _x( '{{label_ucfirst}}', 'taxonomy general name', '{{textdomain}}' ),
18-
'search_items' => __( 'Search {{label_plural_ucfirst}}', '{{textdomain}}' ),
19-
'popular_items' => __( 'Popular {{label_plural_ucfirst}}', '{{textdomain}}' ),
20-
'all_items' => __( 'All {{label_plural_ucfirst}}', '{{textdomain}}' ),
21-
'parent_item' => __( 'Parent {{label_ucfirst}}', '{{textdomain}}' ),
22-
'parent_item_colon' => __( 'Parent {{label_ucfirst}}:', '{{textdomain}}' ),
23-
'edit_item' => __( 'Edit {{label_ucfirst}}', '{{textdomain}}' ),
24-
'update_item' => __( 'Update {{label_ucfirst}}', '{{textdomain}}' ),
25-
'view_item' => __( 'View {{label_ucfirst}}', '{{textdomain}}' ),
26-
'add_new_item' => __( 'Add New {{label_ucfirst}}', '{{textdomain}}' ),
27-
'new_item_name' => __( 'New {{label_ucfirst}}', '{{textdomain}}' ),
28-
'separate_items_with_commas' => __( 'Separate {{label_plural}} with commas', '{{textdomain}}' ),
29-
'add_or_remove_items' => __( 'Add or remove {{label_plural}}', '{{textdomain}}' ),
30-
'choose_from_most_used' => __( 'Choose from the most used {{label_plural}}', '{{textdomain}}' ),
31-
'not_found' => __( 'No {{label_plural}} found.', '{{textdomain}}' ),
32-
'no_terms' => __( 'No {{label_plural}}', '{{textdomain}}' ),
33-
'menu_name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ),
34-
'items_list_navigation' => __( '{{label_plural_ucfirst}} list navigation', '{{textdomain}}' ),
35-
'items_list' => __( '{{label_plural_ucfirst}} list', '{{textdomain}}' ),
36-
'most_used' => _x( 'Most Used', '{{slug}}', '{{textdomain}}' ),
37-
'back_to_items' => __( '&larr; Back to {{label_plural_ucfirst}}', '{{textdomain}}' ),
38-
],
39-
'show_in_rest' => true,
40-
'rest_base' => '{{slug}}',
41-
'rest_controller_class' => 'WP_REST_Terms_Controller',
42-
] );
1+
register_taxonomy(
2+
'{{slug}}',
3+
array( {{post_types}} ),
4+
array(
5+
'hierarchical' => false,
6+
'public' => true,
7+
'show_in_nav_menus' => true,
8+
'show_ui' => true,
9+
'show_admin_column' => false,
10+
'query_var' => true,
11+
'rewrite' => true,
12+
'capabilities' => array(
13+
'manage_terms' => 'edit_posts',
14+
'edit_terms' => 'edit_posts',
15+
'delete_terms' => 'edit_posts',
16+
'assign_terms' => 'edit_posts',
17+
),
18+
'labels' => array(
19+
'name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ),
20+
'singular_name' => _x( '{{label_ucfirst}}', 'taxonomy general name', '{{textdomain}}' ),
21+
'search_items' => __( 'Search {{label_plural_ucfirst}}', '{{textdomain}}' ),
22+
'popular_items' => __( 'Popular {{label_plural_ucfirst}}', '{{textdomain}}' ),
23+
'all_items' => __( 'All {{label_plural_ucfirst}}', '{{textdomain}}' ),
24+
'parent_item' => __( 'Parent {{label_ucfirst}}', '{{textdomain}}' ),
25+
'parent_item_colon' => __( 'Parent {{label_ucfirst}}:', '{{textdomain}}' ),
26+
'edit_item' => __( 'Edit {{label_ucfirst}}', '{{textdomain}}' ),
27+
'update_item' => __( 'Update {{label_ucfirst}}', '{{textdomain}}' ),
28+
'view_item' => __( 'View {{label_ucfirst}}', '{{textdomain}}' ),
29+
'add_new_item' => __( 'Add New {{label_ucfirst}}', '{{textdomain}}' ),
30+
'new_item_name' => __( 'New {{label_ucfirst}}', '{{textdomain}}' ),
31+
'separate_items_with_commas' => __( 'Separate {{label_plural}} with commas', '{{textdomain}}' ),
32+
'add_or_remove_items' => __( 'Add or remove {{label_plural}}', '{{textdomain}}' ),
33+
'choose_from_most_used' => __( 'Choose from the most used {{label_plural}}', '{{textdomain}}' ),
34+
'not_found' => __( 'No {{label_plural}} found.', '{{textdomain}}' ),
35+
'no_terms' => __( 'No {{label_plural}}', '{{textdomain}}' ),
36+
'menu_name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ),
37+
'items_list_navigation' => __( '{{label_plural_ucfirst}} list navigation', '{{textdomain}}' ),
38+
'items_list' => __( '{{label_plural_ucfirst}} list', '{{textdomain}}' ),
39+
'most_used' => _x( 'Most Used', '{{slug}}', '{{textdomain}}' ),
40+
'back_to_items' => __( '&larr; Back to {{label_plural_ucfirst}}', '{{textdomain}}' ),
41+
),
42+
'show_in_rest' => true,
43+
'rest_base' => '{{slug}}',
44+
'rest_controller_class' => 'WP_REST_Terms_Controller',
45+
)
46+
);

templates/taxonomy_extended.mustache

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
<?php
2+
/**
3+
* Custom taxonomy
4+
*
5+
* @package {{prefix}}
6+
*/
27

38
/**
49
* Registers the `{{machine_name}}` taxonomy,
510
* for use with {{post_types}}.
611
*/
7-
function {{machine_name}}_init() {
12+
function {{prefix}}_init() {
813
{{output}}
914
}
1015

11-
add_action( 'init', '{{machine_name}}_init' );
16+
add_action( 'init', '{{prefix}}_init' );
1217

1318
/**
1419
* Sets the post updated messages for the `{{machine_name}}` taxonomy.
1520
*
1621
* @param array $messages Post updated messages.
1722
* @return array Messages for the `{{machine_name}}` taxonomy.
1823
*/
19-
function {{machine_name}}_updated_messages( $messages ) {
24+
function {{prefix}}_updated_messages( $messages ) {
2025
21-
$messages['{{slug}}'] = [
26+
$messages['{{slug}}'] = array(
2227
0 => '', // Unused. Messages start at index 1.
2328
1 => __( '{{label_ucfirst}} added.', '{{textdomain}}' ),
2429
2 => __( '{{label_ucfirst}} deleted.', '{{textdomain}}' ),
2530
3 => __( '{{label_ucfirst}} updated.', '{{textdomain}}' ),
2631
4 => __( '{{label_ucfirst}} not added.', '{{textdomain}}' ),
2732
5 => __( '{{label_ucfirst}} not updated.', '{{textdomain}}' ),
2833
6 => __( '{{label_plural_ucfirst}} deleted.', '{{textdomain}}' ),
29-
];
34+
);
3035
3136
return $messages;
3237
}
3338

34-
add_filter( 'term_updated_messages', '{{machine_name}}_updated_messages' );
39+
add_filter( 'term_updated_messages', '{{prefix}}_updated_messages' );

0 commit comments

Comments
 (0)