From 577406bddbc10a1adcab8635bc4fb354baa43ef0 Mon Sep 17 00:00:00 2001 From: Marius Cristea Date: Wed, 8 Jan 2020 18:48:53 +0200 Subject: [PATCH] feat: adds wp-cli commands for most common operations --- inc/cli.php | 48 ++++++++++++++ inc/cli/settings.php | 152 +++++++++++++++++++++++++++++++++++++++++++ inc/main.php | 12 ++++ optimole-wp.php | 2 +- 4 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 inc/cli.php create mode 100644 inc/cli/settings.php diff --git a/inc/cli.php b/inc/cli.php new file mode 100644 index 00000000..6f1379a2 --- /dev/null +++ b/inc/cli.php @@ -0,0 +1,48 @@ + + * Created on: 19/07/2018 + * + * @package \Optimole\Inc + * @author Optimole + */ + +/** + * Class Optml_Cli + */ +class Optml_Cli { + + /** + * Api version. + * + * @var string Version string. + */ + const CLI_NAMESPACE = 'optimole'; + + /** + * CLI controllers + * + * @var array List of CLI controllers. + */ + private $commands = array( + 'settings', + ); + + /** + * Optml_Cli constructor. + */ + public function __construct() { + foreach ( $this->commands as $command ) { + $class_name = 'Optml_Cli_' . ucfirst( $command ); + $controller = new $class_name(); + try { + \WP_CLI::add_command( self::CLI_NAMESPACE . ' ' . $command, $controller ); + } catch ( \Exception $e ) { + // TODO Log this exception. + } + } + } +} diff --git a/inc/cli/settings.php b/inc/cli/settings.php new file mode 100644 index 00000000..4a98857b --- /dev/null +++ b/inc/cli/settings.php @@ -0,0 +1,152 @@ + 1 ) { + return \WP_CLI::error( 'To many arguments passed' ); + } + + $api_key = $args[0]; + + $request = new Optml_Api(); + $data = $request->get_user_data( $api_key ); + if ( $data === false || is_wp_error( $data ) ) { + $extra = ''; + if ( is_wp_error( $data ) ) { + /** + * Error from api. + * + * @var WP_Error $data Error object. + */ + $extra = sprintf( __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() ); + } + return \WP_CLI::error( __( 'Can not connect to Optimole service', 'optimole-wp' ) . $extra ); + } + $settings = new Optml_Settings(); + $settings->update( 'service_data', $data ); + $settings->update( 'api_key', $api_key ); + + \WP_CLI::success( sprintf( 'Connected API key %s to Optimole Service', $args[0] ) ); + } + + /** + * Disconnect service + */ + public function disconnect() { + $settings = new Optml_Settings(); + $settings->reset(); + \WP_CLI::success( 'Disconnected from Optimole Service' ); + } + + /** + * Replacement toggle + */ + public function replacement( $args ) { + if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' || ! in_array( $args[0], array( 'on', 'off' ) ) ) { + return \WP_CLI::error( 'No argument passed. Required one argument ( on/off )' ); + } + + if ( sizeof( $args ) > 1 ) { + return \WP_CLI::error( 'To many arguments passed' ); + } + + $value = ( $args[0] === 'on' ) ? 'enabled' : 'disabled'; + + $new_value = $this->update_setting( array( 'image_replacer' => $value ) ); + + \WP_CLI::success( sprintf( 'Optimole replacement is: %s', $new_value['image_replacer'] ) ); + } + + /** + * Lazy-load toggle + */ + public function lazy( $args ) { + if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' || ! in_array( $args[0], array( 'on', 'off' ) ) ) { + return \WP_CLI::error( 'No argument passed. Required one argument ( on/off )' ); + } + + if ( sizeof( $args ) > 1 ) { + return \WP_CLI::error( 'To many arguments passed' ); + } + + $value = ( $args[0] === 'on' ) ? 'enabled' : 'disabled'; + + $new_value = $this->update_setting( array( 'lazyload' => $value ) ); + + \WP_CLI::success( sprintf( 'Optimole lazyload is: %s', $new_value['lazyload'] ) ); + } + + /** + * Placeholder toggle + */ + public function placeholder( $args ) { + if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' || ! in_array( $args[0], array( 'on', 'off' ) ) ) { + return \WP_CLI::error( 'No argument passed. Required one argument ( on/off )' ); + } + + if ( sizeof( $args ) > 1 ) { + return \WP_CLI::error( 'To many arguments passed' ); + } + + $value = ( $args[0] === 'on' ) ? 'enabled' : 'disabled'; + + $new_value = $this->update_setting( array( 'lazyload_placeholder' => $value ) ); + + \WP_CLI::success( sprintf( 'Optimole generic placeholder is: %s', $new_value['lazyload_placeholder'] ) ); + } + + /** + * Quality + */ + public function quality( $args ) { + if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' ) { + return \WP_CLI::error( 'No argument passed. Required one argument ( auto or 1-100 )' ); + } + + if ( sizeof( $args ) > 1 ) { + return \WP_CLI::error( 'To many arguments passed' ); + } + + if ( $args[0] !== 'auto' && ( absint( $args[0] ) < 1 || absint( $args[0] ) > 100 ) ) { + return \WP_CLI::error( 'Accepted values are: ( auto or 1-100 )' ); + } + + $value = $args[0]; + + $new_value = $this->update_setting( array( 'quality' => $value ) ); + + \WP_CLI::success( sprintf( 'Optimole quality is: %s', $new_value['quality'] ) ); + } + + /** + * Utility method to update setting + * + * @param mixed $new_setting The setting to parse. + * + * @return array + */ + protected function update_setting( $new_setting ) { + if ( empty( $new_setting ) ) { + \WP_CLI::error( __( 'No setting to update', 'optimole-wp' ) ); + } + $settings = new Optml_Settings(); + return $settings->parse_settings( $new_setting ); + } +} diff --git a/inc/main.php b/inc/main.php index b9d9e571..cfcc91b7 100644 --- a/inc/main.php +++ b/inc/main.php @@ -41,6 +41,15 @@ final class Optml_Main { */ public $admin; + /** + * Holds the cli class. + * + * @access public + * @since 1.0.0 + * @var Optml_Cli Cli instance. + */ + public $cli; + /** * Optml_Main constructor. */ @@ -70,6 +79,9 @@ public static function instance() { self::$_instance->manager = Optml_Manager::instance(); self::$_instance->rest = new Optml_Rest(); self::$_instance->admin = new Optml_Admin(); + if ( class_exists( 'WP_CLI' ) ) { + self::$_instance->cli = new Optml_Cli(); + } } $vendor_file = OPTML_PATH . 'vendor/autoload.php'; if ( is_readable( $vendor_file ) ) { diff --git a/optimole-wp.php b/optimole-wp.php index baf29dc5..aee15343 100644 --- a/optimole-wp.php +++ b/optimole-wp.php @@ -26,7 +26,7 @@ function optml_autoload( $class ) { if ( strpos( $class, $prefix ) !== 0 ) { return; } - foreach ( array( '/inc/', '/inc/traits/', '/inc/image_properties/', '/inc/compatibilities/', '/inc/conflicts/' ) as $folder ) { + foreach ( array( '/inc/', '/inc/traits/', '/inc/image_properties/', '/inc/compatibilities/', '/inc/conflicts/', '/inc/cli/' ) as $folder ) { $file = str_replace( $prefix . '_', '', $class ); $file = strtolower( $file ); $file = dirname( __FILE__ ) . $folder . $file . '.php';