1 | <?php |
||
43 | class CMB2_Option { |
||
44 | |||
45 | /** |
||
46 | * Options array |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $options = array(); |
||
50 | |||
51 | /** |
||
52 | * Current option key |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $key = ''; |
||
56 | |||
57 | /** |
||
58 | * Initiate option object |
||
59 | * @param string $option_key Option key where data will be saved. |
||
60 | * Leave empty for temporary data store. |
||
61 | * @since 2.0.0 |
||
62 | */ |
||
63 | public function __construct( $option_key = '' ) { |
||
66 | |||
67 | /** |
||
68 | * Delete the option from the db |
||
69 | * @since 2.0.0 |
||
70 | * @return bool Delete success or failure |
||
71 | */ |
||
72 | public function delete_option() { |
||
73 | $deleted = $this->key ? delete_option( $this->key ) : true; |
||
74 | $this->options = $deleted ? array() : $this->options; |
||
75 | return $this->options; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Removes an option from an option array |
||
80 | * @since 1.0.1 |
||
81 | * @param string $field_id Option array field key |
||
82 | 2 | * @return array Modified options |
|
83 | 2 | */ |
|
84 | public function remove( $field_id, $resave = false ) { |
||
85 | 2 | ||
86 | $this->get_options(); |
||
87 | 2 | ||
88 | 2 | if ( isset( $this->options[ $field_id ] ) ) { |
|
89 | unset( $this->options[ $field_id ] ); |
||
90 | } |
||
91 | |||
92 | if ( $resave ) { |
||
93 | $this->set(); |
||
94 | } |
||
95 | |||
96 | return $this->options; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Retrieves an option from an option array |
||
101 | * @since 1.0.1 |
||
102 | * @param string $field_id Option array field key |
||
103 | 1 | * @param mixed $default Fallback value for the option |
|
104 | 1 | * @return array Requested field or default |
|
105 | */ |
||
106 | 1 | public function get( $field_id, $default = false ) { |
|
107 | $opts = $this->get_options(); |
||
108 | 1 | ||
109 | if ( 'all' == $field_id ) { |
||
110 | return $opts; |
||
111 | } elseif ( array_key_exists( $field_id, $opts ) ) { |
||
112 | 1 | return false !== $opts[ $field_id ] ? $opts[ $field_id ] : $default; |
|
113 | } |
||
114 | |||
115 | 1 | return $default; |
|
116 | } |
||
117 | 1 | ||
118 | /** |
||
119 | * Updates Option data |
||
120 | * @since 1.0.1 |
||
121 | 1 | * @param string $field_id Option array field key |
|
122 | * @param mixed $value Value to update data with |
||
123 | * @param bool $resave Whether to re-save the data |
||
124 | * @param bool $single Whether data should not be an array |
||
125 | * @return boolean Return status of update |
||
126 | */ |
||
127 | public function update( $field_id, $value = '', $resave = false, $single = true ) { |
||
128 | $this->get_options(); |
||
129 | |||
130 | if ( true !== $field_id ) { |
||
131 | |||
132 | if ( ! $single ) { |
||
133 | 1 | // If multiple, add to array |
|
134 | 1 | $this->options[ $field_id ][] = $value; |
|
135 | } else { |
||
136 | 1 | $this->options[ $field_id ] = $value; |
|
137 | } |
||
138 | 1 | ||
139 | } |
||
140 | |||
141 | if ( $resave || true === $field_id ) { |
||
142 | return $this->set(); |
||
143 | 1 | } |
|
144 | |||
145 | return true; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Saves the option array |
||
150 | * Needs to be run after finished using remove/update_option |
||
151 | * @uses apply_filters() Calls 'cmb2_override_option_save_{$this->key}' hook |
||
152 | * to allow overwriting the option value to be stored. |
||
153 | * |
||
154 | * @since 1.0.1 |
||
155 | * @param array $options Optional options to override |
||
156 | 3 | * @return bool Success/Failure |
|
157 | 3 | */ |
|
158 | public function set( $options = array() ) { |
||
159 | 1 | $this->options = ! empty( $options ) || empty( $options ) && empty( $this->key ) |
|
160 | ? $options |
||
161 | 1 | : $this->options; |
|
162 | |||
163 | if ( empty( $this->key ) ) { |
||
164 | return false; |
||
165 | 1 | } |
|
166 | |||
167 | 1 | $test_save = apply_filters( "cmb2_override_option_save_{$this->key}", 'cmb2_no_override_option_save', $this->options, $this ); |
|
168 | |||
169 | 3 | if ( 'cmb2_no_override_option_save' !== $test_save ) { |
|
170 | return $test_save; |
||
171 | } |
||
172 | |||
173 | 1 | // If no override, update the option |
|
174 | return update_option( $this->key, $this->options ); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Retrieve option value based on name of option. |
||
179 | * @uses apply_filters() Calls 'cmb2_override_option_get_{$this->key}' hook to allow |
||
180 | * overwriting the option value to be retrieved. |
||
181 | * |
||
182 | * @since 1.0.1 |
||
183 | * @param mixed $default Optional. Default value to return if the option does not exist. |
||
184 | * @return mixed Value set for the option. |
||
185 | */ |
||
186 | public function get_options( $default = null ) { |
||
201 | |||
202 | } |
||
203 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.