PluginProbe
Groups – Memberships and Access Control / trunk
Groups – Memberships and Access Control vtrunk
4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 1.13.1 1.2.0 All 129 releases
groups / lib / core / class-groups-options.php

class-groups-options.php in Groups – Memberships and Access Control trunk, at lib/core/class-groups-options.php

244 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class-groups-options.php
4 *
5 * Copyright (c) "kento" Karim Rahimpur www.itthinx.com
6 *
7 * This code is released under the GNU General Public License.
8 * See COPYRIGHT.txt and LICENSE.txt.
9 *
10 * This code is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * This header and all notices must be kept intact.
16 *
17 * @author Karim Rahimpur
18 * @package groups
19 * @since groups 1.0.0
20 */
21
22 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 /**
27 * Groups options handler
28 */
29 class Groups_Options {
30
31 /**
32 * Groups plugin option key.
33 *
34 * @var string
35 */
36 const option_key = 'groups_options';
37
38 /**
39 * General option index.
40 *
41 * @var string
42 */
43 const general = 'general';
44
45 /**
46 * @var Groups_Lock
47 *
48 * @since 4.0.0
49 */
50 private static $lock = null;
51
52 /**
53 * Registers Groups options (not autoloaded).
54 */
55 public static function init() {
56 $options = get_option( self::option_key );
57 if ( $options === false ) {
58 $options = array( self::general => array() );
59 add_option( self::option_key, $options, '', false );
60 }
61 }
62
63 /**
64 * Returns the current Groups options and initializes them
65 * through init() if needed.
66 *
67 * @return array Groups options
68 */
69 private static function get_options() {
70 $options = get_option( self::option_key );
71 if ( $options === false ) {
72 self::init();
73 $options = get_option( self::option_key );
74 }
75 return $options;
76 }
77
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 /**
106 * Returns the value of a general setting.
107 *
108 * @param string $option the option id
109 * @param mixed $default default value to retrieve if option is not set
110 *
111 * @return mixed option value, $default if set or null
112 */
113 public static function get_option( $option, $default = null ) {
114 $options = self::get_options();
115 $value = isset( $options[self::general][$option] ) ? $options[self::general][$option] : null;
116 if ( $value === null ) {
117 $value = $default;
118 }
119 return $value;
120 }
121
122
123 /**
124 * Returns the value of a user setting.
125 *
126 * @param string $option the option id
127 * @param mixed $default default value to retrieve if option is not set
128 * @param int $user_id retrieve option for this user, defaults to null for current user
129 *
130 * @return mixed option value, $default if set or null
131 */
132 public static function get_user_option( $option, $default = null, $user_id = null ) {
133 if ( $user_id === null ) {
134 $current_user = wp_get_current_user();
135 if ( !empty( $current_user ) ) { // @phpstan-ignore empty.variable
136 $user_id = $current_user->ID;
137 }
138 }
139 $value = null;
140 if ( $user_id !== null ) {
141 $options = self::get_options();
142 $value = isset( $options[$user_id][$option] ) ? $options[$user_id][$option] : null;
143 }
144 if ( $value === null ) {
145 $value = $default;
146 }
147 return $value;
148 }
149
150 /**
151 * Updates a general setting.
152 *
153 * @param string $option the option's id
154 * @param mixed $new_value the new value
155 */
156 public static function update_option( $option, $new_value ) {
157 $options = self::get_options();
158 $options[self::general][$option] = $new_value;
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 }
163 }
164
165 /**
166 * Updates a user setting.
167 *
168 * @param string $option the option's id
169 * @param mixed $new_value the new value
170 * @param int $user_id update option for this user, defaults to null for current user
171 */
172 public static function update_user_option( $option, $new_value, $user_id = null ) {
173
174 if ( $user_id === null ) {
175 $current_user = wp_get_current_user();
176 if ( !empty( $current_user ) ) { // @phpstan-ignore empty.variable
177 $user_id = $current_user->ID;
178 }
179 }
180
181 if ( $user_id !== null ) {
182 $options = self::get_options();
183 $options[$user_id][$option] = $new_value;
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 }
188 }
189 }
190
191 /**
192 * Deletes a general setting.
193 *
194 * @param string $option the option's id
195 */
196 public static function delete_option( $option ) {
197 $options = self::get_options();
198 if ( isset( $options[self::general][$option] ) ) {
199 unset( $options[self::general][$option] );
200 wp_cache_delete( self::option_key, 'options' );
201 if ( update_option( self::option_key, $options ) ) {
202 do_action( 'groups_deleted_option', $option );
203 }
204 }
205 }
206
207 /**
208 * Deletes a user setting.
209 *
210 * @param string $option the option's id
211 * @param int $user_id delete option for this user, defaults to null for current user
212 */
213 public static function delete_user_option( $option, $user_id = null ) {
214
215 if ( $user_id === null ) {
216 $current_user = wp_get_current_user();
217 if ( !empty( $current_user ) ) { // @phpstan-ignore empty.variable
218 $user_id = $current_user->ID;
219 }
220 }
221
222 if ( $user_id !== null ) {
223 $options = self::get_options();
224 if ( isset( $options[$user_id][$option] ) ) {
225 unset( $options[$user_id][$option] );
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 }
230 }
231 }
232 }
233
234 /**
235 * Deletes all settings - this includes user and general options.
236 */
237 public static function flush_options() {
238 wp_cache_delete( self::option_key, 'options' );
239 if ( delete_option( self::option_key ) ) {
240 do_action( 'groups_flushed_options' );
241 }
242 }
243 }
244