PluginProbe
WPVulnerability / 5.1.2
WPVulnerability v5.1.2
5.1.6 5.1.2 5.1.1 5.0.1 5.0.0 trunk 0.1 0.2 1.0 1.0.1 1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 57 releases
wpvulnerability / class-wpvulnerability-config-cli.php

class-wpvulnerability-config-cli.php in WPVulnerability 5.1.2, at class-wpvulnerability-config-cli.php

285 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP-CLI configuration commands for WPVulnerability.
4 *
5 * Provides commands to configure WPVulnerability behaviour.
6 *
7 * @package WPVulnerability
8 */
9
10 defined( 'ABSPATH' ) || exit;
11
12 if ( defined( 'WP_CLI' ) && WP_CLI ) { // @phpstan-ignore booleanAnd.rightAlwaysFalse (WP_CLI is false in bootstrap but true at runtime)
13 /**
14 * WPVulnerability configuration commands.
15 */
16 class WPVulnerability_Config_CLI extends WP_CLI_Command {
17 /**
18 * Abort with an error if the current WordPress user lacks admin capabilities.
19 *
20 * All config commands modify plugin options and therefore require the same
21 * privilege as the admin settings page (manage_options / manage_network_options).
22 * Commands must be run with --user=<admin_login> when no privileged WordPress
23 * user is loaded in the current WP-CLI session.
24 *
25 * @since 5.0.0
26 * @return void
27 */
28 private function require_admin() {
29 $capability = is_multisite() ? 'manage_network_options' : 'manage_options';
30 if ( ! current_user_can( $capability ) ) {
31 \WP_CLI::error(
32 __( 'This command requires a WordPress administrator. Run with --user=<admin_login>.', 'wpvulnerability' )
33 );
34 }
35 }
36
37 /**
38 * Hide a component from analysis output.
39 *
40 * ## OPTIONS
41 *
42 * <component>
43 * : Component to hide. Valid options: core, plugins, themes, php, apache, nginx, mariadb, mysql, imagemagick, curl, memcached, redis, sqlite.
44 *
45 * [<status>]
46 * : Set to "off" to show the component again. Defaults to "on".
47 *
48 * ## EXAMPLES
49 *
50 * wp wpvulnerability config hide core
51 * wp wpvulnerability config hide plugins off
52 *
53 * @when after_wp_load
54 *
55 * @param array<int, string> $args Positional arguments.
56 * @param array<string, mixed> $assoc_args Associative arguments.
57 * @return void
58 */
59 public function hide( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
60 $this->require_admin();
61
62 if ( ! isset( $args[0] ) ) {
63 \WP_CLI::error( 'Missing component.' );
64 }
65
66 $component = strtolower( (string) $args[0] );
67 $valid = array( 'core', 'plugins', 'themes', 'php', 'apache', 'nginx', 'mariadb', 'mysql', 'imagemagick', 'curl', 'memcached', 'redis', 'sqlite' );
68
69 if ( ! in_array( $component, $valid, true ) ) {
70 \WP_CLI::error( "'$component' is not a valid component.\nAvailable components: " . implode( ', ', $valid ) );
71 }
72
73 $status = isset( $args[1] ) ? strtolower( (string) $args[1] ) : 'on';
74 $value = in_array( $status, array( '0', 'off', 'false' ), true ) ? 0 : 1;
75
76 $option_raw = is_multisite() ? get_site_option( 'wpvulnerability-analyze', array() ) : get_option( 'wpvulnerability-analyze', array() );
77 $option = is_array( $option_raw ) ? $option_raw : array();
78 $option[ $component ] = $value;
79
80 if ( is_multisite() ) {
81 update_site_option( 'wpvulnerability-analyze', $option );
82 } else {
83 update_option( 'wpvulnerability-analyze', $option );
84 }
85
86 $message = $value ? 'now hidden' : 'no longer hidden';
87 \WP_CLI::success( sprintf( "Component '%s' is %s.", $component, $message ) );
88 }
89
90 /**
91 * Configure notification email addresses.
92 *
93 * ## OPTIONS
94 *
95 * <email>
96 * : Comma-separated list of email addresses.
97 *
98 * ## EXAMPLES
99 *
100 * wp wpvulnerability config email example@example.com
101 * wp wpvulnerability config email "one@example.com,two@example.com"
102 *
103 * @when after_wp_load
104 *
105 * @param array<int, string> $args Positional arguments.
106 * @param array<string, mixed> $assoc_args Associative arguments.
107 * @return void
108 */
109 public function email( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
110 $this->require_admin();
111
112 if ( ! isset( $args[0] ) ) {
113 \WP_CLI::error( 'Missing email address.' );
114 }
115
116 $emails_raw = explode( ',', (string) $args[0] );
117 $sanitized_emails = array();
118
119 foreach ( $emails_raw as $email ) {
120 $email = sanitize_email( trim( (string) $email ) );
121 if ( ! is_email( $email ) ) {
122 \WP_CLI::error( sprintf( "'%s' is not a valid email address.", $email ) );
123 }
124 $sanitized_emails[] = $email;
125 }
126
127 $option_raw = is_multisite() ? get_site_option( 'wpvulnerability-config', array() ) : get_option( 'wpvulnerability-config', array() );
128 $option = is_array( $option_raw ) ? $option_raw : array();
129 $option['emails'] = implode( ',', $sanitized_emails );
130
131 if ( is_multisite() ) {
132 update_site_option( 'wpvulnerability-config', $option );
133 } else {
134 update_option( 'wpvulnerability-config', $option );
135 }
136
137 \WP_CLI::success( sprintf( "Notification email(s) set to '%s'.", $option['emails'] ) );
138 }
139
140 /**
141 * Configure cache expiration in hours.
142 *
143 * ## OPTIONS
144 *
145 * <hours>
146 * : Cache duration in hours. Valid options: 1, 6, 12, 24.
147 *
148 * ## EXAMPLES
149 *
150 * wp wpvulnerability config cache 6
151 *
152 * @when after_wp_load
153 *
154 * @param array<int, string> $args Positional arguments.
155 * @param array<string, mixed> $assoc_args Associative arguments.
156 * @return void
157 */
158 public function cache( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
159 $this->require_admin();
160
161 if ( ! isset( $args[0] ) ) {
162 \WP_CLI::error( 'Missing cache value.' );
163 }
164
165 $cache = (int) $args[0];
166 $valid = array( 1, 6, 12, 24 );
167 if ( ! in_array( $cache, $valid, true ) ) {
168 \WP_CLI::error( sprintf( "'%s' is not a valid cache value." . PHP_EOL . 'Available values: %s', $cache, implode( ',', $valid ) ) );
169 }
170
171 $option_raw = is_multisite() ? get_site_option( 'wpvulnerability-config', array() ) : get_option( 'wpvulnerability-config', array() );
172 $option = is_array( $option_raw ) ? $option_raw : array();
173 $option['cache'] = $cache;
174
175 if ( is_multisite() ) {
176 update_site_option( 'wpvulnerability-config', $option );
177 } else {
178 update_option( 'wpvulnerability-config', $option );
179 }
180
181 \WP_CLI::success( sprintf( 'Cache expiration set to %d hour(s).', $cache ) );
182 }
183
184 /**
185 * Configure log retention in days.
186 *
187 * ## OPTIONS
188 *
189 * <days>
190 * : Number of days to retain logs. Valid options: 0, 1, 7, 14, 28. Use 0 to disable retention.
191 *
192 * ## EXAMPLES
193 *
194 * wp wpvulnerability config log-retention 14
195 *
196 * @when after_wp_load
197 *
198 * @param array<int, string> $args Positional arguments.
199 * @param array<string, mixed> $assoc_args Associative arguments.
200 * @return void
201 */
202 public function log_retention( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
203 $this->require_admin();
204
205 if ( ! isset( $args[0] ) ) {
206 \WP_CLI::error( 'Missing log retention value.' );
207 }
208
209 $retention = (int) $args[0];
210 $valid = wpvulnerability_get_log_retention_values();
211 if ( ! in_array( $retention, $valid, true ) ) {
212 \WP_CLI::error( sprintf( "'%s' is not a valid log retention value." . PHP_EOL . 'Available values: %s', $retention, implode( ', ', $valid ) ) );
213 }
214
215 $option_raw = is_multisite() ? get_site_option( 'wpvulnerability-config', array() ) : get_option( 'wpvulnerability-config', array() );
216 $option = is_array( $option_raw ) ? $option_raw : array();
217 $option['log_retention'] = $retention;
218
219 if ( is_multisite() ) {
220 update_site_option( 'wpvulnerability-config', $option );
221 } else {
222 update_option( 'wpvulnerability-config', $option );
223 }
224
225 if ( 0 === $retention ) {
226 \WP_CLI::success( 'Log retention disabled.' );
227 return;
228 }
229
230 \WP_CLI::success( sprintf( 'Log retention set to %d day(s).', $retention ) );
231 }
232
233 /**
234 * Configure notification period.
235 *
236 * ## OPTIONS
237 *
238 * <period>
239 * : Notification period. Valid options: daily, weekly, never.
240 *
241 * ## EXAMPLES
242 *
243 * wp wpvulnerability config period weekly
244 *
245 * @when after_wp_load
246 *
247 * @param array<int, string> $args Positional arguments.
248 * @param array<string, mixed> $assoc_args Associative arguments.
249 * @return void
250 */
251 public function period( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
252 $this->require_admin();
253
254 if ( ! isset( $args[0] ) ) {
255 \WP_CLI::error( 'Missing period.' );
256 }
257
258 $period = strtolower( (string) $args[0] );
259 $valid = array( 'daily', 'weekly', 'never' );
260
261 if ( ! in_array( $period, $valid, true ) ) {
262 \WP_CLI::error( sprintf( "'%s' is not a valid period." . PHP_EOL . 'Available periods: %s', $period, implode( ', ', $valid ) ) );
263 }
264
265 $option_raw = is_multisite() ? get_site_option( 'wpvulnerability-config', array() ) : get_option( 'wpvulnerability-config', array() );
266 $option = is_array( $option_raw ) ? $option_raw : array();
267 $option['period'] = $period;
268
269 if ( is_multisite() ) {
270 update_site_option( 'wpvulnerability-config', $option );
271 } else {
272 update_option( 'wpvulnerability-config', $option );
273 }
274
275 if ( function_exists( 'wpvulnerability_schedule_notification_event' ) ) {
276 wpvulnerability_schedule_notification_event( $option );
277 }
278
279 \WP_CLI::success( sprintf( 'Notification period set to \'%s\'.', $period ) );
280 }
281 }
282
283 WP_CLI::add_command( 'wpvulnerability config', 'WPVulnerability_Config_CLI' );
284 }
285