@@ -45,6 +45,11 @@ class SubCommand {
45
45
*/
46
46
public string $ successMessage ;
47
47
48
+ /**
49
+ * @var string Message to output if --remove command execution was successful
50
+ */
51
+ public string $ removalMessage ;
52
+
48
53
/**
49
54
* @var array Command line arguments passed as $assoc_args
50
55
*/
@@ -117,6 +122,24 @@ private function setRuleContent() : string|array {
117
122
return $ result ;
118
123
}
119
124
125
+ /**
126
+ * Returns the output message
127
+ *
128
+ * @param string $type
129
+ *
130
+ * @return string
131
+ * @todo I am not too happy about this
132
+ */
133
+ private function getOutputMessage (string $ type = 'success ' ) : string {
134
+ $ message = $ this ->{$ type . 'Message ' } ;
135
+
136
+ if ($ this ->serverType === 'nginx ' ) {
137
+ $ message .= PHP_EOL . 'Since you are using nginx you need to restart web server manually. If you copied rules manually, this command will have no effect. ' ;
138
+ }
139
+
140
+ return $ message ;
141
+ }
142
+
120
143
/**
121
144
* Outputs the result of command execution
122
145
*
@@ -135,10 +158,21 @@ public function output() {
135
158
} else {
136
159
try {
137
160
$ fileManager = new FileManager ($ this ->filePath );
138
- $ result = $ fileManager ->add ($ this ->ruleContent , $ this ->ruleName );
139
161
140
- if ($ result ) {
141
- WP_CLI ::success ($ this ->successMessage );
162
+ if (isset ($ this ->commandArguments ['remove ' ]) && $ this ->commandArguments ['remove ' ] === true ) {
163
+ //We need to remove the rule from file
164
+ $ result = $ fileManager ->remove ($ this ->ruleName );
165
+
166
+ if ($ result ) {
167
+ WP_CLI ::success ($ this ->getOutputMessage ('removal ' ));
168
+ }
169
+ } else {
170
+ //Add the rule
171
+ $ result = $ fileManager ->add ($ this ->ruleContent , $ this ->ruleName );
172
+
173
+ if ($ result ) {
174
+ WP_CLI ::success ($ this ->getOutputMessage ('success ' ));
175
+ }
142
176
}
143
177
} catch (FileDoesNotExist |RuleAlreadyExist |FileIsNotWritable |FileIsNotReadable $ e ) {
144
178
WP_CLI ::error ($ e ->getMessage ());
0 commit comments