GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-is-approved.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-is-approved.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
9 
10  public $name = 'is_approved';
11 
12  public $search_operators = array( 'is', 'isnot' );
13 
14  public $contexts = array( 'single', 'multiple' );
15 
16  public $group = 'meta';
17 
18  public $is_sortable = true;
19 
20  public $is_numeric = true;
21 
22  public $is_searchable = true;
23 
24  /**
25  * @var string Approval status is stored in entry meta under this key
26  * @since 1.18
27  */
28  var $entry_meta_key = 'is_approved';
29 
30  /**
31  * @var bool Don't add to the "columns to display" list; GravityView adds our own approval column
32  * @since 1.18
33  */
35 
36  public $_custom_merge_tag = 'approval_status';
37 
38  public function __construct() {
39 
40  $this->label = esc_html__( 'Approval Status', 'gravityview' );
41  $this->description = esc_html__( 'Display the entry\'s current approval status.', 'gravityview' );
42  $this->default_search_label = __( 'Approval:', 'gravityview' );
43 
44  $this->add_hooks();
45 
46  parent::__construct();
47  }
48 
49  private function add_hooks() {
50  add_filter( 'gravityview_entry_default_fields', array( $this, 'add_default_field' ), 10, 3 );
51 
52  add_filter( 'gravityview_field_entry_value_is_approved_pre_link', array( $this, 'filter_field_value' ), 10, 4 );
53  }
54 
55  /**
56  * Convert entry approval status value to label in the field output. Uses labels from the field setting.
57  *
58  * @since 1.18
59  *
60  * @param string $output HTML value output
61  * @param array $entry The GF entry array
62  * @param array $field_settings Settings for the particular GV field
63  * @param array $field Field array, as fetched from GravityView_View::getCurrentField()
64  *
65  * @return string The field setting label for the current status. Uses defaults, if not configured.
66  */
67  public function filter_field_value( $output = '', $entry = array(), $field_settings = array(), $gv_field_output = array() ) {
68 
70  $status_key = GravityView_Entry_Approval_Status::get_key( $status );
71 
72  // "approved_label", "unapproved_label", "disapproved_label" setting keys
73  $field_setting_key = sprintf( '%s_label', $status_key );
74 
75  $default_label = GravityView_Entry_Approval_Status::get_label( $status );
76 
77  $value = rgar( $field_settings, $field_setting_key, $default_label );
78 
79  return sprintf( '<span class="gv-approval-%s">%s</span>', esc_attr( $status_key ), $value );
80  }
81 
82  /**
83  *
84  *
85  * @filter `gravityview_entry_default_fields`
86  *
87  * @param array $entry_default_fields Array of fields shown by default
88  * @param string|array $form form_ID or form object
89  * @param string $zone Either 'single', 'directory', 'header', 'footer'
90  *
91  * @return array
92  */
93  function add_default_field( $entry_default_fields, $form, $zone ) {
94 
95  if( 'edit' !== $zone ) {
96  $entry_default_fields[ $this->name ] = array(
97  'label' => $this->label,
98  'desc' => $this->description,
99  'type' => $this->name,
100  );
101  }
102 
103  return $entry_default_fields;
104  }
105 
106  /**
107  * Add custom merge tags to merge tag options
108  *
109  * @since 1.16
110  *
111  * @param array $form GF Form array
112  * @param GF_Field[] $fields Array of fields in the form
113  *
114  * @return array Modified merge tags
115  */
116  protected function custom_merge_tags( $form = array(), $fields = array() ) {
117 
118  $merge_tags = array(
119  array(
120  'label' => __('Approval Status', 'gravityview'),
121  'tag' => '{approval_status}'
122  ),
123  );
124 
125  return $merge_tags;
126  }
127 
128  /**
129  * Display the approval status of an entry
130  *
131  * @see http://docs.gravityview.co/article/389-approvalstatus-merge-tag Read how to use the `{approval_status}` merge tag
132  *
133  * @since 1.18
134  *
135  * @param array $matches Array of Merge Tag matches found in text by preg_match_all
136  * @param string $text Text to replace
137  * @param array $form Gravity Forms form array
138  * @param array $entry Entry array
139  * @param bool $url_encode Whether to URL-encode output
140  * @param bool $esc_html Whether to apply `esc_html()` to output
141  *
142  * @return string Text, with user variables replaced, if they existed
143  */
144  public function replace_merge_tag( $matches = array(), $text = '', $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) {
145 
146  $return = $text;
147 
148  /**
149  * @var array $match {
150  * @type string $match[0] Full matched merge tag ("{gv_approval}")
151  * @type string $match[1] Modifier ("value", "label", or empty string)
152  * }
153  */
154  foreach ( $matches as $match ) {
155 
156  if ( empty( $entry ) ) {
157  do_action( 'gravityview_log_error', __METHOD__ . ': No entry data available. Returning empty string.' );
158  $replacement = '';
159  } else {
160  $replacement = GravityView_Entry_Approval::get_entry_status( $entry, $match[1] );
161  }
162 
163  $return = str_replace( $match[0], $replacement, $return );
164  }
165 
166  return $return;
167  }
168 
169  function field_options( $field_options, $template_id, $field_id, $context, $input_type ) {
170 
171  $field_options['approved_label'] = array(
172  'type' => 'text',
173  'label' => __( 'Approved Label', 'gravityview' ),
174  'desc' => __( 'If the entry is approved, display this value', 'gravityview' ),
175  'placeholder' => GravityView_Entry_Approval_Status::get_label('approved'),
176  );
177 
178  $field_options['disapproved_label'] = array(
179  'type' => 'text',
180  'label' => __( 'Disapproved Label', 'gravityview' ),
181  'desc' => __( 'If the entry is not approved, display this value', 'gravityview' ),
182  'placeholder' => GravityView_Entry_Approval_Status::get_label('disapproved'),
183  );
184 
185  $field_options['unapproved_label'] = array(
186  'type' => 'text',
187  'label' => __( 'Unapproved Label', 'gravityview' ),
188  'desc' => __( 'If the entry has not yet been approved or disapproved, display this value', 'gravityview' ),
189  'placeholder' => GravityView_Entry_Approval_Status::get_label('unapproved'),
190  );
191 
192  return $field_options;
193  }
194 
195 }
196 
Modify field settings by extending this class.
static get_key($value)
Get the status key for a value.
field_options($field_options, $template_id, $field_id, $context, $input_type)
static maybe_convert_status($old_value= '')
Convert previously-used values to the current values, for backward compatibility. ...
replace_merge_tag($matches=array(), $text= '', $form=array(), $entry=array(), $url_encode=false, $esc_html=false)
Run GravityView filters when using GFCommon::replace_variables()
$field_settings['content']
Definition: custom.php:27
static get_label($value_or_key)
Get the label for a specific approval value.
add_default_field($entry_default_fields, $form, $zone)
filter_field_value($output= '', $entry=array(), $field_settings=array(), $gv_field_output=array())
Convert entry approval status value to label in the field output.
scale description p description
custom_merge_tags($form=array(), $fields=array())
Add custom merge tags to merge tag options.
$field_id
Definition: time.php:17
$entry
Definition: notes.php:27
static get_entry_status($entry, $value_or_label= 'label')
Get the approval status for an entry.