source: trunk/www.guidonia.net/wp/wp-content/plugins/contact-form-7/modules/icl.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 5.1 KB
Line 
1<?php
2/**
3** ICL module for ICanLocalize translation service
4**/
5
6/* Shortcode handler */
7
8function icl_wpcf7_shortcode_handler( $tag ) {
9
10 if ( ! is_array( $tag ) )
11 return '';
12
13 $name = $tag['name'];
14 $values = (array) $tag['values'];
15 $content = $tag['content'];
16
17 $content = trim( $content );
18 if ( ! empty( $content ) ) {
19 $string_name = icl_wpcf7_string_name( $content, $name );
20 return icl_wpcf7_translate( $string_name, $content );
21 }
22
23 $value = trim( $values[0] );
24 if ( ! empty( $value ) ) {
25 $string_name = icl_wpcf7_string_name( $value, $name, 0 );
26 return icl_wpcf7_translate( $string_name, $value );
27 }
28
29 return '';
30}
31
32wpcf7_add_shortcode( 'icl', 'icl_wpcf7_shortcode_handler', true );
33
34
35/* Form tag filter */
36
37function icl_wpcf7_form_tag_filter( $tag ) {
38 if ( ! is_array( $tag ) )
39 return $tag;
40
41 $type = $tag['type'];
42 $name = $tag['name'];
43 $options = (array) $tag['options'];
44 $values = (array) $tag['values'];
45 $content = $tag['content'];
46
47 $icl_option = array();
48 foreach ( $options as $option ) {
49 if ( 'icl' == $option ) {
50 $icl_option = array( 'icl', null );
51 break;
52 } elseif ( preg_match( '/^icl:(.+)$/', $option, $matches ) ) {
53 $icl_option = array( 'icl', $matches[1] );
54 break;
55 }
56 }
57
58 if ( ! ('icl' == $type || $icl_option ) )
59 return $tag;
60
61 $str_id = $icl_option[1] ? $icl_option[1] : $name;
62
63 if ( ! empty( $values ) ) {
64 $new_values = array();
65 foreach ( $values as $key => $value ) {
66 $string_name = icl_wpcf7_string_name( $value, $str_id, $key );
67 $new_values[$key] = icl_wpcf7_translate( $string_name, $value );
68 }
69
70 if ( preg_match( '/^(?:text|email|textarea|captchar|submit)[*]?$/', $type ) )
71 $tag['values'] = $new_values;
72 else
73 $tag['labels'] = $new_values;
74 }
75
76 $content = trim( $content );
77
78 if ( ! empty( $content ) ) {
79 $string_name = icl_wpcf7_string_name( $content, $str_id );
80 $content = icl_wpcf7_translate( $string_name, $content );
81 $tag['content'] = $content;
82 }
83
84 return $tag;
85}
86
87add_filter( 'wpcf7_form_tag', 'icl_wpcf7_form_tag_filter' );
88
89
90/* Message dispaly filter */
91
92function icl_wpcf7_display_message_filter( $message ) {
93 $shortcode_manager = new WPCF7_ShortcodeManager();
94 $shortcode_manager->add_shortcode( 'icl', 'icl_wpcf7_shortcode_handler', true );
95
96 return $shortcode_manager->do_shortcode( $message );
97}
98
99add_filter( 'wpcf7_display_message', 'icl_wpcf7_display_message_filter' );
100
101
102/* Collecting strings hook after saving */
103
104function icl_wpcf7_collect_strings( &$contact_form ) {
105 $scanned = $contact_form->form_scan_shortcode();
106
107 foreach ( $scanned as $tag ) {
108 if ( ! is_array( $tag ) )
109 continue;
110
111 $type = $tag['type'];
112 $name = $tag['name'];
113 $options = (array) $tag['options'];
114 $values = (array) $tag['values'];
115 $content = $tag['content'];
116
117 $icl_option = array();
118 foreach ( $options as $option ) {
119 if ( 'icl' == $option ) {
120 $icl_option = array( 'icl', null );
121 break;
122 } elseif ( preg_match( '/^icl:(.+)$/', $option, $matches ) ) {
123 $icl_option = array( 'icl', $matches[1] );
124 break;
125 }
126 }
127
128 if ( ! ('icl' == $type || $icl_option ) )
129 continue;
130
131 $str_id = $icl_option[1] ? $icl_option[1] : $name;
132
133 if ( ! empty( $content ) ) {
134 $string_name = icl_wpcf7_string_name( $content, $str_id );
135 icl_wpcf7_register_string( $string_name, $content );
136
137 } elseif ( ! empty( $values ) ) {
138 foreach ( $values as $key => $value ) {
139 $value = trim( $value );
140 $string_name = icl_wpcf7_string_name( $value, $str_id, $key );
141 icl_wpcf7_register_string( $string_name, $value );
142 }
143 }
144 }
145
146 /* From messages */
147
148 $messages = (array) $contact_form->messages;
149
150 $shortcode_manager = new WPCF7_ShortcodeManager();
151 $shortcode_manager->add_shortcode( 'icl', create_function( '$tag', 'return null;' ), true );
152
153 foreach ( $messages as $message ) {
154 $tags = $shortcode_manager->scan_shortcode( $message );
155 foreach ( $tags as $tag ) {
156 $name = $tag['name'];
157 $values = (array) $tag['values'];
158 $content = trim( $tag['content'] );
159
160 if ( ! empty( $content ) ) {
161 $string_name = icl_wpcf7_string_name( $content, $name );
162 icl_wpcf7_register_string( $string_name, $content );
163
164 } else {
165 foreach ( $values as $key => $value ) {
166 $value = trim( $value );
167 $string_name = icl_wpcf7_string_name( $value, $name, $key );
168 icl_wpcf7_register_string( $string_name, $value );
169 }
170 }
171 }
172 }
173}
174
175add_action( 'wpcf7_after_save', 'icl_wpcf7_collect_strings' );
176
177
178/* Functions */
179
180function icl_wpcf7_string_name( $value, $name = '', $key = '' ) {
181 if ( ! empty( $name ) ) {
182 $string_name = '@' . $name;
183 if ( '' !== $key )
184 $string_name .= ' ' . $key;
185 } else {
186 $string_name = '#' . md5( $value );
187 }
188
189 return $string_name;
190}
191
192function icl_wpcf7_register_string( $name, $value ) {
193 if ( ! function_exists( 'icl_register_string' ) )
194 return false;
195
196 $context = 'Contact Form 7';
197
198 $value = trim( $value );
199 if ( empty( $value ) )
200 return false;
201
202 icl_register_string( $context, $name, $value );
203}
204
205function icl_wpcf7_translate( $name, $value = '' ) {
206 if ( ! function_exists( 'icl_t' ) )
207 return $value;
208
209 if ( empty( $name ) )
210 return $value;
211
212 $context = 'Contact Form 7';
213
214 return icl_t( $context, $name, $value );
215}
216
217?>
Note: See TracBrowser for help on using the repository browser.