|
| 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 | +} |
0 commit comments