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