PageRenderTime 34ms CodeModel.GetById 15ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

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