Completed
Push — master ( 2cfa6f...8927a4 )
by Zack
10:00 queued 06:05
created

GravityView_Entry_Approval_Status   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 290
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 290
rs 10
c 0
b 0
f 0
wmc 29
lcom 1
cbo 0

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
C maybe_convert_status() 0 36 8
A is_approved() 0 6 1
A is_disapproved() 0 6 1
A is_unapproved() 0 6 1
A get_labels() 0 8 1
B 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
A is_valid() 0 10 4
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
	private static function get_choices() {
53
		return array(
54
			'disapproved' => array(
55
				'value'  => self::DISAPPROVED,
56
				'label'  => esc_html__( 'Disapproved', 'gravityview' ),
57
				'action' => __( 'Disapprove', 'gravityview' ),
58
				'title'  => __( 'Entry not approved for directory viewing. Click to approve this entry.', 'gravityview' ),
59
			),
60
			'approved'    => array(
61
				'value'  => self::APPROVED,
62
				'label'  => esc_html__( 'Approved', 'gravityview' ),
63
				'action' => __( 'Approve', 'gravityview' ),
64
				'title'  => __( 'Entry approved for directory viewing. Click to disapprove this entry.', 'gravityview' ),
65
			),
66
			'unapproved'  => array(
67
				'value'  => self::UNAPPROVED,
68
				'label'  => esc_html__( 'Unapproved', 'gravityview' ),
69
				'action' => __( 'Reset Approval', 'gravityview' ),
70
				'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
	public static function get_all() {
83
		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
	public static function maybe_convert_status( $old_value = '' ) {
112
113
		$new_value = $old_value;
114
115
		// Meta value does not exist yet
116
		if( false === $old_value ) {
117
			return self::UNAPPROVED;
118
		}
119
120
		// Meta value does not exist yet
121
		if( true === $old_value ) {
122
			return self::APPROVED;
123
		}
124
125
		switch ( (string) $old_value ) {
126
127
			// Approved values
128
			case 'Approved':
129
			case '1':
130
				$new_value = self::APPROVED;
131
				break;
132
133
			//Disapproved values
134
			case '0':
135
			case '2':
136
				$new_value = self::DISAPPROVED;
137
				break;
138
139
			// Unapproved values
140
			case '3':
141
				$new_value = self::UNAPPROVED;
142
				break;
143
		}
144
145
		return $new_value;
146
	}
147
148
	/**
149
	 * Check whether the passed value is one of the defined values for entry approval
150
	 *
151
	 * @since 1.18
152
	 *
153
	 * @param mixed $value
154
	 *
155
	 * @return bool True: value is valid; false: value is not valid
156
	 */
157
	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...
158
159
		if ( ! is_scalar( $value ) || is_null( $value ) || '' === $value ) {
160
			return false;
161
		}
162
163
		$value = self::maybe_convert_status( $value );
164
165
		return in_array( $value, self::get_values(), true );
166
	}
167
168
	/**
169
	 * @param mixed $status Value to check approval of
170
	 *
171
	 * @since 1.18
172
	 *
173
	 * @return bool True: passed $status matches approved value
174
	 */
175
	public static function is_approved( $status ) {
176
177
		$status = self::maybe_convert_status( $status );
178
179
		return ( self::APPROVED === $status );
180
	}
181
182
	/**
183
	 * @param mixed $status Value to check approval of
184
	 *
185
	 * @since 1.18
186
	 *
187
	 * @return bool True: passed $status matches disapproved value
188
	 */
189
	public static function is_disapproved( $status ) {
190
191
		$status = self::maybe_convert_status( $status );
192
193
		return ( self::DISAPPROVED === $status );
194
	}
195
196
	/**
197
	 * @param mixed $status Value to check approval of
198
	 *
199
	 * @since 1.18
200
	 *
201
	 * @return bool True: passed $status matches unapproved value
202
	 */
203
	public static function is_unapproved( $status ) {
204
205
		$status = self::maybe_convert_status( $status );
206
207
		return ( self::UNAPPROVED === $status );
208
	}
209
210
	/**
211
	 * Get the labels for the status choices
212
	 *
213
	 * @since 1.18
214
	 *
215
	 * @return array Array of labels for the status choices ("Approved", "Disapproved")
216
	 */
217
	public static function get_labels() {
218
219
		$choices = self::get_choices();
220
221
		$labels = wp_list_pluck( $choices, 'label' );
222
223
		return $labels;
224
	}
225
226
227
	/**
228
	 * Pluck a certain field value from a status array
229
	 *
230
	 * Examples:
231
	 *
232
	 * <code>
233
	 * self::choice_pluck( 'disapproved', 'value' ); // Returns `2`
234
	 * self::choice_pluck( 'approved', 'label' ); // Returns `Approved`
235
	 * </code>
236
	 *
237
	 * @since 1.18
238
	 *
239
	 * @param int|string $status Valid status value or key (1 or "approved")
240
	 * @param string $attr_key Key name for the "value", "label", "action", "title". If "key", returns the matched key instead of value.
241
	 *
242
	 * @return false|string False if match isn't not found
243
	 */
244
	private static function choice_pluck( $status, $attr_key = '' ) {
245
		$choices = self::get_choices();
246
247
		foreach ( $choices as $key => $choice ) {
248
249
			// Is the passed status value the same as the choice value or key?
250
			if ( $status === $choice['value'] || $status === $key ) {
251
252
				if( 'key' === $attr_key ) {
253
					return $key;
254
				} else {
255
					return rgar( $choice, $attr_key, false );
256
				}
257
			}
258
		}
259
260
		return false;
261
	}
262
263
	/**
264
	 * Get the label for a specific approval value
265
	 *
266
	 * @since 1.18
267
	 *
268
	 * @param int|string $value_or_key Valid status value or key (1 or "approved")
269
	 *
270
	 * @return string|false Label of value ("Approved"). If invalid value, return false.
271
	 */
272
	public static function get_label( $value_or_key ) {
273
		return self::choice_pluck( $value_or_key, 'label' );
274
	}
275
276
	/**
277
	 * Get the label for a specific approval value
278
	 *
279
	 * @since 1.18
280
	 *
281
	 * @param int|string $value_or_key Valid status value or key (1 or "approved")
282
	 *
283
	 * @return string|false Label of value ("Approved"). If invalid value, return false.
284
	 */
285
	public static function get_string( $value_or_key, $string_key = '' ) {
286
		return self::choice_pluck( $value_or_key, $string_key );
287
	}
288
289
	/**
290
	 * Get the label for a specific approval value
291
	 *
292
	 * @since 1.18
293
	 *
294
	 * @param int|string $value_or_key Valid status value or key (1 or "approved")
295
	 *
296
	 * @return string|false Label of value ("Approved"). If invalid value, return false.
297
	 */
298
	public static function get_title_attr( $value_or_key ) {
299
		return self::choice_pluck( $value_or_key, 'title' );
300
	}
301
302
	/**
303
	 * Get the status key for a value
304
	 *
305
	 * @param int $value Status value (1, 2, 3)
306
	 *
307
	 * @return string|false The status key at status $value, if exists. If not exists, false.
308
	 */
309
	public static function get_key( $value ) {
310
		return self::choice_pluck( $value, 'key' );
311
	}
312
}
313