/wp-content/plugins/contact-form-7/includes/functions.php

https://bitbucket.org/devbctph/futura_wp · PHP · 365 lines · 266 code · 91 blank · 8 comment · 46 complexity · 9c1c0dab5215f78394754e1d3e5e87c3 MD5 · raw file

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