| @@ -103,23 +103,95 @@ | ||
| 103 | 103 | * @param string $status Status. |
| 104 | 104 | * @param string $slug Tweak slug. |
| 105 | 105 | */ |
| 106 | 106 | public function mark( $status, $slug ) { |
| 107 | - foreach ( array( 'issues', 'fixed', 'ignore' ) as $collection ) { | |
| 108 | - $arr = $this->$collection; | |
| 109 | - $index = array_search( $slug, $arr, true ); | |
| 107 | + foreach ( array( | |
| 108 | + \WP_Defender\Controller\Security_Tweaks::STATUS_ISSUES, | |
| 109 | + \WP_Defender\Controller\Security_Tweaks::STATUS_RESOLVE, | |
| 110 | + \WP_Defender\Controller\Security_Tweaks::STATUS_IGNORE, | |
| 111 | + ) as $collection_name ) { | |
| 112 | + $collection = $this->get_collection( $collection_name ); | |
| 113 | + | |
| 114 | + if ( ! is_array( $collection ) ) { | |
| 115 | + continue; | |
| 116 | + } | |
| 117 | + | |
| 118 | + $index = array_search( $slug, $collection, true ); | |
| 110 | 119 | if ( false !== $index ) { |
| 111 | - unset( $arr[ $index ] ); | |
| 120 | + unset( $collection[ $index ] ); | |
| 112 | 121 | } |
| 113 | - $this->$collection = $arr; | |
| 122 | + $this->set_collection( $collection_name, $collection ); | |
| 114 | 123 | } |
| 124 | + | |
| 115 | 125 | if ( \WP_Defender\Controller\Security_Tweaks::STATUS_RESTORE === $status ) { |
| 116 | 126 | $status = \WP_Defender\Controller\Security_Tweaks::STATUS_ISSUES; |
| 117 | 127 | } |
| 118 | - $collection = $this->{$status}; | |
| 119 | - $collection[] = $slug; | |
| 120 | - $this->{$status} = $collection; | |
| 128 | + | |
| 129 | + if ( ! property_exists( $this, $status ) ) { | |
| 130 | + return; | |
| 131 | + } | |
| 132 | + | |
| 133 | + try { | |
| 134 | + $collection = $this->get_collection( $status ); | |
| 135 | + } catch ( \InvalidArgumentException $e ) { | |
| 136 | + return; | |
| 137 | + } | |
| 138 | + | |
| 139 | + $collection[] = $slug; | |
| 140 | + $this->set_collection( $status, $collection ); | |
| 121 | 141 | $this->save(); |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Get a tweak collection by name. | |
| 146 | + * | |
| 147 | + * @param string $name Collection name. | |
| 148 | + * | |
| 149 | + * @return array | |
| 150 | + * | |
| 151 | + * @throws \InvalidArgumentException Invalid collection name. | |
| 152 | + */ | |
| 153 | + public function get_collection( string $name ): array { | |
| 154 | + switch ( $name ) { | |
| 155 | + case \WP_Defender\Controller\Security_Tweaks::STATUS_ISSUES: | |
| 156 | + return $this->issues; | |
| 157 | + | |
| 158 | + case \WP_Defender\Controller\Security_Tweaks::STATUS_RESOLVE: | |
| 159 | + return $this->fixed; | |
| 160 | + | |
| 161 | + case \WP_Defender\Controller\Security_Tweaks::STATUS_IGNORE: | |
| 162 | + return $this->ignore; | |
| 163 | + | |
| 164 | + default: | |
| 165 | + throw new \InvalidArgumentException( 'Invalid collection name.' ); | |
| 166 | + } | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Set a tweak collection by name. | |
| 171 | + * | |
| 172 | + * @param string $name Collection name. | |
| 173 | + * @param array $value Collection value. | |
| 174 | + * | |
| 175 | + * @throws \InvalidArgumentException Invalid collection name. | |
| 176 | + */ | |
| 177 | + private function set_collection( string $name, array $value ): void { | |
| 178 | + switch ( $name ) { | |
| 179 | + case \WP_Defender\Controller\Security_Tweaks::STATUS_ISSUES: | |
| 180 | + $this->issues = $value; | |
| 181 | + break; | |
| 182 | + | |
| 183 | + case \WP_Defender\Controller\Security_Tweaks::STATUS_RESOLVE: | |
| 184 | + $this->fixed = $value; | |
| 185 | + break; | |
| 186 | + | |
| 187 | + case \WP_Defender\Controller\Security_Tweaks::STATUS_IGNORE: | |
| 188 | + $this->ignore = $value; | |
| 189 | + break; | |
| 190 | + | |
| 191 | + default: | |
| 192 | + throw new \InvalidArgumentException( 'Invalid collection name.' ); | |
| 193 | + } | |
| 122 | 194 | } |
| 123 | 195 | |
| 124 | 196 | /** |
| 125 | 197 | * Define settings labels. |