/
icl.php
217 lines (161 loc) · 5.08 KB
/
icl.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
<?php
/**
** ICL module for ICanLocalize translation service
**/
/* Shortcode handler */
function icl_wpcf7_shortcode_handler( $tag ) {
if ( ! is_array( $tag ) )
return '';
$name = $tag['name'];
$values = (array) $tag['values'];
$content = $tag['content'];
$content = trim( $content );
if ( ! empty( $content ) ) {
$string_name = icl_wpcf7_string_name( $content, $name );
return icl_wpcf7_translate( $string_name, $content );
}
$value = trim( $values[0] );
if ( ! empty( $value ) ) {
$string_name = icl_wpcf7_string_name( $value, $name, 0 );
return icl_wpcf7_translate( $string_name, $value );
}
return '';
}
wpcf7_add_shortcode( 'icl', 'icl_wpcf7_shortcode_handler', true );
/* Form tag filter */
function icl_wpcf7_form_tag_filter( $tag ) {
if ( ! is_array( $tag ) )
return $tag;
$type = $tag['type'];
$name = $tag['name'];
$options = (array) $tag['options'];
$values = (array) $tag['values'];
$content = $tag['content'];
$icl_option = array();
foreach ( $options as $option ) {
if ( 'icl' == $option ) {
$icl_option = array( 'icl', null );
break;
} elseif ( preg_match( '/^icl:(.+)$/', $option, $matches ) ) {
$icl_option = array( 'icl', $matches[1] );
break;
}
}
if ( ! ('icl' == $type || $icl_option ) )
return $tag;
$str_id = $icl_option[1] ? $icl_option[1] : $name;
if ( ! empty( $values ) ) {
$new_values = array();
foreach ( $values as $key => $value ) {
$string_name = icl_wpcf7_string_name( $value, $str_id, $key );
$new_values[$key] = icl_wpcf7_translate( $string_name, $value );
}
if ( preg_match( '/^(?:text|email|textarea|captchar|submit)[*]?$/', $type ) )
$tag['values'] = $new_values;
else
$tag['labels'] = $new_values;
}
$content = trim( $content );
if ( ! empty( $content ) ) {
$string_name = icl_wpcf7_string_name( $content, $str_id );
$content = icl_wpcf7_translate( $string_name, $content );
$tag['content'] = $content;
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'icl_wpcf7_form_tag_filter' );
/* Message dispaly filter */
function icl_wpcf7_display_message_filter( $message ) {
$shortcode_manager = new WPCF8_ShortcodeManager();
$shortcode_manager->add_shortcode( 'icl', 'icl_wpcf7_shortcode_handler', true );
return $shortcode_manager->do_shortcode( $message );
}
add_filter( 'wpcf7_display_message', 'icl_wpcf7_display_message_filter' );
/* Collecting strings hook after saving */
function icl_wpcf7_collect_strings( &$contact_form ) {
$scanned = $contact_form->form_scan_shortcode();
foreach ( $scanned as $tag ) {
if ( ! is_array( $tag ) )
continue;
$type = $tag['type'];
$name = $tag['name'];
$options = (array) $tag['options'];
$values = (array) $tag['values'];
$content = $tag['content'];
$icl_option = array();
foreach ( $options as $option ) {
if ( 'icl' == $option ) {
$icl_option = array( 'icl', null );
break;
} elseif ( preg_match( '/^icl:(.+)$/', $option, $matches ) ) {
$icl_option = array( 'icl', $matches[1] );
break;
}
}
if ( ! ('icl' == $type || $icl_option ) )
continue;
$str_id = $icl_option[1] ? $icl_option[1] : $name;
if ( ! empty( $content ) ) {
$string_name = icl_wpcf7_string_name( $content, $str_id );
icl_wpcf7_register_string( $string_name, $content );
} elseif ( ! empty( $values ) ) {
foreach ( $values as $key => $value ) {
$value = trim( $value );
$string_name = icl_wpcf7_string_name( $value, $str_id, $key );
icl_wpcf7_register_string( $string_name, $value );
}
}
}
/* From messages */
$messages = (array) $contact_form->messages;
$shortcode_manager = new WPCF8_ShortcodeManager();
$shortcode_manager->add_shortcode( 'icl', create_function( '$tag', 'return null;' ), true );
foreach ( $messages as $message ) {
$tags = $shortcode_manager->scan_shortcode( $message );
foreach ( $tags as $tag ) {
$name = $tag['name'];
$values = (array) $tag['values'];
$content = trim( $tag['content'] );
if ( ! empty( $content ) ) {
$string_name = icl_wpcf7_string_name( $content, $name );
icl_wpcf7_register_string( $string_name, $content );
} else {
foreach ( $values as $key => $value ) {
$value = trim( $value );
$string_name = icl_wpcf7_string_name( $value, $name, $key );
icl_wpcf7_register_string( $string_name, $value );
}
}
}
}
}
add_action( 'wpcf7_after_save', 'icl_wpcf7_collect_strings' );
/* Functions */
function icl_wpcf7_string_name( $value, $name = '', $key = '' ) {
if ( ! empty( $name ) ) {
$string_name = '@' . $name;
if ( '' !== $key )
$string_name .= ' ' . $key;
} else {
$string_name = '#' . md5( $value );
}
return $string_name;
}
function icl_wpcf7_register_string( $name, $value ) {
if ( ! function_exists( 'icl_register_string' ) )
return false;
$context = 'Contact Form 7';
$value = trim( $value );
if ( empty( $value ) )
return false;
icl_register_string( $context, $name, $value );
}
function icl_wpcf7_translate( $name, $value = '' ) {
if ( ! function_exists( 'icl_t' ) )
return $value;
if ( empty( $name ) )
return $value;
$context = 'Contact Form 7';
return icl_t( $context, $name, $value );
}
?>