| 1 |
<?php |
| 2 |
/** |
| 3 |
* The options class. |
| 4 |
* |
| 5 |
* This class will handle setting options and transients. |
| 6 |
* |
| 7 |
* @link http://wpmudev.com |
| 8 |
* @since 1.0.0 |
| 9 |
* @author Joel James <[email protected]> |
| 10 |
* @package WPMUDEV\Hub\Connector |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace WPMUDEV\Hub\Connector; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Options |
| 17 |
*/ |
| 18 |
class Options { |
| 19 |
|
| 20 |
/** |
| 21 |
* Settings options key. |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
const NAME = 'wpmudev_hc_options'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns the value of a module option. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* |
| 32 |
* @param string $name The option name. |
| 33 |
* @param mixed $default_value Optional. Set value to return if option not found. |
| 34 |
* |
| 35 |
* @return mixed The option value. |
| 36 |
*/ |
| 37 |
public static function get( $name, $default_value = false ) { |
| 38 |
if ( ! empty( $name ) ) { |
| 39 |
// Get options. |
| 40 |
$options = self::get_options(); |
| 41 |
|
| 42 |
// Return value. |
| 43 |
return $options[ $name ] ?? $default_value; |
| 44 |
} |
| 45 |
|
| 46 |
return $default_value; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Updates the value of a single module option. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* |
| 54 |
* @param string $name The option name. |
| 55 |
* @param mixed $value The new option value. |
| 56 |
* |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
public static function set( $name, $value = false ) { |
| 60 |
$defaults = self::defaults(); |
| 61 |
|
| 62 |
// Only if name is set. |
| 63 |
if ( isset( $defaults[ $name ] ) ) { |
| 64 |
$options = self::get_options(); |
| 65 |
|
| 66 |
// Set the value. |
| 67 |
$options[ $name ] = $value; |
| 68 |
|
| 69 |
// Update the values. |
| 70 |
self::set_options( $options ); |
| 71 |
} |
| 72 |
|
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns the entire module options. |
| 78 |
* |
| 79 |
* @since 1.0.0 |
| 80 |
* |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
public static function get_options() { |
| 84 |
return (array) get_site_option( self::NAME, array() ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Updates the entire module options. |
| 89 |
* |
| 90 |
* @since 1.0.0 |
| 91 |
* |
| 92 |
* @param array $values The values to update. |
| 93 |
* |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public static function set_options( $values ) { |
| 97 |
return update_site_option( self::NAME, $values ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Returns the value of a module transient. |
| 102 |
* |
| 103 |
* The prefix is automatically added to the transient name. |
| 104 |
* Use this function instead of direct access via get_site_transient(). |
| 105 |
* |
| 106 |
* @since 1.0.0 |
| 107 |
* |
| 108 |
* @param string $name The transient name. |
| 109 |
* @param bool $prefix Optional. Set to false to not prefix the name. |
| 110 |
* |
| 111 |
* @return mixed The transient value. |
| 112 |
*/ |
| 113 |
public static function get_transient( $name, $prefix = true ) { |
| 114 |
$key = $prefix ? 'wpmudev_hc_' . $name : $name; |
| 115 |
|
| 116 |
// Transient name cannot be longer than 167 characters |
| 117 |
// 150 is being safe. |
| 118 |
$key = substr( $key, 0, 150 ); |
| 119 |
|
| 120 |
return get_site_transient( $key ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Updates the value of a module transient. |
| 125 |
* |
| 126 |
* The prefix is automatically added to the transient name. |
| 127 |
* Use this function instead of direct access via set_site_transient(). |
| 128 |
* |
| 129 |
* @since 1.0.0 |
| 130 |
* |
| 131 |
* @param string $name The transient name. |
| 132 |
* @param mixed $value The new transient value. |
| 133 |
* @param int $expiration Time until expiration. Default: No expiration. |
| 134 |
* @param bool $prefix Optional. Set to false to not prefix the name. |
| 135 |
* |
| 136 |
* @return bool |
| 137 |
*/ |
| 138 |
public static function set_transient( $name, $value, $expiration = 0, $prefix = true ) { |
| 139 |
$key = $prefix ? 'wpmudev_hc_' . $name : $name; |
| 140 |
|
| 141 |
// Transient name cannot be longer than 167 characters |
| 142 |
// 150 is being safe. |
| 143 |
$key = substr( $key, 0, 150 ); |
| 144 |
|
| 145 |
// Fix to prevent WP from hashing PHP objects. |
| 146 |
delete_site_transient( $key ); |
| 147 |
|
| 148 |
if ( null !== $value ) { |
| 149 |
return set_site_transient( $key, $value, $expiration ); |
| 150 |
} |
| 151 |
|
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Reset the options to defaults. |
| 157 |
* |
| 158 |
* @since 1.0.0 |
| 159 |
* |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
public static function reset() { |
| 163 |
return self::set_options( self::defaults() ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get the default options values. |
| 168 |
* |
| 169 |
* Only the options in this list will be accepted for update. |
| 170 |
* |
| 171 |
* @since 1.0.0 |
| 172 |
* |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
public static function defaults() { |
| 176 |
$options = array( |
| 177 |
// Bigger options. |
| 178 |
'membership_data' => array(), |
| 179 |
'profile_data' => array(), |
| 180 |
// Other small options. |
| 181 |
'timestamp_sync' => array(), |
| 182 |
'version' => \WPMUDEV_HUB_CONNECTOR_VERSION, |
| 183 |
); |
| 184 |
|
| 185 |
/** |
| 186 |
* Filter to modify default options. |
| 187 |
* |
| 188 |
* @since 1.0.0 |
| 189 |
* |
| 190 |
* @param array $options Default options. |
| 191 |
*/ |
| 192 |
return apply_filters( 'wpmudev_hub_connector_options_defaults', $options ); |
| 193 |
} |
| 194 |
} |
| 195 |
|