Completed
Push — trunk ( 4bf362...177664 )
by
unknown
493:49 queued 491:11
created

CMB2_Option::get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.25

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 11
ccs 3
cts 4
cp 0.75
crap 4.25
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * CMB2 Utility classes for handling multi-dimensional array data for options
4
 *
5
 * @category  WordPress_Plugin
6
 * @package   CMB2
7
 * @author    WebDevStudios
8
 * @license   GPL-2.0+
9
 * @link      http://webdevstudios.com
10
 */
11
12
/**
13
 * Retrieves an instance of CMB2_Option based on the option key
14 4
 *
15
 * @package   CMB2
16 4
 * @author    WebDevStudios
17 2
 */
18 2
class CMB2_Options {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
19
	/**
20 4
	 * Array of all CMB2_Option instances
21
	 *
22
	 * @var   array
23
	 * @since 1.0.0
24
	 */
25
	protected static $option_sets = array();
26
27
	public static function get( $option_key ) {
28
29
		if ( empty( self::$option_sets ) || empty( self::$option_sets[ $option_key ] ) ) {
30
			self::$option_sets[ $option_key ] = new CMB2_Option( $option_key );
31
		}
32
33
		return self::$option_sets[ $option_key ];
34
	}
35
}
36
37
/**
38
 * Handles getting/setting of values to an option array
39
 * for a specific option key
40 2
 *
41 2
 * @package   CMB2
42 2
 * @author    WebDevStudios
43
 */
44
class CMB2_Option {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

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