| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utility functions to make managing options easier with WordPress Multisite. |
| 4 |
* |
| 5 |
* @package Code_Snippets |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Code_Snippets\Utils; |
| 9 |
|
| 10 |
/** |
| 11 |
* Retrieves an option value based on an option name from either the current site or the current network. |
| 12 |
* |
| 13 |
* @param bool $network Whether to get a network-wide option. |
| 14 |
* @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
| 15 |
* @param mixed $default_value Optional value to return if option doesn't exist. Default false. |
| 16 |
* |
| 17 |
* @return mixed Value set for the option. |
| 18 |
*/ |
| 19 |
function get_self_option( bool $network, string $option, $default_value = false ) { |
| 20 |
return $network |
| 21 |
? get_site_option( $option, $default_value ) |
| 22 |
: get_option( $option, $default_value ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Adds a new option option value for either the current site or the current network. |
| 27 |
* |
| 28 |
* @param bool $network Whether to get a network-wide option. |
| 29 |
* @param string $option Name of the option to add. Expected to not be SQL-escaped. |
| 30 |
* @param mixed $value Option value, can be anything. Expected to not be SQL-escaped. |
| 31 |
* |
| 32 |
* @return bool True if the option was added, false otherwise. |
| 33 |
*/ |
| 34 |
function add_self_option( bool $network, string $option, $value ): bool { |
| 35 |
return $network |
| 36 |
? add_site_option( $option, $value ) |
| 37 |
: add_option( $option, $value ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Update the value of an option that was already added on the current site or the current network. |
| 42 |
* |
| 43 |
* @param bool $network Whether to update a network-wide option. |
| 44 |
* @param string $option Name of option. Expected to not be SQL-escaped. |
| 45 |
* @param mixed $value Option value. Expected to not be SQL-escaped. |
| 46 |
* |
| 47 |
* @return bool False if value was not updated. True if value was updated. |
| 48 |
*/ |
| 49 |
function update_self_option( bool $network, string $option, $value ): bool { |
| 50 |
return $network |
| 51 |
? update_site_option( $option, $value ) |
| 52 |
: update_option( $option, $value ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Remove an option on th current site or the current network. |
| 57 |
* |
| 58 |
* @param bool $network Whether to delete a network-wide option. |
| 59 |
* @param string $option Name of option. Expected to not be SQL-escaped. |
| 60 |
* |
| 61 |
* @return bool False if value was not deleted. True if value was deleted. |
| 62 |
*/ |
| 63 |
function delete_self_option( bool $network, string $option ): bool { |
| 64 |
return $network |
| 65 |
? delete_site_option( $option ) |
| 66 |
: delete_option( $option ); |
| 67 |
} |
| 68 |
|