| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Groups Options. |
| 4 |
* |
| 5 |
* @package Plugin_Groups |
| 6 |
* @author David Cramer <david@digilab.co.za> |
| 7 |
* @license GPL-2.0+ |
| 8 |
* @link |
| 9 |
* @copyright 2015 David Cramer <david@digilab.co.za> |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Plugin_Groups_Options class. |
| 14 |
* |
| 15 |
* @package Plugin_Groups |
| 16 |
* @author David Cramer <david@digilab.co.za> |
| 17 |
*/ |
| 18 |
class Plugin_Groups_Options { |
| 19 |
|
| 20 |
public static $option_name = 'plugin_groups'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Create a new plugin_groups. |
| 24 |
* |
| 25 |
* @since 0.0.1 |
| 26 |
* |
| 27 |
* @param string $name Name for plugin_groups. |
| 28 |
* @param string $slug Slug for plugin_groups. |
| 29 |
* |
| 30 |
* @return array|void Config array for new plugin_groups if it exists. Void if not. |
| 31 |
*/ |
| 32 |
public static function create( $name, $slug ) { |
| 33 |
$name = sanitize_text_field( $name ); |
| 34 |
$slug = sanitize_title_with_dashes( $slug ); |
| 35 |
$item_id = self::create_unique_id(); |
| 36 |
$registry = self::get_registry(); |
| 37 |
|
| 38 |
if( ! isset( $registry[ $item_id ] ) ){ |
| 39 |
$new = array( |
| 40 |
'id' => $item_id, |
| 41 |
'name' => $name, |
| 42 |
'slug' => $slug, |
| 43 |
); |
| 44 |
|
| 45 |
$registry[ $item_id ] = $new; |
| 46 |
|
| 47 |
self::update( $new, $registry ); |
| 48 |
|
| 49 |
return $new; |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get an individual item by its ID. |
| 57 |
* |
| 58 |
* @since 0.0.1 |
| 59 |
* |
| 60 |
* @param string $id plugin_groups ID |
| 61 |
* |
| 62 |
* @return bool|array plugin_groups config or false if not found. |
| 63 |
*/ |
| 64 |
public static function get_single( $id ) { |
| 65 |
$option_name = self::item_option_name( $id ); |
| 66 |
$plugin_groups = get_option( $option_name, false ); |
| 67 |
|
| 68 |
// try slug based lookup |
| 69 |
if( false === $plugin_groups ){ |
| 70 |
$registry = self::get_registry(); |
| 71 |
foreach( $registry as $single_id => $single ){ |
| 72 |
if( $single['slug'] === $id ){ |
| 73 |
$option_name = self::item_option_name( $single_id ); |
| 74 |
$plugin_groups = get_option( $option_name, false ); |
| 75 |
break; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Filter a plugin_groups config before returning |
| 82 |
* |
| 83 |
* @param array $plugin_groups The config to be returned |
| 84 |
* @param string $option_name The name of the option it was stored in. |
| 85 |
* |
| 86 |
* @since 0.0.1 |
| 87 |
*/ |
| 88 |
return apply_filters( 'plugin_groups_get_single', $plugin_groups, $option_name ); |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get the registry of plugin_groups. |
| 94 |
* |
| 95 |
* @since 0.0.1 |
| 96 |
* |
| 97 |
* @return mixed|void |
| 98 |
*/ |
| 99 |
|
| 100 |
public static function get_registry() { |
| 101 |
$registry = get_option( self::registry_name(), array() ); |
| 102 |
|
| 103 |
/** |
| 104 |
* Filter the registry before returning |
| 105 |
* |
| 106 |
* @since 0.0.1 |
| 107 |
*/ |
| 108 |
return apply_filters( 'plugin_groups_get_registry', $registry ); |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Update both a single item and the registry |
| 114 |
* |
| 115 |
* @since 0.0.1 |
| 116 |
* |
| 117 |
* @param array $config Single item config. |
| 118 |
* @param array|bool. Optional. If false, current registry will be used, if is array, that reg |
| 119 |
*/ |
| 120 |
public static function update( $config, $update_registry = false ) { |
| 121 |
if ( ! is_array( $update_registry ) ) { |
| 122 |
$update_registry = self::get_registry(); |
| 123 |
} |
| 124 |
|
| 125 |
if( isset( $config['id'] ) && !empty( $update_registry[ $config['id'] ] ) ){ |
| 126 |
$update = array( |
| 127 |
'id' => $config['id'], |
| 128 |
'name' => $config['name'], |
| 129 |
'slug' => $config['slug'], |
| 130 |
|
| 131 |
); |
| 132 |
|
| 133 |
// add search form to registery |
| 134 |
if( ! empty( $config['search_form'] ) ){ |
| 135 |
$updated_registery['search_form'] = $config['search_form']; |
| 136 |
} |
| 137 |
|
| 138 |
$update_registry[ $config[ 'id' ] ] = $update; |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
self::save_registry( $update_registry ); |
| 143 |
|
| 144 |
self::save_single( $config['id'], $config ); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Delete an item and clear it from the registry |
| 150 |
* |
| 151 |
* @since 0.0.1 |
| 152 |
* |
| 153 |
* @param string $id Item ID |
| 154 |
* |
| 155 |
* @return bool True on success. |
| 156 |
*/ |
| 157 |
public static function delete( $id ) { |
| 158 |
$deleted = delete_option( self::item_option_name( $id ) ); |
| 159 |
if ( $deleted ) { |
| 160 |
$registry = self::get_registry(); |
| 161 |
if ( isset( $registry[ $id ] ) ) { |
| 162 |
unset( $registry[ $id ] ); |
| 163 |
return self::save_registry( $registry ); |
| 164 |
|
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Save the registry of items. |
| 172 |
* |
| 173 |
* @since 0.0.1 |
| 174 |
* |
| 175 |
* @param array $registry The registry |
| 176 |
*/ |
| 177 |
protected static function save_registry( $registry ) { |
| 178 |
return update_option( self::registry_name(), $registry ); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Save an individual item. |
| 184 |
* |
| 185 |
* @param string $id plugin_groups ID |
| 186 |
* @param array $config plugin_groups config |
| 187 |
*/ |
| 188 |
protected static function save_single( $id, $config ) { |
| 189 |
return update_option( self::item_option_name( $id ), $config ); |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
/** |
| 196 |
* Get the name to use for an individual item option. |
| 197 |
* |
| 198 |
* @since 0.0.1 |
| 199 |
* |
| 200 |
* @access protected |
| 201 |
* |
| 202 |
* @param string $id |
| 203 |
* |
| 204 |
* @return string |
| 205 |
*/ |
| 206 |
protected static function item_option_name( $id ) { |
| 207 |
$name = self::$option_name . '_' . $id; |
| 208 |
if ( 50 < strlen( $name ) ) { |
| 209 |
$name = md5( $name ); |
| 210 |
} |
| 211 |
|
| 212 |
return $name; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Get the name used for the registry option |
| 218 |
* |
| 219 |
* @since 0.0.1 |
| 220 |
* |
| 221 |
* @access protected |
| 222 |
* |
| 223 |
* @return string |
| 224 |
*/ |
| 225 |
protected static function registry_name() { |
| 226 |
return '_' . self::$option_name . '_registry'; |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Create unique ID |
| 232 |
* |
| 233 |
* @since 0.0.1 |
| 234 |
* |
| 235 |
* @access protected |
| 236 |
* |
| 237 |
* @return string |
| 238 |
*/ |
| 239 |
protected static function create_unique_id() { |
| 240 |
$slug_parts = explode( '_', 'plugin_groups' ); |
| 241 |
$slug = ''; |
| 242 |
|
| 243 |
foreach ( $slug_parts as $slug_part ) { |
| 244 |
$slug .= substr( $slug_part, 0,1 ); |
| 245 |
} |
| 246 |
|
| 247 |
$slug = strtoupper( $slug ); |
| 248 |
|
| 249 |
$item_id = uniqid( $slug ) . rand( 100, 999 ); |
| 250 |
|
| 251 |
return $item_id; |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
} |
| 257 |
|