Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cpt and taxonomy scaffold #341

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion features/scaffold-taxonomy.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Feature: Scaffold a custom taxonomy
"""
And STDOUT should contain:
"""
$messages['fungus'] = [
$messages['fungus'] = array(
"""
And STDOUT should contain:
"""
Expand Down
2 changes: 1 addition & 1 deletion features/scaffold.feature
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Feature: WordPress code scaffolding
"""
And STDOUT should contain:
"""
[ 'prefix-zombie', 'wraith' ]
array( 'prefix-zombie', 'wraith' )
"""
And STDOUT should contain:
"""
Expand Down
18 changes: 17 additions & 1 deletion src/Scaffold_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions templates/post_type.mustache
Original file line number Diff line number Diff line change
@@ -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}}' ),
Expand All @@ -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,
Expand All @@ -41,5 +41,5 @@
'show_in_rest' => true,
'rest_base' => '{{slug}}',
'rest_controller_class' => 'WP_REST_Posts_Controller',
]
)
);
25 changes: 15 additions & 10 deletions templates/post_type_extended.mustache
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
<?php
/**
* Custom post type
*
* @package {{prefix}}
*/

/**
* Registers the `{{machine_name}}` post type.
*/
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}}` post type.
*
* @param array $messages Post updated messages.
* @return array Messages for the `{{machine_name}}` post type.
*/
function {{machine_name}}_updated_messages( $messages ) {
function {{prefix}}_updated_messages( $messages ) {
global $post;

$permalink = get_permalink( $post );

$messages['{{slug}}'] = [
$messages['{{slug}}'] = array(
0 => '', // Unused. Messages start at index 1.
/* translators: %s: post permalink */
1 => sprintf( __( '{{label_ucfirst}} updated. <a target="_blank" href="%s">View {{label}}</a>', '{{textdomain}}' ), esc_url( $permalink ) ),
Expand All @@ -38,12 +43,12 @@ function {{machine_name}}_updated_messages( $messages ) {
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 ) ),
/* translators: %s: post permalink */
10 => sprintf( __( '{{label_ucfirst}} draft updated. <a target="_blank" href="%s">Preview {{label}}</a>', '{{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.
Expand All @@ -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}}' ) :
Expand All @@ -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 );
88 changes: 46 additions & 42 deletions templates/taxonomy.mustache
Original file line number Diff line number Diff line change
@@ -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' => __( '&larr; 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' => __( '&larr; Back to {{label_plural_ucfirst}}', '{{textdomain}}' ),
),
'show_in_rest' => true,
'rest_base' => '{{slug}}',
'rest_controller_class' => 'WP_REST_Terms_Controller',
)
);
17 changes: 11 additions & 6 deletions templates/taxonomy_extended.mustache
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
<?php
/**
* Custom taxonomy
*
* @package {{prefix}}
*/

/**
* 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.
*
* @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}}'] = [
$messages['{{slug}}'] = array(
0 => '', // Unused. Messages start at index 1.
1 => __( '{{label_ucfirst}} added.', '{{textdomain}}' ),
2 => __( '{{label_ucfirst}} deleted.', '{{textdomain}}' ),
3 => __( '{{label_ucfirst}} updated.', '{{textdomain}}' ),
4 => __( '{{label_ucfirst}} not added.', '{{textdomain}}' ),
5 => __( '{{label_ucfirst}} not updated.', '{{textdomain}}' ),
6 => __( '{{label_plural_ucfirst}} deleted.', '{{textdomain}}' ),
];
);

return $messages;
}

add_filter( 'term_updated_messages', '{{machine_name}}_updated_messages' );
add_filter( 'term_updated_messages', '{{prefix}}_updated_messages' );
Loading