PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | jetpack_vendor/automattic/jetpack-waf/src/class-waf-cli.php +171 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,171 @@
1 +<?php
2 +/**
3 + * CLI handler for Jetpack Waf.
4 + *
5 + * @package automattic/jetpack-waf
6 + */
7 +
8 +namespace Automattic\Jetpack\Waf;
9 +
10 +use WP_CLI;
11 +use WP_CLI_Command;
12 +
13 +if ( ! defined( 'ABSPATH' ) ) {
14 + exit( 0 );
15 +}
16 +
17 +/**
18 + * Set up the WAF, change its mode, or generate its rules.
19 + */
20 +class CLI extends WP_CLI_Command {
21 + /**
22 + * View or set the current mode of the WAF.
23 + * ## OPTIONS
24 + *
25 + * [<mode>]
26 + * : The new mode to be set.
27 + * ---
28 + * options:
29 + * - silent
30 + * - normal
31 + * ---
32 + *
33 + * @param array $args Arguments passed to CLI.
34 + * @return void|null
35 + * @throws WP_CLI\ExitException If there is an error switching the mode.
36 + */
37 + public function mode( $args ) {
38 + if ( count( $args ) > 1 ) {
39 +
40 + return WP_CLI::error( __( 'Only one mode may be specified.', 'jetpack-waf' ) );
41 + }
42 + if ( count( $args ) === 1 ) {
43 + if ( ! Waf_Runner::is_allowed_mode( $args[0] ) ) {
44 +
45 + return WP_CLI::error(
46 + sprintf(
47 + /* translators: %1$s is the mode that was actually found. Also note that the expected "silent" and "normal" are hard-coded strings and must therefore stay the same in any translation. */
48 + __( 'Invalid mode: %1$s. Expected "silent" or "normal".', 'jetpack-waf' ),
49 + $args[0]
50 + )
51 + );
52 + }
53 +
54 + update_option( Waf_Runner::MODE_OPTION_NAME, $args[0] );
55 +
56 + try {
57 + ( new Waf_Standalone_Bootstrap() )->generate();
58 + } catch ( \Exception $e ) {
59 + WP_CLI::warning(
60 + sprintf(
61 + /* translators: %1$s is the unexpected error message. */
62 + __( 'Unable to generate waf bootstrap - standalone mode may not work properly: %1$s', 'jetpack-waf' ),
63 + $e->getMessage()
64 + )
65 + );
66 + }
67 +
68 + return WP_CLI::success(
69 + sprintf(
70 + /* translators: %1$s is the name of the mode that was just switched to. */
71 + __( 'Jetpack WAF mode switched to "%1$s".', 'jetpack-waf' ),
72 + get_option( Waf_Runner::MODE_OPTION_NAME )
73 + )
74 + );
75 + }
76 + WP_CLI::line(
77 + sprintf(
78 + /* translators: %1$s is the name of the mode that the waf is currently running in. */
79 + __( 'Jetpack WAF is running in "%1$s" mode.', 'jetpack-waf' ),
80 + get_option( Waf_Runner::MODE_OPTION_NAME )
81 + )
82 + );
83 + }
84 +
85 + /**
86 + * Setup the WAF to run.
87 + * ## OPTIONS
88 + *
89 + * [<mode>]
90 + * : The new mode to be set.
91 + * ---
92 + * options:
93 + * - silent
94 + * - normal
95 + * ---
96 + *
97 + * @param array $args Arguments passed to CLI.
98 + * @return void|null
99 + * @throws WP_CLI\ExitException If there is an error switching the mode.
100 + */
101 + public function setup( $args ) {
102 + // Let is_allowed_mode know we are running from the CLI
103 + define( 'WAF_CLI_MODE', $args[0] );
104 +
105 + // Set the mode and generate the bootstrap
106 + $this->mode( array( $args[0] ) );
107 +
108 + try {
109 + // Add relevant options and generate the rules.php file
110 + Waf_Runner::activate();
111 + } catch ( \Exception $e ) {
112 +
113 + return WP_CLI::error(
114 + sprintf(
115 + /* translators: %1$s is the unexpected error message. */
116 + __( 'Jetpack WAF rules file failed to generate: %1$s', 'jetpack-waf' ),
117 + $e->getMessage()
118 + )
119 + );
120 + }
121 +
122 + return WP_CLI::success( __( 'Jetpack WAF has successfully been set up.', 'jetpack-waf' ) );
123 + }
124 +
125 + /**
126 + * Delete the WAF options.
127 + *
128 + * @return void|null
129 + * @throws WP_CLI\ExitException If deactivating has failures.
130 + */
131 + public function teardown() {
132 + try {
133 + Waf_Runner::deactivate();
134 + } catch ( \Exception $e ) {
135 + WP_CLI::error( __( 'Jetpack WAF failed to fully deactivate.', 'jetpack-waf' ) );
136 + }
137 +
138 + return WP_CLI::success( __( 'Jetpack WAF has been deactivated.', 'jetpack-waf' ) );
139 + }
140 +
141 + /**
142 + * Generate the rules.php file with latest rules for the WAF.
143 + *
144 + * @return void|null
145 + * @throws WP_CLI\ExitException If there is an error switching the mode.
146 + */
147 + public function generate_rules() {
148 + try {
149 + Waf_Constants::define_entrypoint();
150 + Waf_Rules_Manager::generate_automatic_rules();
151 + Waf_Rules_Manager::generate_rules();
152 + } catch ( \Exception $e ) {
153 +
154 + return WP_CLI::error(
155 + sprintf(
156 + /* translators: %1$s is the unexpected error message. */
157 + __( 'Jetpack WAF rules file failed to generate: %1$s', 'jetpack-waf' ),
158 + $e->getMessage()
159 + )
160 + );
161 + }
162 +
163 + return WP_CLI::success(
164 + sprintf(
165 + /* translators: %1$s is the name of the mode that was just switched to. */
166 + __( 'Jetpack WAF rules successfully created to: "%1$s".', 'jetpack-waf' ),
167 + Waf_Runner::get_waf_file_path( JETPACK_WAF_ENTRYPOINT )
168 + )
169 + );
170 + }
171 +}