functions.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. function wpcf7_plugin_path( $path = '' ) {
  3. return path_join( WPCF7_PLUGIN_DIR, trim( $path, '/' ) );
  4. }
  5. function wpcf7_plugin_url( $path = '' ) {
  6. $url = plugins_url( $path, WPCF7_PLUGIN );
  7. if ( is_ssl()
  8. and 'http:' == substr( $url, 0, 5 ) ) {
  9. $url = 'https:' . substr( $url, 5 );
  10. }
  11. return $url;
  12. }
  13. function wpcf7_upload_dir( $type = false ) {
  14. $uploads = wp_get_upload_dir();
  15. $uploads = apply_filters( 'wpcf7_upload_dir', array(
  16. 'dir' => $uploads['basedir'],
  17. 'url' => $uploads['baseurl'],
  18. ) );
  19. if ( 'dir' == $type ) {
  20. return $uploads['dir'];
  21. } if ( 'url' == $type ) {
  22. return $uploads['url'];
  23. }
  24. return $uploads;
  25. }
  26. function wpcf7_verify_nonce( $nonce, $action = 'wp_rest' ) {
  27. return wp_verify_nonce( $nonce, $action );
  28. }
  29. function wpcf7_create_nonce( $action = 'wp_rest' ) {
  30. return wp_create_nonce( $action );
  31. }
  32. function wpcf7_blacklist_check( $target ) {
  33. $mod_keys = trim( get_option( 'blacklist_keys' ) );
  34. if ( empty( $mod_keys ) ) {
  35. return false;
  36. }
  37. $words = explode( "\n", $mod_keys );
  38. foreach ( (array) $words as $word ) {
  39. $word = trim( $word );
  40. if ( empty( $word )
  41. or 256 < strlen( $word ) ) {
  42. continue;
  43. }
  44. $pattern = sprintf( '#%s#i', preg_quote( $word, '#' ) );
  45. if ( preg_match( $pattern, $target ) ) {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. function wpcf7_array_flatten( $input ) {
  52. if ( ! is_array( $input ) ) {
  53. return array( $input );
  54. }
  55. $output = array();
  56. foreach ( $input as $value ) {
  57. $output = array_merge( $output, wpcf7_array_flatten( $value ) );
  58. }
  59. return $output;
  60. }
  61. function wpcf7_flat_join( $input ) {
  62. $input = wpcf7_array_flatten( $input );
  63. $output = array();
  64. foreach ( (array) $input as $value ) {
  65. $output[] = trim( (string) $value );
  66. }
  67. return implode( ', ', $output );
  68. }
  69. function wpcf7_support_html5() {
  70. return (bool) apply_filters( 'wpcf7_support_html5', true );
  71. }
  72. function wpcf7_support_html5_fallback() {
  73. return (bool) apply_filters( 'wpcf7_support_html5_fallback', false );
  74. }
  75. function wpcf7_use_really_simple_captcha() {
  76. return apply_filters( 'wpcf7_use_really_simple_captcha',
  77. WPCF7_USE_REALLY_SIMPLE_CAPTCHA );
  78. }
  79. function wpcf7_validate_configuration() {
  80. return apply_filters( 'wpcf7_validate_configuration',
  81. WPCF7_VALIDATE_CONFIGURATION );
  82. }
  83. function wpcf7_autop_or_not() {
  84. return (bool) apply_filters( 'wpcf7_autop_or_not', WPCF7_AUTOP );
  85. }
  86. function wpcf7_load_js() {
  87. return apply_filters( 'wpcf7_load_js', WPCF7_LOAD_JS );
  88. }
  89. function wpcf7_load_css() {
  90. return apply_filters( 'wpcf7_load_css', WPCF7_LOAD_CSS );
  91. }
  92. function wpcf7_format_atts( $atts ) {
  93. $html = '';
  94. $prioritized_atts = array( 'type', 'name', 'value' );
  95. foreach ( $prioritized_atts as $att ) {
  96. if ( isset( $atts[$att] ) ) {
  97. $value = trim( $atts[$att] );
  98. $html .= sprintf( ' %s="%s"', $att, esc_attr( $value ) );
  99. unset( $atts[$att] );
  100. }
  101. }
  102. foreach ( $atts as $key => $value ) {
  103. $key = strtolower( trim( $key ) );
  104. if ( ! preg_match( '/^[a-z_:][a-z_:.0-9-]*$/', $key ) ) {
  105. continue;
  106. }
  107. $value = trim( $value );
  108. if ( '' !== $value ) {
  109. $html .= sprintf( ' %s="%s"', $key, esc_attr( $value ) );
  110. }
  111. }
  112. $html = trim( $html );
  113. return $html;
  114. }
  115. function wpcf7_link( $url, $anchor_text, $args = '' ) {
  116. $defaults = array(
  117. 'id' => '',
  118. 'class' => '',
  119. );
  120. $args = wp_parse_args( $args, $defaults );
  121. $args = array_intersect_key( $args, $defaults );
  122. $atts = wpcf7_format_atts( $args );
  123. $link = sprintf( '<a href="%1$s"%3$s>%2$s</a>',
  124. esc_url( $url ),
  125. esc_html( $anchor_text ),
  126. $atts ? ( ' ' . $atts ) : '' );
  127. return $link;
  128. }
  129. function wpcf7_get_request_uri() {
  130. static $request_uri = '';
  131. if ( empty( $request_uri ) ) {
  132. $request_uri = add_query_arg( array() );
  133. }
  134. return esc_url_raw( $request_uri );
  135. }
  136. function wpcf7_register_post_types() {
  137. if ( class_exists( 'WPCF7_ContactForm' ) ) {
  138. WPCF7_ContactForm::register_post_type();
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. }
  144. function wpcf7_version( $args = '' ) {
  145. $defaults = array(
  146. 'limit' => -1,
  147. 'only_major' => false,
  148. );
  149. $args = wp_parse_args( $args, $defaults );
  150. if ( $args['only_major'] ) {
  151. $args['limit'] = 2;
  152. }
  153. $args['limit'] = (int) $args['limit'];
  154. $ver = WPCF7_VERSION;
  155. $ver = strtr( $ver, '_-+', '...' );
  156. $ver = preg_replace( '/[^0-9.]+/', ".$0.", $ver );
  157. $ver = preg_replace( '/[.]+/', ".", $ver );
  158. $ver = trim( $ver, '.' );
  159. $ver = explode( '.', $ver );
  160. if ( -1 < $args['limit'] ) {
  161. $ver = array_slice( $ver, 0, $args['limit'] );
  162. }
  163. $ver = implode( '.', $ver );
  164. return $ver;
  165. }
  166. function wpcf7_version_grep( $version, array $input ) {
  167. $pattern = '/^' . preg_quote( (string) $version, '/' ) . '(?:\.|$)/';
  168. return preg_grep( $pattern, $input );
  169. }
  170. function wpcf7_enctype_value( $enctype ) {
  171. $enctype = trim( $enctype );
  172. if ( empty( $enctype ) ) {
  173. return '';
  174. }
  175. $valid_enctypes = array(
  176. 'application/x-www-form-urlencoded',
  177. 'multipart/form-data',
  178. 'text/plain',
  179. );
  180. if ( in_array( $enctype, $valid_enctypes ) ) {
  181. return $enctype;
  182. }
  183. $pattern = '%^enctype="(' . implode( '|', $valid_enctypes ) . ')"$%';
  184. if ( preg_match( $pattern, $enctype, $matches ) ) {
  185. return $matches[1]; // for back-compat
  186. }
  187. return '';
  188. }
  189. function wpcf7_rmdir_p( $dir ) {
  190. if ( is_file( $dir ) ) {
  191. $file = $dir;
  192. if ( @unlink( $file ) ) {
  193. return true;
  194. }
  195. $stat = stat( $file );
  196. if ( @chmod( $file, $stat['mode'] | 0200 ) ) { // add write for owner
  197. if ( @unlink( $file ) ) {
  198. return true;
  199. }
  200. @chmod( $file, $stat['mode'] );
  201. }
  202. return false;
  203. }
  204. if ( ! is_dir( $dir ) ) {
  205. return false;
  206. }
  207. if ( $handle = opendir( $dir ) ) {
  208. while ( false !== ( $file = readdir( $handle ) ) ) {
  209. if ( $file == "."
  210. or $file == ".." ) {
  211. continue;
  212. }
  213. wpcf7_rmdir_p( path_join( $dir, $file ) );
  214. }
  215. closedir( $handle );
  216. }
  217. if ( false !== ( $files = scandir( $dir ) )
  218. and ! array_diff( $files, array( '.', '..' ) ) ) {
  219. return rmdir( $dir );
  220. }
  221. return false;
  222. }
  223. /* From _http_build_query in wp-includes/functions.php */
  224. function wpcf7_build_query( $args, $key = '' ) {
  225. $sep = '&';
  226. $ret = array();
  227. foreach ( (array) $args as $k => $v ) {
  228. $k = urlencode( $k );
  229. if ( ! empty( $key ) ) {
  230. $k = $key . '%5B' . $k . '%5D';
  231. }
  232. if ( null === $v ) {
  233. continue;
  234. } elseif ( false === $v ) {
  235. $v = '0';
  236. }
  237. if ( is_array( $v ) or is_object( $v ) ) {
  238. array_push( $ret, wpcf7_build_query( $v, $k ) );
  239. } else {
  240. array_push( $ret, $k . '=' . urlencode( $v ) );
  241. }
  242. }
  243. return implode( $sep, $ret );
  244. }
  245. /**
  246. * Returns the number of code units in a string.
  247. *
  248. * @see http://www.w3.org/TR/html5/infrastructure.html#code-unit-length
  249. *
  250. * @return int|bool The number of code units, or false if mb_convert_encoding is not available.
  251. */
  252. function wpcf7_count_code_units( $string ) {
  253. static $use_mb = null;
  254. if ( is_null( $use_mb ) ) {
  255. $use_mb = function_exists( 'mb_convert_encoding' );
  256. }
  257. if ( ! $use_mb ) {
  258. return false;
  259. }
  260. $string = (string) $string;
  261. $string = str_replace( "\r\n", "\n", $string );
  262. $encoding = mb_detect_encoding( $string, mb_detect_order(), true );
  263. if ( $encoding ) {
  264. $string = mb_convert_encoding( $string, 'UTF-16', $encoding );
  265. } else {
  266. $string = mb_convert_encoding( $string, 'UTF-16', 'UTF-8' );
  267. }
  268. $byte_count = mb_strlen( $string, '8bit' );
  269. return floor( $byte_count / 2 );
  270. }
  271. function wpcf7_is_localhost() {
  272. $server_name = strtolower( $_SERVER['SERVER_NAME'] );
  273. return in_array( $server_name, array( 'localhost', '127.0.0.1' ) );
  274. }
  275. function wpcf7_deprecated_function( $function, $version, $replacement ) {
  276. $trigger_error = apply_filters( 'deprecated_function_trigger_error', true );
  277. if ( WP_DEBUG and $trigger_error ) {
  278. if ( function_exists( '__' ) ) {
  279. trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since Contact Form 7 version %2$s! Use %3$s instead.', 'contact-form-7' ), $function, $version, $replacement ) );
  280. } else {
  281. trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since Contact Form 7 version %2$s! Use %3$s instead.', $function, $version, $replacement ) );
  282. }
  283. }
  284. }
  285. function wpcf7_log_remote_request( $url, $request, $response ) {
  286. $log = sprintf(
  287. /* translators: 1: response code, 2: message, 3: body, 4: URL */
  288. __( 'HTTP Response: %1$s %2$s %3$s from %4$s', 'contact-form-7' ),
  289. (int) wp_remote_retrieve_response_code( $response ),
  290. wp_remote_retrieve_response_message( $response ),
  291. wp_remote_retrieve_body( $response ),
  292. $url
  293. );
  294. $log = apply_filters( 'wpcf7_log_remote_request',
  295. $log, $url, $request, $response
  296. );
  297. if ( $log ) {
  298. trigger_error( $log );
  299. }
  300. }
  301. function wpcf7_anonymize_ip_addr( $ip_addr ) {
  302. if ( ! function_exists( 'inet_ntop' )
  303. or ! function_exists( 'inet_pton' ) ) {
  304. return $ip_addr;
  305. }
  306. $packed = inet_pton( $ip_addr );
  307. if ( false === $packed ) {
  308. return $ip_addr;
  309. }
  310. if ( 4 == strlen( $packed ) ) { // IPv4
  311. $mask = '255.255.255.0';
  312. } elseif ( 16 == strlen( $packed ) ) { // IPv6
  313. $mask = 'ffff:ffff:ffff:0000:0000:0000:0000:0000';
  314. } else {
  315. return $ip_addr;
  316. }
  317. return inet_ntop( $packed & inet_pton( $mask ) );
  318. }
  319. function wpcf7_is_file_path_in_content_dir( $path ) {
  320. if ( 0 === strpos( realpath( $path ), realpath( WP_CONTENT_DIR ) ) ) {
  321. return true;
  322. }
  323. if ( defined( 'UPLOADS' )
  324. and 0 === strpos( realpath( $path ), realpath( ABSPATH . UPLOADS ) ) ) {
  325. return true;
  326. }
  327. return false;
  328. }