reflection.php
2.27 KB
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
<?php
/**
* Reflection module
*
* @link https://contactform7.com/reflection/
*/
add_action( 'wpcf7_init', 'wpcf7_add_form_tag_reflection', 10, 0 );
/**
* Registers reflection-related form-tag types.
*/
function wpcf7_add_form_tag_reflection() {
wpcf7_add_form_tag( 'reflection',
'wpcf7_reflection_form_tag_handler',
array(
'name-attr' => true,
'display-block' => true,
'not-for-mail' => true,
)
);
wpcf7_add_form_tag( 'output',
'wpcf7_output_form_tag_handler',
array(
'name-attr' => true,
'not-for-mail' => true,
)
);
}
/**
* The form-tag handler for the reflection type.
*/
function wpcf7_reflection_form_tag_handler( $tag ) {
if ( empty( $tag->name ) ) {
return '';
}
$values = $tag->values ? $tag->values : array( '' );
if ( ! wpcf7_get_validation_error( $tag->name ) ) {
$hangover = array_filter( (array) wpcf7_get_hangover( $tag->name ) );
if ( $hangover ) {
$values = $hangover;
}
}
$content = array_reduce(
$values,
static function ( $carry, $item ) use ( $tag ) {
$output_tag = sprintf(
'<output %1$s>%2$s</output>',
wpcf7_format_atts( array(
'name' => $tag->name,
'data-default' => $item,
) ),
( '' !== $item ) ? esc_html( $item ) : ' '
);
return $carry . $output_tag;
},
''
);
$html = sprintf(
'<fieldset %1$s>%2$s</fieldset>',
wpcf7_format_atts( array(
'data-reflection-of' => $tag->name,
'class' => $tag->get_class_option(
wpcf7_form_controls_class( $tag->type )
),
'id' => $tag->get_id_option(),
) ),
$content
);
return $html;
}
/**
* The form-tag handler for the output type.
*/
function wpcf7_output_form_tag_handler( $tag ) {
if ( empty( $tag->name ) ) {
return '';
}
$value = (string) reset( $tag->values );
if ( ! wpcf7_get_validation_error( $tag->name ) ) {
$hangover = array_filter( (array) wpcf7_get_hangover( $tag->name ) );
if ( $hangover ) {
$value = (string) reset( $hangover );
}
}
$html = sprintf(
'<output %1$s>%2$s</output>',
wpcf7_format_atts( array(
'data-reflection-of' => $tag->name,
'data-default' => $value,
'name' => $tag->name,
'class' => $tag->get_class_option(
wpcf7_form_controls_class( $tag->type )
),
'id' => $tag->get_id_option(),
) ),
esc_html( $value )
);
return $html;
}