Completed
Push — master ( 3a59f6...f89873 )
by Zack
43:57 queued 28:41
created

is_disapproved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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' => esc_html__( 'Disapprove', 'gravityview' ),
58 2
				'title'  => esc_html__( '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' => esc_html__( 'Approve', 'gravityview' ),
64 2
				'title'  => esc_html__( 'Entry approved for directory viewing. Click to disapprove this entry.', 'gravityview' ),
65 2
				'title_popover'  => esc_html__( 'Entry approved for directory viewing. Click to disapprove this entry.', 'gravityview' ),
66
			),
67
			'unapproved'  => array(
68
				'value'  => self::UNAPPROVED,
69 2
				'label'  => esc_html__( 'Unapproved', 'gravityview' ),
70 2
				'action' => esc_html__( 'Reset Approval', 'gravityview' ),
71 2
				'title'  => esc_html__( 'Entry not yet reviewed. Click to approve this entry.', 'gravityview' ),
72
			),
73
		);
74
	}
75
76
	/**
77
	 * Return array of status options
78
	 *
79
	 * @see GravityView_Entry_Approval_Status::get_choices
80
	 *
81
	 * @return array Associative array of available statuses
82
	 */
83 1
	public static function get_all() {
84 1
		return self::get_choices();
85
	}
86
87
	/**
88
	 * Get the status values as an array
89
	 *
90
	 * @since 1.18
91
	 *
92
	 * @return array Array of values for approval status choices
93
	 */
94 1
	public static function get_values() {
95
96 1
		$choices = self::get_choices();
97
98 1
		$values = wp_list_pluck( $choices, 'value' );
99
100 1
		return $values;
101
	}
102
103
	/**
104
	 * Convert previously-used values to the current values, for backward compatibility
105
	 *
106
	 * @since 1.18
107
	 *
108
	 * @param string $old_value The status
109
	 *
110
	 * @return int|string Current value, possibly converted from old value
111
	 */
112 7
	public static function maybe_convert_status( $old_value = '' ) {
113
114 7
		$new_value = $old_value;
115
116
		// Meta value does not exist yet
117 7
		if( false === $old_value ) {
118 6
			return self::UNAPPROVED;
119
		}
120
121
		// Meta value does not exist yet
122 7
		if( true === $old_value ) {
123 1
			return self::APPROVED;
124
		}
125
126 7
		switch ( (string) $old_value ) {
127
128
			// Approved values
129 7
			case 'Approved':
130 7
			case '1':
131 5
				$new_value = self::APPROVED;
132 5
				break;
133
134
			//Disapproved values
135 3
			case '0':
136 3
			case '2':
137 2
				$new_value = self::DISAPPROVED;
138 2
				break;
139
140
			// Unapproved values
141 2
			case '3':
142 1
			case '':
143 2
				$new_value = self::UNAPPROVED;
144 2
				break;
145
		}
146
147 7
		return $new_value;
148
	}
149
150
	/**
151
	 * Check whether the passed value is one of the defined values for entry approval
152
	 *
153
	 * @since 1.18
154
	 *
155
	 * @param mixed $value
156
	 *
157
	 * @return bool True: value is valid; false: value is not valid
158
	 */
159 2
	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...
160
161 2
		if ( ! is_scalar( $value ) || is_null( $value ) ) {
162 1
			return false;
163
		}
164
165 2
		$value = self::maybe_convert_status( $value );
166
167 2
		return in_array( $value, self::get_values(), true );
168
	}
169
170
	/**
171
	 * @param mixed $status Value to check approval of
172
	 *
173
	 * @since 1.18
174
	 *
175
	 * @return bool True: passed $status matches approved value
176
	 */
177 5
	public static function is_approved( $status ) {
178
179 5
		$status = self::maybe_convert_status( $status );
180
181 5
		return ( self::APPROVED === $status );
182
	}
183
184
	/**
185
	 * @param mixed $status Value to check approval of
186
	 *
187
	 * @since 1.18
188
	 *
189
	 * @return bool True: passed $status matches disapproved value
190
	 */
191 1
	public static function is_disapproved( $status ) {
192
193 1
		$status = self::maybe_convert_status( $status );
194
195 1
		return ( self::DISAPPROVED === $status );
196
	}
197
198
	/**
199
	 * @param mixed $status Value to check approval of
200
	 *
201
	 * @since 1.18
202
	 *
203
	 * @return bool True: passed $status matches unapproved value
204
	 */
205 1
	public static function is_unapproved( $status ) {
206
207 1
		$status = self::maybe_convert_status( $status );
208
209 1
		return ( self::UNAPPROVED === $status );
210
	}
211
212
	/**
213
	 * Get the labels for the status choices
214
	 *
215
	 * @since 1.18
216
	 *
217
	 * @return array Array of labels for the status choices ("Approved", "Disapproved")
218
	 */
219
	public static function get_labels() {
220
221
		$choices = self::get_choices();
222
223
		$labels = wp_list_pluck( $choices, 'label' );
224
225
		return $labels;
226
	}
227
228
229
	/**
230
	 * Pluck a certain field value from a status array
231
	 *
232
	 * Examples:
233
	 *
234
	 * <code>
235
	 * self::choice_pluck( 'disapproved', 'value' ); // Returns `2`
236
	 * self::choice_pluck( 'approved', 'label' ); // Returns `Approved`
237
	 * </code>
238
	 *
239
	 * @since 1.18
240
	 *
241
	 * @param int|string $status Valid status value or key (1 or "approved")
242
	 * @param string $attr_key Key name for the "value", "label", "action", "title". If "key", returns the matched key instead of value.
243
	 *
244
	 * @return false|string False if match isn't not found
245
	 */
246 2
	private static function choice_pluck( $status, $attr_key = '' ) {
247 2
		$choices = self::get_choices();
248
249 2
		foreach ( $choices as $key => $choice ) {
250
251
			// Is the passed status value the same as the choice value or key?
252 2
			if ( $status === $choice['value'] || $status === $key ) {
253
254 2
				if( 'key' === $attr_key ) {
255 2
					return $key;
256
				} else {
257 2
					return \GV\Utils::get( $choice, $attr_key, false );
258
				}
259
			}
260
		}
261
262
		return false;
263
	}
264
265
	/**
266
	 * Get the label for a specific approval value
267
	 *
268
	 * @since 1.18
269
	 *
270
	 * @param int|string $value_or_key Valid status value or key (1 or "approved")
271
	 *
272
	 * @return string|false Label of value ("Approved"). If invalid value, return false.
273
	 */
274 2
	public static function get_label( $value_or_key ) {
275 2
		return self::choice_pluck( $value_or_key, 'label' );
276
	}
277
278
	/**
279
	 * Get the label for a specific approval value
280
	 *
281
	 * @since 1.18
282
	 *
283
	 * @param int|string $value_or_key Valid status value or key (1 or "approved")
284
	 *
285
	 * @return string|false Label of value ("Approved"). If invalid value, return false.
286
	 */
287 2
	public static function get_string( $value_or_key, $string_key = '' ) {
288 2
		return self::choice_pluck( $value_or_key, $string_key );
289
	}
290
291
	/**
292
	 * Get the label for a specific approval value
293
	 *
294
	 * @since 1.18
295
	 *
296
	 * @param int|string $value_or_key Valid status value or key (1 or "approved")
297
	 *
298
	 * @return string|false Label of value ("Approved"). If invalid value, return false.
299
	 */
300 1
	public static function get_title_attr( $value_or_key ) {
301 1
		return self::choice_pluck( $value_or_key, 'title' );
302
	}
303
304
	/**
305
	 * Get the status key for a value
306
	 *
307
	 * @param int $value Status value (1, 2, 3)
308
	 *
309
	 * @return string|false The status key at status $value, if exists. If not exists, false.
310
	 */
311 3
	public static function get_key( $value ) {
312 3
		return self::choice_pluck( $value, 'key' );
313
	}
314
315
}
316