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 | 5 | 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() { |
||
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 | * @return array Modified options |
||
83 | */ |
||
84 | 1 | public function remove( $field_id, $resave = false ) { |
|
85 | |||
86 | 1 | $this->get_options(); |
|
87 | |||
88 | 1 | if ( isset( $this->options[ $field_id ] ) ) { |
|
89 | unset( $this->options[ $field_id ] ); |
||
90 | } |
||
91 | |||
92 | 1 | if ( $resave ) { |
|
93 | $this->set(); |
||
94 | } |
||
95 | |||
96 | 1 | 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 | * @param mixed $default Fallback value for the option |
||
104 | * @return array Requested field or default |
||
105 | */ |
||
106 | 6 | public function get( $field_id, $default = false ) { |
|
107 | 6 | $opts = $this->get_options(); |
|
108 | |||
109 | 6 | if ( 'all' == $field_id ) { |
|
110 | return $opts; |
||
111 | 6 | } elseif ( array_key_exists( $field_id, $opts ) ) { |
|
112 | 4 | return false !== $opts[ $field_id ] ? $opts[ $field_id ] : $default; |
|
113 | } |
||
114 | |||
115 | 3 | return $default; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Updates Option data |
||
120 | * @since 1.0.1 |
||
121 | * @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 | 7 | public function update( $field_id, $value = '', $resave = false, $single = true ) { |
|
128 | 7 | $this->get_options(); |
|
129 | |||
130 | 7 | if ( true !== $field_id ) { |
|
131 | |||
132 | 7 | if ( ! $single ) { |
|
133 | // If multiple, add to array |
||
134 | $this->options[ $field_id ][] = $value; |
||
135 | } else { |
||
136 | 7 | $this->options[ $field_id ] = $value; |
|
137 | } |
||
138 | |||
139 | 7 | } |
|
140 | |||
141 | 7 | if ( $resave || true === $field_id ) { |
|
142 | 2 | return $this->set(); |
|
143 | } |
||
144 | |||
145 | 5 | 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 | * @return bool Success/Failure |
||
157 | */ |
||
158 | 6 | public function set( $options = array() ) { |
|
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 | 10 | 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.