1 | <?php |
||
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() { |
||
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() { |
||
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() { |
||
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 = '' ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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() { |
||
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 = '' ) { |
||
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 ) { |
||
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 = '' ) { |
||
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 ) { |
||
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 ) { |
||
312 | } |
||
313 |
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.