From 6343b8eedfe92027a0330df6474605f4b79380e7 Mon Sep 17 00:00:00 2001 From: Leon Date: Wed, 4 Nov 2020 12:37:44 +1030 Subject: [PATCH] register with core;CLI --- CHANGELOG.md | 8 +++ src/CLI.php | 100 ++++++++++++++++++++++++++++++++++++ src/Controller.php | 34 ++++++++++++ wp2static-addon-netlify.php | 8 +-- 4 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 CHANGELOG.md create mode 100755 src/CLI.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..6112076 --- /dev/null +++ b/CHANGELOG.md @@ -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 + diff --git a/src/CLI.php b/src/CLI.php new file mode 100755 index 0000000..ba75f9c --- /dev/null +++ b/src/CLI.php @@ -0,0 +1,100 @@ +' ); + } + + if ( $action === 'options' ) { + if ( empty( $arg ) ) { + WP_CLI::error( 'Missing required argument: ' ); + } + + $option_name = isset( $args[2] ) ? $args[2] : null; + + if ( $arg === 'get' ) { + if ( empty( $option_name ) ) { + WP_CLI::error( 'Missing required argument: ' ); + 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: ' ); + 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' ] + ); + } + } + } +} + diff --git a/src/Controller.php b/src/Controller.php index 1a03daf..9208e91 100755 --- a/src/Controller.php +++ b/src/Controller.php @@ -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' ] + ); + } } /** @@ -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' ] + ); + } } diff --git a/wp2static-addon-netlify.php b/wp2static-addon-netlify.php index b6a7640..a9146bd 100644 --- a/wp2static-addon-netlify.php +++ b/wp2static-addon-netlify.php @@ -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 @@ -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();