| 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_Setting |
| 12 |
*/ |
| 13 |
class Optml_Cli_Setting extends WP_CLI_Command { |
| 14 |
/** |
| 15 |
* Connect to service |
| 16 |
* <apikey> |
| 17 |
* : The api key to use. |
| 18 |
*/ |
| 19 |
public function connect( $args ) { |
| 20 |
if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' ) { |
| 21 |
return \WP_CLI::error( 'No argument passed. Required one argument ( api key )' ); |
| 22 |
} |
| 23 |
|
| 24 |
if ( sizeof( $args ) > 1 ) { |
| 25 |
return \WP_CLI::error( 'To many arguments passed' ); |
| 26 |
} |
| 27 |
|
| 28 |
$api_key = $args[0]; |
| 29 |
|
| 30 |
$request = new Optml_Api(); |
| 31 |
$data = $request->get_user_data( $api_key ); |
| 32 |
if ( $data === false || is_wp_error( $data ) ) { |
| 33 |
$extra = ''; |
| 34 |
if ( is_wp_error( $data ) ) { |
| 35 |
$extra = sprintf( |
| 36 |
/* translators: Error details */ |
| 37 |
__( '. ERROR details: %s', 'optimole-wp' ), |
| 38 |
$data->get_error_message() |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
return \WP_CLI::error( __( 'Can not connect to Optimole service', 'optimole-wp' ) . $extra ); |
| 43 |
} |
| 44 |
$settings = new Optml_Settings(); |
| 45 |
$settings->update( 'service_data', $data ); |
| 46 |
$settings->update( 'api_key', $api_key ); |
| 47 |
|
| 48 |
\WP_CLI::success( sprintf( 'Connected API key %s to Optimole Service', $args[0] ) ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Disconnect from service. |
| 53 |
*/ |
| 54 |
public function disconnect() { |
| 55 |
$settings = new Optml_Settings(); |
| 56 |
$settings->reset(); |
| 57 |
\WP_CLI::success( 'Disconnected from Optimole Service' ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Update settings. |
| 62 |
* |
| 63 |
* <setting_name> |
| 64 |
* : The setting name to update. |
| 65 |
* |
| 66 |
* <setting_value> |
| 67 |
* : The setting value to update. |
| 68 |
*/ |
| 69 |
public function update( $args ) { |
| 70 |
if ( empty( $args ) || ! isset( Optml_Settings::$whitelisted_settings[ $args[0] ] ) ) { |
| 71 |
return \WP_CLI::error( sprintf( 'Setting must be one of: %s', implode( ',', array_keys( Optml_Settings::$whitelisted_settings ) ) ) ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( Optml_Settings::$whitelisted_settings[ $args[0] ] === 'bool' && ( ! isset( $args[1] ) || $args[1] === '' || ! in_array( |
| 75 |
$args[1], |
| 76 |
[ |
| 77 |
'on', |
| 78 |
'off', |
| 79 |
], |
| 80 |
true |
| 81 |
) ) ) { |
| 82 |
return \WP_CLI::error( 'No argument passed. Required one argument ( on/off )' ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( Optml_Settings::$whitelisted_settings[ $args[0] ] === 'int' && ( ! isset( $args[1] ) || $args[1] === '' || (int) $args[1] > 100 || (int) $args[1] < 0 ) ) { |
| 86 |
return \WP_CLI::error( 'Invalid argument, must be between 0 and 100.' ); |
| 87 |
} |
| 88 |
|
| 89 |
$value = ( Optml_Settings::$whitelisted_settings[ $args[0] ] === 'bool' ) ? ( $args[1] === 'on' ? 'enabled' : 'disabled' ) : (int) $args[1]; |
| 90 |
|
| 91 |
$new_value = $this->update_setting( [ $args[0] => $value ] ); |
| 92 |
\WP_CLI::success( sprintf( 'Setting %s updated to: %s', $args[0], $new_value[ $args[0] ] ) ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Check settings. |
| 97 |
* |
| 98 |
* <setting_name> |
| 99 |
* : The setting name to check. |
| 100 |
*/ |
| 101 |
public function get( $args ) { |
| 102 |
if ( empty( $args ) || ! isset( Optml_Settings::$whitelisted_settings[ $args[0] ] ) ) { |
| 103 |
return \WP_CLI::error( sprintf( 'Setting must be one of: %s', implode( ',', array_keys( Optml_Settings::$whitelisted_settings ) ) ) ); |
| 104 |
} |
| 105 |
|
| 106 |
$value = ( new Optml_Settings() )->get( $args[0] ); |
| 107 |
|
| 108 |
\WP_CLI::success( sprintf( 'Setting %s is set to: %s', $args[0], $value ) ); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Utility method to update setting |
| 114 |
* |
| 115 |
* @param mixed $new_setting The setting to parse. |
| 116 |
* |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
private function update_setting( $new_setting ) { |
| 120 |
if ( empty( $new_setting ) ) { |
| 121 |
\WP_CLI::error( __( 'No setting to update', 'optimole-wp' ) ); |
| 122 |
} |
| 123 |
$settings = new Optml_Settings(); |
| 124 |
|
| 125 |
return $settings->parse_settings( $new_setting ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Clear cache. |
| 130 |
* |
| 131 |
* [--type=<type>] |
| 132 |
* : The type of cache to clear. Default is images. |
| 133 |
*/ |
| 134 |
public function clear_cache( $args, $assoc_args ) { |
| 135 |
$type = 'images'; |
| 136 |
|
| 137 |
// If assoc_args has a type key, use that. |
| 138 |
if ( isset( $assoc_args['type'] ) && $assoc_args['type'] === 'assets' ) { |
| 139 |
$type = $assoc_args['type']; |
| 140 |
} |
| 141 |
|
| 142 |
$settings = new Optml_Settings(); |
| 143 |
$token = $settings->clear_cache( $type ); |
| 144 |
|
| 145 |
if ( is_wp_error( $token ) ) { |
| 146 |
\WP_CLI::error( $token->get_error_message() ); |
| 147 |
} |
| 148 |
|
| 149 |
\WP_CLI::success( sprintf( 'Cache type %s cleared', $type ) ); |
| 150 |
} |
| 151 |
} |
| 152 |
|