PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 2.5.5
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v2.5.5
4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 3.12.1 All 134 releases
optimole-wp / inc / cli / cli_setting.php

cli_setting.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 2.5.5, at inc/cli/cli_setting.php

145 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * 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 * Connect to service
28 * <apikey>
29 * : The api key to use.
30 */
31 public function connect( $args ) {
32 if ( empty( $args ) || ! isset( $args[0] ) || $args[0] === '' ) {
33 return \WP_CLI::error( 'No argument passed. Required one argument ( api key )' );
34 }
35
36 if ( sizeof( $args ) > 1 ) {
37 return \WP_CLI::error( 'To many arguments passed' );
38 }
39
40 $api_key = $args[0];
41
42 $request = new Optml_Api();
43 $data = $request->get_user_data( $api_key );
44 if ( $data === false || is_wp_error( $data ) ) {
45 $extra = '';
46 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() );
53 }
54
55 return \WP_CLI::error( __( 'Can not connect to Optimole service', 'optimole-wp' ) . $extra );
56 }
57 $settings = new Optml_Settings();
58 $settings->update( 'service_data', $data );
59 $settings->update( 'api_key', $api_key );
60
61 \WP_CLI::success( sprintf( 'Connected API key %s to Optimole Service', $args[0] ) );
62 }
63
64 /**
65 * Disconnect from service.
66 */
67 public function disconnect() {
68 $settings = new Optml_Settings();
69 $settings->reset();
70 \WP_CLI::success( 'Disconnected from Optimole Service' );
71 }
72
73 /**
74 * Update settings.
75 *
76 * <setting_name>
77 * : The setting name to update.
78 *
79 * <setting_value>
80 * : The setting value to update.
81 */
82 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;
87 }
88
89 if ( self::$whitelisted_settings[ $args[0] ] === 'bool' && ( empty( $args ) || ! isset( $args[1] ) || $args[1] === '' || ! in_array(
90 $args[1],
91 array(
92 'on',
93 'off',
94 )
95 ) ) ) {
96 return \WP_CLI::error( 'No argument passed. Required one argument ( on/off )' );
97 }
98
99 if ( self::$whitelisted_settings[ $args[0] ] === 'int' && ( empty( $args ) || ! isset( $args[1] ) || $args[1] === '' || (int) $args[1] > 100 || (int) $args[1] < 0 ) ) {
100 return \WP_CLI::error( 'Invalid argument, must be between 0 and 100.' );
101 }
102
103 $value = ( self::$whitelisted_settings[ $args[0] ] === 'bool' ) ? ( $args[1] === 'on' ? 'enabled' : 'disabled' ) : (int) $args[1];
104
105 $new_value = $this->update_setting( array( $args[0] => $value ) );
106 \WP_CLI::success( sprintf( 'Setting %s updated to: %s', $args[0], $new_value[ $args[0] ] ) );
107 }
108
109 /**
110 * Check settings.
111 *
112 * <setting_name>
113 * : The setting name to check.
114 */
115 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;
120 }
121
122 $value = ( new Optml_Settings() )->get( $args[0] );
123
124 \WP_CLI::success( sprintf( 'Setting %s is set to: %s', $args[0], $value ) );
125 }
126
127
128 /**
129 * Utility method to update setting
130 *
131 * @param mixed $new_setting The setting to parse.
132 *
133 * @return array
134 */
135 private function update_setting( $new_setting ) {
136 if ( empty( $new_setting ) ) {
137 \WP_CLI::error( __( 'No setting to update', 'optimole-wp' ) );
138 }
139 $settings = new Optml_Settings();
140
141 return $settings->parse_settings( $new_setting );
142 }
143
144 }
145