Skip to content

Commit 577406b

Browse files
committed
feat: adds wp-cli commands for most common operations
1 parent db431b9 commit 577406b

File tree

4 files changed

+213
-1
lines changed

4 files changed

+213
-1
lines changed

inc/cli.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* CLI class.
5+
*
6+
* Author: Bogdan Preda <bogdan.preda@themeisle.com>
7+
* Created on: 19/07/2018
8+
*
9+
* @package \Optimole\Inc
10+
* @author Optimole <friends@optimole.com>
11+
*/
12+
13+
/**
14+
* Class Optml_Cli
15+
*/
16+
class Optml_Cli {
17+
18+
/**
19+
* Api version.
20+
*
21+
* @var string Version string.
22+
*/
23+
const CLI_NAMESPACE = 'optimole';
24+
25+
/**
26+
* CLI controllers
27+
*
28+
* @var array List of CLI controllers.
29+
*/
30+
private $commands = array(
31+
'settings',
32+
);
33+
34+
/**
35+
* Optml_Cli constructor.
36+
*/
37+
public function __construct() {
38+
foreach ( $this->commands as $command ) {
39+
$class_name = 'Optml_Cli_' . ucfirst( $command );
40+
$controller = new $class_name();
41+
try {
42+
\WP_CLI::add_command( self::CLI_NAMESPACE . ' ' . $command, $controller );
43+
} catch ( \Exception $e ) {
44+
// TODO Log this exception.
45+
}
46+
}
47+
}
48+
}

inc/cli/settings.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
/**
3+
* CLI commands responsible for the Optimole settings.
4+
*/
5+
6+
if ( ! class_exists( 'WP_CLI' ) ) {
7+
return;
8+
}
9+
10+
/**
11+
* Class Optml_Cli_Settings
12+
*/
13+
class Optml_Cli_Settings extends WP_CLI_Command {
14+
/**
15+
* Connect to service
16+
*/
17+
public function connect( $args ) {
18+
if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' ) {
19+
return \WP_CLI::error( 'No argument passed. Required one argument ( api key )' );
20+
}
21+
22+
if ( sizeof( $args ) > 1 ) {
23+
return \WP_CLI::error( 'To many arguments passed' );
24+
}
25+
26+
$api_key = $args[0];
27+
28+
$request = new Optml_Api();
29+
$data = $request->get_user_data( $api_key );
30+
if ( $data === false || is_wp_error( $data ) ) {
31+
$extra = '';
32+
if ( is_wp_error( $data ) ) {
33+
/**
34+
* Error from api.
35+
*
36+
* @var WP_Error $data Error object.
37+
*/
38+
$extra = sprintf( __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() );
39+
}
40+
return \WP_CLI::error( __( 'Can not connect to Optimole service', 'optimole-wp' ) . $extra );
41+
}
42+
$settings = new Optml_Settings();
43+
$settings->update( 'service_data', $data );
44+
$settings->update( 'api_key', $api_key );
45+
46+
\WP_CLI::success( sprintf( 'Connected API key %s to Optimole Service', $args[0] ) );
47+
}
48+
49+
/**
50+
* Disconnect service
51+
*/
52+
public function disconnect() {
53+
$settings = new Optml_Settings();
54+
$settings->reset();
55+
\WP_CLI::success( 'Disconnected from Optimole Service' );
56+
}
57+
58+
/**
59+
* Replacement toggle
60+
*/
61+
public function replacement( $args ) {
62+
if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' || ! in_array( $args[0], array( 'on', 'off' ) ) ) {
63+
return \WP_CLI::error( 'No argument passed. Required one argument ( on/off )' );
64+
}
65+
66+
if ( sizeof( $args ) > 1 ) {
67+
return \WP_CLI::error( 'To many arguments passed' );
68+
}
69+
70+
$value = ( $args[0] === 'on' ) ? 'enabled' : 'disabled';
71+
72+
$new_value = $this->update_setting( array( 'image_replacer' => $value ) );
73+
74+
\WP_CLI::success( sprintf( 'Optimole replacement is: %s', $new_value['image_replacer'] ) );
75+
}
76+
77+
/**
78+
* Lazy-load toggle
79+
*/
80+
public function lazy( $args ) {
81+
if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' || ! in_array( $args[0], array( 'on', 'off' ) ) ) {
82+
return \WP_CLI::error( 'No argument passed. Required one argument ( on/off )' );
83+
}
84+
85+
if ( sizeof( $args ) > 1 ) {
86+
return \WP_CLI::error( 'To many arguments passed' );
87+
}
88+
89+
$value = ( $args[0] === 'on' ) ? 'enabled' : 'disabled';
90+
91+
$new_value = $this->update_setting( array( 'lazyload' => $value ) );
92+
93+
\WP_CLI::success( sprintf( 'Optimole lazyload is: %s', $new_value['lazyload'] ) );
94+
}
95+
96+
/**
97+
* Placeholder toggle
98+
*/
99+
public function placeholder( $args ) {
100+
if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' || ! in_array( $args[0], array( 'on', 'off' ) ) ) {
101+
return \WP_CLI::error( 'No argument passed. Required one argument ( on/off )' );
102+
}
103+
104+
if ( sizeof( $args ) > 1 ) {
105+
return \WP_CLI::error( 'To many arguments passed' );
106+
}
107+
108+
$value = ( $args[0] === 'on' ) ? 'enabled' : 'disabled';
109+
110+
$new_value = $this->update_setting( array( 'lazyload_placeholder' => $value ) );
111+
112+
\WP_CLI::success( sprintf( 'Optimole generic placeholder is: %s', $new_value['lazyload_placeholder'] ) );
113+
}
114+
115+
/**
116+
* Quality
117+
*/
118+
public function quality( $args ) {
119+
if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' ) {
120+
return \WP_CLI::error( 'No argument passed. Required one argument ( auto or 1-100 )' );
121+
}
122+
123+
if ( sizeof( $args ) > 1 ) {
124+
return \WP_CLI::error( 'To many arguments passed' );
125+
}
126+
127+
if ( $args[0] !== 'auto' && ( absint( $args[0] ) < 1 || absint( $args[0] ) > 100 ) ) {
128+
return \WP_CLI::error( 'Accepted values are: ( auto or 1-100 )' );
129+
}
130+
131+
$value = $args[0];
132+
133+
$new_value = $this->update_setting( array( 'quality' => $value ) );
134+
135+
\WP_CLI::success( sprintf( 'Optimole quality is: %s', $new_value['quality'] ) );
136+
}
137+
138+
/**
139+
* Utility method to update setting
140+
*
141+
* @param mixed $new_setting The setting to parse.
142+
*
143+
* @return array
144+
*/
145+
protected function update_setting( $new_setting ) {
146+
if ( empty( $new_setting ) ) {
147+
\WP_CLI::error( __( 'No setting to update', 'optimole-wp' ) );
148+
}
149+
$settings = new Optml_Settings();
150+
return $settings->parse_settings( $new_setting );
151+
}
152+
}

inc/main.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ final class Optml_Main {
4141
*/
4242
public $admin;
4343

44+
/**
45+
* Holds the cli class.
46+
*
47+
* @access public
48+
* @since 1.0.0
49+
* @var Optml_Cli Cli instance.
50+
*/
51+
public $cli;
52+
4453
/**
4554
* Optml_Main constructor.
4655
*/
@@ -70,6 +79,9 @@ public static function instance() {
7079
self::$_instance->manager = Optml_Manager::instance();
7180
self::$_instance->rest = new Optml_Rest();
7281
self::$_instance->admin = new Optml_Admin();
82+
if ( class_exists( 'WP_CLI' ) ) {
83+
self::$_instance->cli = new Optml_Cli();
84+
}
7385
}
7486
$vendor_file = OPTML_PATH . 'vendor/autoload.php';
7587
if ( is_readable( $vendor_file ) ) {

optimole-wp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function optml_autoload( $class ) {
2626
if ( strpos( $class, $prefix ) !== 0 ) {
2727
return;
2828
}
29-
foreach ( array( '/inc/', '/inc/traits/', '/inc/image_properties/', '/inc/compatibilities/', '/inc/conflicts/' ) as $folder ) {
29+
foreach ( array( '/inc/', '/inc/traits/', '/inc/image_properties/', '/inc/compatibilities/', '/inc/conflicts/', '/inc/cli/' ) as $folder ) {
3030
$file = str_replace( $prefix . '_', '', $class );
3131
$file = strtolower( $file );
3232
$file = dirname( __FILE__ ) . $folder . $file . '.php';

0 commit comments

Comments
 (0)