| 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 |
* Resolve whether an operation should apply to the network or the current site. |
| 12 |
* |
| 13 |
* A null value is resolved from the current admin screen, so a caller that has |
| 14 |
* no explicit preference follows whichever admin the request is being made from. |
| 15 |
* |
| 16 |
* @param bool|null $network Whether the operation is network-wide, or null to infer it. |
| 17 |
* |
| 18 |
* @return bool |
| 19 |
*/ |
| 20 |
function validate_network_param( ?bool $network = null ): bool { |
| 21 |
// If multisite is not active, then assume the value is false. |
| 22 |
if ( ! is_multisite() ) { |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
// If the value is null, try to base it on the current admin page. |
| 27 |
if ( is_null( $network ) && function_exists( 'is_network_admin' ) ) { |
| 28 |
return is_network_admin(); |
| 29 |
} |
| 30 |
|
| 31 |
return (bool) $network; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Retrieves an option value based on an option name from either the current site or the current network. |
| 36 |
* |
| 37 |
* @param bool $network Whether to get a network-wide option. |
| 38 |
* @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
| 39 |
* @param mixed $default_value Optional value to return if option doesn't exist. Default false. |
| 40 |
* |
| 41 |
* @return mixed Value set for the option. |
| 42 |
*/ |
| 43 |
function get_self_option( bool $network, string $option, $default_value = false ) { |
| 44 |
return $network |
| 45 |
? get_site_option( $option, $default_value ) |
| 46 |
: get_option( $option, $default_value ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Adds a new option value for either the current site or the current network. |
| 51 |
* |
| 52 |
* @param bool $network Whether to get a network-wide option. |
| 53 |
* @param string $option Name of the option to add. Expected to not be SQL-escaped. |
| 54 |
* @param mixed $value Option value, can be anything. Expected to not be SQL-escaped. |
| 55 |
* |
| 56 |
* @return bool True if the option was added, false otherwise. |
| 57 |
*/ |
| 58 |
function add_self_option( bool $network, string $option, $value ): bool { |
| 59 |
return $network |
| 60 |
? add_site_option( $option, $value ) |
| 61 |
: add_option( $option, $value ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Update the value of an option that was already added on the current site or the current network. |
| 66 |
* |
| 67 |
* @param bool $network Whether to update a network-wide option. |
| 68 |
* @param string $option Name of option. Expected to not be SQL-escaped. |
| 69 |
* @param mixed $value Option value. Expected to not be SQL-escaped. |
| 70 |
* |
| 71 |
* @return bool False if value was not updated. True if value was updated. |
| 72 |
*/ |
| 73 |
function update_self_option( bool $network, string $option, $value ): bool { |
| 74 |
return $network |
| 75 |
? update_site_option( $option, $value ) |
| 76 |
: update_option( $option, $value ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Remove an option on th current site or the current network. |
| 81 |
* |
| 82 |
* @param bool $network Whether to delete a network-wide option. |
| 83 |
* @param string $option Name of option. Expected to not be SQL-escaped. |
| 84 |
* |
| 85 |
* @return bool False if value was not deleted. True if value was deleted. |
| 86 |
*/ |
| 87 |
function delete_self_option( bool $network, string $option ): bool { |
| 88 |
return $network |
| 89 |
? delete_site_option( $option ) |
| 90 |
: delete_option( $option ); |
| 91 |
} |
| 92 |
|