Completed
Pull Request — trunk (#836)
by Andrew
23:57 queued 15:47
created

CMB2_Option::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
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
	 * @var   array
22
	 * @since 1.0.0
23
	 */
24
	protected static $option_sets = array();
25
26
	public static function get( $option_key ) {
27
28
		if ( empty( self::$option_sets ) || empty( self::$option_sets[ $option_key ] ) ) {
29
			self::$option_sets[ $option_key ] = new CMB2_Option( $option_key );
30
		}
31
32
		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 2
 * @package   CMB2
41 2
 * @author    WebDevStudios
42 2
 */
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
	public function __construct( $option_key = '' ) {
64
		$this->key = ! empty( $option_key ) ? $option_key : '';
65
	}
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 ) {
187
		if ( empty( $this->options ) && ! empty( $this->key ) ) {
188
189
			$test_get = apply_filters( "cmb2_override_option_get_{$this->key}", 'cmb2_no_override_option_get', $default, $this );
190
191
			if ( 'cmb2_no_override_option_get' !== $test_get ) {
192
				$this->options = $test_get;
193
			} else {
194
				// If no override, get the option
195
				$this->options = get_option( $this->key, $default );
196
			}
197
		}
198
199
		return (array) $this->options;
200
	}
201
202
}
203