PluginProbe
Plugin Groups / 1.2.1
Plugin Groups v1.2.1
trunk 1.0.3 1.1.0 1.2.1 1.2.2 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.9 3.0.0
plugin-groups / classes / class-options.php

class-options.php in Plugin Groups 1.2.1, at classes/class-options.php

259 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $default = array( 'id' => $id );
67 $plugin_groups = get_option( $option_name, $default );
68
69 // try slug based lookup
70 if( false === $plugin_groups ){
71 $registry = self::get_registry();
72 foreach( $registry as $single_id => $single ){
73 if( $single['slug'] === $id ){
74 $option_name = self::item_option_name( $single_id );
75 $plugin_groups = get_option( $option_name, array() );
76 break;
77 }
78 }
79 }
80
81 /**
82 * Filter a plugin_groups config before returning
83 *
84 * @param array $plugin_groups The config to be returned
85 * @param string $option_name The name of the option it was stored in.
86 *
87 * @since 0.0.1
88 */
89 return apply_filters( 'plugin_groups_get_single', $plugin_groups, $option_name );
90
91 }
92
93 /**
94 * Get the registry of plugin_groups.
95 *
96 * @since 0.0.1
97 *
98 * @return mixed|void
99 */
100
101 public static function get_registry() {
102 $registry = get_option( self::registry_name(), array() );
103
104 /**
105 * Filter the registry before returning
106 *
107 * @since 0.0.1
108 */
109 return apply_filters( 'plugin_groups_get_registry', $registry );
110
111 }
112
113 /**
114 * Update both a single item and the registry
115 *
116 * @since 0.0.1
117 *
118 * @param array $config Single item config.
119 * @param array|bool. Optional. If false, current registry will be used, if is array, that reg
120 */
121 public static function update( $config, $update_registry = false ) {
122 if ( ! is_array( $update_registry ) ) {
123 $update_registry = self::get_registry();
124 }
125
126 if( isset( $config['id'] ) && !empty( $update_registry[ $config['id'] ] ) ){
127 $update = array(
128 'id' => $config['id'],
129 'name' => $config['name'],
130 'slug' => $config['slug'],
131
132 );
133
134 // add search form to registery
135 if( ! empty( $config['search_form'] ) ){
136 $updated_registery['search_form'] = $config['search_form'];
137 }
138
139 $update_registry[ $config[ 'id' ] ] = $update;
140
141 }
142
143 self::save_registry( $update_registry );
144
145 self::save_single( $config['id'], $config );
146
147 }
148
149 /**
150 * Delete an item and clear it from the registry
151 *
152 * @since 0.0.1
153 *
154 * @param string $id Item ID
155 *
156 * @return bool True on success.
157 */
158 public static function delete( $id ) {
159 $deleted = delete_option( self::item_option_name( $id ) );
160 if ( $deleted ) {
161 $registry = self::get_registry();
162 if ( isset( $registry[ $id ] ) ) {
163 unset( $registry[ $id ] );
164 return self::save_registry( $registry );
165
166 }
167 }
168
169 }
170
171 /**
172 * Save the registry of items.
173 *
174 * @since 0.0.1
175 *
176 * @param array $registry The registry
177 */
178 protected static function save_registry( $registry ) {
179 return update_site_option( self::registry_name(), $registry );
180
181 }
182
183 /**
184 * Save an individual item.
185 *
186 * @param string $id plugin_groups ID
187 * @param array $config plugin_groups config
188 */
189 protected static function save_single( $id, $config ) {
190
191 return update_site_option( self::item_option_name( $id ), $config );
192
193 }
194
195
196
197 /**
198 * Get the name to use for an individual item option.
199 *
200 * @since 0.0.1
201 *
202 * @access protected
203 *
204 * @param string $id
205 *
206 * @return string
207 */
208 protected static function item_option_name( $id ) {
209 $name = self::$option_name . '_' . $id;
210 if ( 50 < strlen( $name ) ) {
211 $name = md5( $name );
212 }
213
214 return $name;
215
216 }
217
218 /**
219 * Get the name used for the registry option
220 *
221 * @since 0.0.1
222 *
223 * @access protected
224 *
225 * @return string
226 */
227 protected static function registry_name() {
228 return '_' . self::$option_name . '_registry';
229
230 }
231
232 /**
233 * Create unique ID
234 *
235 * @since 0.0.1
236 *
237 * @access protected
238 *
239 * @return string
240 */
241 protected static function create_unique_id() {
242 $slug_parts = explode( '_', 'plugin_groups' );
243 $slug = '';
244
245 foreach ( $slug_parts as $slug_part ) {
246 $slug .= substr( $slug_part, 0,1 );
247 }
248
249 $slug = strtoupper( $slug );
250
251 $item_id = uniqid( $slug ) . rand( 100, 999 );
252
253 return $item_id;
254
255 }
256
257
258 }
259