| 1 |
<?php |
| 2 |
|
| 3 |
namespace Averta\WordPress\Models; |
| 4 |
|
| 5 |
/** |
| 6 |
* An interface for WordPress Options API. |
| 7 |
*/ |
| 8 |
class WPOptions { |
| 9 |
|
| 10 |
/** |
| 11 |
* The prefix added to all option keys. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
private $prefix = '__'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Create new Options instance. |
| 19 |
* |
| 20 |
* @param string $prefix The prefix that will be added to option names automatically. |
| 21 |
*/ |
| 22 |
public function __construct( $prefix ) { |
| 23 |
$this->prefix = $prefix; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Retrieves an option value. |
| 28 |
* |
| 29 |
* @param string $option The option name (without the prefix). |
| 30 |
* @param mixed $default Optional. Default value to return if the option does not exist. Default null. |
| 31 |
* @param bool $raw Optional. Use the raw option name (i.e. don't call get_option_name). Default false. |
| 32 |
* |
| 33 |
* @return mixed Value set for the option. |
| 34 |
*/ |
| 35 |
public function get( $option, $default = null, $raw = false ) { |
| 36 |
$value = \get_option( $raw ? $option : $this->get_option_name( $option ), $default ); |
| 37 |
|
| 38 |
if ( is_array( $default ) && '' === $value ) { |
| 39 |
$value = []; |
| 40 |
} |
| 41 |
|
| 42 |
return $value; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Sets or updates an option. |
| 47 |
* |
| 48 |
* @param string $option The option name (without the prefix). |
| 49 |
* @param mixed $value The value to store. |
| 50 |
* @param bool $autoload Optional. Whether to load the option when WordPress |
| 51 |
* starts up. For existing options, $autoload can only |
| 52 |
* be updated using update_option() if $value is also |
| 53 |
* changed. Default true. |
| 54 |
* @param bool $raw Optional. Use the raw option name (i.e. don't call get_option_name). Default false. |
| 55 |
* |
| 56 |
* @return bool False if value was not updated and true if value was updated. |
| 57 |
*/ |
| 58 |
public function set( $option, $value, $autoload = true, $raw = false ) { |
| 59 |
return \update_option( $raw ? $option : $this->get_option_name( $option ), $value, $autoload ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Deletes an option. |
| 64 |
* |
| 65 |
* @param string $option The option name (without the prefix). |
| 66 |
* @param bool $raw Optional. Use the raw option name (i.e. don't call get_option_name). Default false. |
| 67 |
* |
| 68 |
* @return bool True, if option is successfully deleted. False on failure. |
| 69 |
*/ |
| 70 |
public function delete( $option, $raw = false ) { |
| 71 |
return \delete_option( $raw ? $option : $this->get_option_name( $option ) ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Retrieves the complete option name to use. |
| 76 |
* |
| 77 |
* @param string $option The option name (without the prefix). |
| 78 |
* |
| 79 |
* @return string |
| 80 |
*/ |
| 81 |
public function get_option_name( $option ) { |
| 82 |
return "{$this->prefix}{$option}"; |
| 83 |
} |
| 84 |
} |
| 85 |
|