| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle persistent storage of security tweak option. |
| 4 |
* |
| 5 |
* @package WP_Defender\Traits |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Traits; |
| 9 |
|
| 10 |
trait Security_Tweaks_Option { |
| 11 |
|
| 12 |
/** |
| 13 |
* Generic method to update security tweak site option. |
| 14 |
* |
| 15 |
* @param string $key Name of the security tweak option. |
| 16 |
* @param mixed $value Value of the security tweak option. |
| 17 |
* |
| 18 |
* @return bool True if the value was updated, false otherwise. |
| 19 |
*/ |
| 20 |
public function update_option( $key, $value ) { |
| 21 |
$option_name = self::OPTION_PREFIX . $this->slug; |
| 22 |
|
| 23 |
$options = (array) get_site_option( $option_name, array() ); |
| 24 |
|
| 25 |
$options[ $key ] = $value; |
| 26 |
|
| 27 |
return $this->update_all_option( $options ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Generic method to update all option value of specific security tweak. |
| 32 |
* |
| 33 |
* @param mixed $value Value of the security tweak option. |
| 34 |
* |
| 35 |
* @return bool True if the value was updated, false otherwise. |
| 36 |
*/ |
| 37 |
public function update_all_option( $value ) { |
| 38 |
$option_name = self::OPTION_PREFIX . $this->slug; |
| 39 |
|
| 40 |
return update_site_option( $option_name, $value ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Generic method to get security tweak site option value. |
| 45 |
* |
| 46 |
* @param string $key Name of the security tweak option. |
| 47 |
* |
| 48 |
* @return mixed Value of the security tweak option. |
| 49 |
*/ |
| 50 |
public function get_option( $key ) { |
| 51 |
$options = $this->get_all_option(); |
| 52 |
|
| 53 |
return array_key_exists( $key, $options ) ? $options[ $key ] : null; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Generic method to get all option value of specific security tweak. |
| 58 |
* |
| 59 |
* @return mixed All value of the specific security tweak. |
| 60 |
*/ |
| 61 |
public function get_all_option() { |
| 62 |
$option_name = self::OPTION_PREFIX . $this->slug; |
| 63 |
|
| 64 |
return get_site_option( $option_name, array() ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Generic method to delete option of a specific security tweak. |
| 69 |
* |
| 70 |
* @return bool True if the option was deleted, false otherwise. |
| 71 |
*/ |
| 72 |
public function delete_all_option() { |
| 73 |
$option_name = self::OPTION_PREFIX . $this->slug; |
| 74 |
|
| 75 |
return delete_site_option( $option_name ); |
| 76 |
} |
| 77 |
} |
| 78 |
|