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