Skip to content

Commit

Permalink
register with core;CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
leonstafford committed Nov 4, 2020
1 parent 0073578 commit 6343b8e
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## WP2Static Netlify Add-on 1.0-alpha-007

- Get add-on registered with WP2Static core

## WP2Static Netlify Add-on < 1.0-alpha-007

- didn't maintain Changelog or use tags, please review version control if curious

100 changes: 100 additions & 0 deletions src/CLI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace WP2StaticNetlify;

use WP_CLI;

/**
* WP2StaticNetlify WP-CLI commands
*
* Registers WP-CLI commands for WP2StaticNetlify under main wp2static cmd
*
* Usage: wp wp2static options set siteID mysiteid
*/
class CLI {

/**
* Netlify add-on commands
*
* @param string[] $args CLI args
* @param string[] $assoc_args CLI args
*/
public function netlify(
array $args,
array $assoc_args
) : void {
$action = isset( $args[0] ) ? $args[0] : null;
$arg = isset( $args[1] ) ? $args[1] : null;

if ( empty( $action ) ) {
WP_CLI::error( 'Missing required argument: <options>' );
}

if ( $action === 'options' ) {
if ( empty( $arg ) ) {
WP_CLI::error( 'Missing required argument: <get|set|list>' );
}

$option_name = isset( $args[2] ) ? $args[2] : null;

if ( $arg === 'get' ) {
if ( empty( $option_name ) ) {
WP_CLI::error( 'Missing required argument: <option-name>' );
return;
}

// decrypt accessToken
if ( $option_name === 'accessToken' ) {
$option_value = \WP2Static\CoreOptions::encrypt_decrypt(
'decrypt',
Controller::getValue( $option_name )
);
} else {
$option_value = Controller::getValue( $option_name );
}

WP_CLI::line( $option_value );
}

if ( $arg === 'set' ) {
if ( empty( $option_name ) ) {
WP_CLI::error( 'Missing required argument: <option-name>' );
return;
}

$option_value = isset( $args[3] ) ? $args[3] : null;

if ( empty( $option_value ) ) {
$option_value = '';
}

// decrypt accessToken
if ( $option_name === 'accessToken' ) {
$option_value = \WP2Static\CoreOptions::encrypt_decrypt(
'encrypt',
$option_value
);
}

Controller::saveOption( $option_name, $option_value );
}

if ( $arg === 'list' ) {
$options = Controller::getOptions();

// decrypt accessToken
$options['accessToken']->value = \WP2Static\CoreOptions::encrypt_decrypt(
'decrypt',
$options['accessToken']->value
);

WP_CLI\Utils\format_items(
'table',
$options,
[ 'name', 'value' ]
);
}
}
}
}

34 changes: 34 additions & 0 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ public function run() : void {
15,
1
);

add_action(
'admin_menu',
[ $this, 'addOptionsPage' ],
15,
1
);

do_action(
'wp2static_register_addon',
'wp2static-addon-netlify',
'deploy',
'Netlify Deployment',
'https://wp2static.com/addons/netlify/',
'Deploys to Netlify'
);

if ( defined( 'WP_CLI' ) ) {
\WP_CLI::add_command(
'wp2static netlify',
[ CLI::class, 'netlify' ]
);
}
}

/**
Expand Down Expand Up @@ -263,5 +286,16 @@ public static function getValue( string $name ) : string {

return $option_value;
}

public function addOptionsPage() : void {
add_submenu_page(
'',
'Netlify Deployment Options',
'Netlify Deployment Options',
'manage_options',
'wp2static-addon-netlify',
[ $this, 'renderNetlifyPage' ]
);
}
}

8 changes: 5 additions & 3 deletions wp2static-addon-netlify.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: WP2Static Add-on: Netlify Deployment
* Plugin URI: https://wp2static.com
* Description: Netlify deployment add-on for WP2Static.
* Version: 1.0-alpha-006
* Version: 1.0-alpha-007
* Author: Leon Stafford
* Author URI: https://ljs.dev
* License: Unlicense
Expand All @@ -17,9 +17,11 @@
}

define( 'WP2STATIC_NETLIFY_PATH', plugin_dir_path( __FILE__ ) );
define( 'WP2STATIC_NETLIFY_VERSION', '1.0-alpha-006' );
define( 'WP2STATIC_NETLIFY_VERSION', '1.0-alpha-007' );

require WP2STATIC_NETLIFY_PATH . 'vendor/autoload.php';
if ( file_exists( WP2STATIC_NETLIFY_PATH . 'vendor/autoload.php' ) ) {
require WP2STATIC_NETLIFY_PATH . 'vendor/autoload.php';
}

function run_wp2static_addon_netlify() {
$controller = new WP2StaticNetlify\Controller();
Expand Down

0 comments on commit 6343b8e

Please sign in to comment.