| @@ -1,20 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * class-groups-options.php |
| 4 | - * | |
| 4 | + * | |
| 5 | 5 | * Copyright (c) "kento" Karim Rahimpur www.itthinx.com |
| 6 | - * | |
| 6 | + * | |
| 7 | 7 | * This code is released under the GNU General Public License. |
| 8 | 8 | * See COPYRIGHT.txt and LICENSE.txt. |
| 9 | - * | |
| 9 | + * | |
| 10 | 10 | * This code is distributed in the hope that it will be useful, |
| 11 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | 13 | * GNU General Public License for more details. |
| 14 | - * | |
| 14 | + * | |
| 15 | 15 | * This header and all notices must be kept intact. |
| 16 | - * | |
| 16 | + * | |
| 17 | 17 | * @author Karim Rahimpur |
| 18 | 18 | * @package groups |
| 19 | 19 | * @since groups 1.0.0 |
| 20 | 20 | */ |
| @@ -29,39 +29,28 @@ | ||
| 29 | 29 | class Groups_Options { |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | 32 | * Groups plugin option key. |
| 33 | - * | |
| 33 | + * | |
| 34 | 34 | * @var string |
| 35 | 35 | */ |
| 36 | - const option_key = 'groups_options'; | |
| 36 | + const option_key = 'groups_options'; | |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | 39 | * General option index. |
| 40 | - * | |
| 40 | + * | |
| 41 | 41 | * @var string |
| 42 | 42 | */ |
| 43 | 43 | const general = 'general'; |
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | - * No instances are needed. | |
| 46 | + * @var Groups_Lock | |
| 47 | + * | |
| 48 | + * @since 4.0.0 | |
| 47 | 49 | */ |
| 48 | - private function __construct() { | |
| 49 | - } | |
| 50 | + private static $lock = null; | |
| 50 | 51 | |
| 51 | 52 | /** |
| 52 | - * No cloning. | |
| 53 | - */ | |
| 54 | - private function __clone() { | |
| 55 | - } | |
| 56 | - | |
| 57 | - /** | |
| 58 | - * Would be pointless. | |
| 59 | - */ | |
| 60 | - private function __wakeup() { | |
| 61 | - } | |
| 62 | - | |
| 63 | - /** | |
| 64 | 53 | * Registers Groups options (not autoloaded). |
| 65 | 54 | */ |
| 66 | 55 | public static function init() { |
| 67 | 56 | $options = get_option( self::option_key ); |
| @@ -66,9 +55,9 @@ | ||
| 66 | 55 | public static function init() { |
| 67 | 56 | $options = get_option( self::option_key ); |
| 68 | 57 | if ( $options === false ) { |
| 69 | 58 | $options = array( self::general => array() ); |
| 70 | - add_option( self::option_key, $options, null, 'no' ); | |
| 59 | + add_option( self::option_key, $options, '', false ); | |
| 71 | 60 | } |
| 72 | 61 | } |
| 73 | 62 | |
| 74 | 63 | /** |
| @@ -73,8 +62,9 @@ | ||
| 73 | 62 | |
| 74 | 63 | /** |
| 75 | 64 | * Returns the current Groups options and initializes them |
| 76 | 65 | * through init() if needed. |
| 66 | + * | |
| 77 | 67 | * @return array Groups options |
| 78 | 68 | */ |
| 79 | 69 | private static function get_options() { |
| 80 | 70 | $options = get_option( self::option_key ); |
| @@ -85,13 +75,41 @@ | ||
| 85 | 75 | return $options; |
| 86 | 76 | } |
| 87 | 77 | |
| 88 | 78 | /** |
| 79 | + * Soft lock for general option access and modification. | |
| 80 | + * | |
| 81 | + * @since 4.0.0 | |
| 82 | + */ | |
| 83 | + public static function lock() { | |
| 84 | + if ( self::$lock === null ) { | |
| 85 | + try { | |
| 86 | + self::$lock = new Groups_Lock( 'groups-options' ); | |
| 87 | + self::$lock->writer(); | |
| 88 | + } catch ( Groups_Lock_Exception $lex ) { | |
| 89 | + self::$lock = null; | |
| 90 | + } | |
| 91 | + } | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * Release soft lock for general option access. | |
| 96 | + * | |
| 97 | + * @since 4.0.0 | |
| 98 | + */ | |
| 99 | + public static function release() { | |
| 100 | + if ( self::$lock !== null ) { | |
| 101 | + self::$lock->release(); | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 89 | 106 | * Returns the value of a general setting. |
| 90 | 107 | * |
| 91 | 108 | * @param string $option the option id |
| 92 | 109 | * @param mixed $default default value to retrieve if option is not set |
| 93 | - * @return option value, $default if set or null | |
| 110 | + * | |
| 111 | + * @return mixed option value, $default if set or null | |
| 94 | 112 | */ |
| 95 | 113 | public static function get_option( $option, $default = null ) { |
| 96 | 114 | $options = self::get_options(); |
| 97 | 115 | $value = isset( $options[self::general][$option] ) ? $options[self::general][$option] : null; |
| @@ -103,18 +121,19 @@ | ||
| 103 | 121 | |
| 104 | 122 | |
| 105 | 123 | /** |
| 106 | 124 | * Returns the value of a user setting. |
| 107 | - * | |
| 125 | + * | |
| 108 | 126 | * @param string $option the option id |
| 109 | 127 | * @param mixed $default default value to retrieve if option is not set |
| 110 | 128 | * @param int $user_id retrieve option for this user, defaults to null for current user |
| 111 | - * @return option value, $default if set or null | |
| 129 | + * | |
| 130 | + * @return mixed option value, $default if set or null | |
| 112 | 131 | */ |
| 113 | 132 | public static function get_user_option( $option, $default = null, $user_id = null ) { |
| 114 | 133 | if ( $user_id === null ) { |
| 115 | 134 | $current_user = wp_get_current_user(); |
| 116 | - if ( !empty( $current_user ) ) { | |
| 135 | + if ( !empty( $current_user ) ) { // @phpstan-ignore empty.variable | |
| 117 | 136 | $user_id = $current_user->ID; |
| 118 | 137 | } |
| 119 | 138 | } |
| 120 | 139 | $value = null; |
| @@ -136,9 +155,12 @@ | ||
| 136 | 155 | */ |
| 137 | 156 | public static function update_option( $option, $new_value ) { |
| 138 | 157 | $options = self::get_options(); |
| 139 | 158 | $options[self::general][$option] = $new_value; |
| 140 | - update_option( self::option_key, $options ); | |
| 159 | + wp_cache_delete( self::option_key, 'options' ); | |
| 160 | + if ( update_option( self::option_key, $options ) ) { | |
| 161 | + do_action( 'groups_updated_option', $option, $new_value ); | |
| 162 | + } | |
| 141 | 163 | } |
| 142 | 164 | |
| 143 | 165 | /** |
| 144 | 166 | * Updates a user setting. |
| @@ -150,9 +172,9 @@ | ||
| 150 | 172 | public static function update_user_option( $option, $new_value, $user_id = null ) { |
| 151 | 173 | |
| 152 | 174 | if ( $user_id === null ) { |
| 153 | 175 | $current_user = wp_get_current_user(); |
| 154 | - if ( !empty( $current_user ) ) { | |
| 176 | + if ( !empty( $current_user ) ) { // @phpstan-ignore empty.variable | |
| 155 | 177 | $user_id = $current_user->ID; |
| 156 | 178 | } |
| 157 | 179 | } |
| 158 | 180 | |
| @@ -158,9 +180,12 @@ | ||
| 158 | 180 | |
| 159 | 181 | if ( $user_id !== null ) { |
| 160 | 182 | $options = self::get_options(); |
| 161 | 183 | $options[$user_id][$option] = $new_value; |
| 162 | - update_option( self::option_key, $options ); | |
| 184 | + wp_cache_delete( self::option_key, 'options' ); | |
| 185 | + if ( update_option( self::option_key, $options ) ) { | |
| 186 | + do_action( 'groups_updated_user_option', $option, $new_value, $user_id ); | |
| 187 | + } | |
| 163 | 188 | } |
| 164 | 189 | } |
| 165 | 190 | |
| 166 | 191 | /** |
| @@ -171,15 +196,18 @@ | ||
| 171 | 196 | public static function delete_option( $option ) { |
| 172 | 197 | $options = self::get_options(); |
| 173 | 198 | if ( isset( $options[self::general][$option] ) ) { |
| 174 | 199 | unset( $options[self::general][$option] ); |
| 175 | - update_option( self::option_key, $options ); | |
| 200 | + wp_cache_delete( self::option_key, 'options' ); | |
| 201 | + if ( update_option( self::option_key, $options ) ) { | |
| 202 | + do_action( 'groups_deleted_option', $option ); | |
| 203 | + } | |
| 176 | 204 | } |
| 177 | 205 | } |
| 178 | 206 | |
| 179 | 207 | /** |
| 180 | 208 | * Deletes a user setting. |
| 181 | - * | |
| 209 | + * | |
| 182 | 210 | * @param string $option the option's id |
| 183 | 211 | * @param int $user_id delete option for this user, defaults to null for current user |
| 184 | 212 | */ |
| 185 | 213 | public static function delete_user_option( $option, $user_id = null ) { |
| @@ -185,9 +213,9 @@ | ||
| 185 | 213 | public static function delete_user_option( $option, $user_id = null ) { |
| 186 | 214 | |
| 187 | 215 | if ( $user_id === null ) { |
| 188 | 216 | $current_user = wp_get_current_user(); |
| 189 | - if ( !empty( $current_user ) ) { | |
| 217 | + if ( !empty( $current_user ) ) { // @phpstan-ignore empty.variable | |
| 190 | 218 | $user_id = $current_user->ID; |
| 191 | 219 | } |
| 192 | 220 | } |
| 193 | 221 | |
| @@ -194,9 +222,12 @@ | ||
| 194 | 222 | if ( $user_id !== null ) { |
| 195 | 223 | $options = self::get_options(); |
| 196 | 224 | if ( isset( $options[$user_id][$option] ) ) { |
| 197 | 225 | unset( $options[$user_id][$option] ); |
| 198 | - update_option( self::option_key, $options ); | |
| 226 | + wp_cache_delete( self::option_key, 'options' ); | |
| 227 | + if ( update_option( self::option_key, $options ) ) { | |
| 228 | + do_action( 'groups_deleted_user_option', $user_id ); | |
| 229 | + } | |
| 199 | 230 | } |
| 200 | 231 | } |
| 201 | 232 | } |
| 202 | 233 | |
| @@ -203,7 +234,10 @@ | ||
| 203 | 234 | /** |
| 204 | 235 | * Deletes all settings - this includes user and general options. |
| 205 | 236 | */ |
| 206 | 237 | public static function flush_options() { |
| 207 | - delete_option( self::option_key ); | |
| 238 | + wp_cache_delete( self::option_key, 'options' ); | |
| 239 | + if ( delete_option( self::option_key ) ) { | |
| 240 | + do_action( 'groups_flushed_options' ); | |
| 241 | + } | |
| 208 | 242 | } |
| 209 | 243 | } |