PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.10.0
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.10.0
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 3.10.0, at inc/cli/cli_setting.php

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