diff --git a/README.md b/README.md index a476fe5..571907d 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,23 @@ # WordPress Plugin Yeoman Generator -With this Yeoman Generator, it is possible to easily scaffolding a WordPress plugin +With this Yeoman Generator, it is possible to easily scaffold a WordPress plugin following the netzstrategen coding standards. ## Installation -- `npm install -g yo` -- `npm install -g generator-netz-wordpress-plugin` +``` +$ npm install -g yo generator-netz-wordpress-plugin +``` -## Use -- `yo netz-wordpress-plugin` +## Usage +``` +$ cd wp-content/plugins +$ mkdir custom +$ cd custom +$ yo netz-wordpress-plugin +``` +The files are generated in the current working directory. ## Features -- The generated plugin can optionally have assets. In that case, [gulp-task-collection](https://github.com/netzstrategen/gulp-task-collection) -NPM package is installed. Also, netzstrategen ESLint and Stylelint standards are included. -- It's possible to automatically create frequently used PHP Classes (e.g. `Admin`, `WooCommerce`, `CliCommand`). -- A Travis configuration file is automatically generated. It makes the plugin ready to be tested. +- If the generated plugin supports assets, the [gulp-task-collection](https://github.com/netzstrategen/gulp-task-collection) + package is installed and the netzstrategen ESLint and Stylelint rules are included. +- Frequently used PHP classes can be automatically created (e.g. `Admin`, `WooCommerce`, `CliCommand`). +- The automatically genereated Travis configuration file makes the plugin ready to be tested. diff --git a/generators/app/index.js b/generators/app/index.js index 7cb9ded..e351183 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -104,7 +104,7 @@ module.exports = yeoman.generators.Base.extend({ function camelize (string) { return string.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) { return word.toUpperCase(); - }).replace(/\s+/g, ''); + }).replace(/\s+_-/g, ''); } this.namespace = camelize(answers.name); }.bind(this)); @@ -220,6 +220,12 @@ module.exports = yeoman.generators.Base.extend({ includeAdmin: this.includeAdmin } ); + this.fs.copyTpl( + this.templatePath('languages/_plugin.pot'), + this.destinationPath('languages/' + this.props.name.replace(/\s+/g, '-').toLowerCase() + '.pot'), { + name: this.props.name.replace(/\s+/g, '-').toLowerCase() + } + ); this.fs.copyTpl( this.templatePath('src/_Plugin.php'), this.destinationPath('src/Plugin.php'), { diff --git a/generators/app/templates/_plugin.php b/generators/app/templates/_plugin.php index 13f00d5..75857c8 100644 --- a/generators/app/templates/_plugin.php +++ b/generators/app/templates/_plugin.php @@ -39,7 +39,7 @@ function classloader($class) { register_uninstall_hook(__FILE__, __NAMESPACE__ . '\Schema::uninstall'); add_action('plugins_loaded', __NAMESPACE__ . '\Plugin::loadTextdomain'); -add_action('init', __NAMESPACE__ . '\Plugin::init', 20); +add_action('init', __NAMESPACE__ . '\Plugin::init'); <% if (includeAdmin) { %>add_action('admin_init', __NAMESPACE__ . '\Admin::init');<% } %><% if (includeAdmin) { %> if (defined('WP_CLI') && WP_CLI) { diff --git a/generators/app/templates/languages/_plugin.pot b/generators/app/templates/languages/_plugin.pot new file mode 100644 index 0000000..8a47a16 --- /dev/null +++ b/generators/app/templates/languages/_plugin.pot @@ -0,0 +1,17 @@ +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: <%= name %>\n" +"POT-Creation-Date: 2010-02-10 09:00+0200\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Generator: Poedit 2.2.1\n" +"X-Poedit-Basepath: ..\n" +"X-Poedit-SourceCharset: UTF-8\n" +"X-Poedit-KeywordsList: __;_e;__ngettext:1,2;_n:1,2;__ngettext_noop:1,2;_n_noop:1,2;_c,_nc:4c,1,2;_x:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;_ex:1,2c;esc_attr__;esc_attr_e;esc_attr_x:1,2c;esc_html__;esc_html_e;esc_html_x:1,2c\n" +"X-Poedit-SearchPath-0: .\n" +"X-Poedit-SearchPathExcluded-0: .sass-cache\n" +"X-Poedit-SearchPathExcluded-1: node_modules\n" +"X-Poedit-SearchPathExcluded-2: vendor\n" diff --git a/generators/app/templates/src/_Admin.php b/generators/app/templates/src/_Admin.php index 44cb47d..703108f 100644 --- a/generators/app/templates/src/_Admin.php +++ b/generators/app/templates/src/_Admin.php @@ -18,7 +18,6 @@ class Admin { * @implements admin_init */ public static function init() { - } } diff --git a/generators/app/templates/src/_CliCommand.php b/generators/app/templates/src/_CliCommand.php index 52f49c4..6c0a621 100644 --- a/generators/app/templates/src/_CliCommand.php +++ b/generators/app/templates/src/_CliCommand.php @@ -7,10 +7,12 @@ namespace Netzstrategen\<%= namespace %>; +use WP_CLI_Command; + /** - * WP CLI commands. + * WP-CLI commands. */ -class CliCommand { +class CliCommand extends WP_CLI_Command { } diff --git a/generators/app/templates/src/_Plugin.php b/generators/app/templates/src/_Plugin.php index 0ceafbb..912c240 100644 --- a/generators/app/templates/src/_Plugin.php +++ b/generators/app/templates/src/_Plugin.php @@ -26,6 +26,11 @@ class Plugin { */ const L10N = self::PREFIX; + /** + * @var string + */ + private static $baseUrl; + /** * Plugin initialization method. * @@ -44,4 +49,25 @@ public static function loadTextdomain() { load_plugin_textdomain(static::L10N, FALSE, static::L10N . '/languages/'); } + /** + * The absolute filesystem base path of this plugin. + * + * @return string + */ + public static function getBasePath() { + return dirname(__DIR__); + } + + /** + * The base URL path to this plugin's folder. + * + * Uses plugins_url() instead of plugin_dir_url() to avoid a trailing slash. + */ + public static function getBaseUrl() { + if (!isset(static::$baseUrl)) { + static::$baseUrl = plugins_url('', static::getBasePath() . '/plugin.php'); + } + return static::$baseUrl; + } + }