GravityView  2.10.1
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 GravityView <[email protected]>
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' => esc_html__( 'Disapprove', 'gravityview' ),
58  'title' => esc_html__( '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' => esc_html__( 'Approve', 'gravityview' ),
64  'title' => esc_html__( 'Entry approved for directory viewing. Click to disapprove this entry.', 'gravityview' ),
65  'title_popover' => esc_html__( 'Entry approved for directory viewing. Click to disapprove this entry.', 'gravityview' ),
66  ),
67  'unapproved' => array(
68  'value' => self::UNAPPROVED,
69  'label' => esc_html__( 'Unapproved', 'gravityview' ),
70  'action' => esc_html__( 'Reset Approval', 'gravityview' ),
71  '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  public static function get_all() {
84  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  public static function get_values() {
95 
96  $choices = self::get_choices();
97 
98  $values = wp_list_pluck( $choices, 'value' );
99 
100  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  public static function maybe_convert_status( $old_value = '' ) {
113 
114  $new_value = $old_value;
115 
116  // Meta value does not exist yet
117  if( false === $old_value ) {
118  return self::UNAPPROVED;
119  }
120 
121  // Meta value does not exist yet
122  if( true === $old_value ) {
123  return self::APPROVED;
124  }
125 
126  switch ( (string) $old_value ) {
127 
128  // Approved values
129  case 'Approved':
130  case '1':
131  $new_value = self::APPROVED;
132  break;
133 
134  //Disapproved values
135  case '0':
136  case '2':
137  $new_value = self::DISAPPROVED;
138  break;
139 
140  // Unapproved values
141  case '3':
142  case '':
143  $new_value = self::UNAPPROVED;
144  break;
145  }
146 
147  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  public static function is_valid( $value = NULL ) {
160 
161  if ( ! is_scalar( $value ) || is_null( $value ) ) {
162  return false;
163  }
164 
165  $value = self::maybe_convert_status( $value );
166 
167  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  public static function is_approved( $status ) {
178 
179  $status = self::maybe_convert_status( $status );
180 
181  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  public static function is_disapproved( $status ) {
192 
193  $status = self::maybe_convert_status( $status );
194 
195  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  public static function is_unapproved( $status ) {
206 
207  $status = self::maybe_convert_status( $status );
208 
209  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  private static function choice_pluck( $status, $attr_key = '' ) {
247  $choices = self::get_choices();
248 
249  foreach ( $choices as $key => $choice ) {
250 
251  // Is the passed status value the same as the choice value or key?
252  if ( $status === $choice['value'] || $status === $key ) {
253 
254  if( 'key' === $attr_key ) {
255  return $key;
256  } else {
257  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  public static function get_label( $value_or_key ) {
275  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  public static function get_string( $value_or_key, $string_key = '' ) {
288  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  public static function get_title_attr( $value_or_key ) {
301  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  public static function get_key( $value ) {
312  return self::choice_pluck( $value, 'key' );
313  }
314 
315 }
If this file is called directly, abort.
$labels
static get_values()
Get the status values as an array.
static get_choices()
Match values to the labels.
static get_labels()
Get the labels for the status choices.
static choice_pluck( $status, $attr_key='')
Pluck a certain field value from a status array.
static get_key( $value)
Get the status key for a value.
get( $key, $default=null)
Retrieve a setting.
static get_title_attr( $value_or_key)
Get the label for a specific approval value.
static is_valid( $value=NULL)
Check whether the passed value is one of the defined values for entry approval.
const APPROVED
static maybe_convert_status( $old_value='')
Convert previously-used values to the current values, for backward compatibility. ...
const DISAPPROVED
static get_string( $value_or_key, $string_key='')
Get the label for a specific approval value.
const UNAPPROVED
static is_disapproved( $status)
__construct()
GravityView_Entry_Approval_Status constructor.
static get_all()
Return array of status options.
static is_approved( $status)
static is_unapproved( $status)
static get_label( $value_or_key)
Get the label for a specific approval value.