PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 1.1.6
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v1.1.6
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Options.php

Options.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 1.1.6, at classes/Options.php

155 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace JP\CC;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 /**
10 * Class Options
11 * @package JP\CC
12 */
13 class Options {
14
15 /**
16 * Unique Prefix per plugin.
17 *
18 * @var string
19 */
20 public static $_prefix;
21
22 /**
23 * Keeps static copy of the options during runtime.
24 *
25 * @var null|array
26 */
27 private static $_data;
28
29 /**
30 * Initialize Options on run.
31 *
32 * @param string $prefix
33 */
34 public static function init( $prefix = 'jp_cc' ) {
35 // Set the prefix on init.
36 static::$_prefix = ! empty( $prefix ) ? trim( $prefix, '_' ) . '_' : '';
37 static::$_data = static::get_all();
38 }
39
40 /**
41 * Get Settings
42 *
43 * Retrieves all plugin settings
44 *
45 * @return array settings
46 */
47 public static function get_all() {
48 $settings = get_option( static:: $_prefix . 'settings' );
49 if ( ! is_array( $settings ) ) {
50 $settings = array();
51 }
52
53 return apply_filters( static:: $_prefix . 'get_options', $settings );
54 }
55
56 /**
57 * Get an option
58 *
59 * Looks to see if the specified setting exists, returns default if not
60 *
61 * @param string $key
62 * @param bool $default
63 *
64 * @return mixed|void
65 */
66 public static function get( $key = '', $default = false ) {
67 $value = isset( static::$_data[ $key ] ) ? static::$_data[ $key ] : $default;
68
69 return apply_filters( static::$_prefix . 'get_option', $value, $key, $default );
70 }
71
72 /**
73 * Update an option
74 *
75 * Updates an setting value in both the db and the global variable.
76 * Warning: Passing in an empty, false or null string value will remove
77 * the key from the _options array.
78 *
79 * @since 1.0.0
80 *
81 * @param string $key The Key to update
82 * @param string|bool|int $value The value to set the key to
83 *
84 * @return boolean True if updated, false if not.
85 */
86 public static function update( $key = '', $value = false ) {
87
88 // If no key, exit
89 if ( empty( $key ) ) {
90 return false;
91 }
92
93 if ( empty( $value ) ) {
94 $remove_option = static::delete( $key );
95
96 return $remove_option;
97 }
98
99 // First let's grab the current settings
100 $options = (array) get_option( static:: $_prefix . 'settings', array() );
101
102 // Let's let devs alter that value coming in
103 $value = apply_filters( static::$_prefix . 'update_option', $value, $key );
104
105 // Next let's try to update the value
106 $options[ $key ] = $value;
107 $did_update = update_option( static:: $_prefix . 'settings', $options );
108
109 // If it updated, let's update the global variable
110 if ( $did_update ) {
111 static::$_data[ $key ] = $value;
112
113 }
114
115 return $did_update;
116 }
117
118 /**
119 * Remove an option
120 *
121 * Removes a setting value in both the db and the global variable.
122 *
123 * @since 1.0.0
124 *
125 * @param string $key The Key to delete
126 *
127 * @return boolean True if updated, false if not.
128 */
129 public static function delete( $key = '' ) {
130
131 // If no key, exit
132 if ( empty( $key ) ) {
133 return false;
134 }
135
136 // First let's grab the current settings
137 $options = get_option( static:: $_prefix . 'settings' );
138
139 // Next let's try to update the value
140 if ( isset( $options[ $key ] ) ) {
141 unset( $options[ $key ] );
142 }
143
144 $did_update = update_option( static:: $_prefix . 'settings', $options );
145
146 // If it updated, let's update the global variable
147 if ( $did_update ) {
148 static::$_data = $options;
149 }
150
151 return $did_update;
152 }
153
154 }
155