Skip to content

Commit

Permalink
Add a plugin generator
Browse files Browse the repository at this point in the history
Add a new generator to clone code in our WordPress plugin repo:

https://github.com/dxw/wordpress-plugin-template
  • Loading branch information
snim2 committed Sep 18, 2023
1 parent 90d4462 commit f36ba8f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Support for PHP 8
- `whippet generate plugin` to generate a plugin based on our template repo https://github.com/dxw/wordpress-plugin-template/

## [2.3.0] - 2023-06-14
### Added
Expand Down
14 changes: 14 additions & 0 deletions docs/generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,17 @@ This will generate a new Whippet-compliant WordPress theme in `./whippet-theme`.
You can change the location with the `-d` option.

The generated theme is based on the theme in the [dxw WordPress template](https://github.com/dxw/wordpress-template/).

## Generating a Whippet plugin

To create a new Whippet plugin, run:

```
$ whippet generate plugin
```

This will generate a new Whippet-compliant WordPress plugin in `./whippet-plugin`.

You can change the location with the `-d` option.

The generated plugin is based on code in the [dxw WordPress plugin template](https://github.com/dxw/wordpress-plugin-template/).
51 changes: 51 additions & 0 deletions generators/plugin/generate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use Dxw\Whippet\Git\Git;

class PluginGenerator extends \Dxw\Whippet\WhippetGenerator {
use \Dxw\Whippet\Modules\Helpers\WhippetHelpers;

protected $plugin_template_zip = 'https://github.com/dxw/wordpress-plugin-template/archive/main.zip';

private $unique_temp_id;

function __construct($options) {
$this->options = $options;

if(isset($this->options->directory)) {
$this->target_dir = getcwd() . '/' . $this->options->directory;
}
else {
$this->target_dir = getcwd() . "/whippet-plugin";
}

$this->unique_temp_id = uniqid();
}

function generate() {
echo "Creating a new whippet plugin in {$this->target_dir}\n";

if(!file_exists($this->target_dir)) {
mkdir($this->target_dir);
}

$this->downloadAndUnzipTemplate();
$this->copyThemeAndRemoveTemplate();
}

private function downloadAndUnzipTemplate()
{
$this->download_url_to_file($this->plugin_template_zip, '/tmp/plugin_template_' . $this->unique_temp_id . '.zip');
$this->unzip_to_folder('/tmp/plugin_template_' . $this->unique_temp_id . '.zip', '/tmp/plugin_template_' . $this->unique_temp_id);
}

private function copyThemeAndRemoveTemplate()
{
$this->recurse_copy('/tmp/plugin_template_' . $this->unique_temp_id . '/wordpress-plugin-template-main', $this->target_dir);
copy('/tmp/plugin_template_' . $this->unique_temp_id . '/wordpress-plugin-template-main/.gitignore', $this->target_dir . '/.gitignore');
if(isset($this->options->nogitignore)) {
unlink($this->target_dir . '/.gitignore');
}
$this->recurse_rm('/tmp/plugin_template_' . $this->unique_temp_id);
}
};

0 comments on commit f36ba8f

Please sign in to comment.