1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @file class-gravityview-field-is-approved.php |
4
|
|
|
* @package GravityView |
5
|
|
|
* @subpackage includes\fields |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
class GravityView_Field_Is_Approved extends GravityView_Field { |
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
|
|
|
*/ |
34
|
|
|
var $entry_meta_is_default_column = false; |
35
|
|
|
|
36
|
|
|
public $_custom_merge_tag = 'approval_status'; |
37
|
|
|
|
38
|
|
|
public $icon = 'dashicons-yes-alt'; |
39
|
|
|
|
40
|
|
|
public function __construct() { |
41
|
|
|
|
42
|
|
|
$this->label = esc_html__( 'Approval Status', 'gravityview' ); |
43
|
|
|
$this->description = esc_html__( 'Display the entry\'s current approval status.', 'gravityview' ); |
44
|
|
|
$this->default_search_label = __( 'Approval:', 'gravityview' ); |
45
|
|
|
|
46
|
|
|
$this->add_hooks(); |
47
|
|
|
|
48
|
|
|
parent::__construct(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function add_hooks() { |
52
|
|
|
add_filter( 'gravityview_entry_default_fields', array( $this, 'add_default_field' ), 10, 3 ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Convert entry approval status value to label in the field output. Uses labels from the field setting. |
57
|
|
|
* |
58
|
|
|
* @since 2.10 |
59
|
|
|
* |
60
|
|
|
* @param string $approval_status Status to pass to {@see GravityView_Entry_Approval_Status::maybe_convert_status} |
61
|
|
|
* @param bool $html Whether to return HTML or plaintext string value |
62
|
|
|
* |
63
|
|
|
* @return string The field setting label for the current status. Uses defaults, if not configured. |
64
|
|
|
*/ |
65
|
|
|
public static function get_output( $approval_status = '', $field_settings = array(), $html = false ) { |
66
|
|
|
|
67
|
1 |
|
$status = GravityView_Entry_Approval_Status::maybe_convert_status( $approval_status ); |
68
|
|
|
$status_key = GravityView_Entry_Approval_Status::get_key( $status ); |
69
|
1 |
|
|
70
|
1 |
|
// "approved_label", "unapproved_label", "disapproved_label" setting keys |
71
|
|
|
$field_setting_key = sprintf( '%s_label', $status_key ); |
72
|
|
|
|
73
|
1 |
|
$default_label = GravityView_Entry_Approval_Status::get_label( $status ); |
74
|
|
|
|
75
|
1 |
|
$value = \GV\Utils::get( $field_settings, $field_setting_key, $default_label ); |
76
|
|
|
if ( empty( $value ) ) { |
77
|
1 |
|
$value = $default_label; |
78
|
1 |
|
} |
79
|
1 |
|
|
80
|
|
|
if ( ! $html ) { |
81
|
|
|
return $value; |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
return sprintf( '<span class="gv-approval-%s">%s</span>', esc_attr( $status_key ), $value ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
* |
90
|
|
|
* @filter `gravityview_entry_default_fields` |
91
|
|
|
* |
92
|
|
|
* @param array $entry_default_fields Array of fields shown by default |
93
|
|
|
* @param string|array $form form_ID or form object |
94
|
|
|
* @param string $zone Either 'single', 'directory', 'header', 'footer' |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
function add_default_field( $entry_default_fields, $form, $zone ) { |
|
|
|
|
99
|
|
|
|
100
|
|
|
if( 'edit' !== $zone ) { |
101
|
|
|
$entry_default_fields[ $this->name ] = array( |
102
|
|
|
'label' => $this->label, |
103
|
|
|
'desc' => $this->description, |
104
|
|
|
'type' => $this->name, |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $entry_default_fields; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Add custom merge tags to merge tag options |
113
|
|
|
* |
114
|
|
|
* @since 1.16 |
115
|
|
|
* |
116
|
|
|
* @param array $form GF Form array |
117
|
|
|
* @param GF_Field[] $fields Array of fields in the form |
118
|
|
|
* |
119
|
|
|
* @return array Modified merge tags |
120
|
|
|
*/ |
121
|
|
|
protected function custom_merge_tags( $form = array(), $fields = array() ) { |
122
|
|
|
|
123
|
|
|
$merge_tags = array( |
124
|
|
|
array( |
125
|
|
|
'label' => __('Approval Status', 'gravityview'), |
126
|
|
|
'tag' => '{approval_status}' |
127
|
|
|
), |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return $merge_tags; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Display the approval status of an entry |
135
|
|
|
* |
136
|
|
|
* @see https://docs.gravityview.co/article/389-approvalstatus-merge-tag Read how to use the `{approval_status}` merge tag |
137
|
|
|
* |
138
|
|
|
* @since 1.18 |
139
|
|
|
* |
140
|
|
|
* @param array $matches Array of Merge Tag matches found in text by preg_match_all |
141
|
|
|
* @param string $text Text to replace |
142
|
|
|
* @param array $form Gravity Forms form array |
143
|
|
|
* @param array $entry Entry array |
144
|
|
|
* @param bool $url_encode Whether to URL-encode output |
145
|
|
|
* @param bool $esc_html Whether to apply `esc_html()` to output |
146
|
|
|
* |
147
|
|
|
* @return string Text, with user variables replaced, if they existed |
148
|
|
|
*/ |
149
|
|
|
public function replace_merge_tag( $matches = array(), $text = '', $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) { |
150
|
|
|
|
151
|
|
|
$return = $text; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @var array $match { |
155
|
|
|
* @type string $match[0] Full matched merge tag ("{gv_approval}") |
156
|
|
|
* @type string $match[1] Modifier ("value", "label", or empty string) |
157
|
|
|
* } |
158
|
|
|
*/ |
159
|
|
|
foreach ( $matches as $match ) { |
160
|
|
|
|
161
|
|
|
if ( empty( $entry ) ) { |
162
|
|
|
gravityview()->log->error( 'No entry data available. Returning empty string.' ); |
163
|
|
|
$replacement = ''; |
164
|
|
|
} else { |
165
|
|
|
$replacement = GravityView_Entry_Approval::get_entry_status( $entry, $match[1] ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$return = str_replace( $match[0], $replacement, $return ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $return; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) { |
175
|
|
|
|
176
|
|
|
$field_options['approved_label'] = array( |
177
|
|
|
'type' => 'text', |
178
|
|
|
'label' => __( 'Approved Label', 'gravityview' ), |
179
|
|
|
'desc' => __( 'If the entry is approved, display this value', 'gravityview' ), |
180
|
|
|
'placeholder' => GravityView_Entry_Approval_Status::get_label('approved'), |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
$field_options['disapproved_label'] = array( |
184
|
|
|
'type' => 'text', |
185
|
|
|
'label' => __( 'Disapproved Label', 'gravityview' ), |
186
|
|
|
'desc' => __( 'If the entry is not approved, display this value', 'gravityview' ), |
187
|
|
|
'placeholder' => GravityView_Entry_Approval_Status::get_label('disapproved'), |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$field_options['unapproved_label'] = array( |
191
|
|
|
'type' => 'text', |
192
|
|
|
'label' => __( 'Unapproved Label', 'gravityview' ), |
193
|
|
|
'desc' => __( 'If the entry has not yet been approved or disapproved, display this value', 'gravityview' ), |
194
|
|
|
'placeholder' => GravityView_Entry_Approval_Status::get_label('unapproved'), |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
return $field_options; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
new GravityView_Field_Is_Approved; |
203
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.