| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle tweaks settings. |
| 4 |
* |
| 5 |
* @package WP_Defender\Model\Setting |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Model\Setting; |
| 9 |
|
| 10 |
use Calotes\Model\Setting; |
| 11 |
|
| 12 |
/** |
| 13 |
* Model for tweaks settings. |
| 14 |
*/ |
| 15 |
class Security_Tweaks extends Setting { |
| 16 |
|
| 17 |
/** |
| 18 |
* Option name. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $table = 'hardener_settings'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Store a list of issues tweaks, as slug. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
public $issues = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* Store a list of fixed tweaks, as slug. |
| 33 |
* |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
public $fixed = array(); |
| 37 |
|
| 38 |
/** |
| 39 |
* Store a list of ignored tweaks, as slug. |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
public $ignore = array(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Contains all the data generated by rules. |
| 47 |
* |
| 48 |
* @var array |
| 49 |
*/ |
| 50 |
public $data = array(); |
| 51 |
|
| 52 |
/** |
| 53 |
* Last time visit into the hardener page. |
| 54 |
* |
| 55 |
* @var integer |
| 56 |
*/ |
| 57 |
public $last_seen; |
| 58 |
|
| 59 |
/** |
| 60 |
* Last notification sent out. |
| 61 |
* |
| 62 |
* @var integer |
| 63 |
*/ |
| 64 |
public $last_sent; |
| 65 |
/** |
| 66 |
* Automate tweaks |
| 67 |
* |
| 68 |
* @var bool |
| 69 |
*/ |
| 70 |
public $automate = false; |
| 71 |
|
| 72 |
/** |
| 73 |
* Check if a tweak is ignored. |
| 74 |
* |
| 75 |
* @param string $slug Tweak slug. |
| 76 |
* |
| 77 |
* @return bool |
| 78 |
*/ |
| 79 |
public function is_tweak_ignore( string $slug ): bool { |
| 80 |
// Empty ignored tweak is string on old versions, so change it to array. |
| 81 |
if ( is_string( $this->ignore ) ) { |
| 82 |
$this->ignore = '' === $this->ignore ? array() : array( $this->ignore ); |
| 83 |
$this->save(); |
| 84 |
} |
| 85 |
|
| 86 |
return in_array( $slug, $this->ignore, true ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Is the tweak in the Issue list?. |
| 91 |
* |
| 92 |
* @param string $slug Tweak slug. |
| 93 |
* |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public function is_tweak_issue( string $slug ): bool { |
| 97 |
return in_array( $slug, $this->issues, true ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Mark a tweak as actioned. |
| 102 |
* |
| 103 |
* @param string $status Status. |
| 104 |
* @param string $slug Tweak slug. |
| 105 |
*/ |
| 106 |
public function mark( $status, $slug ) { |
| 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 ); |
| 119 |
if ( false !== $index ) { |
| 120 |
unset( $collection[ $index ] ); |
| 121 |
} |
| 122 |
$this->set_collection( $collection_name, $collection ); |
| 123 |
} |
| 124 |
|
| 125 |
if ( \WP_Defender\Controller\Security_Tweaks::STATUS_RESTORE === $status ) { |
| 126 |
$status = \WP_Defender\Controller\Security_Tweaks::STATUS_ISSUES; |
| 127 |
} |
| 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 ); |
| 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 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Define settings labels. |
| 198 |
* |
| 199 |
* @return array |
| 200 |
*/ |
| 201 |
public function labels(): array { |
| 202 |
return array( |
| 203 |
'data' => 'data', |
| 204 |
'fixed' => esc_html__( 'Actioned', 'defender-security' ), |
| 205 |
'issues' => esc_html__( 'Recommendations', 'defender-security' ), |
| 206 |
'ignore' => esc_html__( 'Ignored', 'defender-security' ), |
| 207 |
'automate' => 'automate', |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Todo: Find a less expensive way. |
| 213 |
* |
| 214 |
* @return array |
| 215 |
*/ |
| 216 |
public function get_tweak_types(): array { |
| 217 |
return array( |
| 218 |
'count_fixed' => count( $this->fixed ), |
| 219 |
'count_ignored' => count( $this->ignore ), |
| 220 |
'count_issues' => count( $this->issues ), |
| 221 |
); |
| 222 |
} |
| 223 |
} |
| 224 |
|