Completed
Push — trunk ( 7752bd...b7e3cf )
by Justin
07:15
created

CMB2_Option::get_options()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 1
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
ccs 7
cts 9
cp 0.7778
crap 4.1755
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
 *
15
 * @package   CMB2
16
 * @author    WebDevStudios
17
 */
18
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
	 * Array of all CMB2_Option instances
21
	 * @var   array
22
	 * @since 1.0.0
23
	 */
24
	protected static $option_sets = array();
25
26 12
	public static function get( $option_key ) {
27
28 12
		if ( empty( self::$option_sets ) || empty( self::$option_sets[ $option_key ] ) ) {
29 6
			self::$option_sets[ $option_key ] = new CMB2_Option( $option_key );
30 6
		}
31
32 12
		return self::$option_sets[ $option_key ];
33
	}
34
}
35
36
/**
37
 * Handles getting/setting of values to an option array
38
 * for a specific option key
39
 *
40
 * @package   CMB2
41
 * @author    WebDevStudios
42
 */
43
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...
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 6
	public function __construct( $option_key = '' ) {
64 6
		$this->key = ! empty( $option_key ) ? $option_key : '';
65 6
	}
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
	 * @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 7
	public function get( $field_id, $default = false ) {
107 7
		$opts = $this->get_options();
108
109 7
		if ( 'all' == $field_id ) {
110
			return $opts;
111 7
		} elseif ( array_key_exists( $field_id, $opts ) ) {
112 3
			return false !== $opts[ $field_id ] ? $opts[ $field_id ] : $default;
113
		}
114
115 4
		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 3
			return $this->set();
143
		}
144
145 4
		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 7
	public function set( $options = array() ) {
159 7
		$this->options = ! empty( $options ) || empty( $options ) && empty( $this->key )
160 7
			? $options
161 7
			: $this->options;
162
163 7
		if ( empty( $this->key ) ) {
164 2
			return false;
165
		}
166
167 5
		$test_save = apply_filters( "cmb2_override_option_save_{$this->key}", 'cmb2_no_override_option_save', $this->options, $this );
168
169 5
		if ( 'cmb2_no_override_option_save' !== $test_save ) {
170
			return $test_save;
171
		}
172
173
		// If no override, update the option
174 5
		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 11
	public function get_options( $default = null ) {
187 11
		if ( empty( $this->options ) && ! empty( $this->key ) ) {
188
189 4
			$test_get = apply_filters( "cmb2_override_option_get_{$this->key}", 'cmb2_no_override_option_get', $default, $this );
190
191 4
			if ( 'cmb2_no_override_option_get' !== $test_get ) {
192
				$this->options = $test_get;
193
			} else {
194
				// If no override, get the option
195 4
				$this->options = get_option( $this->key, $default );
196
			}
197 4
		}
198
199 11
		return (array) $this->options;
200
	}
201
202
}
203