Merge pull request #265 from wp-cli/scaffold-mustache

Use Mustache for scaffold templates
This commit is contained in:
Cristi Burcă 2013-01-01 04:19:36 -08:00
commit c69a528472
6 changed files with 192 additions and 209 deletions

View file

@ -9,7 +9,8 @@
],
"require": {
"php": ">=5.3",
"wp-cli/php-cli-tools": "dev-master"
"wp-cli/php-cli-tools": "dev-master",
"mustache/mustache": "2.0.x"
},
"require-dev": {
"phpunit/phpunit": "3.7.x",

View file

@ -1,13 +1,9 @@
<?php

WP_CLI::add_command( 'scaffold', 'Scaffold_Command' );

/**
* Implement scaffold command
* Generate code for post types, taxonomies, etc.
*
* @package wp-cli
* @subpackage commands/internals
* @maintainer LinePress (http://www.linespress.org)
*/
class Scaffold_Command extends WP_CLI_Command {

@ -22,29 +18,11 @@ class Scaffold_Command extends WP_CLI_Command {
*
* @alias cpt
*
* @synopsis <slug> [--description=<description>] [--public=<public>] [--exclude_from_search=<exclude_from_search>] [--show_ui=<show_ui>] [--show_in_nav_menus=<show_in_nav_menus>] [--show_in_menu=<show_in_menu>] [--show_in_admin_bar=<show_in_admin_bar>] [--menu_position=<menu_position>] [--menu_icon=<menu_icon>] [--capability_type=<capability_type>] [--hierarchical=<hierarchical>] [--supports=<supports>] [--has_archive=<has_archive>] [--slug=<slug>] [--feed=<feed>] [--pages=<pages>] [--query_var=<query_var>] [--can_export=<can_export>] [--textdomain=<textdomain>] [--theme] [--plugin=<plugin>] [--raw]
* @synopsis <slug> [--singular=<label>] [--description=<description>] [--public=<public>] [--exclude_from_search=<exclude_from_search>] [--show_ui=<show_ui>] [--show_in_nav_menus=<show_in_nav_menus>] [--show_in_menu=<show_in_menu>] [--show_in_admin_bar=<show_in_admin_bar>] [--menu_position=<menu_position>] [--menu_icon=<menu_icon>] [--capability_type=<capability_type>] [--hierarchical=<hierarchical>] [--supports=<supports>] [--has_archive=<has_archive>] [--query_var=<query_var>] [--can_export=<can_export>] [--textdomain=<textdomain>] [--theme] [--plugin=<plugin>] [--raw]
*/
function post_type( $args, $assoc_args ) {
global $wp_filesystem;

// Set the args to variables with normal names to keep our sanity
$post_type = strtolower( $args[0] );

// We use the machine name for function declarations
$machine_name = preg_replace( '/-/', '_', $post_type );
$machine_name_plural = $this->pluralize( $post_type );

// If no label is given use the slug and prettify it as good as possible
if ( ! isset( $assoc_args['label'] ) ) {
$label = preg_replace( '/_|-/', ' ', strtolower( $post_type ) );
$label_ucfirst = ucfirst( $label );
$label_plural = $this->pluralize( $label );
$label_plural_ucfirst = ucfirst( $label_plural );
}

// set up defaults and merge theme with assoc_args
$defaults = array(
'description' => "",
'description' => '',
'public' => 'true',
'exclude_from_search' => 'false',
'show_ui' => 'true',
@ -57,45 +35,16 @@ class Scaffold_Command extends WP_CLI_Command {
'hierarchical' => 'false',
'supports' => "'title', 'editor'",
'has_archive' => 'true',
'slug' => $machine_name_plural,
'feeds' => 'true',
'pages' => 'true',
'rewrite' => 'true',
'query_var' => 'true',
'can_export' => 'true',
'textdomain' => '',
'theme' => false,
'plugin' => false,
'raw' => false,
);

// Generate the variables from the defaults and associated arguments if they are set
extract( wp_parse_args( $assoc_args, $defaults ), EXTR_SKIP );

$textdomain = $this->get_textdomain( $textdomain, $theme, $plugin );

if ( ! $raw ) {
include WP_CLI_ROOT . '/templates/post_type.php';
$output = str_replace( "<?php", "", $output );
include WP_CLI_ROOT . '/templates/post_type_extended.php';
} else {
include WP_CLI_ROOT . '/templates/post_type.php';
}

if ( $theme || ! empty( $plugin ) ) {
// Write file to theme or given plugin
$assoc_args = array(
'type' => 'post_type',
'output' => $output,
'theme' => $theme,
'plugin' => $plugin,
'machine_name' => $machine_name,
);
$assoc_args['path'] = $this->get_output_path( $assoc_args );
$this->save_skeleton_output( $assoc_args );
} else {
// STDOUT
echo $output;
}
$this->_scaffold( $args[0], $assoc_args, $defaults, array(
'post_type.php',
'post_type_extended.php'
) );
}

/**
@ -105,27 +54,9 @@ class Scaffold_Command extends WP_CLI_Command {
*
* @alias tax
*
* @synopsis <slug> [--public=<public>] [--show_in_nav_menus=<show_in_nav_menus>] [--show_ui=<show_ui>] [--show_tagcloud=<show_tagcloud>] [--hierarchical=<hierarchical>] [--rewrite=<rewrite>] [--query_var=<query_var>] [--slug=<slug>] [--textdomain=<textdomain>] [--post_types=<post_types>] [--theme] [--plugin=<plugin>] [--raw]
* @synopsis <slug> [--singular=<label>] [--public=<public>] [--show_in_nav_menus=<show_in_nav_menus>] [--show_ui=<show_ui>] [--show_tagcloud=<show_tagcloud>] [--hierarchical=<hierarchical>] [--rewrite=<rewrite>] [--query_var=<query_var>] [--textdomain=<textdomain>] [--post_types=<post_types>] [--theme] [--plugin=<plugin>] [--raw]
*/
function taxonomy( $args, $assoc_args ) {
global $wp_filesystem;

// Set the args to variables with normal names to keep our sanity
$taxonomy = strtolower( $args[0] );

// We use the machine name for function declarations
$machine_name = preg_replace( '/-/', '_', $taxonomy );
$machine_name_plural = $this->pluralize( $taxonomy );

// If no label is given use the slug and prettify it as good as possible
if ( ! isset( $assoc_args['label'] ) ) {
$label = preg_replace( '/_|-/', ' ', strtolower( $taxonomy ) );
$label_ucfirst = ucfirst( $label );
$label_plural = $this->pluralize( $label );
$label_plural_ucfirst = ucfirst( $label_plural );
}

// Set up defaults and merge theme with assoc_args
$defaults = array(
'public' => 'true',
'show_in_nav_menus' => 'true',
@ -134,7 +65,6 @@ class Scaffold_Command extends WP_CLI_Command {
'hierarchical' => 'false',
'rewrite' => 'true',
'query_var' => 'true',
'slug' => $taxonomy,
'post_types' => 'post',
'textdomain' => '',
'theme' => false,
@ -142,24 +72,65 @@ class Scaffold_Command extends WP_CLI_Command {
'raw' => false,
);

// Generate the variables from the defaults and associated arguments if they are set
extract( wp_parse_args( $assoc_args, $defaults ), EXTR_SKIP );
$this->_scaffold( $args[0], $assoc_args, $defaults, array(
'taxonomy.php',
'taxonomy_extended.php'
) );
}

$textdomain = $this->get_textdomain( $textdomain, $theme, $plugin );
private function _scaffold( $slug, $assoc_args, $defaults, $templates ) {
global $wp_filesystem;

$control_args = $this->extract_args( $assoc_args, array(
'theme' => false,
'plugin' => false,
'raw' => false,
) );

$vars = $this->extract_args( $assoc_args, $defaults );

$vars['slug'] = $slug;

$vars['textdomain'] = $this->get_textdomain( $vars['textdomain'], $control_args );

// If no label is given use the slug and prettify it as good as possible
if ( isset( $assoc_args['singular'] ) ) {
$vars['label'] = $assoc_args['singular'];
} else {
$vars['label'] = preg_replace( '/_|-/', ' ', strtolower( $slug ) );
}

$vars['label_ucfirst'] = ucfirst( $vars['label'] );
$vars['label_plural'] = $this->pluralize( $vars['label'] );
$vars['label_plural_ucfirst'] = ucfirst( $vars['label_plural'] );

// We use the machine name for function declarations
$machine_name = preg_replace( '/-/', '_', $slug );
$machine_name_plural = $this->pluralize( $slug );

list( $raw_template, $extended_template ) = $templates;

$raw_output = $this->render( $raw_template, $vars );
$raw_output = str_replace( "<?php", '', $raw_output );

extract( $control_args );

if ( ! $raw ) {
include WP_CLI_ROOT . '/templates/taxonomy.php';
$output = str_replace( "<?php", "", $output );
include WP_CLI_ROOT . '/templates/taxonomy_extended.php';
$vars = array_merge( $vars, array(
'machine_name' => $machine_name,
'output' => $raw_output
) );

$final_output = $this->render( $extended_template, $vars );
} else {
include WP_CLI_ROOT . '/templates/taxonomy.php';
$final_output = $raw_output;
}

if ( $theme || ! empty( $plugin ) ) {
// Write file to theme or given plugin
// Write file to theme or plugin dir
$assoc_args = array(
'type' => 'taxonomy',
'output' => $output,
'type' => 'post_type',
'output' => $final_output,
'theme' => $theme,
'plugin' => $plugin,
'machine_name' => $machine_name,
@ -168,7 +139,7 @@ class Scaffold_Command extends WP_CLI_Command {
$this->save_skeleton_output( $assoc_args );
} else {
// STDOUT
echo $output;
echo $final_output;
}
}

@ -229,16 +200,17 @@ class Scaffold_Command extends WP_CLI_Command {
* If you're writing your files to your theme directory your textdomain also needs to be the same as your theme.
* Same goes for when plugin is being used.
*/
private function get_textdomain( $textdomain, $theme, $plugin ) {
if ( empty( $textdomain ) && $theme ) {
$textdomain = strtolower( wp_get_theme()->template );
} elseif ( empty( $textdomain ) && $plugin ) {
$textdomain = $plugin;
} elseif ( empty( $textdomain ) || gettype( $textdomain ) == 'boolean' ) { //This mean just a flag
$textdomain = 'YOUR-TEXTDOMAIN';
}
private function get_textdomain( $textdomain, $args ) {
if ( strlen( $textdomain ) )
return $textdomain;

return $textdomain;
if ( $args['theme'] )
return strtolower( wp_get_theme()->template );

if ( $args['plugin'] && true !== $args['plugin'] )
return $args['plugin'];

return 'YOUR-TEXTDOMAIN';
}

private function pluralize( $word ) {
@ -295,4 +267,29 @@ class Scaffold_Command extends WP_CLI_Command {
}
return false;
}

protected function extract_args( $assoc_args, $defaults ) {
$out = array();

foreach ( $defaults as $key => $value ) {
$out[ $key ] = isset( $assoc_args[ $key ] )
? $assoc_args[ $key ]
: $value;
}

return $out;
}

private function render( $template, $data ) {
$scaffolds_dir = WP_CLI_ROOT . 'templates';

$template = file_get_contents( $scaffolds_dir . '/' . $template );

$m = new Mustache_Engine;

return $m->render( $template, $data );
}
}

WP_CLI::add_command( 'scaffold', 'Scaffold_Command' );


View file

@ -1,38 +1,34 @@
<?php
$output = "<?php
register_post_type( '{$post_type}',
array(
'label' => __( '{$label_plural_ucfirst}', '{$textdomain}' ),
'description' => __( '{$description}', '{$textdomain}' ),
'public' => {$public},
'exclude_from_search' => {$exclude_from_search},
'show_ui' => {$show_ui},
'show_in_nav_menus' => {$show_in_nav_menus},
'show_in_menu' => {$show_in_menu},
'show_in_admin_bar' => {$show_in_admin_bar},
'menu_position' => {$menu_position},
'menu_icon' => {$menu_icon},
'capability_type' => '{$capability_type}',
'hierarchical' => {$hierarchical},
'supports' => array( {$supports} ),
'has_archive' => {$has_archive},
'rewrite' => array( 'slug' => '{$slug}', 'feeds' => {$feeds}, 'pages' => {$pages} ),
'query_var' => {$query_var},
'can_export' => {$can_export},
'labels' => array(
'name' => __( '{$label_plural_ucfirst}', '{$textdomain}' ),
'singular_name' => __( '{$label_ucfirst}', '{$textdomain}' ),
'add_new' => __( 'Add new {$label}', '{$textdomain}' ),
'all_items' => __( '{$label_plural_ucfirst}', '{$textdomain}' ),
'add_new_item' => __( 'Add new {$label}', '{$textdomain}' ),
'edit_item' => __( 'Edit {$label}', '{$textdomain}' ),
'new_item' => __( 'New {$label}', '{$textdomain}' ),
'view_item' => __( 'View {$label}', '{$textdomain}' ),
'search_items' => __( 'Search {$label_plural}', '{$textdomain}' ),
'not_found' => __( 'No {$label_plural} found', '{$textdomain}' ),
'not_found_in_trash' => __( 'No {$label_plural} found in trash', '{$textdomain}' ),
'parent_item_colon' => __( 'Parent {$label}', '{$textdomain}' ),
'menu_name' => __( '{$label_plural_ucfirst}', '{$textdomain}' ),
),
)
);";
register_post_type( '{{slug}}', array(
'description' => __( '{{description}}', '{{textdomain}}' ),
'public' => {{public}},
'exclude_from_search' => {{exclude_from_search}},
'show_ui' => {{show_ui}},
'show_in_nav_menus' => {{show_in_nav_menus}},
'show_in_menu' => {{show_in_menu}},
'show_in_admin_bar' => {{show_in_admin_bar}},
'menu_position' => {{menu_position}},
'menu_icon' => {{menu_icon}},
'capability_type' => '{{capability_type}}',
'hierarchical' => {{hierarchical}},
'supports' => array( {{supports}} ),
'has_archive' => {{has_archive}},
'rewrite' => {{rewrite}},
'query_var' => {{query_var}},
'can_export' => {{can_export}},
'labels' => array(
'name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ),
'singular_name' => __( '{{label_ucfirst}}', '{{textdomain}}' ),
'add_new' => __( 'Add new {{label}}', '{{textdomain}}' ),
'all_items' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ),
'add_new_item' => __( 'Add new {{label}}', '{{textdomain}}' ),
'edit_item' => __( 'Edit {{label}}', '{{textdomain}}' ),
'new_item' => __( 'New {{label}}', '{{textdomain}}' ),
'view_item' => __( 'View {{label}}', '{{textdomain}}' ),
'search_items' => __( 'Search {{label_plural}}', '{{textdomain}}' ),
'not_found' => __( 'No {{label_plural}} found', '{{textdomain}}' ),
'not_found_in_trash' => __( 'No {{label_plural}} found in trash', '{{textdomain}}' ),
'parent_item_colon' => __( 'Parent {{label}}', '{{textdomain}}' ),
'menu_name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ),
),
) );

View file

@ -1,32 +1,32 @@
<?php
$output = "<?php

function {$machine_name}_init() {
{$output}
function {{machine_name}}_init() {
{{{output}}}
}
add_action( 'init', '{$machine_name}_init' );
add_action( 'init', '{{machine_name}}_init' );

function {$machine_name}_updated_messages( \$messages ) {
global \$post, \$post_ID;
function {{machine_name}}_updated_messages( $messages ) {
global $post;

\$messages['{$post_type}'] = array(
0 => '', // Unused. Messages start at index 1.
1 => sprintf( __('{$label_ucfirst} updated. <a target=\"_blank\" href=\"%s\">View {$label}</a>', '{$textdomain}'), esc_url( get_permalink(\$post_ID) ) ),
2 => __('Custom field updated.', '{$textdomain}'),
3 => __('Custom field deleted.', '{$textdomain}'),
4 => __('{$label_ucfirst} updated.', '{$textdomain}'),
/* translators: %s: date and time of the revision */
5 => isset(\$_GET['revision']) ? sprintf( __('{$label_ucfirst} restored to revision from %s', '{$textdomain}'), wp_post_revision_title( (int) \$_GET['revision'], false ) ) : false,
6 => sprintf( __('{$label_ucfirst} published. <a href=\"%s\">View {$label}</a>', '{$textdomain}'), esc_url( get_permalink(\$post_ID) ) ),
7 => __('{$label_ucfirst} saved.', '{$textdomain}'),
8 => sprintf( __('{$label_ucfirst} submitted. <a target=\"_blank\" href=\"%s\">Preview {$post_type}</a>', '{$textdomain}'), esc_url( add_query_arg( 'preview', 'true', get_permalink(\$post_ID) ) ) ),
9 => sprintf( __('{$label_ucfirst} scheduled for: <strong>%1\$s</strong>. <a target=\"_blank\" href=\"%2\$s\">Preview {$label}</a>', '{$textdomain}'),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i' ), strtotime( \$post->post_date ) ), esc_url( get_permalink( \$post_ID ) ) ),
10 => sprintf( __('{$label_ucfirst} draft updated. <a target=\"_blank\" href=\"%s\">Preview {$post_type}</a>', '{$textdomain}'), esc_url( add_query_arg( 'preview', 'true', get_permalink( \$post_ID ) ) ) ),
);
$permalink = get_permalink( $post );

return \$messages;
$messages['{{slug}}'] = array(
0 => '', // Unused. Messages start at index 1.
1 => sprintf( __('{{label_ucfirst}} updated. <a target=\"_blank\" href=\"%s\">View {{label}}</a>', '{{textdomain}}'), esc_url( $permalink ) ),
2 => __('Custom field updated.', '{{textdomain}}'),
3 => __('Custom field deleted.', '{{textdomain}}'),
4 => __('{{label_ucfirst}} updated.', '{{textdomain}}'),
/* translators: %s: date and time of the revision */
5 => isset($_GET['revision']) ? sprintf( __('{{label_ucfirst}} restored to revision from %s', '{{textdomain}}'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('{{label_ucfirst}} published. <a href=\"%s\">View {{label}}</a>', '{{textdomain}}'), esc_url( $permalink ) ),
7 => __('{{label_ucfirst}} saved.', '{{textdomain}}'),
8 => sprintf( __('{{label_ucfirst}} submitted. <a target=\"_blank\" href=\"%s\">Preview {{label}}</a>', '{{textdomain}}'), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ),
9 => sprintf( __('{{label_ucfirst}} scheduled for: <strong>%1$s</strong>. <a target=\"_blank\" href=\"%2$s\">Preview {{label}}</a>', '{{textdomain}}'),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( $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', '{{machine_name}}_updated_messages' );

View file

@ -1,43 +1,34 @@
<?php
$output = "<?php
\$labels = array(
'name' => __( '{$label_plural_ucfirst}', '{$textdomain}' ),
'singular_name' => __( '{$label_ucfirst}', '{$textdomain}' ),
'search_items' => __( 'Search {$taxonomy}', '{$textdomain}' ),
'popular_items' => __( 'Popular {$taxonomy}', '{$textdomain}' ),
'all_items' => __( 'All {$label_plural}', '{$textdomain}' ),
'parent_item' => __( 'Parent {$label}', '{$textdomain}' ),
'parent_item_colon' => __( 'Parent {$label}:', '{$textdomain}' ),
'edit_item' => __( 'Edit {$label}', '{$textdomain}' ),
'update_item' => __( 'Update {$label}', '{$textdomain}' ),
'add_new_item' => __( 'New {$label}', '{$textdomain}' ),
'new_item_name' => __( 'New {$label}', '{$textdomain}' ),
'separate_items_with_commas' => __( '{$label_plural_ucfirst} seperated by comma', '{$textdomain}' ),
'add_or_remove_items' => __( 'Add or remove {$label}', '{$textdomain}' ),
'choose_from_most_used' => __( 'Choose from the most used {$label_plural}', '{$textdomain}' ),
'menu_name' => __( '{$label_plural_ucfirst}', '{$textdomain}' ),
);

\$args = array(
'labels' => \$labels,
'public' => {$public},
'show_in_nav_menus' => {$show_in_nav_menus},
'show_ui' => {$show_ui},
'show_tagcloud' => {$show_tagcloud},
'hierarchical' => {$hierarchical},
'update_count_callback' => '_update_post_term_count',
'rewrite' => {$rewrite},
'query_var' => {$query_var},
'capabilities' => array (
'manage_terms' => 'edit_posts',
'edit_terms' => 'edit_posts',
'delete_terms' => 'edit_posts',
'assign_terms' => 'edit_posts'
),
'rewrite' => array(
'slug' => '{$taxonomy}',
'hierarchical' => {$hierarchical}
),
);
register_taxonomy( '{$taxonomy}', array( '{$post_types}' ), \$args );";
register_taxonomy( '{{slug}}', array( '{{post_types}}' ), array(
'public' => {{public}},
'show_in_nav_menus' => {{show_in_nav_menus}},
'show_ui' => {{show_ui}},
'show_tagcloud' => {{show_tagcloud}},
'hierarchical' => {{hierarchical}},
'update_count_callback' => '_update_post_term_count',
'query_var' => {{query_var}},
'rewrite' => {{rewrite}},
'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' => __( '{{label_ucfirst}}', '{{textdomain}}' ),
'search_items' => __( 'Search {{label_plural}}', '{{textdomain}}' ),
'popular_items' => __( 'Popular {{label_plural}}', '{{textdomain}}' ),
'all_items' => __( 'All {{label_plural}}', '{{textdomain}}' ),
'parent_item' => __( 'Parent {{label}}', '{{textdomain}}' ),
'parent_item_colon' => __( 'Parent {{label}}:', '{{textdomain}}' ),
'edit_item' => __( 'Edit {{label}}', '{{textdomain}}' ),
'update_item' => __( 'Update {{label}}', '{{textdomain}}' ),
'add_new_item' => __( 'New {{label}}', '{{textdomain}}' ),
'new_item_name' => __( 'New {{label}}', '{{textdomain}}' ),
'separate_items_with_commas' => __( '{{label_plural_ucfirst}} separated by comma', '{{textdomain}}' ),
'add_or_remove_items' => __( 'Add or remove {{label_plural}}', '{{textdomain}}' ),
'choose_from_most_used' => __( 'Choose from the most used {{label_plural}}', '{{textdomain}}' ),
'menu_name' => __( '{{label_plural_ucfirst}}', '{{textdomain}}' ),
),
) );

View file

@ -1,8 +1,6 @@
<?php
$output = "<?php

function {$machine_name}_init() {
{$output}

function {{machine_name}}_init() {
{{{output}}}
}
add_action( 'init', '{$machine_name}_init' );";
add_action( 'init', '{{machine_name}}_init' );