-
Notifications
You must be signed in to change notification settings - Fork 166
/
summary.php
executable file
·270 lines (231 loc) · 6.79 KB
/
summary.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* Abstract class that all magic tag parsing classes should extend
*
* @package Caldera_Forms
* @author Josh Pollock <Josh@CalderaWP.com>
* @license GPL-2.0+
* @link
* @copyright 2016 CalderaWP LLC
*/
class Caldera_Forms_Magic_Summary extends Caldera_Forms_Magic_Parser {
/**
* Is email HTML?
*
* @since 1.4.6
*
* @var bool
*/
protected $html = true;
/**
* Sprintf pattern for HTML
*
* @since 1.4.6
*
* @var string
*/
protected $pattern = '';
/**
* Fields ordered
*
* @since 1.5.0.10
*
* @var array
*/
protected $ordered_fields;
/**
* Set if we should use HTML tags or not
*
* @since 1.4.6
*
* @param bool $html Use HTML?
*/
public function set_html_mode( $html ){
$this->html = (bool) $html;
}
public function set_fields( $fields ){
$this->ordered_fields = $fields;
}
/**
* @inheritdoc
*/
protected function parse(){
if( $this->html ){
$this->set_pattern();
}
$out = array();
if( empty( $this->ordered_fields ) ){
$this->ordered_fields = $ordered_fields = Caldera_Forms_Forms::get_fields( $this->form );
}else{
$ordered_fields = $this->ordered_fields;
}
/**
* Modify fields used in summary magic tag
*
* @since 1.5.0.10
*
* @param array $ordered_fields Fields in order they will be displayed
* @param array $form Form config
*/
$this->ordered_fields = $ordered_fields = apply_filters( 'caldera_forms_summary_magic_fields', $ordered_fields, $this->form );
if ( ! empty( $ordered_fields ) ) {
$tag_i = -1;
foreach ( $ordered_fields as $field_id => $field ) {
$tag_i++;
$type = Caldera_Forms_Field_Util::get_type( $field, $this->form );
$not_support = Caldera_Forms_Fields::not_support( $type, 'entry_list' );
if( $not_support ){
if( 'html' !== $type || empty( $field[ 'config' ][ 'show_in_summary' ] ) ){
continue;
}
}
if( Caldera_Forms_Field_Util::is_file_field( $field_id, $this->form ) && Caldera_Forms_Files::is_private( $field ) ){
continue;
}
$field_value = false;
switch( $type ){
case 'html' :
$field_value = Caldera_Forms::do_magic_tags( Caldera_Forms_Field_Util::get_default( $field_id, $this->form ) );
break;
case 'file' :
$field_value = Caldera_Forms_Magic_Doer::magic_image( $field, $this->get_field_value( $field_id ), $this->form );
break;
case 'calculation' :
$field_value = Caldera_Forms_Magic_Doer::calculation_magic( $field, $this->get_field_value( $field_id ) );
break;
case 'credit_card_number' :
$field_value = $this->get_credit_card_hasher()->credit_card_number( $this->get_field_value( $field_id ) );
break;
case 'credit_card_cvc' :
$field_value = $this->get_credit_card_hasher()->credit_card_cvc( $this->get_field_value( $field_id ) );
break;
default :
if ( null == $this->data ) {
$field_values = (array) Caldera_Forms::get_field_data( $field_id, $this->form );
}else{
if( ! isset( $this->data[ $field_id ] ) ){
break;
}
$field_values = (array) $this->get_field_value( $field_id );
}
if ( isset( $field_values[ 'label' ] ) ) {
$field_values = $field_values[ 'value' ];
} else {
foreach ( $field_values as $field_key => $field_value ) {
if ( true === is_array( $field_value ) && true === array_key_exists( 'label', $field_value ) && true === array_key_exists( 'value', $field_value ) ) {
$field_values[ $field_key ] = $field_value[ 'value' ];
}
}
}
$should_use_label = false;
if ( is_array( $field ) ) {
$should_use_label = $this->should_use_label( $field );
}
if( $should_use_label ){
foreach ( $field_values as $field_key => $field_value ) {
$field_values[ $field_key ] = $this->option_value_to_label( $field_value, $field );
}
}
$field_value = implode( ', ', (array) $field_values );
break;
}
if ( $field_value !== null && ! is_array( $field_value ) && strlen( (string) $field_value ) > 0 ) {
/**
* Change value displayed for field inside Caldera Forms summary magic tag
*
* @since 1.5.0.10
*
* @param string $field_value The value
* @param array $field Field config
* @param array $form Form config
*/
$field_value = apply_filters( 'caldera_forms_magic_summary_field_value', $field_value, $field, $this->form );
if ( $this->html ) {
$out[ $tag_i ] = sprintf( $this->pattern, $field[ 'label' ], $field_value );
} else {
$out[ $tag_i ] = $field[ 'label' ] . ': ' . $field_value;
}
}
}
}
// vars
if ( ! empty( $this_form[ 'variables' ] ) ) {
foreach ( $this_form[ 'variables' ][ 'keys' ] as $var_key => $var_label ) {
if ( $this_form[ 'variables' ][ 'types' ][ $var_key ] == 'entryitem' ) {
$label = ucfirst( str_replace( '_', ' ', $var_label ) );
if ( $this->html ) {
$out[] = sprintf( $this->pattern, $label, $this_form[ 'variables' ][ 'values' ][ $var_key ] );
} else {
$out[] = $label . ': ' . $this_form[ 'variables' ][ 'values' ][ $var_key ];
}
}
}
}
if ( ! empty( $out ) ) {
$this->tag = implode( "\r\n", $out );
} else {
$this->tag = '';
}
}
/**
* Set sprintf pattern in pattern property
*
* @since 1.4.6
*/
protected function set_pattern(){
/**
* Change the sprintf pattern for the {summary} magic tag
*
* @since 1.4.5
*
* @param string $pattern The sprintf pattern to use
* @param array $this_form Form config
*/
$this->pattern = apply_filters( 'caldera_forms_summary_magic_pattern', '<strong>%s</strong><div style="margin-bottom:20px;">%s</div>', $this->form );
}
/**
* Check if field should use label instead of value
*
* @since 1.4.6
*
* @param array $field Field config
*
* @return bool
*/
protected function should_use_label( array $field ){
if( empty( $field[ 'config' ][ 'option' ] ) ){
return false;
}
/**
* Set a single-select field to show label instead of value in {summary} magic tag
*
* @since 1.4.6
*
* @param bool $use Use or not?
* @param array $field Field config
* @param array $form Form config
*/
return wp_validate_boolean( apply_filters( 'caldera_forms_magic_summary_should_use_label', false, $field, $this->form ) );
}
/**
* Convert option value to label
*
* @since 1.4.6
*
* @param mixed $value Value to find label for
* @param array$field
*
* @return bool|mixed False if not found, else value
*/
protected function option_value_to_label( $value, $field ){
if( empty( $field[ 'config' ][ 'option' ] ) ){
return false;
}
foreach ( $field[ 'config' ][ 'option' ] as $opt_id => $option ){
if( $value == $option[ 'value'] ){
return $option[ 'label' ];
}
}
return false;
}
}