| @@ -11,20 +11,8 @@ | ||
| 11 | 11 | * Class Optml_Cli_Setting |
| 12 | 12 | */ |
| 13 | 13 | class Optml_Cli_Setting extends WP_CLI_Command { |
| 14 | 14 | /** |
| 15 | - * Holds an array of possible settings to alter. | |
| 16 | - * | |
| 17 | - * @var array Whitelisted settings. | |
| 18 | - */ | |
| 19 | - public static $whitelisted_settings = [ | |
| 20 | - 'image_replacer' => 'bool', | |
| 21 | - 'quality' => 'int', | |
| 22 | - 'lazyload' => 'bool', | |
| 23 | - 'lazyload_placeholder' => 'bool', | |
| 24 | - ]; | |
| 25 | - | |
| 26 | - /** | |
| 27 | 15 | * Connect to service |
| 28 | 16 | * <apikey> |
| 29 | 17 | * : The api key to use. |
| 30 | 18 | */ |
| @@ -43,14 +31,13 @@ | ||
| 43 | 31 | $data = $request->get_user_data( $api_key ); |
| 44 | 32 | if ( $data === false || is_wp_error( $data ) ) { |
| 45 | 33 | $extra = ''; |
| 46 | 34 | if ( is_wp_error( $data ) ) { |
| 47 | - /** | |
| 48 | - * Error from api. | |
| 49 | - * | |
| 50 | - * @var WP_Error $data Error object. | |
| 51 | - */ | |
| 52 | - $extra = sprintf( __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() ); | |
| 35 | + $extra = sprintf( | |
| 36 | + /* translators: Error details */ | |
| 37 | + __( '. ERROR details: %s', 'optimole-wp' ), | |
| 38 | + $data->get_error_message() | |
| 39 | + ); | |
| 53 | 40 | } |
| 54 | 41 | |
| 55 | 42 | return \WP_CLI::error( __( 'Can not connect to Optimole service', 'optimole-wp' ) . $extra ); |
| 56 | 43 | } |
| @@ -79,31 +66,30 @@ | ||
| 79 | 66 | * <setting_value> |
| 80 | 67 | * : The setting value to update. |
| 81 | 68 | */ |
| 82 | 69 | public function update( $args ) { |
| 83 | - if ( empty( $args ) || ! isset( self::$whitelisted_settings[ $args[0] ] ) ) { | |
| 84 | - \WP_CLI::error( sprintf( 'Setting must be one of: %s', implode( ',', array_keys( self::$whitelisted_settings ) ) ) ); | |
| 85 | - | |
| 86 | - return false; | |
| 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 ) ) ) ); | |
| 87 | 72 | } |
| 88 | 73 | |
| 89 | - if ( self::$whitelisted_settings[ $args[0] ] === 'bool' && ( empty( $args ) || ! isset( $args[1] ) || $args[1] === '' || ! in_array( | |
| 74 | + if ( Optml_Settings::$whitelisted_settings[ $args[0] ] === 'bool' && ( ! isset( $args[1] ) || $args[1] === '' || ! in_array( | |
| 90 | 75 | $args[1], |
| 91 | - array( | |
| 76 | + [ | |
| 92 | 77 | 'on', |
| 93 | 78 | 'off', |
| 94 | - ) | |
| 79 | + ], | |
| 80 | + true | |
| 95 | 81 | ) ) ) { |
| 96 | 82 | return \WP_CLI::error( 'No argument passed. Required one argument ( on/off )' ); |
| 97 | 83 | } |
| 98 | 84 | |
| 99 | - if ( self::$whitelisted_settings[ $args[0] ] === 'int' && ( empty( $args ) || ! isset( $args[1] ) || $args[1] === '' || (int) $args[1] > 100 || (int) $args[1] < 0 ) ) { | |
| 85 | + if ( Optml_Settings::$whitelisted_settings[ $args[0] ] === 'int' && ( ! isset( $args[1] ) || $args[1] === '' || (int) $args[1] > 100 || (int) $args[1] < 0 ) ) { | |
| 100 | 86 | return \WP_CLI::error( 'Invalid argument, must be between 0 and 100.' ); |
| 101 | 87 | } |
| 102 | 88 | |
| 103 | - $value = ( self::$whitelisted_settings[ $args[0] ] === 'bool' ) ? ( $args[1] === 'on' ? 'enabled' : 'disabled' ) : (int) $args[1]; | |
| 89 | + $value = ( Optml_Settings::$whitelisted_settings[ $args[0] ] === 'bool' ) ? ( $args[1] === 'on' ? 'enabled' : 'disabled' ) : (int) $args[1]; | |
| 104 | 90 | |
| 105 | - $new_value = $this->update_setting( array( $args[0] => $value ) ); | |
| 91 | + $new_value = $this->update_setting( [ $args[0] => $value ] ); | |
| 106 | 92 | \WP_CLI::success( sprintf( 'Setting %s updated to: %s', $args[0], $new_value[ $args[0] ] ) ); |
| 107 | 93 | } |
| 108 | 94 | |
| 109 | 95 | /** |
| @@ -112,12 +98,10 @@ | ||
| 112 | 98 | * <setting_name> |
| 113 | 99 | * : The setting name to check. |
| 114 | 100 | */ |
| 115 | 101 | public function get( $args ) { |
| 116 | - if ( empty( $args ) || ! isset( self::$whitelisted_settings[ $args[0] ] ) ) { | |
| 117 | - \WP_CLI::error( sprintf( 'Setting must be one of: %s', implode( ',', array_keys( self::$whitelisted_settings ) ) ) ); | |
| 118 | - | |
| 119 | - return false; | |
| 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 ) ) ) ); | |
| 120 | 104 | } |
| 121 | 105 | |
| 122 | 106 | $value = ( new Optml_Settings() )->get( $args[0] ); |
| 123 | 107 | |
| @@ -140,5 +124,28 @@ | ||
| 140 | 124 | |
| 141 | 125 | return $settings->parse_settings( $new_setting ); |
| 142 | 126 | } |
| 143 | 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 | + } | |
| 144 | 151 | } |