Completed
Push — develop ( 0c9afc...d4082e )
by Zack
21:17 queued 17:22
created

GravityView_Entry_Approval_Status   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 291
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.32%

Importance

Changes 0
Metric Value
dl 0
loc 291
ccs 62
cts 71
cp 0.8732
rs 10
c 0
b 0
f 0
wmc 29
lcom 1
cbo 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A get_choices() 0 22 1
A get_all() 0 3 1
A get_values() 0 8 1
A is_valid() 0 10 3
A is_approved() 0 6 1
A is_disapproved() 0 6 1
A is_unapproved() 0 6 1
A get_labels() 0 8 1
A choice_pluck() 0 18 5
A get_label() 0 3 1
A get_string() 0 3 1
A get_title_attr() 0 3 1
A get_key() 0 3 1
B maybe_convert_status() 0 37 9
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @file class-gravityview-entry-approval-status.php
4
 * @package   GravityView
5
 * @license   GPL2+
6
 * @author    Katz Web Services, Inc.
7
 * @link      https://gravityview.co
8
 * @copyright Copyright 2016, Katz Web Services, Inc.
9
 *
10
 * @since 1.18
11
 */
12
13
/** If this file is called directly, abort. */
14
if ( ! defined( 'ABSPATH' ) ) {
15
	die;
16
}
17
18
/**
19
 * There are specific values of entry approval that are valid. This class holds them and manages access to them.
20
 *
21
 * @since 1.18
22
 */
23
final class GravityView_Entry_Approval_Status {
24
25
	/**
26
	 * @var int The value of the "Approved" status
27
	 */
28
	const APPROVED = 1;
29
30
	/**
31
	 * @var int The value of the "Disapproved" status
32
	 */
33
	const DISAPPROVED = 2;
34
35
	/**
36
	 * @var int Placeholder value for "Unapproved" status; in reality, it's not stored in the DB; the meta gets deleted.
37
	 */
38
	const UNAPPROVED = 3;
39
40
	/**
41
	 * GravityView_Entry_Approval_Status constructor.
42
	 */
43
	private function __construct() {}
44
45
	/**
46
	 * Match values to the labels
47
	 *
48
	 * @since 1.18
49
	 *
50
	 * @return array
51
	 */
52 2
	private static function get_choices() {
53
		return array(
54 2
			'disapproved' => array(
55
				'value'  => self::DISAPPROVED,
56 2
				'label'  => esc_html__( 'Disapproved', 'gravityview' ),
57 2
				'action' => __( 'Disapprove', 'gravityview' ),
58 2
				'title'  => __( 'Entry not approved for directory viewing. Click to approve this entry.', 'gravityview' ),
59
			),
60
			'approved'    => array(
61
				'value'  => self::APPROVED,
62 2
				'label'  => esc_html__( 'Approved', 'gravityview' ),
63 2
				'action' => __( 'Approve', 'gravityview' ),
64 2
				'title'  => __( 'Entry approved for directory viewing. Click to disapprove this entry.', 'gravityview' ),
65
			),
66
			'unapproved'  => array(
67
				'value'  => self::UNAPPROVED,
68 2
				'label'  => esc_html__( 'Unapproved', 'gravityview' ),
69 2
				'action' => __( 'Reset Approval', 'gravityview' ),
70 2
				'title'  => __( 'Entry not yet reviewed. Click to approve this entry.', 'gravityview' ),
71
			),
72
		);
73
	}
74
75
	/**
76
	 * Return array of status options
77
	 *
78
	 * @see GravityView_Entry_Approval_Status::get_choices
79
	 *
80
	 * @return array Associative array of available statuses
81
	 */
82 1
	public static function get_all() {
83 1
		return self::get_choices();
84
	}
85
86
	/**
87
	 * Get the status values as an array
88
	 *
89
	 * @since 1.18
90
	 *
91
	 * @return array Array of values for approval status choices
92
	 */
93
	public static function get_values() {
94
95
		$choices = self::get_choices();
96
97
		$values = wp_list_pluck( $choices, 'value' );
98
99
		return $values;
100
	}
101
102
	/**
103
	 * Convert previously-used values to the current values, for backward compatibility
104
	 *
105
	 * @since 1.18
106
	 *
107
	 * @param string $old_value The status
108
	 *
109
	 * @return int|string Current value, possibly converted from old value
110
	 */
111 7
	public static function maybe_convert_status( $old_value = '' ) {
112
113 7
		$new_value = $old_value;
114
115
		// Meta value does not exist yet
116 7
		if( false === $old_value ) {
117 6
			return self::UNAPPROVED;
118
		}
119
120
		// Meta value does not exist yet
121 6
		if( true === $old_value ) {
122 1
			return self::APPROVED;
123
		}
124
125 6
		switch ( (string) $old_value ) {
126
127
			// Approved values
128 6
			case 'Approved':
129 6
			case '1':
130 5
				$new_value = self::APPROVED;
131 5
				break;
132
133
			//Disapproved values
134 2
			case '0':
135 2
			case '2':
136 1
				$new_value = self::DISAPPROVED;
137 1
				break;
138
139
			// Unapproved values
140 2
			case '3':
141 1
			case '':
142 2
				$new_value = self::UNAPPROVED;
143 2
				break;
144
		}
145
146 6
		return $new_value;
147
	}
148
149
	/**
150
	 * Check whether the passed value is one of the defined values for entry approval
151
	 *
152
	 * @since 1.18
153
	 *
154
	 * @param mixed $value
155
	 *
156
	 * @return bool True: value is valid; false: value is not valid
157
	 */
158 1
	public static function is_valid( $value = NULL ) {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
159
160 1
		if ( ! is_scalar( $value ) || is_null( $value ) ) {
161 1
			return false;
162
		}
163
164 1
		$value = self::maybe_convert_status( $value );
165
166 1
		return in_array( $value, self::get_values(), true );
167
	}
168
169
	/**
170
	 * @param mixed $status Value to check approval of
171
	 *
172
	 * @since 1.18
173
	 *
174
	 * @return bool True: passed $status matches approved value
175
	 */
176 5
	public static function is_approved( $status ) {
177
178 5
		$status = self::maybe_convert_status( $status );
179
180 5
		return ( self::APPROVED === $status );
181
	}
182
183
	/**
184
	 * @param mixed $status Value to check approval of
185
	 *
186
	 * @since 1.18
187
	 *
188
	 * @return bool True: passed $status matches disapproved value
189
	 */
190 1
	public static function is_disapproved( $status ) {
191
192 1
		$status = self::maybe_convert_status( $status );
193
194 1
		return ( self::DISAPPROVED === $status );
195
	}
196
197
	/**
198
	 * @param mixed $status Value to check approval of
199
	 *
200
	 * @since 1.18
201
	 *
202
	 * @return bool True: passed $status matches unapproved value
203
	 */
204 1
	public static function is_unapproved( $status ) {
205
206 1
		$status = self::maybe_convert_status( $status );
207
208 1
		return ( self::UNAPPROVED === $status );
209
	}
210
211
	/**
212
	 * Get the labels for the status choices
213
	 *
214
	 * @since 1.18
215
	 *
216
	 * @return array Array of labels for the status choices ("Approved", "Disapproved")
217
	 */
218
	public static function get_labels() {
219
220
		$choices = self::get_choices();
221
222
		$labels = wp_list_pluck( $choices, 'label' );
223
224
		return $labels;
225
	}
226
227
228
	/**
229
	 * Pluck a certain field value from a status array
230
	 *
231
	 * Examples:
232
	 *
233
	 * <code>
234
	 * self::choice_pluck( 'disapproved', 'value' ); // Returns `2`
235
	 * self::choice_pluck( 'approved', 'label' ); // Returns `Approved`
236
	 * </code>
237
	 *
238
	 * @since 1.18
239
	 *
240
	 * @param int|string $status Valid status value or key (1 or "approved")
241
	 * @param string $attr_key Key name for the "value", "label", "action", "title". If "key", returns the matched key instead of value.
242
	 *
243
	 * @return false|string False if match isn't not found
244
	 */
245 2
	private static function choice_pluck( $status, $attr_key = '' ) {
246 2
		$choices = self::get_choices();
247
248 2
		foreach ( $choices as $key => $choice ) {
249
250
			// Is the passed status value the same as the choice value or key?
251 2
			if ( $status === $choice['value'] || $status === $key ) {
252
253 2
				if( 'key' === $attr_key ) {
254 2
					return $key;
255
				} else {
256 2
					return \GV\Utils::get( $choice, $attr_key, false );
257
				}
258
			}
259
		}
260
261
		return false;
262
	}
263
264
	/**
265
	 * Get the label for a specific approval value
266
	 *
267
	 * @since 1.18
268
	 *
269
	 * @param int|string $value_or_key Valid status value or key (1 or "approved")
270
	 *
271
	 * @return string|false Label of value ("Approved"). If invalid value, return false.
272
	 */
273 2
	public static function get_label( $value_or_key ) {
274 2
		return self::choice_pluck( $value_or_key, 'label' );
275
	}
276
277
	/**
278
	 * Get the label for a specific approval value
279
	 *
280
	 * @since 1.18
281
	 *
282
	 * @param int|string $value_or_key Valid status value or key (1 or "approved")
283
	 *
284
	 * @return string|false Label of value ("Approved"). If invalid value, return false.
285
	 */
286 2
	public static function get_string( $value_or_key, $string_key = '' ) {
287 2
		return self::choice_pluck( $value_or_key, $string_key );
288
	}
289
290
	/**
291
	 * Get the label for a specific approval value
292
	 *
293
	 * @since 1.18
294
	 *
295
	 * @param int|string $value_or_key Valid status value or key (1 or "approved")
296
	 *
297
	 * @return string|false Label of value ("Approved"). If invalid value, return false.
298
	 */
299 1
	public static function get_title_attr( $value_or_key ) {
300 1
		return self::choice_pluck( $value_or_key, 'title' );
301
	}
302
303
	/**
304
	 * Get the status key for a value
305
	 *
306
	 * @param int $value Status value (1, 2, 3)
307
	 *
308
	 * @return string|false The status key at status $value, if exists. If not exists, false.
309
	 */
310 3
	public static function get_key( $value ) {
311 3
		return self::choice_pluck( $value, 'key' );
312
	}
313
}
314