GravityView  2.1.1
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 = \GV\Utils::get( $field_settings, $field_setting_key, $default_label );
78  if ( empty( $value ) ) {
79  $value = $default_label;
80  }
81 
82  return sprintf( '<span class="gv-approval-%s">%s</span>', esc_attr( $status_key ), $value );
83  }
84 
85  /**
86  *
87  *
88  * @filter `gravityview_entry_default_fields`
89  *
90  * @param array $entry_default_fields Array of fields shown by default
91  * @param string|array $form form_ID or form object
92  * @param string $zone Either 'single', 'directory', 'header', 'footer'
93  *
94  * @return array
95  */
96  function add_default_field( $entry_default_fields, $form, $zone ) {
97 
98  if( 'edit' !== $zone ) {
99  $entry_default_fields[ $this->name ] = array(
100  'label' => $this->label,
101  'desc' => $this->description,
102  'type' => $this->name,
103  );
104  }
105 
106  return $entry_default_fields;
107  }
108 
109  /**
110  * Add custom merge tags to merge tag options
111  *
112  * @since 1.16
113  *
114  * @param array $form GF Form array
115  * @param GF_Field[] $fields Array of fields in the form
116  *
117  * @return array Modified merge tags
118  */
119  protected function custom_merge_tags( $form = array(), $fields = array() ) {
120 
121  $merge_tags = array(
122  array(
123  'label' => __('Approval Status', 'gravityview'),
124  'tag' => '{approval_status}'
125  ),
126  );
127 
128  return $merge_tags;
129  }
130 
131  /**
132  * Display the approval status of an entry
133  *
134  * @see https://docs.gravityview.co/article/389-approvalstatus-merge-tag Read how to use the `{approval_status}` merge tag
135  *
136  * @since 1.18
137  *
138  * @param array $matches Array of Merge Tag matches found in text by preg_match_all
139  * @param string $text Text to replace
140  * @param array $form Gravity Forms form array
141  * @param array $entry Entry array
142  * @param bool $url_encode Whether to URL-encode output
143  * @param bool $esc_html Whether to apply `esc_html()` to output
144  *
145  * @return string Text, with user variables replaced, if they existed
146  */
147  public function replace_merge_tag( $matches = array(), $text = '', $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) {
148 
149  $return = $text;
150 
151  /**
152  * @var array $match {
153  * @type string $match[0] Full matched merge tag ("{gv_approval}")
154  * @type string $match[1] Modifier ("value", "label", or empty string)
155  * }
156  */
157  foreach ( $matches as $match ) {
158 
159  if ( empty( $entry ) ) {
160  gravityview()->log->error( 'No entry data available. Returning empty string.' );
161  $replacement = '';
162  } else {
163  $replacement = GravityView_Entry_Approval::get_entry_status( $entry, $match[1] );
164  }
165 
166  $return = str_replace( $match[0], $replacement, $return );
167  }
168 
169  return $return;
170  }
171 
172  function field_options( $field_options, $template_id, $field_id, $context, $input_type ) {
173 
174  $field_options['approved_label'] = array(
175  'type' => 'text',
176  'label' => __( 'Approved Label', 'gravityview' ),
177  'desc' => __( 'If the entry is approved, display this value', 'gravityview' ),
178  'placeholder' => GravityView_Entry_Approval_Status::get_label('approved'),
179  );
180 
181  $field_options['disapproved_label'] = array(
182  'type' => 'text',
183  'label' => __( 'Disapproved Label', 'gravityview' ),
184  'desc' => __( 'If the entry is not approved, display this value', 'gravityview' ),
185  'placeholder' => GravityView_Entry_Approval_Status::get_label('disapproved'),
186  );
187 
188  $field_options['unapproved_label'] = array(
189  'type' => 'text',
190  'label' => __( 'Unapproved Label', 'gravityview' ),
191  'desc' => __( 'If the entry has not yet been approved or disapproved, display this value', 'gravityview' ),
192  'placeholder' => GravityView_Entry_Approval_Status::get_label('unapproved'),
193  );
194 
195  return $field_options;
196  }
197 
198 }
199 
filter_field_value( $output='', $entry=array(), $field_settings=array(), $gv_field_output=array())
Convert entry approval status value to label in the field output.
Modify field settings by extending this class.
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_key( $value)
Get the status key for a value.
gravityview()
Definition: _stubs.php:26
scale description p description
add_default_field( $entry_default_fields, $form, $zone)
static maybe_convert_status( $old_value='')
Convert previously-used values to the current values, for backward compatibility. ...
$field_id
Definition: time.php:17
static get_entry_status( $entry, $value_or_label='label')
Get the approval status for an entry.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
custom_merge_tags( $form=array(), $fields=array())
Add custom merge tags to merge tag options.
$entry
Definition: notes.php:27
field_options( $field_options, $template_id, $field_id, $context, $input_type)
static get_label( $value_or_key)
Get the label for a specific approval value.