GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-entry-approval-status.php
Go to the documentation of this file.
1 <?php
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  */
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  case '':
142  $new_value = self::UNAPPROVED;
143  break;
144  }
145 
146  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  public static function is_valid( $value = NULL ) {
159 
160  if ( ! is_scalar( $value ) || is_null( $value ) ) {
161  return false;
162  }
163 
164  $value = self::maybe_convert_status( $value );
165 
166  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  public static function is_approved( $status ) {
177 
178  $status = self::maybe_convert_status( $status );
179 
180  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  public static function is_disapproved( $status ) {
191 
192  $status = self::maybe_convert_status( $status );
193 
194  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  public static function is_unapproved( $status ) {
205 
206  $status = self::maybe_convert_status( $status );
207 
208  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  private static function choice_pluck( $status, $attr_key = '' ) {
246  $choices = self::get_choices();
247 
248  foreach ( $choices as $key => $choice ) {
249 
250  // Is the passed status value the same as the choice value or key?
251  if ( $status === $choice['value'] || $status === $key ) {
252 
253  if( 'key' === $attr_key ) {
254  return $key;
255  } else {
256  return rgar( $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  public static function get_label( $value_or_key ) {
274  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  public static function get_string( $value_or_key, $string_key = '' ) {
287  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  public static function get_title_attr( $value_or_key ) {
300  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  public static function get_key( $value ) {
311  return self::choice_pluck( $value, 'key' );
312  }
313 }
If this file is called directly, abort.
static get_key($value)
Get the status key for a value.
static choice_pluck($status, $attr_key= '')
Pluck a certain field value from a status array.
$labels
static get_values()
Get the status values as an array.
static is_valid($value=NULL)
Check whether the passed value is one of the defined values for entry approval.
static get_choices()
Match values to the labels.
static get_labels()
Get the labels for the status choices.
static maybe_convert_status($old_value= '')
Convert previously-used values to the current values, for backward compatibility. ...
static get_label($value_or_key)
Get the label for a specific approval value.
const APPROVED
static is_disapproved($status)
const DISAPPROVED
static is_unapproved($status)
const UNAPPROVED
__construct()
GravityView_Entry_Approval_Status constructor.
static is_approved($status)
static get_string($value_or_key, $string_key= '')
Get the label for a specific approval value.
static get_all()
Return array of status options.
static get_title_attr($value_or_key)
Get the label for a specific approval value.