OSDN Git Service

Implemented the STDOUT --theme flag and or --plugin="" for taxonomies
[wvm/wvm.git] / src / php / wp-cli / commands / internals / scaffold.php
1 <?php
2
3 WP_CLI::add_command( 'scaffold', 'Scaffold_Command' );
4
5 /**
6  * Implement scaffold command
7  *
8  * @package wp-cli
9  * @subpackage commands/internals
10  * @maintainer LinePress (http://www.linespress.org)
11  */
12 class Scaffold_Command extends WP_CLI_Command {
13   
14   function __construct() {
15     WP_Filesystem();
16   }
17
18   /**
19    * @subcommand post-type
20    *
21    * @alias pt
22    *
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>]
24    */
25   function post_type( $args, $assoc_args ) {
26     global $wp_filesystem;
27
28     // Set the args to variables with normal names to keep our sanity
29     $post_type                = strtolower( $args[0] );
30
31     // We use the machine name for function declarations 
32     $machine_name             = preg_replace( '/-/', '_', $post_type );
33     $machine_name_plural      = $this->pluralize( $post_type );
34
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 );
41     }
42     
43     // set up defaults and merge theme with assoc_args
44     $defaults = array(
45       'description'         => "",
46       'public'              => 'true',
47       'exclude_from_search' => 'false',
48       'show_ui'             => 'true',
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,
59       'feeds'               => 'true',
60       'pages'               => 'true',
61       'query_var'           => 'true',
62       'can_export'          => 'true',
63       'textdomain'          => strtolower( wp_get_theme()->template ),
64       'theme'               => false,
65       'plugin'              => false
66     );
67
68     // Generate the variables from the defaults and associated arguments if they are set
69     extract( wp_parse_args( $assoc_args, $defaults ), EXTR_SKIP );
70     
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 );
73     
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 );
78     } else {
79       // STDOUT
80       echo $this->parse_skeleton( $assoc_args );
81     }
82   }
83
84   /**
85    * @subcommand taxonomy
86    *
87    * @alias tax
88    *
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>]
90    */
91   function taxonomy( $args, $assoc_args ) {
92     global $wp_filesystem;
93
94     // Set the args to variables with normal names to keep our sanity
95     $taxonomy       = strtolower( $args[0] );
96
97     // We use the machine name for function declarations 
98     $machine_name             = preg_replace( '/-/', '_', $taxonomy );
99     $machine_name_plural      = $this->pluralize( $taxonomy );
100
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 );
107     }
108
109     // Set up defaults and merge theme with assoc_args
110     $defaults = array(
111       'public'              => 'true',
112       'show_in_nav_menus'   => 'true',
113       'show_ui'             => 'true',
114       'show_tagcloud'       => 'true',
115       'hierarchical'        => 'false',
116       'rewrite'             => 'true',
117       'query_var'           => 'true',
118       'slug'                => $taxonomy,
119       'textdomain'          => strtolower( wp_get_theme()->template ),
120       'post_types'          => 'post',
121       'theme'               => false,
122       'plugin'              => false
123     );
124     
125     // Generate the variables from the defaults and associated arguments if they are set
126     extract( wp_parse_args( $assoc_args, $defaults ), EXTR_SKIP );
127
128     include 'skeletons/taxonomy_skeleton.php';
129     $assoc_args = array('type' => 'taxonomy', 'output' => $output, 'theme' => $theme, 'plugin' => $plugin, 'machine_name' => $machine_name, 'path' => false );
130     
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 );
135     } else {
136       // STDOUT
137       echo $this->parse_skeleton( $assoc_args );
138     }
139   }
140
141   private function get_output_path( $assoc_args ) {
142     global $wp_filesystem;
143
144     extract( $assoc_args, EXTR_SKIP );
145
146     // Implements the --theme flag || --plugin=<plugin-name>
147     if( $theme ) {
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
153     } else {
154       // STDOUT
155       return false;
156     }
157
158     if ( $type === "post_type") {
159       $path .= '/post-types/';
160     } elseif ( $type === "taxonomy" ) {
161       $path .= '/taxonomies/';
162     }
163
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}" );
170     } else {
171       WP_CLI::error( "Couldn't create dir exists: {$path}" );
172     }
173
174     return $path;
175   }
176
177   private function parse_skeleton( $assoc_args = array() ) {
178     global $wp_filesystem;
179
180     extract( $assoc_args, EXTR_SKIP );
181
182     // Write to file
183     if( $path ) {
184       $filename = $path . $machine_name .'.php';
185
186       if ( ! $wp_filesystem->put_contents( $filename, $output ) ) {
187         WP_CLI::error( "Error while saving file: {$filename}" );
188       } else {
189         WP_CLI::success( ucfirst($type) . " {$machine_name} created" );
190       }
191     } else {
192       // Return for STDOUT
193       return $output;
194     }
195   }
196
197   private function pluralize( $word ) {
198     $plural = array(
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',
208       '/sis$/i'                   => 'ses',
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',
215       '/s$/i'                     => 's',
216       '/$/'                       => 's'
217     );
218
219     $uncountable = array( 'equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep' );
220
221     $irregular = array(
222       'person'    => 'people',
223       'man'       => 'men',
224       'woman'     => 'women',
225       'child'     => 'children',
226       'sex'       => 'sexes',
227       'move'      => 'moves'
228     );
229
230     $lowercased_word = strtolower( $word );
231
232     foreach ( $uncountable as $_uncountable ){
233       if( substr( $lowercased_word, ( -1 * strlen( $_uncountable) ) ) == $_uncountable ){
234         return $word;
235       }
236     }
237
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 );
241       }
242     }
243
244     foreach ( $plural as $rule => $replacement ) {
245       if ( preg_match( $rule, $word ) ) {
246         return preg_replace( $rule, $replacement, $word );
247       }
248     }
249     return false;
250   }
251 }