Skip to content

Commit

Permalink
feat: adds wp-cli commands for most common operations
Browse files Browse the repository at this point in the history
  • Loading branch information
selul committed Jan 14, 2020
1 parent db431b9 commit 577406b
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 1 deletion.
48 changes: 48 additions & 0 deletions inc/cli.php
@@ -0,0 +1,48 @@
<?php

/**
* CLI class.
*
* Author: Bogdan Preda <bogdan.preda@themeisle.com>
* Created on: 19/07/2018
*
* @package \Optimole\Inc
* @author Optimole <friends@optimole.com>
*/

/**
* 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.
}
}
}
}
152 changes: 152 additions & 0 deletions inc/cli/settings.php
@@ -0,0 +1,152 @@
<?php
/**
* CLI commands responsible for the Optimole settings.
*/

if ( ! class_exists( 'WP_CLI' ) ) {
return;
}

/**
* Class Optml_Cli_Settings
*/
class Optml_Cli_Settings extends WP_CLI_Command {
/**
* Connect to service
*/
public function connect( $args ) {
if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' ) {
return \WP_CLI::error( 'No argument passed. Required one argument ( api key )' );
}

if ( sizeof( $args ) > 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 );
}
}
12 changes: 12 additions & 0 deletions inc/main.php
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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 ) ) {
Expand Down
2 changes: 1 addition & 1 deletion optimole-wp.php
Expand Up @@ -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';
Expand Down

0 comments on commit 577406b

Please sign in to comment.