Completed
Push — develop ( 5c91dc...778b65 )
by Zack
04:09
created

maybe_convert_status()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 7
nop 1
dl 0
loc 26
rs 6.7272
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
	 * @return array
49
	 */
50
	private static function get_choices() {
51
		return array(
52
			self::DISAPPROVED => esc_html__( 'Disapproved', 'gravityview' ),
53
			self::APPROVED    => esc_html__( 'Approved', 'gravityview' ),
54
			self::UNAPPROVED => esc_html__( 'Unapproved', 'gravityview' ),
55
		);
56
	}
57
58
	/**
59
	 * Get the status values as an array
60
	 *
61
	 * @return array Array of values for approval status choices
62
	 */
63
	public static function get_values() {
64
65
		$values = self::get_choices();
66
67
		return array_keys( $values );
68
	}
69
70
	/**
71
	 * Convert previously-used values to the current values, for backward compatibility
72
	 *
73
	 * @param string $old_value The status
74
	 *
75
	 * @return int|string Current value, possibly converted from old value
76
	 */
77
	public static function maybe_convert_status( $old_value = '' ) {
78
79
		$new_value = $old_value;
80
81
		switch ( (string) $old_value ) {
82
83
			// Approved values
84
			case 'Approved':
85
			case '1':
86
				$new_value = self::APPROVED;
87
				break;
88
89
			//Disapproved values
90
			case '0':
91
			case '2':
92
				$new_value = self::DISAPPROVED;
93
				break;
94
95
			case '3':
96
			case false: // Meta value does not exist yet
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing (string) $old_value of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
97
				$new_value = self::UNAPPROVED;
98
				break;
99
		}
100
101
		return $new_value;
102
	}
103
104
	/**
105
	 * Check whether the passed value is one of the defined values for entry approval
106
	 *
107
	 * @param mixed $value
108
	 *
109
	 * @return bool True: value is valid; false: value is not valid
110
	 */
111
	public static function is_valid( $value ) {
112
113
		$value = self::maybe_convert_status( $value );
114
115
		return in_array( $value, self::get_values(), true );
116
	}
117
118
	/**
119
	 * @param mixed $status Value to check approval of
120
	 *
121
	 * @return bool True: passed $status matches approved value
122
	 */
123
	public static function is_approved( $status ) {
124
125
		$status = self::maybe_convert_status( $status );
126
127
		return ( self::APPROVED === $status );
128
	}
129
130
	/**
131
	 * @param mixed $status Value to check approval of
132
	 *
133
	 * @return bool True: passed $status matches disapproved value
134
	 */
135
	public static function is_disapproved( $status ) {
136
137
		$status = self::maybe_convert_status( $status );
138
139
		return ( self::DISAPPROVED === $status );
140
	}
141
142
	/**
143
	 * @param mixed $status Value to check approval of
144
	 *
145
	 * @return bool True: passed $status matches unapproved value
146
	 */
147
	public static function is_unapproved( $status ) {
148
149
		$status = self::maybe_convert_status( $status );
150
151
		return ( self::UNAPPROVED === $status );
152
	}
153
154
	/**
155
	 * Get the labels for the status choices
156
	 *
157
	 * @return array Array of labels for the status choices ("Approved", "Disapproved")
158
	 */
159
	public static function get_labels() {
160
161
		$values = self::get_choices();
162
163
		return array_values( $values );
164
	}
165
166
	/**
167
	 * Get the label for a specific approval value
168
	 *
169
	 * @param string $value Valid approval value
170
	 *
171
	 * @return string|false Label of value ("Approved"). If invalid value, return false.
172
	 */
173
	public static function get_label( $value ) {
174
175
		$values = self::get_choices();
176
177
		return rgar( $values, $value, false );
178
	}
179
}
180