Skip to content

Commit

Permalink
Update scaffold cpt
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Jun 11, 2024
1 parent 7a7d145 commit 480d4fa
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
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'];
} 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 {
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 );
8 changes: 4 additions & 4 deletions templates/taxonomy_extended.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
* 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}}'] = [
0 => '', // Unused. Messages start at index 1.
Expand All @@ -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' );

0 comments on commit 480d4fa

Please sign in to comment.