3 WP_CLI::add_command( 'scaffold', 'Scaffold_Command' );
6 * Implement scaffold command
9 * @subpackage commands/internals
10 * @maintainer LinePress (http://www.linespress.org)
12 class Scaffold_Command extends WP_CLI_Command {
14 function __construct() {
19 * @subcommand post-type
23 * @synopsis [--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-name>]
25 function post_type( $args, $assoc_args ) {
26 global $wp_filesystem;
28 // Set the args to variables with normal names to keep our sanity
29 $post_type = strtolower( $args[0] );
31 // We use the machine name for function declarations
32 $machine_name = preg_replace( '/-/', '_', $post_type );
33 $machine_name_plural = $this->pluralize( $post_type );
35 // If no label is given use the slug and prettify it as good as possible
36 if( !isset( $assoc_args['label'] ) ) {
37 $label = preg_replace( '/_|-/', ' ', strtolower( $post_type ) );
38 $label_ucfirst = ucfirst( $label );
39 $label_plural = $this->pluralize( $label );
40 $label_plural_ucfirst = ucfirst( $label_plural );
43 // set up defaults and merge theme with assoc_args
47 'exclude_from_search' => 'false',
49 'show_in_nav_menus' => 'true',
50 'show_in_menu' => 'true',
51 'show_in_admin_bar' => 'true',
52 'menu_position' => 'null',
53 'menu_icon' => 'null',
54 'capability_type' => 'post',
55 'hierarchical' => 'false',
56 'supports' => "'title', 'editor'",
57 'has_archive' => 'true',
58 'slug' => $machine_name_plural,
61 'query_var' => 'true',
62 'can_export' => 'true',
63 'textdomain' => strtolower( wp_get_theme()->template ),
68 // Generate the variables from the defaults and associated arguments if they are set
69 extract( wp_parse_args( $assoc_args, $defaults ), EXTR_SKIP );
71 include "skeletons/post_type_skeleton.php";
72 $assoc_args = array('type' => 'post_type', 'output' => $output, 'theme' => $theme, 'plugin' => $plugin, 'machine_name' => $machine_name, 'path' => false );
74 if ( $theme || !empty( $plugin ) ) {
75 // Write file to theme or (given) plugin path
76 $assoc_args['path'] = $this->get_output_path( $assoc_args );
77 $this->parse_skeleton( $assoc_args );
80 echo $this->parse_skeleton( $assoc_args );
85 * @subcommand taxonomy
89 * @synopsis [--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-name>]
91 function taxonomy( $args, $assoc_args ) {
92 global $wp_filesystem;
94 // Set the args to variables with normal names to keep our sanity
95 $taxonomy = strtolower( $args[0] );
97 // We use the machine name for function declarations
98 $machine_name = preg_replace( '/-/', '_', $taxonomy );
99 $machine_name_plural = $this->pluralize( $taxonomy );
101 // If no label is given use the slug and prettify it as good as possible
102 if( !isset( $assoc_args['label'] ) ) {
103 $label = preg_replace( '/_|-/', ' ', strtolower( $taxonomy ) );
104 $label_ucfirst = ucfirst( $label );
105 $label_plural = $this->pluralize( $label );
106 $label_plural_ucfirst = ucfirst( $label_plural );
109 // Set up defaults and merge theme with assoc_args
112 'show_in_nav_menus' => 'true',
114 'show_tagcloud' => 'true',
115 'hierarchical' => 'false',
117 'query_var' => 'true',
119 'textdomain' => strtolower( wp_get_theme()->template ),
120 'post_types' => 'post',
125 // Generate the variables from the defaults and associated arguments if they are set
126 extract( wp_parse_args( $assoc_args, $defaults ), EXTR_SKIP );
128 include 'skeletons/taxonomy_skeleton.php';
129 $assoc_args = array('type' => 'taxonomy', 'output' => $output, 'theme' => $theme, 'plugin' => $plugin, 'machine_name' => $machine_name, 'path' => false );
131 if ( $theme || !empty( $plugin ) ) {
132 // Write file to theme or (given) plugin path
133 $assoc_args['path'] = $this->get_output_path( $assoc_args );
134 $this->parse_skeleton( $assoc_args );
137 echo $this->parse_skeleton( $assoc_args );
141 private function get_output_path( $assoc_args ) {
142 global $wp_filesystem;
144 extract( $assoc_args, EXTR_SKIP );
146 // Implements the --theme flag || --plugin=<plugin-name>
148 //Here we assume you got a theme installed
149 $path = TEMPLATEPATH;
150 } elseif ( !empty( $plugin ) ){
151 $path = WP_PLUGIN_DIR . '/' . $plugin; //Faking recursive mkdir for down the line
152 $wp_filesystem->mkdir( WP_PLUGIN_DIR . '/' . $plugin ); //Faking recursive mkdir for down the line
158 if ( $type === "post_type") {
159 $path .= '/post-types/';
160 } elseif ( $type === "taxonomy" ) {
161 $path .= '/taxonomies/';
164 // If it doesn't exists create it
165 if( !$wp_filesystem->is_dir( $path ) ) {
166 $wp_filesystem->mkdir( $path );
167 WP_CLI::success( "Created dir: {$path}" );
168 } elseif( $wp_filesystem->is_dir( $path ) ) {
169 WP_CLI::success( "Dir already exists: {$path}" );
171 WP_CLI::error( "Couldn't create dir exists: {$path}" );
177 private function parse_skeleton( $assoc_args = array() ) {
178 global $wp_filesystem;
180 extract( $assoc_args, EXTR_SKIP );
184 $filename = $path . $machine_name .'.php';
186 if ( ! $wp_filesystem->put_contents( $filename, $output ) ) {
187 WP_CLI::error( "Error while saving file: {$filename}" );
189 WP_CLI::success( ucfirst($type) . " {$machine_name} created" );
197 private function pluralize( $word ) {
199 '/(quiz)$/i' => '\1zes',
200 '/^(ox)$/i' => '\1en',
201 '/([m|l])ouse$/i' => '\1ice',
202 '/(matr|vert|ind)ix|ex$/i' => '\1ices',
203 '/(x|ch|ss|sh)$/i' => '\1es',
204 '/([^aeiouy]|qu)ies$/i' => '\1y',
205 '/([^aeiouy]|qu)y$/i' => '\1ies',
206 '/(hive)$/i' => '\1s',
207 '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
209 '/([ti])um$/i' => '\1a',
210 '/(buffal|tomat)o$/i' => '\1oes',
211 '/(bu)s$/i' => '1ses',
212 '/(alias|status)/i' => '\1es',
213 '/(octop|vir)us$/i' => '1i',
214 '/(ax|test)is$/i' => '\1es',
219 $uncountable = array( 'equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep' );
222 'person' => 'people',
225 'child' => 'children',
230 $lowercased_word = strtolower( $word );
232 foreach ( $uncountable as $_uncountable ){
233 if( substr( $lowercased_word, ( -1 * strlen( $_uncountable) ) ) == $_uncountable ){
238 foreach ( $irregular as $_plural=> $_singular ){
239 if ( preg_match( '/('.$_plural.')$/i', $word, $arr ) ) {
240 return preg_replace( '/('.$_plural.')$/i', substr( $arr[0], 0, 1 ).substr( $_singular, 1 ), $word );
244 foreach ( $plural as $rule => $replacement ) {
245 if ( preg_match( $rule, $word ) ) {
246 return preg_replace( $rule, $replacement, $word );