PluginProbe
Groups – Memberships and Access Control / 1.11.1
Groups – Memberships and Access Control v1.11.1
4.7.1 4.7.0 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 All 131 releases
groups / lib / core / class-groups-options.php

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

210 lines 4.9 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 * No instances are needed.
47 */
48 private function __construct() {
49 }
50
51 /**
52 * No cloning.
53 */
54 private function __clone() {
55 }
56
57 /**
58 * Would be pointless.
59 */
60 private function __wakeup() {
61 }
62
63 /**
64 * Registers Groups options (not autoloaded).
65 */
66 public static function init() {
67 $options = get_option( self::option_key );
68 if ( $options === false ) {
69 $options = array( self::general => array() );
70 add_option( self::option_key, $options, null, 'no' );
71 }
72 }
73
74 /**
75 * Returns the current Groups options and initializes them
76 * through init() if needed.
77 * @return array Groups options
78 */
79 private static function get_options() {
80 $options = get_option( self::option_key );
81 if ( $options === false ) {
82 self::init();
83 $options = get_option( self::option_key );
84 }
85 return $options;
86 }
87
88 /**
89 * Returns the value of a general setting.
90 *
91 * @param string $option the option id
92 * @param mixed $default default value to retrieve if option is not set
93 * @return option value, $default if set or null
94 */
95 public static function get_option( $option, $default = null ) {
96 $options = self::get_options();
97 $value = isset( $options[self::general][$option] ) ? $options[self::general][$option] : null;
98 if ( $value === null ) {
99 $value = $default;
100 }
101 return $value;
102 }
103
104
105 /**
106 * Returns the value of a user setting.
107 *
108 * @param string $option the option id
109 * @param mixed $default default value to retrieve if option is not set
110 * @param int $user_id retrieve option for this user, defaults to null for current user
111 * @return option value, $default if set or null
112 */
113 public static function get_user_option( $option, $default = null, $user_id = null ) {
114 if ( $user_id === null ) {
115 $current_user = wp_get_current_user();
116 if ( !empty( $current_user ) ) {
117 $user_id = $current_user->ID;
118 }
119 }
120 $value = null;
121 if ( $user_id !== null ) {
122 $options = self::get_options();
123 $value = isset( $options[$user_id][$option] ) ? $options[$user_id][$option] : null;
124 }
125 if ( $value === null ) {
126 $value = $default;
127 }
128 return $value;
129 }
130
131 /**
132 * Updates a general setting.
133 *
134 * @param string $option the option's id
135 * @param mixed $new_value the new value
136 */
137 public static function update_option( $option, $new_value ) {
138 $options = self::get_options();
139 $options[self::general][$option] = $new_value;
140 update_option( self::option_key, $options );
141 }
142
143 /**
144 * Updates a user setting.
145 *
146 * @param string $option the option's id
147 * @param mixed $new_value the new value
148 * @param int $user_id update option for this user, defaults to null for current user
149 */
150 public static function update_user_option( $option, $new_value, $user_id = null ) {
151
152 if ( $user_id === null ) {
153 $current_user = wp_get_current_user();
154 if ( !empty( $current_user ) ) {
155 $user_id = $current_user->ID;
156 }
157 }
158
159 if ( $user_id !== null ) {
160 $options = self::get_options();
161 $options[$user_id][$option] = $new_value;
162 update_option( self::option_key, $options );
163 }
164 }
165
166 /**
167 * Deletes a general setting.
168 *
169 * @param string $option the option's id
170 */
171 public static function delete_option( $option ) {
172 $options = self::get_options();
173 if ( isset( $options[self::general][$option] ) ) {
174 unset( $options[self::general][$option] );
175 update_option( self::option_key, $options );
176 }
177 }
178
179 /**
180 * Deletes a user setting.
181 *
182 * @param string $option the option's id
183 * @param int $user_id delete option for this user, defaults to null for current user
184 */
185 public static function delete_user_option( $option, $user_id = null ) {
186
187 if ( $user_id === null ) {
188 $current_user = wp_get_current_user();
189 if ( !empty( $current_user ) ) {
190 $user_id = $current_user->ID;
191 }
192 }
193
194 if ( $user_id !== null ) {
195 $options = self::get_options();
196 if ( isset( $options[$user_id][$option] ) ) {
197 unset( $options[$user_id][$option] );
198 update_option( self::option_key, $options );
199 }
200 }
201 }
202
203 /**
204 * Deletes all settings - this includes user and general options.
205 */
206 public static function flush_options() {
207 delete_option( self::option_key );
208 }
209 }
210