PluginProbe
Code Snippets / 3.10.0
Code Snippets v3.10.0
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / Utils / options.php

options.php in Code Snippets 3.10.0, at php/Utils/options.php

68 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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