Issues (4967)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/wp-includes/functions.php (63 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Main WordPress API
4
 *
5
 * @package WordPress
6
 */
7
8
require( ABSPATH . WPINC . '/option.php' );
9
10
/**
11
 * Convert given date string into a different format.
12
 *
13
 * $format should be either a PHP date format string, e.g. 'U' for a Unix
14
 * timestamp, or 'G' for a Unix timestamp assuming that $date is GMT.
15
 *
16
 * If $translate is true then the given date and format string will
17
 * be passed to date_i18n() for translation.
18
 *
19
 * @since 0.71
20
 *
21
 * @param string $format    Format of the date to return.
22
 * @param string $date      Date string to convert.
23
 * @param bool   $translate Whether the return date should be translated. Default true.
24
 * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty.
0 ignored issues
show
Consider making the return type a bit more specific; maybe use false|integer|string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
25
 */
26
function mysql2date( $format, $date, $translate = true ) {
27
	if ( empty( $date ) )
28
		return false;
29
30
	if ( 'G' == $format )
31
		return strtotime( $date . ' +0000' );
32
33
	$i = strtotime( $date );
34
35
	if ( 'U' == $format )
36
		return $i;
37
38
	if ( $translate )
39
		return date_i18n( $format, $i );
40
	else
41
		return date( $format, $i );
42
}
43
44
/**
45
 * Retrieve the current time based on specified type.
46
 *
47
 * The 'mysql' type will return the time in the format for MySQL DATETIME field.
48
 * The 'timestamp' type will return the current timestamp.
49
 * Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d').
50
 *
51
 * If $gmt is set to either '1' or 'true', then both types will use GMT time.
52
 * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
53
 *
54
 * @since 1.0.0
55
 *
56
 * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date
57
 *                       format string (e.g. 'Y-m-d').
58
 * @param int|bool $gmt  Optional. Whether to use GMT timezone. Default false.
0 ignored issues
show
Consider making the type for parameter $gmt a bit more specific; maybe use integer.
Loading history...
59
 * @return int|string Integer if $type is 'timestamp', string otherwise.
0 ignored issues
show
Should the return type not be string|integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
60
 */
61
function current_time( $type, $gmt = 0 ) {
62
	switch ( $type ) {
63
		case 'mysql':
64
			return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
65 View Code Duplication
		case 'timestamp':
66
			return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
67 View Code Duplication
		default:
68
			return ( $gmt ) ? date( $type ) : date( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
69
	}
70
}
71
72
/**
73
 * Retrieve the date in localized format, based on timestamp.
74
 *
75
 * If the locale specifies the locale month and weekday, then the locale will
76
 * take over the format for the date. If it isn't, then the date format string
77
 * will be used instead.
78
 *
79
 * @since 0.71
80
 *
81
 * @global WP_Locale $wp_locale
82
 *
83
 * @param string   $dateformatstring Format to display the date.
84
 * @param bool|int $unixtimestamp    Optional. Unix timestamp. Default false.
85
 * @param bool     $gmt              Optional. Whether to use GMT timezone. Default false.
86
 *
87
 * @return string The date, translated if locale specifies it.
88
 */
89
function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
90
	global $wp_locale;
91
	$i = $unixtimestamp;
92
93
	if ( false === $i ) {
94
		$i = current_time( 'timestamp', $gmt );
95
	}
96
97
	/*
98
	 * Store original value for language with untypical grammars.
99
	 * See https://core.trac.wordpress.org/ticket/9396
100
	 */
101
	$req_format = $dateformatstring;
102
103
	if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
104
		$datemonth = $wp_locale->get_month( date( 'm', $i ) );
105
		$datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
106
		$dateweekday = $wp_locale->get_weekday( date( 'w', $i ) );
107
		$dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
108
		$datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) );
109
		$datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) );
110
		$dateformatstring = ' '.$dateformatstring;
111
		$dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
112
		$dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
113
		$dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
114
		$dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
115
		$dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
116
		$dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
117
118
		$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
119
	}
120
	$timezone_formats = array( 'P', 'I', 'O', 'T', 'Z', 'e' );
121
	$timezone_formats_re = implode( '|', $timezone_formats );
122
	if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) {
123
		$timezone_string = get_option( 'timezone_string' );
124
		if ( $timezone_string ) {
125
			$timezone_object = timezone_open( $timezone_string );
126
			$date_object = date_create( null, $timezone_object );
127
			foreach ( $timezone_formats as $timezone_format ) {
128
				if ( false !== strpos( $dateformatstring, $timezone_format ) ) {
129
					$formatted = date_format( $date_object, $timezone_format );
130
					$dateformatstring = ' '.$dateformatstring;
131
					$dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring );
132
					$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
133
				}
134
			}
135
		}
136
	}
137
	$j = @date( $dateformatstring, $i );
138
139
	/**
140
	 * Filters the date formatted based on the locale.
141
	 *
142
	 * @since 2.8.0
143
	 *
144
	 * @param string $j          Formatted date string.
145
	 * @param string $req_format Format to display the date.
146
	 * @param int    $i          Unix timestamp.
147
	 * @param bool   $gmt        Whether to convert to GMT for time. Default false.
148
	 */
149
	$j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt );
150
	return $j;
151
}
152
153
/**
154
 * Determines if the date should be declined.
155
 *
156
 * If the locale specifies that month names require a genitive case in certain
157
 * formats (like 'j F Y'), the month name will be replaced with a correct form.
158
 *
159
 * @since 4.4.0
160
 *
161
 * @global WP_Locale $wp_locale
162
 *
163
 * @param string $date Formatted date string.
164
 * @return string The date, declined if locale specifies it.
165
 */
166
function wp_maybe_decline_date( $date ) {
167
	global $wp_locale;
168
169
	// i18n functions are not available in SHORTINIT mode
170
	if ( ! function_exists( '_x' ) ) {
171
		return $date;
172
	}
173
174
	/* translators: If months in your language require a genitive case,
175
	 * translate this to 'on'. Do not translate into your own language.
176
	 */
177
	if ( 'on' === _x( 'off', 'decline months names: on or off' ) ) {
178
		// Match a format like 'j F Y' or 'j. F'
179
		if ( @preg_match( '#^\d{1,2}\.? [^\d ]+#u', $date ) ) {
180
			$months          = $wp_locale->month;
181
			$months_genitive = $wp_locale->month_genitive;
182
183
			foreach ( $months as $key => $month ) {
184
				$months[ $key ] = '# ' . $month . '( |$)#u';
185
			}
186
187
			foreach ( $months_genitive as $key => $month ) {
188
				$months_genitive[ $key ] = ' ' . $month . '$1';
189
			}
190
191
			$date = preg_replace( $months, $months_genitive, $date );
192
		}
193
	}
194
195
	// Used for locale-specific rules
196
	$locale = get_locale();
197
198
	if ( 'ca' === $locale ) {
199
		// " de abril| de agost| de octubre..." -> " d'abril| d'agost| d'octubre..."
200
		$date = preg_replace( '# de ([ao])#i', " d'\\1", $date );
201
	}
202
203
	return $date;
204
}
205
206
/**
207
 * Convert float number to format based on the locale.
208
 *
209
 * @since 2.3.0
210
 *
211
 * @global WP_Locale $wp_locale
212
 *
213
 * @param float $number   The number to convert based on locale.
214
 * @param int   $decimals Optional. Precision of the number of decimal places. Default 0.
215
 * @return string Converted number in string format.
216
 */
217
function number_format_i18n( $number, $decimals = 0 ) {
218
	global $wp_locale;
219
220
	if ( isset( $wp_locale ) ) {
221
		$formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
222
	} else {
223
		$formatted = number_format( $number, absint( $decimals ) );
224
	}
225
226
	/**
227
	 * Filters the number formatted based on the locale.
228
	 *
229
	 * @since  2.8.0
230
	 *
231
	 * @param string $formatted Converted number in string format.
232
	 */
233
	return apply_filters( 'number_format_i18n', $formatted );
234
}
235
236
/**
237
 * Convert number of bytes largest unit bytes will fit into.
238
 *
239
 * It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts
240
 * number of bytes to human readable number by taking the number of that unit
241
 * that the bytes will go into it. Supports TB value.
242
 *
243
 * Please note that integers in PHP are limited to 32 bits, unless they are on
244
 * 64 bit architecture, then they have 64 bit size. If you need to place the
245
 * larger size then what PHP integer type will hold, then use a string. It will
246
 * be converted to a double, which should always have 64 bit length.
247
 *
248
 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
249
 *
250
 * @since 2.3.0
251
 *
252
 * @param int|string $bytes    Number of bytes. Note max integer size for integers.
253
 * @param int        $decimals Optional. Precision of number of decimal places. Default 0.
254
 * @return string|false False on failure. Number string on success.
255
 */
256
function size_format( $bytes, $decimals = 0 ) {
257
	$quant = array(
258
		'TB' => TB_IN_BYTES,
259
		'GB' => GB_IN_BYTES,
260
		'MB' => MB_IN_BYTES,
261
		'KB' => KB_IN_BYTES,
262
		'B'  => 1,
263
	);
264
265
	if ( 0 === $bytes ) {
266
		return number_format_i18n( 0, $decimals ) . ' B';
267
	}
268
269
	foreach ( $quant as $unit => $mag ) {
270
		if ( doubleval( $bytes ) >= $mag ) {
271
			return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
272
		}
273
	}
274
275
	return false;
276
}
277
278
/**
279
 * Get the week start and end from the datetime or date string from MySQL.
280
 *
281
 * @since 0.71
282
 *
283
 * @param string     $mysqlstring   Date or datetime field type from MySQL.
284
 * @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string.
285
 * @return array Keys are 'start' and 'end'.
286
 */
287
function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
288
	// MySQL string year.
289
	$my = substr( $mysqlstring, 0, 4 );
290
291
	// MySQL string month.
292
	$mm = substr( $mysqlstring, 8, 2 );
293
294
	// MySQL string day.
295
	$md = substr( $mysqlstring, 5, 2 );
296
297
	// The timestamp for MySQL string day.
298
	$day = mktime( 0, 0, 0, $md, $mm, $my );
299
300
	// The day of the week from the timestamp.
301
	$weekday = date( 'w', $day );
302
303
	if ( !is_numeric($start_of_week) )
304
		$start_of_week = get_option( 'start_of_week' );
305
306
	if ( $weekday < $start_of_week )
307
		$weekday += 7;
308
309
	// The most recent week start day on or before $day.
310
	$start = $day - DAY_IN_SECONDS * ( $weekday - $start_of_week );
311
312
	// $start + 1 week - 1 second.
313
	$end = $start + WEEK_IN_SECONDS - 1;
314
	return compact( 'start', 'end' );
315
}
316
317
/**
318
 * Unserialize value only if it was serialized.
319
 *
320
 * @since 2.0.0
321
 *
322
 * @param string $original Maybe unserialized original, if is needed.
323
 * @return mixed Unserialized data can be any type.
324
 */
325
function maybe_unserialize( $original ) {
326
	if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
327
		return @unserialize( $original );
328
	return $original;
329
}
330
331
/**
332
 * Check value to find if it was serialized.
333
 *
334
 * If $data is not an string, then returned value will always be false.
335
 * Serialized data is always a string.
336
 *
337
 * @since 2.0.5
338
 *
339
 * @param string $data   Value to check to see if was serialized.
340
 * @param bool   $strict Optional. Whether to be strict about the end of the string. Default true.
341
 * @return bool False if not serialized and true if it was.
342
 */
343
function is_serialized( $data, $strict = true ) {
344
	// if it isn't a string, it isn't serialized.
345
	if ( ! is_string( $data ) ) {
346
		return false;
347
	}
348
	$data = trim( $data );
349
 	if ( 'N;' == $data ) {
350
		return true;
351
	}
352
	if ( strlen( $data ) < 4 ) {
353
		return false;
354
	}
355
	if ( ':' !== $data[1] ) {
356
		return false;
357
	}
358
	if ( $strict ) {
359
		$lastc = substr( $data, -1 );
360
		if ( ';' !== $lastc && '}' !== $lastc ) {
361
			return false;
362
		}
363
	} else {
364
		$semicolon = strpos( $data, ';' );
365
		$brace     = strpos( $data, '}' );
366
		// Either ; or } must exist.
367
		if ( false === $semicolon && false === $brace )
368
			return false;
369
		// But neither must be in the first X characters.
370
		if ( false !== $semicolon && $semicolon < 3 )
371
			return false;
372
		if ( false !== $brace && $brace < 4 )
373
			return false;
374
	}
375
	$token = $data[0];
376
	switch ( $token ) {
377
		case 's' :
378
			if ( $strict ) {
379
				if ( '"' !== substr( $data, -2, 1 ) ) {
380
					return false;
381
				}
382
			} elseif ( false === strpos( $data, '"' ) ) {
383
				return false;
384
			}
385
			// or else fall through
386
		case 'a' :
387
		case 'O' :
388
			return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
389
		case 'b' :
390
		case 'i' :
391
		case 'd' :
392
			$end = $strict ? '$' : '';
393
			return (bool) preg_match( "/^{$token}:[0-9.E-]+;$end/", $data );
394
	}
395
	return false;
396
}
397
398
/**
399
 * Check whether serialized data is of string type.
400
 *
401
 * @since 2.0.5
402
 *
403
 * @param string $data Serialized data.
404
 * @return bool False if not a serialized string, true if it is.
405
 */
406
function is_serialized_string( $data ) {
407
	// if it isn't a string, it isn't a serialized string.
408
	if ( ! is_string( $data ) ) {
409
		return false;
410
	}
411
	$data = trim( $data );
412
	if ( strlen( $data ) < 4 ) {
413
		return false;
414
	} elseif ( ':' !== $data[1] ) {
415
		return false;
416
	} elseif ( ';' !== substr( $data, -1 ) ) {
417
		return false;
418
	} elseif ( $data[0] !== 's' ) {
419
		return false;
420
	} elseif ( '"' !== substr( $data, -2, 1 ) ) {
421
		return false;
422
	} else {
423
		return true;
424
	}
425
}
426
427
/**
428
 * Serialize data, if needed.
429
 *
430
 * @since 2.0.5
431
 *
432
 * @param string|array|object $data Data that might be serialized.
433
 * @return mixed A scalar data
0 ignored issues
show
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
434
 */
435
function maybe_serialize( $data ) {
436
	if ( is_array( $data ) || is_object( $data ) )
437
		return serialize( $data );
438
439
	// Double serialization is required for backward compatibility.
440
	// See https://core.trac.wordpress.org/ticket/12930
441
	// Also the world will end. See WP 3.6.1.
442
	if ( is_serialized( $data, false ) )
443
		return serialize( $data );
444
445
	return $data;
446
}
447
448
/**
449
 * Retrieve post title from XMLRPC XML.
450
 *
451
 * If the title element is not part of the XML, then the default post title from
452
 * the $post_default_title will be used instead.
453
 *
454
 * @since 0.71
455
 *
456
 * @global string $post_default_title Default XML-RPC post title.
457
 *
458
 * @param string $content XMLRPC XML Request content
459
 * @return string Post title
460
 */
461
function xmlrpc_getposttitle( $content ) {
462
	global $post_default_title;
463
	if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) {
464
		$post_title = $matchtitle[1];
465
	} else {
466
		$post_title = $post_default_title;
467
	}
468
	return $post_title;
469
}
470
471
/**
472
 * Retrieve the post category or categories from XMLRPC XML.
473
 *
474
 * If the category element is not found, then the default post category will be
475
 * used. The return type then would be what $post_default_category. If the
476
 * category is found, then it will always be an array.
477
 *
478
 * @since 0.71
479
 *
480
 * @global string $post_default_category Default XML-RPC post category.
481
 *
482
 * @param string $content XMLRPC XML Request content
483
 * @return string|array List of categories or category name.
484
 */
485
function xmlrpc_getpostcategory( $content ) {
486
	global $post_default_category;
487
	if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) {
488
		$post_category = trim( $matchcat[1], ',' );
489
		$post_category = explode( ',', $post_category );
490
	} else {
491
		$post_category = $post_default_category;
492
	}
493
	return $post_category;
494
}
495
496
/**
497
 * XMLRPC XML content without title and category elements.
498
 *
499
 * @since 0.71
500
 *
501
 * @param string $content XML-RPC XML Request content.
502
 * @return string XMLRPC XML Request content without title and category elements.
503
 */
504 View Code Duplication
function xmlrpc_removepostdata( $content ) {
0 ignored issues
show
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
505
	$content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content );
506
	$content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content );
507
	$content = trim( $content );
508
	return $content;
509
}
510
511
/**
512
 * Use RegEx to extract URLs from arbitrary content.
513
 *
514
 * @since 3.7.0
515
 *
516
 * @param string $content Content to extract URLs from.
517
 * @return array URLs found in passed string.
518
 */
519
function wp_extract_urls( $content ) {
520
	preg_match_all(
521
		"#([\"']?)("
522
			. "(?:([\w-]+:)?//?)"
523
			. "[^\s()<>]+"
524
			. "[.]"
525
			. "(?:"
526
				. "\([\w\d]+\)|"
527
				. "(?:"
528
					. "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|"
529
					. "(?:[:]\d+)?/?"
530
				. ")+"
531
			. ")"
532
		. ")\\1#",
533
		$content,
534
		$post_links
535
	);
536
537
	$post_links = array_unique( array_map( 'html_entity_decode', $post_links[2] ) );
538
539
	return array_values( $post_links );
540
}
541
542
/**
543
 * Check content for video and audio links to add as enclosures.
544
 *
545
 * Will not add enclosures that have already been added and will
546
 * remove enclosures that are no longer in the post. This is called as
547
 * pingbacks and trackbacks.
548
 *
549
 * @since 1.5.0
550
 *
551
 * @global wpdb $wpdb WordPress database abstraction object.
552
 *
553
 * @param string $content Post Content.
554
 * @param int    $post_ID Post ID.
555
 */
556
function do_enclose( $content, $post_ID ) {
557
	global $wpdb;
558
559
	//TODO: Tidy this ghetto code up and make the debug code optional
560
	include_once( ABSPATH . WPINC . '/class-IXR.php' );
561
562
	$post_links = array();
563
564
	$pung = get_enclosed( $post_ID );
565
566
	$post_links_temp = wp_extract_urls( $content );
567
568
	foreach ( $pung as $link_test ) {
569
		if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post
570
			$mids = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like( $link_test ) . '%') );
571
			foreach ( $mids as $mid )
572
				delete_metadata_by_mid( 'post', $mid );
573
		}
574
	}
575
576
	foreach ( (array) $post_links_temp as $link_test ) {
577
		if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
578
			$test = @parse_url( $link_test );
579
			if ( false === $test )
580
				continue;
581 View Code Duplication
			if ( isset( $test['query'] ) )
582
				$post_links[] = $link_test;
583
			elseif ( isset($test['path']) && ( $test['path'] != '/' ) &&  ($test['path'] != '' ) )
584
				$post_links[] = $link_test;
585
		}
586
	}
587
588
	/**
589
	 * Filters the list of enclosure links before querying the database.
590
	 *
591
	 * Allows for the addition and/or removal of potential enclosures to save
592
	 * to postmeta before checking the database for existing enclosures.
593
	 *
594
	 * @since 4.4.0
595
	 *
596
	 * @param array $post_links An array of enclosure links.
597
	 * @param int   $post_ID    Post ID.
598
	 */
599
	$post_links = apply_filters( 'enclosure_links', $post_links, $post_ID );
600
601
	foreach ( (array) $post_links as $url ) {
602
		if ( $url != '' && !$wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like( $url ) . '%' ) ) ) {
603
604
			if ( $headers = wp_get_http_headers( $url) ) {
605
				$len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0;
606
				$type = isset( $headers['content-type'] ) ? $headers['content-type'] : '';
607
				$allowed_types = array( 'video', 'audio' );
608
609
				// Check to see if we can figure out the mime type from
610
				// the extension
611
				$url_parts = @parse_url( $url );
612
				if ( false !== $url_parts ) {
613
					$extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
614
					if ( !empty( $extension ) ) {
615
						foreach ( wp_get_mime_types() as $exts => $mime ) {
616
							if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
617
								$type = $mime;
618
								break;
619
							}
620
						}
621
					}
622
				}
623
624
				if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
625
					add_post_meta( $post_ID, 'enclosure', "$url\n$len\n$mime\n" );
0 ignored issues
show
The variable $mime seems to be defined by a foreach iteration on line 615. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
626
				}
627
			}
628
		}
629
	}
630
}
631
632
/**
633
 * Retrieve HTTP Headers from URL.
634
 *
635
 * @since 1.5.1
636
 *
637
 * @param string $url        URL to retrieve HTTP headers from.
638
 * @param bool   $deprecated Not Used.
639
 * @return bool|string False on failure, headers on success.
0 ignored issues
show
Should the return type not be false|array|Requests_Uti...seInsensitiveDictionary? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
640
 */
641
function wp_get_http_headers( $url, $deprecated = false ) {
642
	if ( !empty( $deprecated ) )
643
		_deprecated_argument( __FUNCTION__, '2.7.0' );
644
645
	$response = wp_safe_remote_head( $url );
646
647
	if ( is_wp_error( $response ) )
648
		return false;
649
650
	return wp_remote_retrieve_headers( $response );
651
}
652
653
/**
654
 * Whether the publish date of the current post in the loop is different from the
655
 * publish date of the previous post in the loop.
656
 *
657
 * @since 0.71
658
 *
659
 * @global string $currentday  The day of the current post in the loop.
660
 * @global string $previousday The day of the previous post in the loop.
661
 *
662
 * @return int 1 when new day, 0 if not a new day.
663
 */
664
function is_new_day() {
665
	global $currentday, $previousday;
666
	if ( $currentday != $previousday )
667
		return 1;
668
	else
669
		return 0;
670
}
671
672
/**
673
 * Build URL query based on an associative and, or indexed array.
674
 *
675
 * This is a convenient function for easily building url queries. It sets the
676
 * separator to '&' and uses _http_build_query() function.
677
 *
678
 * @since 2.3.0
679
 *
680
 * @see _http_build_query() Used to build the query
681
 * @link https://secure.php.net/manual/en/function.http-build-query.php for more on what
682
 *		 http_build_query() does.
683
 *
684
 * @param array $data URL-encode key/value pairs.
685
 * @return string URL-encoded string.
686
 */
687
function build_query( $data ) {
688
	return _http_build_query( $data, null, '&', '', false );
689
}
690
691
/**
692
 * From php.net (modified by Mark Jaquith to behave like the native PHP5 function).
693
 *
694
 * @since 3.2.0
695
 * @access private
696
 *
697
 * @see https://secure.php.net/manual/en/function.http-build-query.php
698
 *
699
 * @param array|object  $data       An array or object of data. Converted to array.
700
 * @param string        $prefix     Optional. Numeric index. If set, start parameter numbering with it.
701
 *                                  Default null.
702
 * @param string        $sep        Optional. Argument separator; defaults to 'arg_separator.output'.
703
 *                                  Default null.
704
 * @param string        $key        Optional. Used to prefix key name. Default empty.
705
 * @param bool          $urlencode  Optional. Whether to use urlencode() in the result. Default true.
706
 *
707
 * @return string The query string.
708
 */
709
function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) {
710
	$ret = array();
711
712
	foreach ( (array) $data as $k => $v ) {
713
		if ( $urlencode)
714
			$k = urlencode($k);
715
		if ( is_int($k) && $prefix != null )
0 ignored issues
show
It seems like you are loosely comparing $prefix of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
716
			$k = $prefix.$k;
717
		if ( !empty($key) )
718
			$k = $key . '%5B' . $k . '%5D';
719
		if ( $v === null )
720
			continue;
721
		elseif ( $v === false )
722
			$v = '0';
723
724
		if ( is_array($v) || is_object($v) )
725
			array_push($ret,_http_build_query($v, '', $sep, $k, $urlencode));
726
		elseif ( $urlencode )
727
			array_push($ret, $k.'='.urlencode($v));
728
		else
729
			array_push($ret, $k.'='.$v);
730
	}
731
732
	if ( null === $sep )
733
		$sep = ini_get('arg_separator.output');
734
735
	return implode($sep, $ret);
736
}
737
738
/**
739
 * Retrieves a modified URL query string.
740
 *
741
 * You can rebuild the URL and append query variables to the URL query by using this function.
742
 * There are two ways to use this function; either a single key and value, or an associative array.
743
 *
744
 * Using a single key and value:
745
 *
746
 *     add_query_arg( 'key', 'value', 'http://example.com' );
747
 *
748
 * Using an associative array:
749
 *
750
 *     add_query_arg( array(
751
 *         'key1' => 'value1',
752
 *         'key2' => 'value2',
753
 *     ), 'http://example.com' );
754
 *
755
 * Omitting the URL from either use results in the current URL being used
756
 * (the value of `$_SERVER['REQUEST_URI']`).
757
 *
758
 * Values are expected to be encoded appropriately with urlencode() or rawurlencode().
759
 *
760
 * Setting any query variable's value to boolean false removes the key (see remove_query_arg()).
761
 *
762
 * Important: The return value of add_query_arg() is not escaped by default. Output should be
763
 * late-escaped with esc_url() or similar to help prevent vulnerability to cross-site scripting
764
 * (XSS) attacks.
765
 *
766
 * @since 1.5.0
767
 *
768
 * @param string|array $key   Either a query variable key, or an associative array of query variables.
0 ignored issues
show
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
769
 * @param string       $value Optional. Either a query variable value, or a URL to act upon.
0 ignored issues
show
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
770
 * @param string       $url   Optional. A URL to act upon.
0 ignored issues
show
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
771
 * @return string New URL query string (unescaped).
772
 */
773
function add_query_arg() {
774
	$args = func_get_args();
775
	if ( is_array( $args[0] ) ) {
776 View Code Duplication
		if ( count( $args ) < 2 || false === $args[1] )
777
			$uri = $_SERVER['REQUEST_URI'];
778
		else
779
			$uri = $args[1];
780 View Code Duplication
	} else {
781
		if ( count( $args ) < 3 || false === $args[2] )
782
			$uri = $_SERVER['REQUEST_URI'];
783
		else
784
			$uri = $args[2];
785
	}
786
787
	if ( $frag = strstr( $uri, '#' ) )
788
		$uri = substr( $uri, 0, -strlen( $frag ) );
789
	else
790
		$frag = '';
791
792
	if ( 0 === stripos( $uri, 'http://' ) ) {
793
		$protocol = 'http://';
794
		$uri = substr( $uri, 7 );
795
	} elseif ( 0 === stripos( $uri, 'https://' ) ) {
796
		$protocol = 'https://';
797
		$uri = substr( $uri, 8 );
798
	} else {
799
		$protocol = '';
800
	}
801
802
	if ( strpos( $uri, '?' ) !== false ) {
803
		list( $base, $query ) = explode( '?', $uri, 2 );
804
		$base .= '?';
805
	} elseif ( $protocol || strpos( $uri, '=' ) === false ) {
806
		$base = $uri . '?';
807
		$query = '';
808
	} else {
809
		$base = '';
810
		$query = $uri;
811
	}
812
813
	wp_parse_str( $query, $qs );
814
	$qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
815
	if ( is_array( $args[0] ) ) {
816
		foreach ( $args[0] as $k => $v ) {
817
			$qs[ $k ] = $v;
818
		}
819
	} else {
820
		$qs[ $args[0] ] = $args[1];
821
	}
822
823
	foreach ( $qs as $k => $v ) {
824
		if ( $v === false )
825
			unset( $qs[$k] );
826
	}
827
828
	$ret = build_query( $qs );
829
	$ret = trim( $ret, '?' );
830
	$ret = preg_replace( '#=(&|$)#', '$1', $ret );
831
	$ret = $protocol . $base . $ret . $frag;
832
	$ret = rtrim( $ret, '?' );
833
	return $ret;
834
}
835
836
/**
837
 * Removes an item or items from a query string.
838
 *
839
 * @since 1.5.0
840
 *
841
 * @param string|array $key   Query key or keys to remove.
842
 * @param bool|string  $query Optional. When false uses the current URL. Default false.
843
 * @return string New URL query string.
0 ignored issues
show
Should the return type not be boolean|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
844
 */
845
function remove_query_arg( $key, $query = false ) {
846
	if ( is_array( $key ) ) { // removing multiple keys
847
		foreach ( $key as $k )
848
			$query = add_query_arg( $k, false, $query );
849
		return $query;
850
	}
851
	return add_query_arg( $key, false, $query );
852
}
853
854
/**
855
 * Returns an array of single-use query variable names that can be removed from a URL.
856
 *
857
 * @since 4.4.0
858
 *
859
 * @return array An array of parameters to remove from the URL.
860
 */
861
function wp_removable_query_args() {
862
	$removable_query_args = array(
863
		'activate',
864
		'activated',
865
		'approved',
866
		'deactivate',
867
		'deleted',
868
		'disabled',
869
		'enabled',
870
		'error',
871
		'hotkeys_highlight_first',
872
		'hotkeys_highlight_last',
873
		'locked',
874
		'message',
875
		'same',
876
		'saved',
877
		'settings-updated',
878
		'skipped',
879
		'spammed',
880
		'trashed',
881
		'unspammed',
882
		'untrashed',
883
		'update',
884
		'updated',
885
		'wp-post-new-reload',
886
	);
887
888
	/**
889
	 * Filters the list of query variables to remove.
890
	 *
891
	 * @since 4.2.0
892
	 *
893
	 * @param array $removable_query_args An array of query variables to remove from a URL.
894
	 */
895
	return apply_filters( 'removable_query_args', $removable_query_args );
896
}
897
898
/**
899
 * Walks the array while sanitizing the contents.
900
 *
901
 * @since 0.71
902
 *
903
 * @param array $array Array to walk while sanitizing contents.
904
 * @return array Sanitized $array.
905
 */
906
function add_magic_quotes( $array ) {
907 View Code Duplication
	foreach ( (array) $array as $k => $v ) {
908
		if ( is_array( $v ) ) {
909
			$array[$k] = add_magic_quotes( $v );
910
		} else {
911
			$array[$k] = addslashes( $v );
912
		}
913
	}
914
	return $array;
915
}
916
917
/**
918
 * HTTP request for URI to retrieve content.
919
 *
920
 * @since 1.5.1
921
 *
922
 * @see wp_safe_remote_get()
923
 *
924
 * @param string $uri URI/URL of web page to retrieve.
925
 * @return false|string HTTP content. False on failure.
926
 */
927
function wp_remote_fopen( $uri ) {
928
	$parsed_url = @parse_url( $uri );
929
930
	if ( !$parsed_url || !is_array( $parsed_url ) )
931
		return false;
932
933
	$options = array();
934
	$options['timeout'] = 10;
935
936
	$response = wp_safe_remote_get( $uri, $options );
937
938
	if ( is_wp_error( $response ) )
939
		return false;
940
941
	return wp_remote_retrieve_body( $response );
942
}
943
944
/**
945
 * Set up the WordPress query.
946
 *
947
 * @since 2.0.0
948
 *
949
 * @global WP       $wp_locale
950
 * @global WP_Query $wp_query
951
 * @global WP_Query $wp_the_query
952
 *
953
 * @param string|array $query_vars Default WP_Query arguments.
954
 */
955
function wp( $query_vars = '' ) {
0 ignored issues
show
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
956
	global $wp, $wp_query, $wp_the_query;
957
	$wp->main( $query_vars );
958
959
	if ( !isset($wp_the_query) )
960
		$wp_the_query = $wp_query;
961
}
962
963
/**
964
 * Retrieve the description for the HTTP status.
965
 *
966
 * @since 2.3.0
967
 *
968
 * @global array $wp_header_to_desc
969
 *
970
 * @param int $code HTTP status code.
971
 * @return string Empty string if not found, or description if found.
972
 */
973
function get_status_header_desc( $code ) {
974
	global $wp_header_to_desc;
975
976
	$code = absint( $code );
977
978
	if ( !isset( $wp_header_to_desc ) ) {
979
		$wp_header_to_desc = array(
980
			100 => 'Continue',
981
			101 => 'Switching Protocols',
982
			102 => 'Processing',
983
984
			200 => 'OK',
985
			201 => 'Created',
986
			202 => 'Accepted',
987
			203 => 'Non-Authoritative Information',
988
			204 => 'No Content',
989
			205 => 'Reset Content',
990
			206 => 'Partial Content',
991
			207 => 'Multi-Status',
992
			226 => 'IM Used',
993
994
			300 => 'Multiple Choices',
995
			301 => 'Moved Permanently',
996
			302 => 'Found',
997
			303 => 'See Other',
998
			304 => 'Not Modified',
999
			305 => 'Use Proxy',
1000
			306 => 'Reserved',
1001
			307 => 'Temporary Redirect',
1002
			308 => 'Permanent Redirect',
1003
1004
			400 => 'Bad Request',
1005
			401 => 'Unauthorized',
1006
			402 => 'Payment Required',
1007
			403 => 'Forbidden',
1008
			404 => 'Not Found',
1009
			405 => 'Method Not Allowed',
1010
			406 => 'Not Acceptable',
1011
			407 => 'Proxy Authentication Required',
1012
			408 => 'Request Timeout',
1013
			409 => 'Conflict',
1014
			410 => 'Gone',
1015
			411 => 'Length Required',
1016
			412 => 'Precondition Failed',
1017
			413 => 'Request Entity Too Large',
1018
			414 => 'Request-URI Too Long',
1019
			415 => 'Unsupported Media Type',
1020
			416 => 'Requested Range Not Satisfiable',
1021
			417 => 'Expectation Failed',
1022
			418 => 'I\'m a teapot',
1023
			421 => 'Misdirected Request',
1024
			422 => 'Unprocessable Entity',
1025
			423 => 'Locked',
1026
			424 => 'Failed Dependency',
1027
			426 => 'Upgrade Required',
1028
			428 => 'Precondition Required',
1029
			429 => 'Too Many Requests',
1030
			431 => 'Request Header Fields Too Large',
1031
			451 => 'Unavailable For Legal Reasons',
1032
1033
			500 => 'Internal Server Error',
1034
			501 => 'Not Implemented',
1035
			502 => 'Bad Gateway',
1036
			503 => 'Service Unavailable',
1037
			504 => 'Gateway Timeout',
1038
			505 => 'HTTP Version Not Supported',
1039
			506 => 'Variant Also Negotiates',
1040
			507 => 'Insufficient Storage',
1041
			510 => 'Not Extended',
1042
			511 => 'Network Authentication Required',
1043
		);
1044
	}
1045
1046
	if ( isset( $wp_header_to_desc[$code] ) )
1047
		return $wp_header_to_desc[$code];
1048
	else
1049
		return '';
1050
}
1051
1052
/**
1053
 * Set HTTP status header.
1054
 *
1055
 * @since 2.0.0
1056
 * @since 4.4.0 Added the `$description` parameter.
1057
 *
1058
 * @see get_status_header_desc()
1059
 *
1060
 * @param int    $code        HTTP status code.
1061
 * @param string $description Optional. A custom description for the HTTP status.
1062
 */
1063
function status_header( $code, $description = '' ) {
1064
	if ( ! $description ) {
1065
		$description = get_status_header_desc( $code );
1066
	}
1067
1068
	if ( empty( $description ) ) {
1069
		return;
1070
	}
1071
1072
	$protocol = wp_get_server_protocol();
1073
	$status_header = "$protocol $code $description";
1074
	if ( function_exists( 'apply_filters' ) )
1075
1076
		/**
1077
		 * Filters an HTTP status header.
1078
		 *
1079
		 * @since 2.2.0
1080
		 *
1081
		 * @param string $status_header HTTP status header.
1082
		 * @param int    $code          HTTP status code.
1083
		 * @param string $description   Description for the status code.
1084
		 * @param string $protocol      Server protocol.
1085
		 */
1086
		$status_header = apply_filters( 'status_header', $status_header, $code, $description, $protocol );
1087
1088
	@header( $status_header, true, $code );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1089
}
1090
1091
/**
1092
 * Get the header information to prevent caching.
1093
 *
1094
 * The several different headers cover the different ways cache prevention
1095
 * is handled by different browsers
1096
 *
1097
 * @since 2.8.0
1098
 *
1099
 * @return array The associative array of header names and field values.
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,false>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
1100
 */
1101
function wp_get_nocache_headers() {
1102
	$headers = array(
1103
		'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
1104
		'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
1105
	);
1106
1107
	if ( function_exists('apply_filters') ) {
1108
		/**
1109
		 * Filters the cache-controlling headers.
1110
		 *
1111
		 * @since 2.8.0
1112
		 *
1113
		 * @see wp_get_nocache_headers()
1114
		 *
1115
		 * @param array $headers {
1116
		 *     Header names and field values.
1117
		 *
1118
		 *     @type string $Expires       Expires header.
1119
		 *     @type string $Cache-Control Cache-Control header.
1120
		 * }
1121
		 */
1122
		$headers = (array) apply_filters( 'nocache_headers', $headers );
1123
	}
1124
	$headers['Last-Modified'] = false;
1125
	return $headers;
1126
}
1127
1128
/**
1129
 * Set the headers to prevent caching for the different browsers.
1130
 *
1131
 * Different browsers support different nocache headers, so several
1132
 * headers must be sent so that all of them get the point that no
1133
 * caching should occur.
1134
 *
1135
 * @since 2.0.0
1136
 *
1137
 * @see wp_get_nocache_headers()
1138
 */
1139
function nocache_headers() {
1140
	$headers = wp_get_nocache_headers();
1141
1142
	unset( $headers['Last-Modified'] );
1143
1144
	// In PHP 5.3+, make sure we are not sending a Last-Modified header.
1145 View Code Duplication
	if ( function_exists( 'header_remove' ) ) {
1146
		@header_remove( 'Last-Modified' );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1147
	} else {
1148
		// In PHP 5.2, send an empty Last-Modified header, but only as a
1149
		// last resort to override a header already sent. #WP23021
1150
		foreach ( headers_list() as $header ) {
1151
			if ( 0 === stripos( $header, 'Last-Modified' ) ) {
1152
				$headers['Last-Modified'] = '';
1153
				break;
1154
			}
1155
		}
1156
	}
1157
1158
	foreach ( $headers as $name => $field_value )
1159
		@header("{$name}: {$field_value}");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1160
}
1161
1162
/**
1163
 * Set the headers for caching for 10 days with JavaScript content type.
1164
 *
1165
 * @since 2.1.0
1166
 */
1167
function cache_javascript_headers() {
1168
	$expiresOffset = 10 * DAY_IN_SECONDS;
1169
1170
	header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
1171
	header( "Vary: Accept-Encoding" ); // Handle proxies
1172
	header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
1173
}
1174
1175
/**
1176
 * Retrieve the number of database queries during the WordPress execution.
1177
 *
1178
 * @since 2.0.0
1179
 *
1180
 * @global wpdb $wpdb WordPress database abstraction object.
1181
 *
1182
 * @return int Number of database queries.
1183
 */
1184
function get_num_queries() {
1185
	global $wpdb;
1186
	return $wpdb->num_queries;
1187
}
1188
1189
/**
1190
 * Whether input is yes or no.
1191
 *
1192
 * Must be 'y' to be true.
1193
 *
1194
 * @since 1.0.0
1195
 *
1196
 * @param string $yn Character string containing either 'y' (yes) or 'n' (no).
1197
 * @return bool True if yes, false on anything else.
1198
 */
1199
function bool_from_yn( $yn ) {
1200
	return ( strtolower( $yn ) == 'y' );
1201
}
1202
1203
/**
1204
 * Load the feed template from the use of an action hook.
1205
 *
1206
 * If the feed action does not have a hook, then the function will die with a
1207
 * message telling the visitor that the feed is not valid.
1208
 *
1209
 * It is better to only have one hook for each feed.
1210
 *
1211
 * @since 2.1.0
1212
 *
1213
 * @global WP_Query $wp_query Used to tell if the use a comment feed.
1214
 */
1215
function do_feed() {
1216
	global $wp_query;
1217
1218
	$feed = get_query_var( 'feed' );
1219
1220
	// Remove the pad, if present.
1221
	$feed = preg_replace( '/^_+/', '', $feed );
1222
1223
	if ( $feed == '' || $feed == 'feed' )
1224
		$feed = get_default_feed();
1225
1226
	if ( ! has_action( "do_feed_{$feed}" ) ) {
1227
		wp_die( __( 'ERROR: This is not a valid feed template.' ), '', array( 'response' => 404 ) );
1228
	}
1229
1230
	/**
1231
	 * Fires once the given feed is loaded.
1232
	 *
1233
	 * The dynamic portion of the hook name, `$feed`, refers to the feed template name.
1234
	 * Possible values include: 'rdf', 'rss', 'rss2', and 'atom'.
1235
	 *
1236
	 * @since 2.1.0
1237
	 * @since 4.4.0 The `$feed` parameter was added.
1238
	 *
1239
	 * @param bool   $is_comment_feed Whether the feed is a comment feed.
1240
	 * @param string $feed            The feed name.
1241
	 */
1242
	do_action( "do_feed_{$feed}", $wp_query->is_comment_feed, $feed );
1243
}
1244
1245
/**
1246
 * Load the RDF RSS 0.91 Feed template.
1247
 *
1248
 * @since 2.1.0
1249
 *
1250
 * @see load_template()
1251
 */
1252
function do_feed_rdf() {
1253
	load_template( ABSPATH . WPINC . '/feed-rdf.php' );
1254
}
1255
1256
/**
1257
 * Load the RSS 1.0 Feed Template.
1258
 *
1259
 * @since 2.1.0
1260
 *
1261
 * @see load_template()
1262
 */
1263
function do_feed_rss() {
1264
	load_template( ABSPATH . WPINC . '/feed-rss.php' );
1265
}
1266
1267
/**
1268
 * Load either the RSS2 comment feed or the RSS2 posts feed.
1269
 *
1270
 * @since 2.1.0
1271
 *
1272
 * @see load_template()
1273
 *
1274
 * @param bool $for_comments True for the comment feed, false for normal feed.
1275
 */
1276
function do_feed_rss2( $for_comments ) {
1277 View Code Duplication
	if ( $for_comments )
1278
		load_template( ABSPATH . WPINC . '/feed-rss2-comments.php' );
1279
	else
1280
		load_template( ABSPATH . WPINC . '/feed-rss2.php' );
1281
}
1282
1283
/**
1284
 * Load either Atom comment feed or Atom posts feed.
1285
 *
1286
 * @since 2.1.0
1287
 *
1288
 * @see load_template()
1289
 *
1290
 * @param bool $for_comments True for the comment feed, false for normal feed.
1291
 */
1292
function do_feed_atom( $for_comments ) {
1293 View Code Duplication
	if ($for_comments)
1294
		load_template( ABSPATH . WPINC . '/feed-atom-comments.php');
1295
	else
1296
		load_template( ABSPATH . WPINC . '/feed-atom.php' );
1297
}
1298
1299
/**
1300
 * Display the robots.txt file content.
1301
 *
1302
 * The echo content should be with usage of the permalinks or for creating the
1303
 * robots.txt file.
1304
 *
1305
 * @since 2.1.0
1306
 */
1307
function do_robots() {
1308
	header( 'Content-Type: text/plain; charset=utf-8' );
1309
1310
	/**
1311
	 * Fires when displaying the robots.txt file.
1312
	 *
1313
	 * @since 2.1.0
1314
	 */
1315
	do_action( 'do_robotstxt' );
1316
1317
	$output = "User-agent: *\n";
1318
	$public = get_option( 'blog_public' );
1319
	if ( '0' == $public ) {
1320
		$output .= "Disallow: /\n";
1321
	} else {
1322
		$site_url = parse_url( site_url() );
1323
		$path = ( !empty( $site_url['path'] ) ) ? $site_url['path'] : '';
1324
		$output .= "Disallow: $path/wp-admin/\n";
1325
		$output .= "Allow: $path/wp-admin/admin-ajax.php\n";
1326
	}
1327
1328
	/**
1329
	 * Filters the robots.txt output.
1330
	 *
1331
	 * @since 3.0.0
1332
	 *
1333
	 * @param string $output Robots.txt output.
1334
	 * @param bool   $public Whether the site is considered "public".
1335
	 */
1336
	echo apply_filters( 'robots_txt', $output, $public );
1337
}
1338
1339
/**
1340
 * Test whether WordPress is already installed.
1341
 *
1342
 * The cache will be checked first. If you have a cache plugin, which saves
1343
 * the cache values, then this will work. If you use the default WordPress
1344
 * cache, and the database goes away, then you might have problems.
1345
 *
1346
 * Checks for the 'siteurl' option for whether WordPress is installed.
1347
 *
1348
 * @since 2.1.0
1349
 *
1350
 * @global wpdb $wpdb WordPress database abstraction object.
1351
 *
1352
 * @return bool Whether the site is already installed.
1353
 */
1354
function is_blog_installed() {
1355
	global $wpdb;
1356
1357
	/*
1358
	 * Check cache first. If options table goes away and we have true
1359
	 * cached, oh well.
1360
	 */
1361
	if ( wp_cache_get( 'is_blog_installed' ) )
1362
		return true;
1363
1364
	$suppress = $wpdb->suppress_errors();
1365
	if ( ! wp_installing() ) {
1366
		$alloptions = wp_load_alloptions();
1367
	}
1368
	// If siteurl is not set to autoload, check it specifically
1369
	if ( !isset( $alloptions['siteurl'] ) )
1370
		$installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" );
1371
	else
1372
		$installed = $alloptions['siteurl'];
1373
	$wpdb->suppress_errors( $suppress );
1374
1375
	$installed = !empty( $installed );
1376
	wp_cache_set( 'is_blog_installed', $installed );
1377
1378
	if ( $installed )
1379
		return true;
1380
1381
	// If visiting repair.php, return true and let it take over.
1382
	if ( defined( 'WP_REPAIRING' ) )
1383
		return true;
1384
1385
	$suppress = $wpdb->suppress_errors();
1386
1387
	/*
1388
	 * Loop over the WP tables. If none exist, then scratch install is allowed.
1389
	 * If one or more exist, suggest table repair since we got here because the
1390
	 * options table could not be accessed.
1391
	 */
1392
	$wp_tables = $wpdb->tables();
1393
	foreach ( $wp_tables as $table ) {
1394
		// The existence of custom user tables shouldn't suggest an insane state or prevent a clean install.
1395
		if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE == $table )
1396
			continue;
1397
		if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE == $table )
1398
			continue;
1399
1400
		if ( ! $wpdb->get_results( "DESCRIBE $table;" ) )
1401
			continue;
1402
1403
		// One or more tables exist. We are insane.
1404
1405
		wp_load_translations_early();
1406
1407
		// Die with a DB error.
1408
		$wpdb->error = sprintf(
1409
			/* translators: %s: database repair URL */
1410
			__( 'One or more database tables are unavailable. The database may need to be <a href="%s">repaired</a>.' ),
1411
			'maint/repair.php?referrer=is_blog_installed'
1412
		);
1413
1414
		dead_db();
1415
	}
1416
1417
	$wpdb->suppress_errors( $suppress );
1418
1419
	wp_cache_set( 'is_blog_installed', false );
1420
1421
	return false;
1422
}
1423
1424
/**
1425
 * Retrieve URL with nonce added to URL query.
1426
 *
1427
 * @since 2.0.4
1428
 *
1429
 * @param string     $actionurl URL to add nonce action.
0 ignored issues
show
Consider making the type for parameter $action a bit more specific; maybe use integer.
Loading history...
1430
 * @param int|string $action    Optional. Nonce action name. Default -1.
1431
 * @param string     $name      Optional. Nonce name. Default '_wpnonce'.
1432
 * @return string Escaped URL with nonce action added.
1433
 */
1434
function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) {
1435
	$actionurl = str_replace( '&amp;', '&', $actionurl );
1436
	return esc_html( add_query_arg( $name, wp_create_nonce( $action ), $actionurl ) );
1437
}
1438
1439
/**
1440
 * Retrieve or display nonce hidden field for forms.
1441
 *
1442
 * The nonce field is used to validate that the contents of the form came from
1443
 * the location on the current site and not somewhere else. The nonce does not
1444
 * offer absolute protection, but should protect against most cases. It is very
1445
 * important to use nonce field in forms.
1446
 *
1447
 * The $action and $name are optional, but if you want to have better security,
1448
 * it is strongly suggested to set those two parameters. It is easier to just
1449
 * call the function without any parameters, because validation of the nonce
1450
 * doesn't require any parameters, but since crackers know what the default is
1451
 * it won't be difficult for them to find a way around your nonce and cause
1452
 * damage.
1453
 *
1454
 * The input name will be whatever $name value you gave. The input value will be
1455
 * the nonce creation value.
1456
 *
1457
 * @since 2.0.4
1458
 *
1459
 * @param int|string $action  Optional. Action name. Default -1.
0 ignored issues
show
Consider making the type for parameter $action a bit more specific; maybe use integer.
Loading history...
1460
 * @param string     $name    Optional. Nonce name. Default '_wpnonce'.
1461
 * @param bool       $referer Optional. Whether to set the referer field for validation. Default true.
1462
 * @param bool       $echo    Optional. Whether to display or return hidden form field. Default true.
1463
 * @return string Nonce field HTML markup.
1464
 */
1465
function wp_nonce_field( $action = -1, $name = "_wpnonce", $referer = true , $echo = true ) {
1466
	$name = esc_attr( $name );
1467
	$nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />';
1468
1469
	if ( $referer )
1470
		$nonce_field .= wp_referer_field( false );
1471
1472
	if ( $echo )
1473
		echo $nonce_field;
1474
1475
	return $nonce_field;
1476
}
1477
1478
/**
1479
 * Retrieve or display referer hidden field for forms.
1480
 *
1481
 * The referer link is the current Request URI from the server super global. The
1482
 * input name is '_wp_http_referer', in case you wanted to check manually.
1483
 *
1484
 * @since 2.0.4
1485
 *
1486
 * @param bool $echo Optional. Whether to echo or return the referer field. Default true.
1487
 * @return string Referer field HTML markup.
1488
 */
1489
function wp_referer_field( $echo = true ) {
1490
	$referer_field = '<input type="hidden" name="_wp_http_referer" value="'. esc_attr( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . '" />';
0 ignored issues
show
It seems like wp_unslash($_SERVER['REQUEST_URI']) targeting wp_unslash() can also be of type array; however, esc_attr() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
1491
1492
	if ( $echo )
1493
		echo $referer_field;
1494
	return $referer_field;
1495
}
1496
1497
/**
1498
 * Retrieve or display original referer hidden field for forms.
1499
 *
1500
 * The input name is '_wp_original_http_referer' and will be either the same
1501
 * value of wp_referer_field(), if that was posted already or it will be the
1502
 * current page, if it doesn't exist.
1503
 *
1504
 * @since 2.0.4
1505
 *
1506
 * @param bool   $echo         Optional. Whether to echo the original http referer. Default true.
1507
 * @param string $jump_back_to Optional. Can be 'previous' or page you want to jump back to.
1508
 *                             Default 'current'.
1509
 * @return string Original referer field.
1510
 */
1511
function wp_original_referer_field( $echo = true, $jump_back_to = 'current' ) {
1512
	if ( ! $ref = wp_get_original_referer() ) {
1513
		$ref = 'previous' == $jump_back_to ? wp_get_referer() : wp_unslash( $_SERVER['REQUEST_URI'] );
1514
	}
1515
	$orig_referer_field = '<input type="hidden" name="_wp_original_http_referer" value="' . esc_attr( $ref ) . '" />';
0 ignored issues
show
It seems like $ref can also be of type array; however, esc_attr() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1516
	if ( $echo )
1517
		echo $orig_referer_field;
1518
	return $orig_referer_field;
1519
}
1520
1521
/**
1522
 * Retrieve referer from '_wp_http_referer' or HTTP referer.
1523
 *
1524
 * If it's the same as the current request URL, will return false.
1525
 *
1526
 * @since 2.0.4
1527
 *
1528
 * @return false|string False on failure. Referer URL on success.
1529
 */
1530
function wp_get_referer() {
1531
	if ( ! function_exists( 'wp_validate_redirect' ) ) {
1532
		return false;
1533
	}
1534
1535
	$ref = wp_get_raw_referer();
1536
1537
	if ( $ref && $ref !== wp_unslash( $_SERVER['REQUEST_URI'] ) && $ref !== home_url() . wp_unslash( $_SERVER['REQUEST_URI'] ) ) {
1538
		return wp_validate_redirect( $ref, false );
1539
	}
1540
1541
	return false;
1542
}
1543
1544
/**
1545
 * Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer.
1546
 *
1547
 * Do not use for redirects, use wp_get_referer() instead.
1548
 *
1549
 * @since 4.5.0
1550
 *
1551
 * @return string|false Referer URL on success, false on failure.
0 ignored issues
show
Should the return type not be string|array|false? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
1552
 */
1553
function wp_get_raw_referer() {
1554
	if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
1555
		return wp_unslash( $_REQUEST['_wp_http_referer'] );
1556
	} else if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
1557
		return wp_unslash( $_SERVER['HTTP_REFERER'] );
1558
	}
1559
1560
	return false;
1561
}
1562
1563
/**
1564
 * Retrieve original referer that was posted, if it exists.
1565
 *
1566
 * @since 2.0.4
1567
 *
1568
 * @return string|false False if no original referer or original referer if set.
1569
 */
1570
function wp_get_original_referer() {
1571
	if ( ! empty( $_REQUEST['_wp_original_http_referer'] ) && function_exists( 'wp_validate_redirect' ) )
1572
		return wp_validate_redirect( wp_unslash( $_REQUEST['_wp_original_http_referer'] ), false );
0 ignored issues
show
It seems like wp_unslash($_REQUEST['_w...riginal_http_referer']) targeting wp_unslash() can also be of type array; however, wp_validate_redirect() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
1573
	return false;
1574
}
1575
1576
/**
1577
 * Recursive directory creation based on full path.
1578
 *
1579
 * Will attempt to set permissions on folders.
1580
 *
1581
 * @since 2.0.1
1582
 *
1583
 * @param string $target Full path to attempt to create.
1584
 * @return bool Whether the path was created. True if path already exists.
1585
 */
1586
function wp_mkdir_p( $target ) {
1587
	$wrapper = null;
1588
1589
	// Strip the protocol.
1590
	if ( wp_is_stream( $target ) ) {
1591
		list( $wrapper, $target ) = explode( '://', $target, 2 );
1592
	}
1593
1594
	// From php.net/mkdir user contributed notes.
1595
	$target = str_replace( '//', '/', $target );
1596
1597
	// Put the wrapper back on the target.
1598
	if ( $wrapper !== null ) {
1599
		$target = $wrapper . '://' . $target;
1600
	}
1601
1602
	/*
1603
	 * Safe mode fails with a trailing slash under certain PHP versions.
1604
	 * Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.
1605
	 */
1606
	$target = rtrim($target, '/');
1607
	if ( empty($target) )
1608
		$target = '/';
1609
1610
	if ( file_exists( $target ) )
1611
		return @is_dir( $target );
1612
1613
	// We need to find the permissions of the parent folder that exists and inherit that.
1614
	$target_parent = dirname( $target );
1615
	while ( '.' != $target_parent && ! is_dir( $target_parent ) ) {
1616
		$target_parent = dirname( $target_parent );
1617
	}
1618
1619
	// Get the permission bits.
1620
	if ( $stat = @stat( $target_parent ) ) {
1621
		$dir_perms = $stat['mode'] & 0007777;
1622
	} else {
1623
		$dir_perms = 0777;
1624
	}
1625
1626
	if ( @mkdir( $target, $dir_perms, true ) ) {
1627
1628
		/*
1629
		 * If a umask is set that modifies $dir_perms, we'll have to re-set
1630
		 * the $dir_perms correctly with chmod()
1631
		 */
1632
		if ( $dir_perms != ( $dir_perms & ~umask() ) ) {
1633
			$folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) );
1634
			for ( $i = 1, $c = count( $folder_parts ); $i <= $c; $i++ ) {
1635
				@chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1636
			}
1637
		}
1638
1639
		return true;
1640
	}
1641
1642
	return false;
1643
}
1644
1645
/**
1646
 * Test if a give filesystem path is absolute.
1647
 *
1648
 * For example, '/foo/bar', or 'c:\windows'.
1649
 *
1650
 * @since 2.5.0
1651
 *
1652
 * @param string $path File path.
1653
 * @return bool True if path is absolute, false is not absolute.
1654
 */
1655
function path_is_absolute( $path ) {
1656
	/*
1657
	 * This is definitive if true but fails if $path does not exist or contains
1658
	 * a symbolic link.
1659
	 */
1660
	if ( realpath($path) == $path )
1661
		return true;
1662
1663
	if ( strlen($path) == 0 || $path[0] == '.' )
1664
		return false;
1665
1666
	// Windows allows absolute paths like this.
1667
	if ( preg_match('#^[a-zA-Z]:\\\\#', $path) )
1668
		return true;
1669
1670
	// A path starting with / or \ is absolute; anything else is relative.
1671
	return ( $path[0] == '/' || $path[0] == '\\' );
1672
}
1673
1674
/**
1675
 * Join two filesystem paths together.
1676
 *
1677
 * For example, 'give me $path relative to $base'. If the $path is absolute,
1678
 * then it the full path is returned.
1679
 *
1680
 * @since 2.5.0
1681
 *
1682
 * @param string $base Base path.
1683
 * @param string $path Path relative to $base.
1684
 * @return string The path with the base or absolute path.
1685
 */
1686
function path_join( $base, $path ) {
1687
	if ( path_is_absolute($path) )
1688
		return $path;
1689
1690
	return rtrim($base, '/') . '/' . ltrim($path, '/');
1691
}
1692
1693
/**
1694
 * Normalize a filesystem path.
1695
 *
1696
 * On windows systems, replaces backslashes with forward slashes
1697
 * and forces upper-case drive letters.
1698
 * Allows for two leading slashes for Windows network shares, but
1699
 * ensures that all other duplicate slashes are reduced to a single.
1700
 *
1701
 * @since 3.9.0
1702
 * @since 4.4.0 Ensures upper-case drive letters on Windows systems.
1703
 * @since 4.5.0 Allows for Windows network shares.
1704
 *
1705
 * @param string $path Path to normalize.
1706
 * @return string Normalized path.
1707
 */
1708
function wp_normalize_path( $path ) {
1709
	$path = str_replace( '\\', '/', $path );
1710
	$path = preg_replace( '|(?<=.)/+|', '/', $path );
1711
	if ( ':' === substr( $path, 1, 1 ) ) {
1712
		$path = ucfirst( $path );
1713
	}
1714
	return $path;
1715
}
1716
1717
/**
1718
 * Determine a writable directory for temporary files.
1719
 *
1720
 * Function's preference is the return value of sys_get_temp_dir(),
1721
 * followed by your PHP temporary upload directory, followed by WP_CONTENT_DIR,
1722
 * before finally defaulting to /tmp/
1723
 *
1724
 * In the event that this function does not find a writable location,
1725
 * It may be overridden by the WP_TEMP_DIR constant in your wp-config.php file.
1726
 *
1727
 * @since 2.5.0
1728
 *
1729
 * @staticvar string $temp
1730
 *
1731
 * @return string Writable temporary directory.
1732
 */
1733
function get_temp_dir() {
1734
	static $temp = '';
1735
	if ( defined('WP_TEMP_DIR') )
1736
		return trailingslashit(WP_TEMP_DIR);
1737
1738
	if ( $temp )
1739
		return trailingslashit( $temp );
1740
1741
	if ( function_exists('sys_get_temp_dir') ) {
1742
		$temp = sys_get_temp_dir();
1743
		if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
1744
			return trailingslashit( $temp );
1745
	}
1746
1747
	$temp = ini_get('upload_tmp_dir');
1748
	if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
1749
		return trailingslashit( $temp );
1750
1751
	$temp = WP_CONTENT_DIR . '/';
1752
	if ( is_dir( $temp ) && wp_is_writable( $temp ) )
1753
		return $temp;
1754
1755
	return '/tmp/';
1756
}
1757
1758
/**
1759
 * Determine if a directory is writable.
1760
 *
1761
 * This function is used to work around certain ACL issues in PHP primarily
1762
 * affecting Windows Servers.
1763
 *
1764
 * @since 3.6.0
1765
 *
1766
 * @see win_is_writable()
1767
 *
1768
 * @param string $path Path to check for write-ability.
1769
 * @return bool Whether the path is writable.
1770
 */
1771
function wp_is_writable( $path ) {
1772
	if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) )
1773
		return win_is_writable( $path );
1774
	else
1775
		return @is_writable( $path );
1776
}
1777
1778
/**
1779
 * Workaround for Windows bug in is_writable() function
1780
 *
1781
 * PHP has issues with Windows ACL's for determine if a
1782
 * directory is writable or not, this works around them by
1783
 * checking the ability to open files rather than relying
1784
 * upon PHP to interprate the OS ACL.
1785
 *
1786
 * @since 2.8.0
1787
 *
1788
 * @see https://bugs.php.net/bug.php?id=27609
1789
 * @see https://bugs.php.net/bug.php?id=30931
1790
 *
1791
 * @param string $path Windows path to check for write-ability.
1792
 * @return bool Whether the path is writable.
1793
 */
1794
function win_is_writable( $path ) {
1795
1796
	if ( $path[strlen( $path ) - 1] == '/' ) { // if it looks like a directory, check a random file within the directory
1797
		return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp');
1798
	} elseif ( is_dir( $path ) ) { // If it's a directory (and not a file) check a random file within the directory
1799
		return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' );
1800
	}
1801
	// check tmp file for read/write capabilities
1802
	$should_delete_tmp_file = !file_exists( $path );
1803
	$f = @fopen( $path, 'a' );
1804
	if ( $f === false )
1805
		return false;
1806
	fclose( $f );
1807
	if ( $should_delete_tmp_file )
1808
		unlink( $path );
1809
	return true;
1810
}
1811
1812
/**
1813
 * Retrieves uploads directory information.
1814
 *
1815
 * Same as wp_upload_dir() but "light weight" as it doesn't attempt to create the uploads directory.
1816
 * Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases
1817
 * when not uploading files.
1818
 *
1819
 * @since 4.5.0
1820
 *
1821
 * @see wp_upload_dir()
1822
 *
1823
 * @return array See wp_upload_dir() for description.
1824
 */
1825
function wp_get_upload_dir() {
1826
	return wp_upload_dir( null, false );
1827
}
1828
1829
/**
1830
 * Get an array containing the current upload directory's path and url.
1831
 *
1832
 * Checks the 'upload_path' option, which should be from the web root folder,
1833
 * and if it isn't empty it will be used. If it is empty, then the path will be
1834
 * 'WP_CONTENT_DIR/uploads'. If the 'UPLOADS' constant is defined, then it will
1835
 * override the 'upload_path' option and 'WP_CONTENT_DIR/uploads' path.
1836
 *
1837
 * The upload URL path is set either by the 'upload_url_path' option or by using
1838
 * the 'WP_CONTENT_URL' constant and appending '/uploads' to the path.
1839
 *
1840
 * If the 'uploads_use_yearmonth_folders' is set to true (checkbox if checked in
1841
 * the administration settings panel), then the time will be used. The format
1842
 * will be year first and then month.
1843
 *
1844
 * If the path couldn't be created, then an error will be returned with the key
1845
 * 'error' containing the error message. The error suggests that the parent
1846
 * directory is not writable by the server.
1847
 *
1848
 * On success, the returned array will have many indices:
1849
 * 'path' - base directory and sub directory or full path to upload directory.
1850
 * 'url' - base url and sub directory or absolute URL to upload directory.
1851
 * 'subdir' - sub directory if uploads use year/month folders option is on.
1852
 * 'basedir' - path without subdir.
1853
 * 'baseurl' - URL path without subdir.
1854
 * 'error' - false or error message.
1855
 *
1856
 * @since 2.0.0
1857
 * @uses _wp_upload_dir()
1858
 *
1859
 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
1860
 * @param bool   $create_dir Optional. Whether to check and create the uploads directory.
1861
 *                           Default true for backward compatibility.
1862
 * @param bool   $refresh_cache Optional. Whether to refresh the cache. Default false.
1863
 * @return array See above for description.
1864
 */
1865
function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) {
1866
	static $cache = array(), $tested_paths = array();
1867
1868
	$key = sprintf( '%d-%s', get_current_blog_id(), (string) $time );
1869
1870
	if ( $refresh_cache || empty( $cache[ $key ] ) ) {
1871
		$cache[ $key ] = _wp_upload_dir( $time );
1872
	}
1873
1874
	/**
1875
	 * Filters the uploads directory data.
1876
	 *
1877
	 * @since 2.0.0
1878
	 *
1879
	 * @param array $uploads Array of upload directory data with keys of 'path',
1880
	 *                       'url', 'subdir, 'basedir', and 'error'.
1881
	 */
1882
	$uploads = apply_filters( 'upload_dir', $cache[ $key ] );
1883
1884
	if ( $create_dir ) {
1885
		$path = $uploads['path'];
1886
1887
		if ( array_key_exists( $path, $tested_paths ) ) {
1888
			$uploads['error'] = $tested_paths[ $path ];
1889
		} else {
1890 View Code Duplication
			if ( ! wp_mkdir_p( $path ) ) {
1891
				if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
1892
					$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
1893
				} else {
1894
					$error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
1895
				}
1896
1897
				$uploads['error'] = sprintf(
1898
					/* translators: %s: directory path */
1899
					__( 'Unable to create directory %s. Is its parent directory writable by the server?' ),
1900
					esc_html( $error_path )
1901
				);
1902
			}
1903
1904
			$tested_paths[ $path ] = $uploads['error'];
1905
		}
1906
	}
1907
1908
	return $uploads;
1909
}
1910
1911
/**
1912
 * A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path.
1913
 *
1914
 * @since 4.5.0
1915
 * @access private
1916
 *
1917
 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
1918
 * @return array See wp_upload_dir()
1919
 */
1920
function _wp_upload_dir( $time = null ) {
1921
	$siteurl = get_option( 'siteurl' );
1922
	$upload_path = trim( get_option( 'upload_path' ) );
1923
1924
	if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
1925
		$dir = WP_CONTENT_DIR . '/uploads';
1926
	} elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
1927
		// $dir is absolute, $upload_path is (maybe) relative to ABSPATH
1928
		$dir = path_join( ABSPATH, $upload_path );
1929
	} else {
1930
		$dir = $upload_path;
1931
	}
1932
1933
	if ( !$url = get_option( 'upload_url_path' ) ) {
1934
		if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )
1935
			$url = WP_CONTENT_URL . '/uploads';
1936
		else
1937
			$url = trailingslashit( $siteurl ) . $upload_path;
1938
	}
1939
1940
	/*
1941
	 * Honor the value of UPLOADS. This happens as long as ms-files rewriting is disabled.
1942
	 * We also sometimes obey UPLOADS when rewriting is enabled -- see the next block.
1943
	 */
1944 View Code Duplication
	if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) {
1945
		$dir = ABSPATH . UPLOADS;
1946
		$url = trailingslashit( $siteurl ) . UPLOADS;
1947
	}
1948
1949
	// If multisite (and if not the main site in a post-MU network)
1950
	if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
1951
1952
		if ( ! get_site_option( 'ms_files_rewriting' ) ) {
1953
			/*
1954
			 * If ms-files rewriting is disabled (networks created post-3.5), it is fairly
1955
			 * straightforward: Append sites/%d if we're not on the main site (for post-MU
1956
			 * networks). (The extra directory prevents a four-digit ID from conflicting with
1957
			 * a year-based directory for the main site. But if a MU-era network has disabled
1958
			 * ms-files rewriting manually, they don't need the extra directory, as they never
1959
			 * had wp-content/uploads for the main site.)
1960
			 */
1961
1962
			if ( defined( 'MULTISITE' ) )
1963
				$ms_dir = '/sites/' . get_current_blog_id();
1964
			else
1965
				$ms_dir = '/' . get_current_blog_id();
1966
1967
			$dir .= $ms_dir;
1968
			$url .= $ms_dir;
1969
1970 View Code Duplication
		} elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) {
1971
			/*
1972
			 * Handle the old-form ms-files.php rewriting if the network still has that enabled.
1973
			 * When ms-files rewriting is enabled, then we only listen to UPLOADS when:
1974
			 * 1) We are not on the main site in a post-MU network, as wp-content/uploads is used
1975
			 *    there, and
1976
			 * 2) We are not switched, as ms_upload_constants() hardcodes these constants to reflect
1977
			 *    the original blog ID.
1978
			 *
1979
			 * Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute.
1980
			 * (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as
1981
			 * as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files
1982
			 * rewriting in multisite, the resulting URL is /files. (#WP22702 for background.)
1983
			 */
1984
1985
			if ( defined( 'BLOGUPLOADDIR' ) )
1986
				$dir = untrailingslashit( BLOGUPLOADDIR );
1987
			else
1988
				$dir = ABSPATH . UPLOADS;
1989
			$url = trailingslashit( $siteurl ) . 'files';
1990
		}
1991
	}
1992
1993
	$basedir = $dir;
1994
	$baseurl = $url;
1995
1996
	$subdir = '';
1997
	if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
1998
		// Generate the yearly and monthly dirs
1999
		if ( !$time )
0 ignored issues
show
Bug Best Practice introduced by
The expression $time of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2000
			$time = current_time( 'mysql' );
2001
		$y = substr( $time, 0, 4 );
2002
		$m = substr( $time, 5, 2 );
2003
		$subdir = "/$y/$m";
2004
	}
2005
2006
	$dir .= $subdir;
2007
	$url .= $subdir;
2008
2009
	return array(
2010
		'path'    => $dir,
2011
		'url'     => $url,
2012
		'subdir'  => $subdir,
2013
		'basedir' => $basedir,
2014
		'baseurl' => $baseurl,
2015
		'error'   => false,
2016
	);
2017
}
2018
2019
/**
2020
 * Get a filename that is sanitized and unique for the given directory.
2021
 *
2022
 * If the filename is not unique, then a number will be added to the filename
2023
 * before the extension, and will continue adding numbers until the filename is
2024
 * unique.
2025
 *
2026
 * The callback is passed three parameters, the first one is the directory, the
2027
 * second is the filename, and the third is the extension.
2028
 *
2029
 * @since 2.5.0
2030
 *
2031
 * @param string   $dir                      Directory.
2032
 * @param string   $filename                 File name.
2033
 * @param callable $unique_filename_callback Callback. Default null.
0 ignored issues
show
Should the type for parameter $unique_filename_callback not be callable|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
2034
 * @return string New filename, if given wasn't unique.
2035
 */
2036
function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) {
2037
	// Sanitize the file name before we begin processing.
2038
	$filename = sanitize_file_name($filename);
2039
2040
	// Separate the filename into a name and extension.
2041
	$ext = pathinfo( $filename, PATHINFO_EXTENSION );
2042
	$name = pathinfo( $filename, PATHINFO_BASENAME );
2043
	if ( $ext ) {
2044
		$ext = '.' . $ext;
2045
	}
2046
2047
	// Edge case: if file is named '.ext', treat as an empty name.
2048
	if ( $name === $ext ) {
2049
		$name = '';
2050
	}
2051
2052
	/*
2053
	 * Increment the file number until we have a unique file to save in $dir.
2054
	 * Use callback if supplied.
2055
	 */
2056
	if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) {
2057
		$filename = call_user_func( $unique_filename_callback, $dir, $name, $ext );
2058
	} else {
2059
		$number = '';
2060
2061
		// Change '.ext' to lower case.
2062
		if ( $ext && strtolower($ext) != $ext ) {
2063
			$ext2 = strtolower($ext);
2064
			$filename2 = preg_replace( '|' . preg_quote($ext) . '$|', $ext2, $filename );
2065
2066
			// Check for both lower and upper case extension or image sub-sizes may be overwritten.
2067
			while ( file_exists($dir . "/$filename") || file_exists($dir . "/$filename2") ) {
2068
				$new_number = (int) $number + 1;
2069
				$filename = str_replace( array( "-$number$ext", "$number$ext" ), "-$new_number$ext", $filename );
2070
				$filename2 = str_replace( array( "-$number$ext2", "$number$ext2" ), "-$new_number$ext2", $filename2 );
2071
				$number = $new_number;
2072
			}
2073
2074
			/**
2075
			 * Filters the result when generating a unique file name.
2076
			 *
2077
			 * @since 4.5.0
2078
			 *
2079
			 * @param string        $filename                 Unique file name.
2080
			 * @param string        $ext                      File extension, eg. ".png".
2081
			 * @param string        $dir                      Directory path.
2082
			 * @param callable|null $unique_filename_callback Callback function that generates the unique file name.
2083
			 */
2084
			return apply_filters( 'wp_unique_filename', $filename2, $ext, $dir, $unique_filename_callback );
2085
		}
2086
2087
		while ( file_exists( $dir . "/$filename" ) ) {
2088
			$new_number = (int) $number + 1;
2089
			if ( '' == "$number$ext" ) {
2090
				$filename = "$filename-" . $new_number;
2091
			} else {
2092
				$filename = str_replace( array( "-$number$ext", "$number$ext" ), "-" . $new_number . $ext, $filename );
2093
			}
2094
			$number = $new_number;
2095
		}
2096
	}
2097
2098
	/** This filter is documented in wp-includes/functions.php */
2099
	return apply_filters( 'wp_unique_filename', $filename, $ext, $dir, $unique_filename_callback );
2100
}
2101
2102
/**
2103
 * Create a file in the upload folder with given content.
2104
 *
2105
 * If there is an error, then the key 'error' will exist with the error message.
2106
 * If success, then the key 'file' will have the unique file path, the 'url' key
2107
 * will have the link to the new file. and the 'error' key will be set to false.
2108
 *
2109
 * This function will not move an uploaded file to the upload folder. It will
2110
 * create a new file with the content in $bits parameter. If you move the upload
2111
 * file, read the content of the uploaded file, and then you can give the
2112
 * filename and content to this function, which will add it to the upload
2113
 * folder.
2114
 *
2115
 * The permissions will be set on the new file automatically by this function.
2116
 *
2117
 * @since 2.0.0
2118
 *
2119
 * @param string       $name       Filename.
2120
 * @param null|string  $deprecated Never used. Set to null.
2121
 * @param mixed        $bits       File content
2122
 * @param string       $time       Optional. Time formatted in 'yyyy/mm'. Default null.
2123
 * @return array
2124
 */
2125
function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {
2126
	if ( !empty( $deprecated ) )
2127
		_deprecated_argument( __FUNCTION__, '2.0.0' );
2128
2129
	if ( empty( $name ) )
2130
		return array( 'error' => __( 'Empty filename' ) );
2131
2132
	$wp_filetype = wp_check_filetype( $name );
2133
	if ( ! $wp_filetype['ext'] && ! current_user_can( 'unfiltered_upload' ) )
2134
		return array( 'error' => __( 'Sorry, this file type is not permitted for security reasons.' ) );
2135
2136
	$upload = wp_upload_dir( $time );
2137
2138
	if ( $upload['error'] !== false )
2139
		return $upload;
2140
2141
	/**
2142
	 * Filters whether to treat the upload bits as an error.
2143
	 *
2144
	 * Passing a non-array to the filter will effectively short-circuit preparing
2145
	 * the upload bits, returning that value instead.
2146
	 *
2147
	 * @since 3.0.0
2148
	 *
2149
	 * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
2150
	 */
2151
	$upload_bits_error = apply_filters( 'wp_upload_bits', array( 'name' => $name, 'bits' => $bits, 'time' => $time ) );
2152
	if ( !is_array( $upload_bits_error ) ) {
2153
		$upload[ 'error' ] = $upload_bits_error;
2154
		return $upload;
2155
	}
2156
2157
	$filename = wp_unique_filename( $upload['path'], $name );
2158
2159
	$new_file = $upload['path'] . "/$filename";
2160 View Code Duplication
	if ( ! wp_mkdir_p( dirname( $new_file ) ) ) {
2161
		if ( 0 === strpos( $upload['basedir'], ABSPATH ) )
2162
			$error_path = str_replace( ABSPATH, '', $upload['basedir'] ) . $upload['subdir'];
2163
		else
2164
			$error_path = basename( $upload['basedir'] ) . $upload['subdir'];
2165
2166
		$message = sprintf(
2167
			/* translators: %s: directory path */
2168
			__( 'Unable to create directory %s. Is its parent directory writable by the server?' ),
2169
			$error_path
2170
		);
2171
		return array( 'error' => $message );
2172
	}
2173
2174
	$ifp = @ fopen( $new_file, 'wb' );
2175
	if ( ! $ifp )
2176
		return array( 'error' => sprintf( __( 'Could not write file %s' ), $new_file ) );
2177
2178
	@fwrite( $ifp, $bits );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
2179
	fclose( $ifp );
2180
	clearstatcache();
2181
2182
	// Set correct file permissions
2183
	$stat = @ stat( dirname( $new_file ) );
2184
	$perms = $stat['mode'] & 0007777;
2185
	$perms = $perms & 0000666;
2186
	@ chmod( $new_file, $perms );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
2187
	clearstatcache();
2188
2189
	// Compute the URL
2190
	$url = $upload['url'] . "/$filename";
2191
2192
	/** This filter is documented in wp-admin/includes/file.php */
2193
	return apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false ), 'sideload' );
2194
}
2195
2196
/**
2197
 * Retrieve the file type based on the extension name.
2198
 *
2199
 * @since 2.5.0
2200
 *
2201
 * @param string $ext The extension to search.
2202
 * @return string|void The file type, example: audio, video, document, spreadsheet, etc.
0 ignored issues
show
Should the return type not be integer|string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
2203
 */
2204
function wp_ext2type( $ext ) {
2205
	$ext = strtolower( $ext );
2206
2207
	$ext2type = wp_get_ext_types();
2208
	foreach ( $ext2type as $type => $exts )
2209
		if ( in_array( $ext, $exts ) )
2210
			return $type;
2211
}
2212
2213
/**
2214
 * Retrieve the file type from the file name.
2215
 *
2216
 * You can optionally define the mime array, if needed.
2217
 *
2218
 * @since 2.0.4
2219
 *
2220
 * @param string $filename File name or path.
2221
 * @param array  $mimes    Optional. Key is the file extension with value as the mime type.
0 ignored issues
show
Should the type for parameter $mimes not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
2222
 * @return array Values with extension first and mime type.
2223
 */
2224
function wp_check_filetype( $filename, $mimes = null ) {
2225
	if ( empty($mimes) )
2226
		$mimes = get_allowed_mime_types();
2227
	$type = false;
2228
	$ext = false;
2229
2230
	foreach ( $mimes as $ext_preg => $mime_match ) {
2231
		$ext_preg = '!\.(' . $ext_preg . ')$!i';
2232
		if ( preg_match( $ext_preg, $filename, $ext_matches ) ) {
2233
			$type = $mime_match;
2234
			$ext = $ext_matches[1];
2235
			break;
2236
		}
2237
	}
2238
2239
	return compact( 'ext', 'type' );
2240
}
2241
2242
/**
2243
 * Attempt to determine the real file type of a file.
2244
 *
2245
 * If unable to, the file name extension will be used to determine type.
2246
 *
2247
 * If it's determined that the extension does not match the file's real type,
2248
 * then the "proper_filename" value will be set with a proper filename and extension.
2249
 *
2250
 * Currently this function only supports renaming images validated via wp_get_image_mime().
2251
 *
2252
 * @since 3.0.0
2253
 *
2254
 * @param string $file     Full path to the file.
2255
 * @param string $filename The name of the file (may differ from $file due to $file being
2256
 *                         in a tmp directory).
2257
 * @param array   $mimes   Optional. Key is the file extension with value as the mime type.
0 ignored issues
show
Should the type for parameter $mimes not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
2258
 * @return array Values for the extension, MIME, and either a corrected filename or false
2259
 *               if original $filename is valid.
2260
 */
2261
function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
2262
	$proper_filename = false;
2263
2264
	// Do basic extension validation and MIME mapping
2265
	$wp_filetype = wp_check_filetype( $filename, $mimes );
2266
	$ext = $wp_filetype['ext'];
2267
	$type = $wp_filetype['type'];
2268
2269
	// We can't do any further validation without a file to work with
2270
	if ( ! file_exists( $file ) ) {
2271
		return compact( 'ext', 'type', 'proper_filename' );
2272
	}
2273
2274
	$real_mime = false;
2275
2276
	// Validate image types.
2277
	if ( $type && 0 === strpos( $type, 'image/' ) ) {
2278
2279
		// Attempt to figure out what type of image it actually is
2280
		$real_mime = wp_get_image_mime( $file );
2281
2282
		if ( $real_mime && $real_mime != $type ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $real_mime of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2283
			/**
2284
			 * Filters the list mapping image mime types to their respective extensions.
2285
			 *
2286
			 * @since 3.0.0
2287
			 *
2288
			 * @param  array $mime_to_ext Array of image mime types and their matching extensions.
2289
			 */
2290
			$mime_to_ext = apply_filters( 'getimagesize_mimes_to_exts', array(
2291
				'image/jpeg' => 'jpg',
2292
				'image/png'  => 'png',
2293
				'image/gif'  => 'gif',
2294
				'image/bmp'  => 'bmp',
2295
				'image/tiff' => 'tif',
2296
			) );
2297
2298
			// Replace whatever is after the last period in the filename with the correct extension
2299
			if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
2300
				$filename_parts = explode( '.', $filename );
2301
				array_pop( $filename_parts );
2302
				$filename_parts[] = $mime_to_ext[ $real_mime ];
2303
				$new_filename = implode( '.', $filename_parts );
2304
2305
				if ( $new_filename != $filename ) {
2306
					$proper_filename = $new_filename; // Mark that it changed
2307
				}
2308
				// Redefine the extension / MIME
2309
				$wp_filetype = wp_check_filetype( $new_filename, $mimes );
2310
				$ext = $wp_filetype['ext'];
2311
				$type = $wp_filetype['type'];
2312
			} else {
2313
				// Reset $real_mime and try validating again.
2314
				$real_mime = false;
2315
			}
2316
		}
2317
	}
2318
2319
	// Validate files that didn't get validated during previous checks.
2320
	if ( $type && ! $real_mime && extension_loaded( 'fileinfo' ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $real_mime of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2321
		$finfo = finfo_open( FILEINFO_MIME_TYPE );
2322
		$real_mime = finfo_file( $finfo, $file );
2323
		finfo_close( $finfo );
2324
2325
		/*
2326
		 * If $real_mime doesn't match what we're expecting, we need to do some extra
2327
		 * vetting of application mime types to make sure this type of file is allowed.
2328
		 * Other mime types are assumed to be safe, but should be considered unverified.
2329
		 */
2330
		if ( $real_mime && ( $real_mime !== $type ) && ( 0 === strpos( $real_mime, 'application' ) ) ) {
2331
			$allowed = get_allowed_mime_types();
2332
2333
			if ( ! in_array( $real_mime, $allowed ) ) {
2334
				$type = $ext = false;
2335
			}
2336
		}
2337
	}
2338
2339
	/**
2340
	 * Filters the "real" file type of the given file.
2341
	 *
2342
	 * @since 3.0.0
2343
	 *
2344
	 * @param array  $wp_check_filetype_and_ext File data array containing 'ext', 'type', and
2345
	 *                                          'proper_filename' keys.
2346
	 * @param string $file                      Full path to the file.
2347
	 * @param string $filename                  The name of the file (may differ from $file due to
2348
	 *                                          $file being in a tmp directory).
2349
	 * @param array  $mimes                     Key is the file extension with value as the mime type.
2350
	 */
2351
	return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );
2352
}
2353
2354
/**
2355
 * Returns the real mime type of an image file.
2356
 *
2357
 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
2358
 *
2359
 * @since 4.7.1
2360
 *
2361
 * @param string $file Full path to the file.
2362
 * @return string|false The actual mime type or false if the type cannot be determined.
2363
 */
2364
function wp_get_image_mime( $file ) {
2365
	/*
2366
	 * Use exif_imagetype() to check the mimetype if available or fall back to
2367
	 * getimagesize() if exif isn't avaialbe. If either function throws an Exception
2368
	 * we assume the file could not be validated.
2369
	 */
2370
	try {
2371
		if ( is_callable( 'exif_imagetype' ) ) {
2372
			$imagetype = exif_imagetype( $file );
2373
			$mime = ( $imagetype ) ? image_type_to_mime_type( $imagetype ) : false;
2374
		} elseif ( function_exists( 'getimagesize' ) ) {
2375
			$imagesize = getimagesize( $file );
2376
			$mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false;
2377
		} else {
2378
			$mime = false;
2379
		}
2380
	} catch ( Exception $e ) {
2381
		$mime = false;
2382
	}
2383
2384
	return $mime;
2385
}
2386
2387
/**
2388
 * Retrieve list of mime types and file extensions.
2389
 *
2390
 * @since 3.5.0
2391
 * @since 4.2.0 Support was added for GIMP (xcf) files.
2392
 *
2393
 * @return array Array of mime types keyed by the file extension regex corresponding to those types.
2394
 */
2395
function wp_get_mime_types() {
2396
	/**
2397
	 * Filters the list of mime types and file extensions.
2398
	 *
2399
	 * This filter should be used to add, not remove, mime types. To remove
2400
	 * mime types, use the {@see 'upload_mimes'} filter.
2401
	 *
2402
	 * @since 3.5.0
2403
	 *
2404
	 * @param array $wp_get_mime_types Mime types keyed by the file extension regex
2405
	 *                                 corresponding to those types.
2406
	 */
2407
	return apply_filters( 'mime_types', array(
2408
	// Image formats.
2409
	'jpg|jpeg|jpe' => 'image/jpeg',
2410
	'gif' => 'image/gif',
2411
	'png' => 'image/png',
2412
	'bmp' => 'image/bmp',
2413
	'tiff|tif' => 'image/tiff',
2414
	'ico' => 'image/x-icon',
2415
	// Video formats.
2416
	'asf|asx' => 'video/x-ms-asf',
2417
	'wmv' => 'video/x-ms-wmv',
2418
	'wmx' => 'video/x-ms-wmx',
2419
	'wm' => 'video/x-ms-wm',
2420
	'avi' => 'video/avi',
2421
	'divx' => 'video/divx',
2422
	'flv' => 'video/x-flv',
2423
	'mov|qt' => 'video/quicktime',
2424
	'mpeg|mpg|mpe' => 'video/mpeg',
2425
	'mp4|m4v' => 'video/mp4',
2426
	'ogv' => 'video/ogg',
2427
	'webm' => 'video/webm',
2428
	'mkv' => 'video/x-matroska',
2429
	'3gp|3gpp' => 'video/3gpp', // Can also be audio
2430
	'3g2|3gp2' => 'video/3gpp2', // Can also be audio
2431
	// Text formats.
2432
	'txt|asc|c|cc|h|srt' => 'text/plain',
2433
	'csv' => 'text/csv',
2434
	'tsv' => 'text/tab-separated-values',
2435
	'ics' => 'text/calendar',
2436
	'rtx' => 'text/richtext',
2437
	'css' => 'text/css',
2438
	'htm|html' => 'text/html',
2439
	'vtt' => 'text/vtt',
2440
	'dfxp' => 'application/ttaf+xml',
2441
	// Audio formats.
2442
	'mp3|m4a|m4b' => 'audio/mpeg',
2443
	'ra|ram' => 'audio/x-realaudio',
2444
	'wav' => 'audio/wav',
2445
	'ogg|oga' => 'audio/ogg',
2446
	'mid|midi' => 'audio/midi',
2447
	'wma' => 'audio/x-ms-wma',
2448
	'wax' => 'audio/x-ms-wax',
2449
	'mka' => 'audio/x-matroska',
2450
	// Misc application formats.
2451
	'rtf' => 'application/rtf',
2452
	'js' => 'application/javascript',
2453
	'pdf' => 'application/pdf',
2454
	'swf' => 'application/x-shockwave-flash',
2455
	'class' => 'application/java',
2456
	'tar' => 'application/x-tar',
2457
	'zip' => 'application/zip',
2458
	'gz|gzip' => 'application/x-gzip',
2459
	'rar' => 'application/rar',
2460
	'7z' => 'application/x-7z-compressed',
2461
	'exe' => 'application/x-msdownload',
2462
	'psd' => 'application/octet-stream',
2463
	'xcf' => 'application/octet-stream',
2464
	// MS Office formats.
2465
	'doc' => 'application/msword',
2466
	'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
2467
	'wri' => 'application/vnd.ms-write',
2468
	'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
2469
	'mdb' => 'application/vnd.ms-access',
2470
	'mpp' => 'application/vnd.ms-project',
2471
	'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
2472
	'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
2473
	'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
2474
	'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
2475
	'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
2476
	'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
2477
	'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
2478
	'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
2479
	'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
2480
	'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
2481
	'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
2482
	'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
2483
	'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
2484
	'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
2485
	'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
2486
	'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
2487
	'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
2488
	'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
2489
	'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
2490
	'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote',
2491
	'oxps' => 'application/oxps',
2492
	'xps' => 'application/vnd.ms-xpsdocument',
2493
	// OpenOffice formats.
2494
	'odt' => 'application/vnd.oasis.opendocument.text',
2495
	'odp' => 'application/vnd.oasis.opendocument.presentation',
2496
	'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
2497
	'odg' => 'application/vnd.oasis.opendocument.graphics',
2498
	'odc' => 'application/vnd.oasis.opendocument.chart',
2499
	'odb' => 'application/vnd.oasis.opendocument.database',
2500
	'odf' => 'application/vnd.oasis.opendocument.formula',
2501
	// WordPerfect formats.
2502
	'wp|wpd' => 'application/wordperfect',
2503
	// iWork formats.
2504
	'key' => 'application/vnd.apple.keynote',
2505
	'numbers' => 'application/vnd.apple.numbers',
2506
	'pages' => 'application/vnd.apple.pages',
2507
	) );
2508
}
2509
2510
/**
2511
 * Retrieves the list of common file extensions and their types.
2512
 *
2513
 * @since 4.6.0
2514
 *
2515
 * @return array Array of file extensions types keyed by the type of file.
2516
 */
2517
function wp_get_ext_types() {
2518
2519
	/**
2520
	 * Filters file type based on the extension name.
2521
	 *
2522
	 * @since 2.5.0
2523
	 *
2524
	 * @see wp_ext2type()
2525
	 *
2526
	 * @param array $ext2type Multi-dimensional array with extensions for a default set
2527
	 *                        of file types.
2528
	 */
2529
	return apply_filters( 'ext2type', array(
2530
		'image'       => array( 'jpg', 'jpeg', 'jpe',  'gif',  'png',  'bmp',   'tif',  'tiff', 'ico' ),
2531
		'audio'       => array( 'aac', 'ac3',  'aif',  'aiff', 'm3a',  'm4a',   'm4b',  'mka',  'mp1',  'mp2',  'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
2532
		'video'       => array( '3g2',  '3gp', '3gpp', 'asf', 'avi',  'divx', 'dv',   'flv',  'm4v',   'mkv',  'mov',  'mp4',  'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt',  'rm', 'vob', 'wmv' ),
2533
		'document'    => array( 'doc', 'docx', 'docm', 'dotm', 'odt',  'pages', 'pdf',  'xps',  'oxps', 'rtf',  'wp', 'wpd', 'psd', 'xcf' ),
2534
		'spreadsheet' => array( 'numbers',     'ods',  'xls',  'xlsx', 'xlsm',  'xlsb' ),
2535
		'interactive' => array( 'swf', 'key',  'ppt',  'pptx', 'pptm', 'pps',   'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ),
2536
		'text'        => array( 'asc', 'csv',  'tsv',  'txt' ),
2537
		'archive'     => array( 'bz2', 'cab',  'dmg',  'gz',   'rar',  'sea',   'sit',  'sqx',  'tar',  'tgz',  'zip', '7z' ),
2538
		'code'        => array( 'css', 'htm',  'html', 'php',  'js' ),
2539
	) );
2540
}
2541
2542
/**
2543
 * Retrieve list of allowed mime types and file extensions.
2544
 *
2545
 * @since 2.8.6
2546
 *
2547
 * @param int|WP_User $user Optional. User to check. Defaults to current user.
2548
 * @return array Array of mime types keyed by the file extension regex corresponding
2549
 *               to those types.
2550
 */
2551
function get_allowed_mime_types( $user = null ) {
2552
	$t = wp_get_mime_types();
2553
2554
	unset( $t['swf'], $t['exe'] );
2555
	if ( function_exists( 'current_user_can' ) )
2556
		$unfiltered = $user ? user_can( $user, 'unfiltered_html' ) : current_user_can( 'unfiltered_html' );
2557
2558
	if ( empty( $unfiltered ) )
2559
		unset( $t['htm|html'] );
2560
2561
	/**
2562
	 * Filters list of allowed mime types and file extensions.
2563
	 *
2564
	 * @since 2.0.0
2565
	 *
2566
	 * @param array            $t    Mime types keyed by the file extension regex corresponding to
2567
	 *                               those types. 'swf' and 'exe' removed from full list. 'htm|html' also
2568
	 *                               removed depending on '$user' capabilities.
2569
	 * @param int|WP_User|null $user User ID, User object or null if not provided (indicates current user).
2570
	 */
2571
	return apply_filters( 'upload_mimes', $t, $user );
2572
}
2573
2574
/**
2575
 * Display "Are You Sure" message to confirm the action being taken.
2576
 *
2577
 * If the action has the nonce explain message, then it will be displayed
2578
 * along with the "Are you sure?" message.
2579
 *
2580
 * @since 2.0.4
2581
 *
2582
 * @param string $action The nonce action.
2583
 */
2584
function wp_nonce_ays( $action ) {
2585
	if ( 'log-out' == $action ) {
2586
		$html = sprintf(
2587
			/* translators: %s: site name */
2588
			__( 'You are attempting to log out of %s' ),
2589
			get_bloginfo( 'name' )
2590
		);
2591
		$html .= '</p><p>';
2592
		$redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
2593
		$html .= sprintf(
2594
			/* translators: %s: logout URL */
2595
			__( 'Do you really want to <a href="%s">log out</a>?' ),
2596
			wp_logout_url( $redirect_to )
2597
		);
2598
	} else {
2599
		$html = __( 'Are you sure you want to do this?' );
2600
		if ( wp_get_referer() ) {
2601
			$html .= '</p><p>';
2602
			$html .= sprintf( '<a href="%s">%s</a>',
2603
				esc_url( remove_query_arg( 'updated', wp_get_referer() ) ),
0 ignored issues
show
It seems like remove_query_arg('updated', wp_get_referer()) targeting remove_query_arg() can also be of type boolean; however, esc_url() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
2604
				__( 'Please try again.' )
2605
			);
2606
		}
2607
	}
2608
2609
	wp_die( $html, __( 'WordPress Failure Notice' ), 403 );
2610
}
2611
2612
/**
2613
 * Kill WordPress execution and display HTML message with error message.
2614
 *
2615
 * This function complements the `die()` PHP function. The difference is that
2616
 * HTML will be displayed to the user. It is recommended to use this function
2617
 * only when the execution should not continue any further. It is not recommended
2618
 * to call this function very often, and try to handle as many errors as possible
2619
 * silently or more gracefully.
2620
 *
2621
 * As a shorthand, the desired HTTP response code may be passed as an integer to
2622
 * the `$title` parameter (the default title would apply) or the `$args` parameter.
2623
 *
2624
 * @since 2.0.4
2625
 * @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept
2626
 *              an integer to be used as the response code.
2627
 *
2628
 * @param string|WP_Error  $message Optional. Error message. If this is a WP_Error object,
2629
 *                                  and not an Ajax or XML-RPC request, the error's messages are used.
2630
 *                                  Default empty.
2631
 * @param string|int       $title   Optional. Error title. If `$message` is a `WP_Error` object,
2632
 *                                  error data with the key 'title' may be used to specify the title.
2633
 *                                  If `$title` is an integer, then it is treated as the response
2634
 *                                  code. Default empty.
2635
 * @param string|array|int $args {
2636
 *     Optional. Arguments to control behavior. If `$args` is an integer, then it is treated
2637
 *     as the response code. Default empty array.
2638
 *
2639
 *     @type int    $response       The HTTP response code. Default 200 for Ajax requests, 500 otherwise.
2640
 *     @type bool   $back_link      Whether to include a link to go back. Default false.
2641
 *     @type string $text_direction The text direction. This is only useful internally, when WordPress
2642
 *                                  is still loading and the site's locale is not set up yet. Accepts 'rtl'.
2643
 *                                  Default is the value of is_rtl().
2644
 * }
2645
 */
2646
function wp_die( $message = '', $title = '', $args = array() ) {
2647
2648
	if ( is_int( $args ) ) {
2649
		$args = array( 'response' => $args );
2650
	} elseif ( is_int( $title ) ) {
2651
		$args  = array( 'response' => $title );
2652
		$title = '';
2653
	}
2654
2655
	if ( wp_doing_ajax() ) {
2656
		/**
2657
		 * Filters the callback for killing WordPress execution for Ajax requests.
2658
		 *
2659
		 * @since 3.4.0
2660
		 *
2661
		 * @param callable $function Callback function name.
2662
		 */
2663
		$function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
2664
	} elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
2665
		/**
2666
		 * Filters the callback for killing WordPress execution for XML-RPC requests.
2667
		 *
2668
		 * @since 3.4.0
2669
		 *
2670
		 * @param callable $function Callback function name.
2671
		 */
2672
		$function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
2673
	} else {
2674
		/**
2675
		 * Filters the callback for killing WordPress execution for all non-Ajax, non-XML-RPC requests.
2676
		 *
2677
		 * @since 3.0.0
2678
		 *
2679
		 * @param callable $function Callback function name.
2680
		 */
2681
		$function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
2682
	}
2683
2684
	call_user_func( $function, $message, $title, $args );
2685
}
2686
2687
/**
2688
 * Kills WordPress execution and display HTML message with error message.
2689
 *
2690
 * This is the default handler for wp_die if you want a custom one for your
2691
 * site then you can overload using the {@see 'wp_die_handler'} filter in wp_die().
2692
 *
2693
 * @since 3.0.0
2694
 * @access private
2695
 *
2696
 * @param string|WP_Error $message Error message or WP_Error object.
2697
 * @param string          $title   Optional. Error title. Default empty.
2698
 * @param string|array    $args    Optional. Arguments to control behavior. Default empty array.
2699
 */
2700
function _default_wp_die_handler( $message, $title = '', $args = array() ) {
2701
	$defaults = array( 'response' => 500 );
2702
	$r = wp_parse_args($args, $defaults);
2703
2704
	$have_gettext = function_exists('__');
2705
2706
	if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
2707
		if ( empty( $title ) ) {
2708
			$error_data = $message->get_error_data();
0 ignored issues
show
It seems like $message is not always an object, but can also be of type string. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
2709
			if ( is_array( $error_data ) && isset( $error_data['title'] ) )
2710
				$title = $error_data['title'];
2711
		}
2712
		$errors = $message->get_error_messages();
2713
		switch ( count( $errors ) ) {
2714
		case 0 :
2715
			$message = '';
2716
			break;
2717
		case 1 :
2718
			$message = "<p>{$errors[0]}</p>";
2719
			break;
2720
		default :
2721
			$message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $errors ) . "</li>\n\t</ul>";
2722
			break;
2723
		}
2724
	} elseif ( is_string( $message ) ) {
2725
		$message = "<p>$message</p>";
2726
	}
2727
2728
	if ( isset( $r['back_link'] ) && $r['back_link'] ) {
2729
		$back_text = $have_gettext? __('&laquo; Back') : '&laquo; Back';
2730
		$message .= "\n<p><a href='javascript:history.back()'>$back_text</a></p>";
2731
	}
2732
2733
	if ( ! did_action( 'admin_head' ) ) :
2734
		if ( !headers_sent() ) {
2735
			status_header( $r['response'] );
2736
			nocache_headers();
2737
			header( 'Content-Type: text/html; charset=utf-8' );
2738
		}
2739
2740
		if ( empty($title) )
2741
			$title = $have_gettext ? __('WordPress &rsaquo; Error') : 'WordPress &rsaquo; Error';
2742
2743
		$text_direction = 'ltr';
2744
		if ( isset($r['text_direction']) && 'rtl' == $r['text_direction'] )
2745
			$text_direction = 'rtl';
2746
		elseif ( function_exists( 'is_rtl' ) && is_rtl() )
2747
			$text_direction = 'rtl';
2748
?>
2749
<!DOCTYPE html>
2750
<!-- Ticket #11289, IE bug fix: always pad the error page with enough characters such that it is greater than 512 bytes, even after gzip compression abcdefghijklmnopqrstuvwxyz1234567890aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz11223344556677889900abacbcbdcdcededfefegfgfhghgihihjijikjkjlklkmlmlnmnmononpopoqpqprqrqsrsrtstsubcbcdcdedefefgfabcadefbghicjkldmnoepqrfstugvwxhyz1i234j567k890laabmbccnddeoeffpgghqhiirjjksklltmmnunoovppqwqrrxsstytuuzvvw0wxx1yyz2z113223434455666777889890091abc2def3ghi4jkl5mno6pqr7stu8vwx9yz11aab2bcc3dd4ee5ff6gg7hh8ii9j0jk1kl2lmm3nnoo4p5pq6qrr7ss8tt9uuvv0wwx1x2yyzz13aba4cbcb5dcdc6dedfef8egf9gfh0ghg1ihi2hji3jik4jkj5lkl6kml7mln8mnm9ono
2751
-->
2752
<html xmlns="http://www.w3.org/1999/xhtml" <?php if ( function_exists( 'language_attributes' ) && function_exists( 'is_rtl' ) ) language_attributes(); else echo "dir='$text_direction'"; ?>>
2753
<head>
2754
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2755
	<meta name="viewport" content="width=device-width">
2756
	<?php
2757
	if ( function_exists( 'wp_no_robots' ) ) {
2758
		wp_no_robots();
2759
	}
2760
	?>
2761
	<title><?php echo $title ?></title>
2762
	<style type="text/css">
2763
		html {
2764
			background: #f1f1f1;
2765
		}
2766
		body {
2767
			background: #fff;
2768
			color: #444;
2769
			font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
2770
			margin: 2em auto;
2771
			padding: 1em 2em;
2772
			max-width: 700px;
2773
			-webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);
2774
			box-shadow: 0 1px 3px rgba(0,0,0,0.13);
2775
		}
2776
		h1 {
2777
			border-bottom: 1px solid #dadada;
2778
			clear: both;
2779
			color: #666;
2780
			font-size: 24px;
2781
			margin: 30px 0 0 0;
2782
			padding: 0;
2783
			padding-bottom: 7px;
2784
		}
2785
		#error-page {
2786
			margin-top: 50px;
2787
		}
2788
		#error-page p {
2789
			font-size: 14px;
2790
			line-height: 1.5;
2791
			margin: 25px 0 20px;
2792
		}
2793
		#error-page code {
2794
			font-family: Consolas, Monaco, monospace;
2795
		}
2796
		ul li {
2797
			margin-bottom: 10px;
2798
			font-size: 14px ;
2799
		}
2800
		a {
2801
			color: #0073aa;
2802
		}
2803
		a:hover,
2804
		a:active {
2805
			color: #00a0d2;
2806
		}
2807
		a:focus {
2808
			color: #124964;
2809
		    -webkit-box-shadow:
2810
		    	0 0 0 1px #5b9dd9,
2811
				0 0 2px 1px rgba(30, 140, 190, .8);
2812
		    box-shadow:
2813
		    	0 0 0 1px #5b9dd9,
2814
				0 0 2px 1px rgba(30, 140, 190, .8);
2815
			outline: none;
2816
		}
2817
		.button {
2818
			background: #f7f7f7;
2819
			border: 1px solid #ccc;
2820
			color: #555;
2821
			display: inline-block;
2822
			text-decoration: none;
2823
			font-size: 13px;
2824
			line-height: 26px;
2825
			height: 28px;
2826
			margin: 0;
2827
			padding: 0 10px 1px;
2828
			cursor: pointer;
2829
			-webkit-border-radius: 3px;
2830
			-webkit-appearance: none;
2831
			border-radius: 3px;
2832
			white-space: nowrap;
2833
			-webkit-box-sizing: border-box;
2834
			-moz-box-sizing:    border-box;
2835
			box-sizing:         border-box;
2836
2837
			-webkit-box-shadow: 0 1px 0 #ccc;
2838
			box-shadow: 0 1px 0 #ccc;
2839
		 	vertical-align: top;
2840
		}
2841
2842
		.button.button-large {
2843
			height: 30px;
2844
			line-height: 28px;
2845
			padding: 0 12px 2px;
2846
		}
2847
2848
		.button:hover,
2849
		.button:focus {
2850
			background: #fafafa;
2851
			border-color: #999;
2852
			color: #23282d;
2853
		}
2854
2855
		.button:focus  {
2856
			border-color: #5b9dd9;
2857
			-webkit-box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
2858
			box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
2859
			outline: none;
2860
		}
2861
2862
		.button:active {
2863
			background: #eee;
2864
			border-color: #999;
2865
		 	-webkit-box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
2866
		 	box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
2867
		 	-webkit-transform: translateY(1px);
2868
		 	-ms-transform: translateY(1px);
2869
		 	transform: translateY(1px);
2870
		}
2871
2872
		<?php
2873
		if ( 'rtl' == $text_direction ) {
2874
			echo 'body { font-family: Tahoma, Arial; }';
2875
		}
2876
		?>
2877
	</style>
2878
</head>
2879
<body id="error-page">
2880
<?php endif; // ! did_action( 'admin_head' ) ?>
2881
	<?php echo $message; ?>
2882
</body>
2883
</html>
2884
<?php
2885
	die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function _default_wp_die_handler() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
2886
}
2887
2888
/**
2889
 * Kill WordPress execution and display XML message with error message.
2890
 *
2891
 * This is the handler for wp_die when processing XMLRPC requests.
2892
 *
2893
 * @since 3.2.0
2894
 * @access private
2895
 *
2896
 * @global wp_xmlrpc_server $wp_xmlrpc_server
2897
 *
2898
 * @param string       $message Error message.
2899
 * @param string       $title   Optional. Error title. Default empty.
2900
 * @param string|array $args    Optional. Arguments to control behavior. Default empty array.
2901
 */
2902
function _xmlrpc_wp_die_handler( $message, $title = '', $args = array() ) {
0 ignored issues
show
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2903
	global $wp_xmlrpc_server;
2904
	$defaults = array( 'response' => 500 );
2905
2906
	$r = wp_parse_args($args, $defaults);
2907
2908
	if ( $wp_xmlrpc_server ) {
2909
		$error = new IXR_Error( $r['response'] , $message);
2910
		$wp_xmlrpc_server->output( $error->getXml() );
2911
	}
2912
	die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function _xmlrpc_wp_die_handler() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
2913
}
2914
2915
/**
2916
 * Kill WordPress ajax execution.
2917
 *
2918
 * This is the handler for wp_die when processing Ajax requests.
2919
 *
2920
 * @since 3.4.0
2921
 * @access private
2922
 *
2923
 * @param string       $message Error message.
2924
 * @param string       $title   Optional. Error title (unused). Default empty.
2925
 * @param string|array $args    Optional. Arguments to control behavior. Default empty array.
2926
 */
2927
function _ajax_wp_die_handler( $message, $title = '', $args = array() ) {
0 ignored issues
show
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2928
	$defaults = array(
2929
		'response' => 200,
2930
	);
2931
	$r = wp_parse_args( $args, $defaults );
2932
2933
	if ( ! headers_sent() && null !== $r['response'] ) {
2934
		status_header( $r['response'] );
2935
	}
2936
2937
	if ( is_scalar( $message ) )
2938
		die( (string) $message );
0 ignored issues
show
Coding Style Compatibility introduced by
The function _ajax_wp_die_handler() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
2939
	die( '0' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function _ajax_wp_die_handler() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
2940
}
2941
2942
/**
2943
 * Kill WordPress execution.
2944
 *
2945
 * This is the handler for wp_die when processing APP requests.
2946
 *
2947
 * @since 3.4.0
2948
 * @access private
2949
 *
2950
 * @param string $message Optional. Response to print. Default empty.
2951
 */
2952
function _scalar_wp_die_handler( $message = '' ) {
2953
	if ( is_scalar( $message ) )
2954
		die( (string) $message );
0 ignored issues
show
Coding Style Compatibility introduced by
The function _scalar_wp_die_handler() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
2955
	die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function _scalar_wp_die_handler() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
2956
}
2957
2958
/**
2959
 * Encode a variable into JSON, with some sanity checks.
2960
 *
2961
 * @since 4.1.0
2962
 *
2963
 * @param mixed $data    Variable (usually an array or object) to encode as JSON.
2964
 * @param int   $options Optional. Options to be passed to json_encode(). Default 0.
2965
 * @param int   $depth   Optional. Maximum depth to walk through $data. Must be
2966
 *                       greater than 0. Default 512.
2967
 * @return string|false The JSON encoded string, or false if it cannot be encoded.
2968
 */
2969
function wp_json_encode( $data, $options = 0, $depth = 512 ) {
2970
	/*
2971
	 * json_encode() has had extra params added over the years.
2972
	 * $options was added in 5.3, and $depth in 5.5.
2973
	 * We need to make sure we call it with the correct arguments.
2974
	 */
2975
	if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) {
2976
		$args = array( $data, $options, $depth );
2977
	} elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
2978
		$args = array( $data, $options );
2979
	} else {
2980
		$args = array( $data );
2981
	}
2982
2983
	// Prepare the data for JSON serialization.
2984
	$args[0] = _wp_json_prepare_data( $data );
2985
2986
	$json = @call_user_func_array( 'json_encode', $args );
2987
2988
	// If json_encode() was successful, no need to do more sanity checking.
2989
	// ... unless we're in an old version of PHP, and json_encode() returned
2990
	// a string containing 'null'. Then we need to do more sanity checking.
2991
	if ( false !== $json && ( version_compare( PHP_VERSION, '5.5', '>=' ) || false === strpos( $json, 'null' ) ) )  {
2992
		return $json;
2993
	}
2994
2995
	try {
2996
		$args[0] = _wp_json_sanity_check( $data, $depth );
2997
	} catch ( Exception $e ) {
2998
		return false;
2999
	}
3000
3001
	return call_user_func_array( 'json_encode', $args );
3002
}
3003
3004
/**
3005
 * Perform sanity checks on data that shall be encoded to JSON.
3006
 *
3007
 * @ignore
3008
 * @since 4.1.0
3009
 * @access private
3010
 *
3011
 * @see wp_json_encode()
3012
 *
3013
 * @param mixed $data  Variable (usually an array or object) to encode as JSON.
3014
 * @param int   $depth Maximum depth to walk through $data. Must be greater than 0.
3015
 * @return mixed The sanitized data that shall be encoded to JSON.
0 ignored issues
show
Consider making the return type a bit more specific; maybe use string|integer|double|null|boolean|array|stdClass.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
3016
 */
3017
function _wp_json_sanity_check( $data, $depth ) {
3018
	if ( $depth < 0 ) {
3019
		throw new Exception( 'Reached depth limit' );
3020
	}
3021
3022
	if ( is_array( $data ) ) {
3023
		$output = array();
3024
		foreach ( $data as $id => $el ) {
3025
			// Don't forget to sanitize the ID!
3026
			if ( is_string( $id ) ) {
3027
				$clean_id = _wp_json_convert_string( $id );
3028
			} else {
3029
				$clean_id = $id;
3030
			}
3031
3032
			// Check the element type, so that we're only recursing if we really have to.
3033
			if ( is_array( $el ) || is_object( $el ) ) {
3034
				$output[ $clean_id ] = _wp_json_sanity_check( $el, $depth - 1 );
3035
			} elseif ( is_string( $el ) ) {
3036
				$output[ $clean_id ] = _wp_json_convert_string( $el );
3037
			} else {
3038
				$output[ $clean_id ] = $el;
3039
			}
3040
		}
3041
	} elseif ( is_object( $data ) ) {
3042
		$output = new stdClass;
3043
		foreach ( $data as $id => $el ) {
3044
			if ( is_string( $id ) ) {
3045
				$clean_id = _wp_json_convert_string( $id );
3046
			} else {
3047
				$clean_id = $id;
3048
			}
3049
3050
			if ( is_array( $el ) || is_object( $el ) ) {
3051
				$output->$clean_id = _wp_json_sanity_check( $el, $depth - 1 );
3052
			} elseif ( is_string( $el ) ) {
3053
				$output->$clean_id = _wp_json_convert_string( $el );
3054
			} else {
3055
				$output->$clean_id = $el;
3056
			}
3057
		}
3058
	} elseif ( is_string( $data ) ) {
3059
		return _wp_json_convert_string( $data );
3060
	} else {
3061
		return $data;
3062
	}
3063
3064
	return $output;
3065
}
3066
3067
/**
3068
 * Convert a string to UTF-8, so that it can be safely encoded to JSON.
3069
 *
3070
 * @ignore
3071
 * @since 4.1.0
3072
 * @access private
3073
 *
3074
 * @see _wp_json_sanity_check()
3075
 *
3076
 * @staticvar bool $use_mb
3077
 *
3078
 * @param string $string The string which is to be converted.
3079
 * @return string The checked string.
3080
 */
3081
function _wp_json_convert_string( $string ) {
3082
	static $use_mb = null;
3083
	if ( is_null( $use_mb ) ) {
3084
		$use_mb = function_exists( 'mb_convert_encoding' );
3085
	}
3086
3087
	if ( $use_mb ) {
3088
		$encoding = mb_detect_encoding( $string, mb_detect_order(), true );
3089
		if ( $encoding ) {
3090
			return mb_convert_encoding( $string, 'UTF-8', $encoding );
3091
		} else {
3092
			return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' );
3093
		}
3094
	} else {
3095
		return wp_check_invalid_utf8( $string, true );
3096
	}
3097
}
3098
3099
/**
3100
 * Prepares response data to be serialized to JSON.
3101
 *
3102
 * This supports the JsonSerializable interface for PHP 5.2-5.3 as well.
3103
 *
3104
 * @ignore
3105
 * @since 4.4.0
3106
 * @access private
3107
 *
3108
 * @param mixed $data Native representation.
3109
 * @return bool|int|float|null|string|array Data ready for `json_encode()`.
3110
 */
3111
function _wp_json_prepare_data( $data ) {
3112
	if ( ! defined( 'WP_JSON_SERIALIZE_COMPATIBLE' ) || WP_JSON_SERIALIZE_COMPATIBLE === false ) {
3113
		return $data;
3114
	}
3115
3116
	switch ( gettype( $data ) ) {
3117
		case 'boolean':
3118
		case 'integer':
3119
		case 'double':
3120
		case 'string':
3121
		case 'NULL':
3122
			// These values can be passed through.
3123
			return $data;
3124
3125
		case 'array':
3126
			// Arrays must be mapped in case they also return objects.
3127
			return array_map( '_wp_json_prepare_data', $data );
3128
3129
		case 'object':
3130
			// If this is an incomplete object (__PHP_Incomplete_Class), bail.
3131
			if ( ! is_object( $data ) ) {
3132
				return null;
3133
			}
3134
3135
			if ( $data instanceof JsonSerializable ) {
3136
				$data = $data->jsonSerialize();
3137
			} else {
3138
				$data = get_object_vars( $data );
3139
			}
3140
3141
			// Now, pass the array (or whatever was returned from jsonSerialize through).
3142
			return _wp_json_prepare_data( $data );
3143
3144
		default:
3145
			return null;
3146
	}
3147
}
3148
3149
/**
3150
 * Send a JSON response back to an Ajax request.
3151
 *
3152
 * @since 3.5.0
3153
 * @since 4.7.0 The `$status_code` parameter was added.
3154
 *
3155
 * @param mixed $response    Variable (usually an array or object) to encode as JSON,
3156
 *                           then print and die.
3157
 * @param int   $status_code The HTTP status code to output.
3158
 */
3159
function wp_send_json( $response, $status_code = null ) {
3160
	@header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
3161
	if ( null !== $status_code ) {
3162
		status_header( $status_code );
3163
	}
3164
	echo wp_json_encode( $response );
3165
3166
	if ( wp_doing_ajax() ) {
3167
		wp_die( '', '', array(
3168
			'response' => null,
3169
		) );
3170
	} else {
3171
		die;
0 ignored issues
show
Coding Style Compatibility introduced by
The function wp_send_json() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3172
	}
3173
}
3174
3175
/**
3176
 * Send a JSON response back to an Ajax request, indicating success.
3177
 *
3178
 * @since 3.5.0
3179
 * @since 4.7.0 The `$status_code` parameter was added.
3180
 *
3181
 * @param mixed $data        Data to encode as JSON, then print and die.
3182
 * @param int   $status_code The HTTP status code to output.
3183
 */
3184
function wp_send_json_success( $data = null, $status_code = null ) {
3185
	$response = array( 'success' => true );
3186
3187
	if ( isset( $data ) )
3188
		$response['data'] = $data;
3189
3190
	wp_send_json( $response, $status_code );
3191
}
3192
3193
/**
3194
 * Send a JSON response back to an Ajax request, indicating failure.
3195
 *
3196
 * If the `$data` parameter is a WP_Error object, the errors
3197
 * within the object are processed and output as an array of error
3198
 * codes and corresponding messages. All other types are output
3199
 * without further processing.
3200
 *
3201
 * @since 3.5.0
3202
 * @since 4.1.0 The `$data` parameter is now processed if a WP_Error object is passed in.
3203
 * @since 4.7.0 The `$status_code` parameter was added.
3204
 *
3205
 * @param mixed $data        Data to encode as JSON, then print and die.
3206
 * @param int   $status_code The HTTP status code to output.
3207
 */
3208
function wp_send_json_error( $data = null, $status_code = null ) {
3209
	$response = array( 'success' => false );
3210
3211
	if ( isset( $data ) ) {
3212
		if ( is_wp_error( $data ) ) {
3213
			$result = array();
3214
			foreach ( $data->errors as $code => $messages ) {
3215
				foreach ( $messages as $message ) {
3216
					$result[] = array( 'code' => $code, 'message' => $message );
3217
				}
3218
			}
3219
3220
			$response['data'] = $result;
3221
		} else {
3222
			$response['data'] = $data;
3223
		}
3224
	}
3225
3226
	wp_send_json( $response, $status_code );
3227
}
3228
3229
/**
3230
 * Checks that a JSONP callback is a valid JavaScript callback.
3231
 *
3232
 * Only allows alphanumeric characters and the dot character in callback
3233
 * function names. This helps to mitigate XSS attacks caused by directly
3234
 * outputting user input.
3235
 *
3236
 * @since 4.6.0
3237
 *
3238
 * @param string $callback Supplied JSONP callback function.
3239
 * @return bool True if valid callback, otherwise false.
3240
 */
3241
function wp_check_jsonp_callback( $callback ) {
3242
	if ( ! is_string( $callback ) ) {
3243
		return false;
3244
	}
3245
3246
	preg_replace( '/[^\w\.]/', '', $callback, -1, $illegal_char_count );
3247
3248
	return 0 === $illegal_char_count;
3249
}
3250
3251
/**
3252
 * Retrieve the WordPress home page URL.
3253
 *
3254
 * If the constant named 'WP_HOME' exists, then it will be used and returned
3255
 * by the function. This can be used to counter the redirection on your local
3256
 * development environment.
3257
 *
3258
 * @since 2.2.0
3259
 * @access private
3260
 *
3261
 * @see WP_HOME
3262
 *
3263
 * @param string $url URL for the home location.
3264
 * @return string Homepage location.
3265
 */
3266
function _config_wp_home( $url = '' ) {
3267
	if ( defined( 'WP_HOME' ) )
3268
		return untrailingslashit( WP_HOME );
3269
	return $url;
3270
}
3271
3272
/**
3273
 * Retrieve the WordPress site URL.
3274
 *
3275
 * If the constant named 'WP_SITEURL' is defined, then the value in that
3276
 * constant will always be returned. This can be used for debugging a site
3277
 * on your localhost while not having to change the database to your URL.
3278
 *
3279
 * @since 2.2.0
3280
 * @access private
3281
 *
3282
 * @see WP_SITEURL
3283
 *
3284
 * @param string $url URL to set the WordPress site location.
3285
 * @return string The WordPress Site URL.
3286
 */
3287
function _config_wp_siteurl( $url = '' ) {
3288
	if ( defined( 'WP_SITEURL' ) )
3289
		return untrailingslashit( WP_SITEURL );
3290
	return $url;
3291
}
3292
3293
/**
3294
 * Delete the fresh site option.
3295
 *
3296
 * @since 4.7.0
3297
 * @access private
3298
 */
3299
function _delete_option_fresh_site() {
3300
	update_option( 'fresh_site', 0 );
3301
}
3302
3303
/**
3304
 * Set the localized direction for MCE plugin.
3305
 *
3306
 * Will only set the direction to 'rtl', if the WordPress locale has
3307
 * the text direction set to 'rtl'.
3308
 *
3309
 * Fills in the 'directionality' setting, enables the 'directionality'
3310
 * plugin, and adds the 'ltr' button to 'toolbar1', formerly
3311
 * 'theme_advanced_buttons1' array keys. These keys are then returned
3312
 * in the $mce_init (TinyMCE settings) array.
3313
 *
3314
 * @since 2.1.0
3315
 * @access private
3316
 *
3317
 * @param array $mce_init MCE settings array.
3318
 * @return array Direction set for 'rtl', if needed by locale.
3319
 */
3320
function _mce_set_direction( $mce_init ) {
3321
	if ( is_rtl() ) {
3322
		$mce_init['directionality'] = 'rtl';
3323
		$mce_init['rtl_ui'] = true;
3324
3325
		if ( ! empty( $mce_init['plugins'] ) && strpos( $mce_init['plugins'], 'directionality' ) === false ) {
3326
			$mce_init['plugins'] .= ',directionality';
3327
		}
3328
3329
		if ( ! empty( $mce_init['toolbar1'] ) && ! preg_match( '/\bltr\b/', $mce_init['toolbar1'] ) ) {
3330
			$mce_init['toolbar1'] .= ',ltr';
3331
		}
3332
	}
3333
3334
	return $mce_init;
3335
}
3336
3337
3338
/**
3339
 * Convert smiley code to the icon graphic file equivalent.
3340
 *
3341
 * You can turn off smilies, by going to the write setting screen and unchecking
3342
 * the box, or by setting 'use_smilies' option to false or removing the option.
3343
 *
3344
 * Plugins may override the default smiley list by setting the $wpsmiliestrans
3345
 * to an array, with the key the code the blogger types in and the value the
3346
 * image file.
3347
 *
3348
 * The $wp_smiliessearch global is for the regular expression and is set each
3349
 * time the function is called.
3350
 *
3351
 * The full list of smilies can be found in the function and won't be listed in
3352
 * the description. Probably should create a Codex page for it, so that it is
3353
 * available.
3354
 *
3355
 * @global array $wpsmiliestrans
3356
 * @global array $wp_smiliessearch
3357
 *
3358
 * @since 2.2.0
3359
 */
3360
function smilies_init() {
3361
	global $wpsmiliestrans, $wp_smiliessearch;
3362
3363
	// don't bother setting up smilies if they are disabled
3364
	if ( !get_option( 'use_smilies' ) )
3365
		return;
3366
3367
	if ( !isset( $wpsmiliestrans ) ) {
3368
		$wpsmiliestrans = array(
3369
		':mrgreen:' => 'mrgreen.png',
3370
		':neutral:' => "\xf0\x9f\x98\x90",
3371
		':twisted:' => "\xf0\x9f\x98\x88",
3372
		  ':arrow:' => "\xe2\x9e\xa1",
3373
		  ':shock:' => "\xf0\x9f\x98\xaf",
3374
		  ':smile:' => "\xf0\x9f\x99\x82",
3375
		    ':???:' => "\xf0\x9f\x98\x95",
3376
		   ':cool:' => "\xf0\x9f\x98\x8e",
3377
		   ':evil:' => "\xf0\x9f\x91\xbf",
3378
		   ':grin:' => "\xf0\x9f\x98\x80",
3379
		   ':idea:' => "\xf0\x9f\x92\xa1",
3380
		   ':oops:' => "\xf0\x9f\x98\xb3",
3381
		   ':razz:' => "\xf0\x9f\x98\x9b",
3382
		   ':roll:' => "\xf0\x9f\x99\x84",
3383
		   ':wink:' => "\xf0\x9f\x98\x89",
3384
		    ':cry:' => "\xf0\x9f\x98\xa5",
3385
		    ':eek:' => "\xf0\x9f\x98\xae",
3386
		    ':lol:' => "\xf0\x9f\x98\x86",
3387
		    ':mad:' => "\xf0\x9f\x98\xa1",
3388
		    ':sad:' => "\xf0\x9f\x99\x81",
3389
		      '8-)' => "\xf0\x9f\x98\x8e",
3390
		      '8-O' => "\xf0\x9f\x98\xaf",
3391
		      ':-(' => "\xf0\x9f\x99\x81",
3392
		      ':-)' => "\xf0\x9f\x99\x82",
3393
		      ':-?' => "\xf0\x9f\x98\x95",
3394
		      ':-D' => "\xf0\x9f\x98\x80",
3395
		      ':-P' => "\xf0\x9f\x98\x9b",
3396
		      ':-o' => "\xf0\x9f\x98\xae",
3397
		      ':-x' => "\xf0\x9f\x98\xa1",
3398
		      ':-|' => "\xf0\x9f\x98\x90",
3399
		      ';-)' => "\xf0\x9f\x98\x89",
3400
		// This one transformation breaks regular text with frequency.
3401
		//     '8)' => "\xf0\x9f\x98\x8e",
3402
		       '8O' => "\xf0\x9f\x98\xaf",
3403
		       ':(' => "\xf0\x9f\x99\x81",
3404
		       ':)' => "\xf0\x9f\x99\x82",
3405
		       ':?' => "\xf0\x9f\x98\x95",
3406
		       ':D' => "\xf0\x9f\x98\x80",
3407
		       ':P' => "\xf0\x9f\x98\x9b",
3408
		       ':o' => "\xf0\x9f\x98\xae",
3409
		       ':x' => "\xf0\x9f\x98\xa1",
3410
		       ':|' => "\xf0\x9f\x98\x90",
3411
		       ';)' => "\xf0\x9f\x98\x89",
3412
		      ':!:' => "\xe2\x9d\x97",
3413
		      ':?:' => "\xe2\x9d\x93",
3414
		);
3415
	}
3416
3417
	/**
3418
	 * Filters all the smilies.
3419
	 *
3420
	 * This filter must be added before `smilies_init` is run, as
3421
	 * it is normally only run once to setup the smilies regex.
3422
	 *
3423
	 * @since 4.7.0
3424
	 *
3425
	 * @param array $wpsmiliestrans List of the smilies.
3426
	 */
3427
	$wpsmiliestrans = apply_filters('smilies', $wpsmiliestrans);
3428
3429
	if (count($wpsmiliestrans) == 0) {
3430
		return;
3431
	}
3432
3433
	/*
3434
	 * NOTE: we sort the smilies in reverse key order. This is to make sure
3435
	 * we match the longest possible smilie (:???: vs :?) as the regular
3436
	 * expression used below is first-match
3437
	 */
3438
	krsort($wpsmiliestrans);
3439
3440
	$spaces = wp_spaces_regexp();
3441
3442
	// Begin first "subpattern"
3443
	$wp_smiliessearch = '/(?<=' . $spaces . '|^)';
3444
3445
	$subchar = '';
3446
	foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
3447
		$firstchar = substr($smiley, 0, 1);
3448
		$rest = substr($smiley, 1);
3449
3450
		// new subpattern?
3451
		if ($firstchar != $subchar) {
3452
			if ($subchar != '') {
3453
				$wp_smiliessearch .= ')(?=' . $spaces . '|$)';  // End previous "subpattern"
3454
				$wp_smiliessearch .= '|(?<=' . $spaces . '|^)'; // Begin another "subpattern"
3455
			}
3456
			$subchar = $firstchar;
3457
			$wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:';
3458
		} else {
3459
			$wp_smiliessearch .= '|';
3460
		}
3461
		$wp_smiliessearch .= preg_quote($rest, '/');
3462
	}
3463
3464
	$wp_smiliessearch .= ')(?=' . $spaces . '|$)/m';
3465
3466
}
3467
3468
/**
3469
 * Merge user defined arguments into defaults array.
3470
 *
3471
 * This function is used throughout WordPress to allow for both string or array
3472
 * to be merged into another array.
3473
 *
3474
 * @since 2.2.0
3475
 * @since 2.3.0 `$args` can now also be an object.
3476
 *
3477
 * @param string|array|object $args     Value to merge with $defaults.
3478
 * @param array               $defaults Optional. Array that serves as the defaults. Default empty.
0 ignored issues
show
Should the type for parameter $defaults not be string|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
3479
 * @return array Merged user defined values with defaults.
3480
 */
3481
function wp_parse_args( $args, $defaults = '' ) {
3482
	if ( is_object( $args ) )
3483
		$r = get_object_vars( $args );
3484
	elseif ( is_array( $args ) )
3485
		$r =& $args;
3486
	else
3487
		wp_parse_str( $args, $r );
3488
3489
	if ( is_array( $defaults ) )
3490
		return array_merge( $defaults, $r );
3491
	return $r;
3492
}
3493
3494
/**
3495
 * Clean up an array, comma- or space-separated list of IDs.
3496
 *
3497
 * @since 3.0.0
3498
 *
3499
 * @param array|string $list List of ids.
3500
 * @return array Sanitized array of IDs.
3501
 */
3502
function wp_parse_id_list( $list ) {
3503
	if ( !is_array($list) )
3504
		$list = preg_split('/[\s,]+/', $list);
3505
3506
	return array_unique(array_map('absint', $list));
3507
}
3508
3509
/**
3510
 * Clean up an array, comma- or space-separated list of slugs.
3511
 *
3512
 * @since 4.7.0
3513
 *
3514
 * @param  array|string $list List of slugs.
3515
 * @return array Sanitized array of slugs.
3516
 */
3517
function wp_parse_slug_list( $list ) {
3518
	if ( ! is_array( $list ) ) {
3519
		$list = preg_split( '/[\s,]+/', $list );
3520
	}
3521
3522
	foreach ( $list as $key => $value ) {
3523
		$list[ $key ] = sanitize_title( $value );
3524
	}
3525
3526
	return array_unique( $list );
3527
}
3528
3529
/**
3530
 * Extract a slice of an array, given a list of keys.
3531
 *
3532
 * @since 3.1.0
3533
 *
3534
 * @param array $array The original array.
3535
 * @param array $keys  The list of keys.
3536
 * @return array The array slice.
3537
 */
3538
function wp_array_slice_assoc( $array, $keys ) {
3539
	$slice = array();
3540
	foreach ( $keys as $key )
3541
		if ( isset( $array[ $key ] ) )
3542
			$slice[ $key ] = $array[ $key ];
3543
3544
	return $slice;
3545
}
3546
3547
/**
3548
 * Determines if the variable is a numeric-indexed array.
3549
 *
3550
 * @since 4.4.0
3551
 *
3552
 * @param mixed $data Variable to check.
3553
 * @return bool Whether the variable is a list.
3554
 */
3555
function wp_is_numeric_array( $data ) {
3556
	if ( ! is_array( $data ) ) {
3557
		return false;
3558
	}
3559
3560
	$keys = array_keys( $data );
3561
	$string_keys = array_filter( $keys, 'is_string' );
3562
	return count( $string_keys ) === 0;
3563
}
3564
3565
/**
3566
 * Filters a list of objects, based on a set of key => value arguments.
3567
 *
3568
 * @since 3.0.0
3569
 * @since 4.7.0 Uses WP_List_Util class.
3570
 *
3571
 * @param array       $list     An array of objects to filter
3572
 * @param array       $args     Optional. An array of key => value arguments to match
3573
 *                              against each object. Default empty array.
3574
 * @param string      $operator Optional. The logical operation to perform. 'or' means
3575
 *                              only one element from the array needs to match; 'and'
3576
 *                              means all elements must match; 'not' means no elements may
3577
 *                              match. Default 'and'.
3578
 * @param bool|string $field    A field from the object to place instead of the entire object.
3579
 *                              Default false.
3580
 * @return array A list of objects or object fields.
3581
 */
3582
function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
3583
	if ( ! is_array( $list ) ) {
3584
		return array();
3585
	}
3586
3587
	$util = new WP_List_Util( $list );
3588
3589
	$util->filter( $args, $operator );
3590
3591
	if ( $field ) {
3592
		$util->pluck( $field );
0 ignored issues
show
It seems like $field defined by parameter $field on line 3582 can also be of type boolean; however, WP_List_Util::pluck() does only seem to accept integer|string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
3593
	}
3594
3595
	return $util->get_output();
3596
}
3597
3598
/**
3599
 * Filters a list of objects, based on a set of key => value arguments.
3600
 *
3601
 * @since 3.1.0
3602
 * @since 4.7.0 Uses WP_List_Util class.
3603
 *
3604
 * @param array  $list     An array of objects to filter.
3605
 * @param array  $args     Optional. An array of key => value arguments to match
3606
 *                         against each object. Default empty array.
3607
 * @param string $operator Optional. The logical operation to perform. 'AND' means
3608
 *                         all elements from the array must match. 'OR' means only
3609
 *                         one element needs to match. 'NOT' means no elements may
3610
 *                         match. Default 'AND'.
3611
 * @return array Array of found values.
3612
 */
3613
function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
3614
	if ( ! is_array( $list ) ) {
3615
		return array();
3616
	}
3617
3618
	$util = new WP_List_Util( $list );
3619
	return $util->filter( $args, $operator );
3620
}
3621
3622
/**
3623
 * Pluck a certain field out of each object in a list.
3624
 *
3625
 * This has the same functionality and prototype of
3626
 * array_column() (PHP 5.5) but also supports objects.
3627
 *
3628
 * @since 3.1.0
3629
 * @since 4.0.0 $index_key parameter added.
3630
 * @since 4.7.0 Uses WP_List_Util class.
3631
 *
3632
 * @param array      $list      List of objects or arrays
3633
 * @param int|string $field     Field from the object to place instead of the entire object
3634
 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
3635
 *                              Default null.
3636
 * @return array Array of found values. If `$index_key` is set, an array of found values with keys
3637
 *               corresponding to `$index_key`. If `$index_key` is null, array keys from the original
3638
 *               `$list` will be preserved in the results.
3639
 */
3640
function wp_list_pluck( $list, $field, $index_key = null ) {
3641
	$util = new WP_List_Util( $list );
3642
	return $util->pluck( $field, $index_key );
3643
}
3644
3645
/**
3646
 * Sorts a list of objects, based on one or more orderby arguments.
3647
 *
3648
 * @since 4.7.0
3649
 *
3650
 * @param array        $list          An array of objects to filter.
3651
 * @param string|array $orderby       Optional. Either the field name to order by or an array
3652
 *                                    of multiple orderby fields as $orderby => $order.
3653
 * @param string       $order         Optional. Either 'ASC' or 'DESC'. Only used if $orderby
3654
 *                                    is a string.
3655
 * @param bool         $preserve_keys Optional. Whether to preserve keys. Default false.
3656
 * @return array The sorted array.
3657
 */
3658
function wp_list_sort( $list, $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
3659
	if ( ! is_array( $list ) ) {
3660
		return array();
3661
	}
3662
3663
	$util = new WP_List_Util( $list );
3664
	return $util->sort( $orderby, $order, $preserve_keys );
3665
}
3666
3667
/**
3668
 * Determines if Widgets library should be loaded.
3669
 *
3670
 * Checks to make sure that the widgets library hasn't already been loaded.
3671
 * If it hasn't, then it will load the widgets library and run an action hook.
3672
 *
3673
 * @since 2.2.0
3674
 */
3675
function wp_maybe_load_widgets() {
3676
	/**
3677
	 * Filters whether to load the Widgets library.
3678
	 *
3679
	 * Passing a falsey value to the filter will effectively short-circuit
3680
	 * the Widgets library from loading.
3681
	 *
3682
	 * @since 2.8.0
3683
	 *
3684
	 * @param bool $wp_maybe_load_widgets Whether to load the Widgets library.
3685
	 *                                    Default true.
3686
	 */
3687
	if ( ! apply_filters( 'load_default_widgets', true ) ) {
3688
		return;
3689
	}
3690
3691
	require_once( ABSPATH . WPINC . '/default-widgets.php' );
3692
3693
	add_action( '_admin_menu', 'wp_widgets_add_menu' );
3694
}
3695
3696
/**
3697
 * Append the Widgets menu to the themes main menu.
3698
 *
3699
 * @since 2.2.0
3700
 *
3701
 * @global array $submenu
3702
 */
3703
function wp_widgets_add_menu() {
3704
	global $submenu;
3705
3706
	if ( ! current_theme_supports( 'widgets' ) )
3707
		return;
3708
3709
	$submenu['themes.php'][7] = array( __( 'Widgets' ), 'edit_theme_options', 'widgets.php' );
3710
	ksort( $submenu['themes.php'], SORT_NUMERIC );
3711
}
3712
3713
/**
3714
 * Flush all output buffers for PHP 5.2.
3715
 *
3716
 * Make sure all output buffers are flushed before our singletons are destroyed.
3717
 *
3718
 * @since 2.2.0
3719
 */
3720
function wp_ob_end_flush_all() {
3721
	$levels = ob_get_level();
3722
	for ($i=0; $i<$levels; $i++)
3723
		ob_end_flush();
3724
}
3725
3726
/**
3727
 * Load custom DB error or display WordPress DB error.
3728
 *
3729
 * If a file exists in the wp-content directory named db-error.php, then it will
3730
 * be loaded instead of displaying the WordPress DB error. If it is not found,
3731
 * then the WordPress DB error will be displayed instead.
3732
 *
3733
 * The WordPress DB error sets the HTTP status header to 500 to try to prevent
3734
 * search engines from caching the message. Custom DB messages should do the
3735
 * same.
3736
 *
3737
 * This function was backported to WordPress 2.3.2, but originally was added
3738
 * in WordPress 2.5.0.
3739
 *
3740
 * @since 2.3.2
3741
 *
3742
 * @global wpdb $wpdb WordPress database abstraction object.
3743
 */
3744
function dead_db() {
3745
	global $wpdb;
3746
3747
	wp_load_translations_early();
3748
3749
	// Load custom DB error template, if present.
3750
	if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
3751
		require_once( WP_CONTENT_DIR . '/db-error.php' );
3752
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function dead_db() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3753
	}
3754
3755
	// If installing or in the admin, provide the verbose message.
3756
	if ( wp_installing() || defined( 'WP_ADMIN' ) )
3757
		wp_die($wpdb->error);
3758
3759
	// Otherwise, be terse.
3760
	status_header( 500 );
3761
	nocache_headers();
3762
	header( 'Content-Type: text/html; charset=utf-8' );
3763
?>
3764
<!DOCTYPE html>
3765
<html xmlns="http://www.w3.org/1999/xhtml"<?php if ( is_rtl() ) echo ' dir="rtl"'; ?>>
3766
<head>
3767
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
3768
	<title><?php _e( 'Database Error' ); ?></title>
3769
3770
</head>
3771
<body>
3772
	<h1><?php _e( 'Error establishing a database connection' ); ?></h1>
3773
</body>
3774
</html>
3775
<?php
3776
	die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function dead_db() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3777
}
3778
3779
/**
3780
 * Convert a value to non-negative integer.
3781
 *
3782
 * @since 2.5.0
3783
 *
3784
 * @param mixed $maybeint Data you wish to have converted to a non-negative integer.
3785
 * @return int A non-negative integer.
0 ignored issues
show
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
3786
 */
3787
function absint( $maybeint ) {
3788
	return abs( intval( $maybeint ) );
3789
}
3790
3791
/**
3792
 * Mark a function as deprecated and inform when it has been used.
3793
 *
3794
 * There is a {@see 'hook deprecated_function_run'} that will be called that can be used
3795
 * to get the backtrace up to what file and function called the deprecated
3796
 * function.
3797
 *
3798
 * The current behavior is to trigger a user error if `WP_DEBUG` is true.
3799
 *
3800
 * This function is to be used in every function that is deprecated.
3801
 *
3802
 * @since 2.5.0
3803
 * @access private
3804
 *
3805
 * @param string $function    The function that was called.
3806
 * @param string $version     The version of WordPress that deprecated the function.
3807
 * @param string $replacement Optional. The function that should have been called. Default null.
3808
 */
3809 View Code Duplication
function _deprecated_function( $function, $version, $replacement = null ) {
0 ignored issues
show
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3810
3811
	/**
3812
	 * Fires when a deprecated function is called.
3813
	 *
3814
	 * @since 2.5.0
3815
	 *
3816
	 * @param string $function    The function that was called.
3817
	 * @param string $replacement The function that should have been called.
3818
	 * @param string $version     The version of WordPress that deprecated the function.
3819
	 */
3820
	do_action( 'deprecated_function_run', $function, $replacement, $version );
3821
3822
	/**
3823
	 * Filters whether to trigger an error for deprecated functions.
3824
	 *
3825
	 * @since 2.5.0
3826
	 *
3827
	 * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
3828
	 */
3829
	if ( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true ) ) {
3830
		if ( function_exists( '__' ) ) {
3831
			if ( ! is_null( $replacement ) ) {
3832
				/* translators: 1: PHP function name, 2: version number, 3: alternative function name */
3833
				trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $function, $version, $replacement ) );
3834
			} else {
3835
				/* translators: 1: PHP function name, 2: version number */
3836
				trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $function, $version ) );
3837
			}
3838
		} else {
3839
			if ( ! is_null( $replacement ) ) {
3840
				trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $function, $version, $replacement ) );
3841
			} else {
3842
				trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version ) );
3843
			}
3844
		}
3845
	}
3846
}
3847
3848
/**
3849
 * Marks a constructor as deprecated and informs when it has been used.
3850
 *
3851
 * Similar to _deprecated_function(), but with different strings. Used to
3852
 * remove PHP4 style constructors.
3853
 *
3854
 * The current behavior is to trigger a user error if `WP_DEBUG` is true.
3855
 *
3856
 * This function is to be used in every PHP4 style constructor method that is deprecated.
3857
 *
3858
 * @since 4.3.0
3859
 * @since 4.5.0 Added the `$parent_class` parameter.
3860
 *
3861
 * @access private
3862
 *
3863
 * @param string $class        The class containing the deprecated constructor.
3864
 * @param string $version      The version of WordPress that deprecated the function.
3865
 * @param string $parent_class Optional. The parent class calling the deprecated constructor.
3866
 *                             Default empty string.
3867
 */
3868
function _deprecated_constructor( $class, $version, $parent_class = '' ) {
3869
3870
	/**
3871
	 * Fires when a deprecated constructor is called.
3872
	 *
3873
	 * @since 4.3.0
3874
	 * @since 4.5.0 Added the `$parent_class` parameter.
3875
	 *
3876
	 * @param string $class        The class containing the deprecated constructor.
3877
	 * @param string $version      The version of WordPress that deprecated the function.
3878
	 * @param string $parent_class The parent class calling the deprecated constructor.
3879
	 */
3880
	do_action( 'deprecated_constructor_run', $class, $version, $parent_class );
3881
3882
	/**
3883
	 * Filters whether to trigger an error for deprecated functions.
3884
	 *
3885
	 * `WP_DEBUG` must be true in addition to the filter evaluating to true.
3886
	 *
3887
	 * @since 4.3.0
3888
	 *
3889
	 * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
3890
	 */
3891
	if ( WP_DEBUG && apply_filters( 'deprecated_constructor_trigger_error', true ) ) {
3892
		if ( function_exists( '__' ) ) {
3893 View Code Duplication
			if ( ! empty( $parent_class ) ) {
3894
				/* translators: 1: PHP class name, 2: PHP parent class name, 3: version number, 4: __construct() method */
3895
				trigger_error( sprintf( __( 'The called constructor method for %1$s in %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead.' ),
3896
					$class, $parent_class, $version, '<pre>__construct()</pre>' ) );
3897
			} else {
3898
				/* translators: 1: PHP class name, 2: version number, 3: __construct() method */
3899
				trigger_error( sprintf( __( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ),
3900
					$class, $version, '<pre>__construct()</pre>' ) );
3901
			}
3902 View Code Duplication
		} else {
3903
			if ( ! empty( $parent_class ) ) {
3904
				trigger_error( sprintf( 'The called constructor method for %1$s in %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead.',
3905
					$class, $parent_class, $version, '<pre>__construct()</pre>' ) );
3906
			} else {
3907
				trigger_error( sprintf( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
3908
					$class, $version, '<pre>__construct()</pre>' ) );
3909
			}
3910
		}
3911
	}
3912
3913
}
3914
3915
/**
3916
 * Mark a file as deprecated and inform when it has been used.
3917
 *
3918
 * There is a hook {@see 'deprecated_file_included'} that will be called that can be used
3919
 * to get the backtrace up to what file and function included the deprecated
3920
 * file.
3921
 *
3922
 * The current behavior is to trigger a user error if `WP_DEBUG` is true.
3923
 *
3924
 * This function is to be used in every file that is deprecated.
3925
 *
3926
 * @since 2.5.0
3927
 * @access private
3928
 *
3929
 * @param string $file        The file that was included.
3930
 * @param string $version     The version of WordPress that deprecated the file.
3931
 * @param string $replacement Optional. The file that should have been included based on ABSPATH.
3932
 *                            Default null.
3933
 * @param string $message     Optional. A message regarding the change. Default empty.
3934
 */
3935
function _deprecated_file( $file, $version, $replacement = null, $message = '' ) {
3936
3937
	/**
3938
	 * Fires when a deprecated file is called.
3939
	 *
3940
	 * @since 2.5.0
3941
	 *
3942
	 * @param string $file        The file that was called.
3943
	 * @param string $replacement The file that should have been included based on ABSPATH.
3944
	 * @param string $version     The version of WordPress that deprecated the file.
3945
	 * @param string $message     A message regarding the change.
3946
	 */
3947
	do_action( 'deprecated_file_included', $file, $replacement, $version, $message );
3948
3949
	/**
3950
	 * Filters whether to trigger an error for deprecated files.
3951
	 *
3952
	 * @since 2.5.0
3953
	 *
3954
	 * @param bool $trigger Whether to trigger the error for deprecated files. Default true.
3955
	 */
3956
	if ( WP_DEBUG && apply_filters( 'deprecated_file_trigger_error', true ) ) {
3957
		$message = empty( $message ) ? '' : ' ' . $message;
3958
		if ( function_exists( '__' ) ) {
3959
			if ( ! is_null( $replacement ) ) {
3960
				/* translators: 1: PHP file name, 2: version number, 3: alternative file name */
3961
				trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $file, $version, $replacement ) . $message );
3962
			} else {
3963
				/* translators: 1: PHP file name, 2: version number */
3964
				trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $file, $version ) . $message );
3965
			}
3966
		} else {
3967
			if ( ! is_null( $replacement ) ) {
3968
				trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $file, $version, $replacement ) . $message );
3969
			} else {
3970
				trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $file, $version ) . $message );
3971
			}
3972
		}
3973
	}
3974
}
3975
/**
3976
 * Mark a function argument as deprecated and inform when it has been used.
3977
 *
3978
 * This function is to be used whenever a deprecated function argument is used.
3979
 * Before this function is called, the argument must be checked for whether it was
3980
 * used by comparing it to its default value or evaluating whether it is empty.
3981
 * For example:
3982
 *
3983
 *     if ( ! empty( $deprecated ) ) {
3984
 *         _deprecated_argument( __FUNCTION__, '3.0.0' );
3985
 *     }
3986
 *
3987
 *
3988
 * There is a hook deprecated_argument_run that will be called that can be used
3989
 * to get the backtrace up to what file and function used the deprecated
3990
 * argument.
3991
 *
3992
 * The current behavior is to trigger a user error if WP_DEBUG is true.
3993
 *
3994
 * @since 3.0.0
3995
 * @access private
3996
 *
3997
 * @param string $function The function that was called.
3998
 * @param string $version  The version of WordPress that deprecated the argument used.
3999
 * @param string $message  Optional. A message regarding the change. Default null.
4000
 */
4001 View Code Duplication
function _deprecated_argument( $function, $version, $message = null ) {
0 ignored issues
show
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
4002
4003
	/**
4004
	 * Fires when a deprecated argument is called.
4005
	 *
4006
	 * @since 3.0.0
4007
	 *
4008
	 * @param string $function The function that was called.
4009
	 * @param string $message  A message regarding the change.
4010
	 * @param string $version  The version of WordPress that deprecated the argument used.
4011
	 */
4012
	do_action( 'deprecated_argument_run', $function, $message, $version );
4013
4014
	/**
4015
	 * Filters whether to trigger an error for deprecated arguments.
4016
	 *
4017
	 * @since 3.0.0
4018
	 *
4019
	 * @param bool $trigger Whether to trigger the error for deprecated arguments. Default true.
4020
	 */
4021
	if ( WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) {
4022
		if ( function_exists( '__' ) ) {
4023
			if ( ! is_null( $message ) ) {
4024
				/* translators: 1: PHP function name, 2: version number, 3: optional message regarding the change */
4025
				trigger_error( sprintf( __('%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s'), $function, $version, $message ) );
4026
			} else {
4027
				/* translators: 1: PHP function name, 2: version number */
4028
				trigger_error( sprintf( __('%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.'), $function, $version ) );
4029
			}
4030
		} else {
4031
			if ( ! is_null( $message ) ) {
4032
				trigger_error( sprintf( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s', $function, $version, $message ) );
4033
			} else {
4034
				trigger_error( sprintf( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version ) );
4035
			}
4036
		}
4037
	}
4038
}
4039
4040
/**
4041
 * Marks a deprecated action or filter hook as deprecated and throws a notice.
4042
 *
4043
 * Use the {@see 'deprecated_hook_run'} action to get the backtrace describing where
4044
 * the deprecated hook was called.
4045
 *
4046
 * Default behavior is to trigger a user error if `WP_DEBUG` is true.
4047
 *
4048
 * This function is called by the do_action_deprecated() and apply_filters_deprecated()
4049
 * functions, and so generally does not need to be called directly.
4050
 *
4051
 * @since 4.6.0
4052
 * @access private
4053
 *
4054
 * @param string $hook        The hook that was used.
4055
 * @param string $version     The version of WordPress that deprecated the hook.
4056
 * @param string $replacement Optional. The hook that should have been used.
4057
 * @param string $message     Optional. A message regarding the change.
4058
 */
4059
function _deprecated_hook( $hook, $version, $replacement = null, $message = null ) {
4060
	/**
4061
	 * Fires when a deprecated hook is called.
4062
	 *
4063
	 * @since 4.6.0
4064
	 *
4065
	 * @param string $hook        The hook that was called.
4066
	 * @param string $replacement The hook that should be used as a replacement.
4067
	 * @param string $version     The version of WordPress that deprecated the argument used.
4068
	 * @param string $message     A message regarding the change.
4069
	 */
4070
	do_action( 'deprecated_hook_run', $hook, $replacement, $version, $message );
4071
4072
	/**
4073
	 * Filters whether to trigger deprecated hook errors.
4074
	 *
4075
	 * @since 4.6.0
4076
	 *
4077
	 * @param bool $trigger Whether to trigger deprecated hook errors. Requires
4078
	 *                      `WP_DEBUG` to be defined true.
4079
	 */
4080
	if ( WP_DEBUG && apply_filters( 'deprecated_hook_trigger_error', true ) ) {
4081
		$message = empty( $message ) ? '' : ' ' . $message;
4082
		if ( ! is_null( $replacement ) ) {
4083
			/* translators: 1: WordPress hook name, 2: version number, 3: alternative hook name */
4084
			trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), $hook, $version, $replacement ) . $message );
4085
		} else {
4086
			/* translators: 1: WordPress hook name, 2: version number */
4087
			trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $hook, $version ) . $message );
4088
		}
4089
	}
4090
}
4091
4092
/**
4093
 * Mark something as being incorrectly called.
4094
 *
4095
 * There is a hook {@see 'doing_it_wrong_run'} that will be called that can be used
4096
 * to get the backtrace up to what file and function called the deprecated
4097
 * function.
4098
 *
4099
 * The current behavior is to trigger a user error if `WP_DEBUG` is true.
4100
 *
4101
 * @since 3.1.0
4102
 * @access private
4103
 *
4104
 * @param string $function The function that was called.
4105
 * @param string $message  A message explaining what has been done incorrectly.
4106
 * @param string $version  The version of WordPress where the message was added.
4107
 */
4108
function _doing_it_wrong( $function, $message, $version ) {
4109
4110
	/**
4111
	 * Fires when the given function is being used incorrectly.
4112
	 *
4113
	 * @since 3.1.0
4114
	 *
4115
	 * @param string $function The function that was called.
4116
	 * @param string $message  A message explaining what has been done incorrectly.
4117
	 * @param string $version  The version of WordPress where the message was added.
4118
	 */
4119
	do_action( 'doing_it_wrong_run', $function, $message, $version );
4120
4121
	/**
4122
	 * Filters whether to trigger an error for _doing_it_wrong() calls.
4123
	 *
4124
	 * @since 3.1.0
4125
	 *
4126
	 * @param bool $trigger Whether to trigger the error for _doing_it_wrong() calls. Default true.
4127
	 */
4128
	if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
4129
		if ( function_exists( '__' ) ) {
4130
			if ( is_null( $version ) ) {
4131
				$version = '';
4132
			} else {
4133
				/* translators: %s: version number */
4134
				$version = sprintf( __( '(This message was added in version %s.)' ), $version );
4135
			}
4136
			/* translators: %s: Codex URL */
4137
			$message .= ' ' . sprintf( __( 'Please see <a href="%s">Debugging in WordPress</a> for more information.' ),
4138
				__( 'https://codex.wordpress.org/Debugging_in_WordPress' )
4139
			);
4140
			/* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message, 3: Version information message */
4141
			trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) );
4142
		} else {
4143
			if ( is_null( $version ) ) {
4144
				$version = '';
4145
			} else {
4146
				$version = sprintf( '(This message was added in version %s.)', $version );
4147
			}
4148
			$message .= sprintf( ' Please see <a href="%s">Debugging in WordPress</a> for more information.',
4149
				'https://codex.wordpress.org/Debugging_in_WordPress'
4150
			);
4151
			trigger_error( sprintf( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', $function, $message, $version ) );
4152
		}
4153
	}
4154
}
4155
4156
/**
4157
 * Is the server running earlier than 1.5.0 version of lighttpd?
4158
 *
4159
 * @since 2.5.0
4160
 *
4161
 * @return bool Whether the server is running lighttpd < 1.5.0.
4162
 */
4163
function is_lighttpd_before_150() {
4164
	$server_parts = explode( '/', isset( $_SERVER['SERVER_SOFTWARE'] )? $_SERVER['SERVER_SOFTWARE'] : '' );
4165
	$server_parts[1] = isset( $server_parts[1] )? $server_parts[1] : '';
4166
	return  'lighttpd' == $server_parts[0] && -1 == version_compare( $server_parts[1], '1.5.0' );
4167
}
4168
4169
/**
4170
 * Does the specified module exist in the Apache config?
4171
 *
4172
 * @since 2.5.0
4173
 *
4174
 * @global bool $is_apache
4175
 *
4176
 * @param string $mod     The module, e.g. mod_rewrite.
4177
 * @param bool   $default Optional. The default return value if the module is not found. Default false.
4178
 * @return bool Whether the specified module is loaded.
4179
 */
4180
function apache_mod_loaded($mod, $default = false) {
4181
	global $is_apache;
4182
4183
	if ( !$is_apache )
4184
		return false;
4185
4186
	if ( function_exists( 'apache_get_modules' ) ) {
4187
		$mods = apache_get_modules();
4188
		if ( in_array($mod, $mods) )
4189
			return true;
4190
	} elseif ( function_exists( 'phpinfo' ) && false === strpos( ini_get( 'disable_functions' ), 'phpinfo' ) ) {
4191
			ob_start();
4192
			phpinfo(8);
4193
			$phpinfo = ob_get_clean();
4194
			if ( false !== strpos($phpinfo, $mod) )
4195
				return true;
4196
	}
4197
	return $default;
4198
}
4199
4200
/**
4201
 * Check if IIS 7+ supports pretty permalinks.
4202
 *
4203
 * @since 2.8.0
4204
 *
4205
 * @global bool $is_iis7
4206
 *
4207
 * @return bool Whether IIS7 supports permalinks.
4208
 */
4209
function iis7_supports_permalinks() {
4210
	global $is_iis7;
4211
4212
	$supports_permalinks = false;
4213
	if ( $is_iis7 ) {
4214
		/* First we check if the DOMDocument class exists. If it does not exist, then we cannot
4215
		 * easily update the xml configuration file, hence we just bail out and tell user that
4216
		 * pretty permalinks cannot be used.
4217
		 *
4218
		 * Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When
4219
		 * URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'.
4220
		 * Lastly we make sure that PHP is running via FastCGI. This is important because if it runs
4221
		 * via ISAPI then pretty permalinks will not work.
4222
		 */
4223
		$supports_permalinks = class_exists( 'DOMDocument', false ) && isset($_SERVER['IIS_UrlRewriteModule']) && ( PHP_SAPI == 'cgi-fcgi' );
4224
	}
4225
4226
	/**
4227
	 * Filters whether IIS 7+ supports pretty permalinks.
4228
	 *
4229
	 * @since 2.8.0
4230
	 *
4231
	 * @param bool $supports_permalinks Whether IIS7 supports permalinks. Default false.
4232
	 */
4233
	return apply_filters( 'iis7_supports_permalinks', $supports_permalinks );
4234
}
4235
4236
/**
4237
 * File validates against allowed set of defined rules.
4238
 *
4239
 * A return value of '1' means that the $file contains either '..' or './'. A
4240
 * return value of '2' means that the $file contains ':' after the first
4241
 * character. A return value of '3' means that the file is not in the allowed
4242
 * files list.
4243
 *
4244
 * @since 1.2.0
4245
 *
4246
 * @param string $file File path.
4247
 * @param array  $allowed_files List of allowed files.
0 ignored issues
show
Should the type for parameter $allowed_files not be string|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
4248
 * @return int 0 means nothing is wrong, greater than 0 means something was wrong.
4249
 */
4250
function validate_file( $file, $allowed_files = '' ) {
4251
	if ( false !== strpos( $file, '..' ) )
4252
		return 1;
4253
4254
	if ( false !== strpos( $file, './' ) )
4255
		return 1;
4256
4257
	if ( ! empty( $allowed_files ) && ! in_array( $file, $allowed_files ) )
4258
		return 3;
4259
4260
	if (':' == substr( $file, 1, 1 ) )
4261
		return 2;
4262
4263
	return 0;
4264
}
4265
4266
/**
4267
 * Whether to force SSL used for the Administration Screens.
4268
 *
4269
 * @since 2.6.0
4270
 *
4271
 * @staticvar bool $forced
4272
 *
4273
 * @param string|bool $force Optional. Whether to force SSL in admin screens. Default null.
4274
 * @return bool True if forced, false if not forced.
4275
 */
4276
function force_ssl_admin( $force = null ) {
4277
	static $forced = false;
4278
4279
	if ( !is_null( $force ) ) {
4280
		$old_forced = $forced;
4281
		$forced = $force;
4282
		return $old_forced;
4283
	}
4284
4285
	return $forced;
4286
}
4287
4288
/**
4289
 * Guess the URL for the site.
4290
 *
4291
 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
4292
 * directory.
4293
 *
4294
 * @since 2.6.0
4295
 *
4296
 * @return string The guessed URL.
4297
 */
4298
function wp_guess_url() {
4299
	if ( defined('WP_SITEURL') && '' != WP_SITEURL ) {
4300
		$url = WP_SITEURL;
4301
	} else {
4302
		$abspath_fix = str_replace( '\\', '/', ABSPATH );
4303
		$script_filename_dir = dirname( $_SERVER['SCRIPT_FILENAME'] );
4304
4305
		// The request is for the admin
4306
		if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ) {
4307
			$path = preg_replace( '#/(wp-admin/.*|wp-login.php)#i', '', $_SERVER['REQUEST_URI'] );
4308
4309
		// The request is for a file in ABSPATH
4310
		} elseif ( $script_filename_dir . '/' == $abspath_fix ) {
4311
			// Strip off any file/query params in the path
4312
			$path = preg_replace( '#/[^/]*$#i', '', $_SERVER['PHP_SELF'] );
4313
4314
		} else {
4315
			if ( false !== strpos( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) {
4316
				// Request is hitting a file inside ABSPATH
4317
				$directory = str_replace( ABSPATH, '', $script_filename_dir );
4318
				// Strip off the sub directory, and any file/query params
4319
				$path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] );
4320
			} elseif ( false !== strpos( $abspath_fix, $script_filename_dir ) ) {
4321
				// Request is hitting a file above ABSPATH
4322
				$subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) );
4323
				// Strip off any file/query params from the path, appending the sub directory to the install
4324
				$path = preg_replace( '#/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] ) . $subdirectory;
4325
			} else {
4326
				$path = $_SERVER['REQUEST_URI'];
4327
			}
4328
		}
4329
4330
		$schema = is_ssl() ? 'https://' : 'http://'; // set_url_scheme() is not defined yet
4331
		$url = $schema . $_SERVER['HTTP_HOST'] . $path;
4332
	}
4333
4334
	return rtrim($url, '/');
4335
}
4336
4337
/**
4338
 * Temporarily suspend cache additions.
4339
 *
4340
 * Stops more data being added to the cache, but still allows cache retrieval.
4341
 * This is useful for actions, such as imports, when a lot of data would otherwise
4342
 * be almost uselessly added to the cache.
4343
 *
4344
 * Suspension lasts for a single page load at most. Remember to call this
4345
 * function again if you wish to re-enable cache adds earlier.
4346
 *
4347
 * @since 3.3.0
4348
 *
4349
 * @staticvar bool $_suspend
4350
 *
4351
 * @param bool $suspend Optional. Suspends additions if true, re-enables them if false.
4352
 * @return bool The current suspend setting
4353
 */
4354
function wp_suspend_cache_addition( $suspend = null ) {
4355
	static $_suspend = false;
4356
4357
	if ( is_bool( $suspend ) )
4358
		$_suspend = $suspend;
4359
4360
	return $_suspend;
4361
}
4362
4363
/**
4364
 * Suspend cache invalidation.
4365
 *
4366
 * Turns cache invalidation on and off. Useful during imports where you don't wont to do
4367
 * invalidations every time a post is inserted. Callers must be sure that what they are
4368
 * doing won't lead to an inconsistent cache when invalidation is suspended.
4369
 *
4370
 * @since 2.7.0
4371
 *
4372
 * @global bool $_wp_suspend_cache_invalidation
4373
 *
4374
 * @param bool $suspend Optional. Whether to suspend or enable cache invalidation. Default true.
4375
 * @return bool The current suspend setting.
4376
 */
4377
function wp_suspend_cache_invalidation( $suspend = true ) {
4378
	global $_wp_suspend_cache_invalidation;
4379
4380
	$current_suspend = $_wp_suspend_cache_invalidation;
4381
	$_wp_suspend_cache_invalidation = $suspend;
4382
	return $current_suspend;
4383
}
4384
4385
/**
4386
 * Determine whether a site is the main site of the current network.
4387
 *
4388
 * @since 3.0.0
4389
 *
4390
 * @param int $site_id Optional. Site ID to test. Defaults to current site.
4391
 * @return bool True if $site_id is the main site of the network, or if not
4392
 *              running Multisite.
4393
 */
4394
function is_main_site( $site_id = null ) {
4395
	if ( ! is_multisite() )
4396
		return true;
4397
4398
	if ( ! $site_id )
0 ignored issues
show
Bug Best Practice introduced by
The expression $site_id of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
4399
		$site_id = get_current_blog_id();
4400
4401
	return (int) $site_id === (int) get_network()->site_id;
4402
}
4403
4404
/**
4405
 * Determine whether a network is the main network of the Multisite install.
4406
 *
4407
 * @since 3.7.0
4408
 *
4409
 * @param int $network_id Optional. Network ID to test. Defaults to current network.
4410
 * @return bool True if $network_id is the main network, or if not running Multisite.
4411
 */
4412
function is_main_network( $network_id = null ) {
4413
	if ( ! is_multisite() ) {
4414
		return true;
4415
	}
4416
4417
	if ( null === $network_id ) {
4418
		$network_id = get_current_network_id();
4419
	}
4420
4421
	$network_id = (int) $network_id;
4422
4423
	return ( $network_id === get_main_network_id() );
4424
}
4425
4426
/**
4427
 * Get the main network ID.
4428
 *
4429
 * @since 4.3.0
4430
 *
4431
 * @return int The ID of the main network.
4432
 */
4433
function get_main_network_id() {
4434
	if ( ! is_multisite() ) {
4435
		return 1;
4436
	}
4437
4438
	$current_network = get_network();
4439
4440
	if ( defined( 'PRIMARY_NETWORK_ID' ) ) {
4441
		$main_network_id = PRIMARY_NETWORK_ID;
4442
	} elseif ( isset( $current_network->id ) && 1 === (int) $current_network->id ) {
4443
		// If the current network has an ID of 1, assume it is the main network.
4444
		$main_network_id = 1;
4445
	} else {
4446
		$_networks = get_networks( array( 'fields' => 'ids', 'number' => 1 ) );
4447
		$main_network_id = array_shift( $_networks );
4448
	}
4449
4450
	/**
4451
	 * Filters the main network ID.
4452
	 *
4453
	 * @since 4.3.0
4454
	 *
4455
	 * @param int $main_network_id The ID of the main network.
4456
	 */
4457
	return (int) apply_filters( 'get_main_network_id', $main_network_id );
4458
}
4459
4460
/**
4461
 * Determine whether global terms are enabled.
4462
 *
4463
 * @since 3.0.0
4464
 *
4465
 * @staticvar bool $global_terms
4466
 *
4467
 * @return bool True if multisite and global terms enabled.
4468
 */
4469
function global_terms_enabled() {
4470
	if ( ! is_multisite() )
4471
		return false;
4472
4473
	static $global_terms = null;
4474
	if ( is_null( $global_terms ) ) {
4475
4476
		/**
4477
		 * Filters whether global terms are enabled.
4478
		 *
4479
		 * Passing a non-null value to the filter will effectively short-circuit the function,
4480
		 * returning the value of the 'global_terms_enabled' site option instead.
4481
		 *
4482
		 * @since 3.0.0
4483
		 *
4484
		 * @param null $enabled Whether global terms are enabled.
4485
		 */
4486
		$filter = apply_filters( 'global_terms_enabled', null );
4487
		if ( ! is_null( $filter ) )
4488
			$global_terms = (bool) $filter;
4489
		else
4490
			$global_terms = (bool) get_site_option( 'global_terms_enabled', false );
4491
	}
4492
	return $global_terms;
4493
}
4494
4495
/**
4496
 * gmt_offset modification for smart timezone handling.
4497
 *
4498
 * Overrides the gmt_offset option if we have a timezone_string available.
4499
 *
4500
 * @since 2.8.0
4501
 *
4502
 * @return float|false Timezone GMT offset, false otherwise.
4503
 */
4504
function wp_timezone_override_offset() {
4505
	if ( !$timezone_string = get_option( 'timezone_string' ) ) {
4506
		return false;
4507
	}
4508
4509
	$timezone_object = timezone_open( $timezone_string );
4510
	$datetime_object = date_create();
4511
	if ( false === $timezone_object || false === $datetime_object ) {
4512
		return false;
4513
	}
4514
	return round( timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS, 2 );
4515
}
4516
4517
/**
4518
 * Sort-helper for timezones.
4519
 *
4520
 * @since 2.9.0
4521
 * @access private
4522
 *
4523
 * @param array $a
4524
 * @param array $b
4525
 * @return int
4526
 */
4527
function _wp_timezone_choice_usort_callback( $a, $b ) {
4528
	// Don't use translated versions of Etc
4529
	if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) {
4530
		// Make the order of these more like the old dropdown
4531
		if ( 'GMT+' === substr( $a['city'], 0, 4 ) && 'GMT+' === substr( $b['city'], 0, 4 ) ) {
4532
			return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) );
4533
		}
4534 View Code Duplication
		if ( 'UTC' === $a['city'] ) {
4535
			if ( 'GMT+' === substr( $b['city'], 0, 4 ) ) {
4536
				return 1;
4537
			}
4538
			return -1;
4539
		}
4540 View Code Duplication
		if ( 'UTC' === $b['city'] ) {
4541
			if ( 'GMT+' === substr( $a['city'], 0, 4 ) ) {
4542
				return -1;
4543
			}
4544
			return 1;
4545
		}
4546
		return strnatcasecmp( $a['city'], $b['city'] );
4547
	}
4548
	if ( $a['t_continent'] == $b['t_continent'] ) {
4549
		if ( $a['t_city'] == $b['t_city'] ) {
4550
			return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] );
4551
		}
4552
		return strnatcasecmp( $a['t_city'], $b['t_city'] );
4553
	} else {
4554
		// Force Etc to the bottom of the list
4555
		if ( 'Etc' === $a['continent'] ) {
4556
			return 1;
4557
		}
4558
		if ( 'Etc' === $b['continent'] ) {
4559
			return -1;
4560
		}
4561
		return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
4562
	}
4563
}
4564
4565
/**
4566
 * Gives a nicely-formatted list of timezone strings.
4567
 *
4568
 * @since 2.9.0
4569
 * @since 4.7.0 Added the `$locale` parameter.
4570
 *
4571
 * @staticvar bool $mo_loaded
4572
 * @staticvar string $locale_loaded
4573
 *
4574
 * @param string $selected_zone Selected timezone.
4575
 * @param string $locale        Optional. Locale to load the timezones in. Default current site locale.
4576
 * @return string
4577
 */
4578
function wp_timezone_choice( $selected_zone, $locale = null ) {
4579
	static $mo_loaded = false, $locale_loaded = null;
4580
4581
	$continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
4582
4583
	// Load translations for continents and cities.
4584
	if ( ! $mo_loaded || $locale !== $locale_loaded ) {
4585
		$locale_loaded = $locale ? $locale : get_locale();
4586
		$mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo';
4587
		unload_textdomain( 'continents-cities' );
4588
		load_textdomain( 'continents-cities', $mofile );
4589
		$mo_loaded = true;
4590
	}
4591
4592
	$zonen = array();
4593
	foreach ( timezone_identifiers_list() as $zone ) {
4594
		$zone = explode( '/', $zone );
4595
		if ( !in_array( $zone[0], $continents ) ) {
4596
			continue;
4597
		}
4598
4599
		// This determines what gets set and translated - we don't translate Etc/* strings here, they are done later
4600
		$exists = array(
4601
			0 => ( isset( $zone[0] ) && $zone[0] ),
4602
			1 => ( isset( $zone[1] ) && $zone[1] ),
4603
			2 => ( isset( $zone[2] ) && $zone[2] ),
4604
		);
4605
		$exists[3] = ( $exists[0] && 'Etc' !== $zone[0] );
4606
		$exists[4] = ( $exists[1] && $exists[3] );
4607
		$exists[5] = ( $exists[2] && $exists[3] );
4608
4609
		$zonen[] = array(
4610
			'continent'   => ( $exists[0] ? $zone[0] : '' ),
4611
			'city'        => ( $exists[1] ? $zone[1] : '' ),
4612
			'subcity'     => ( $exists[2] ? $zone[2] : '' ),
4613
			't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ),
4614
			't_city'      => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ),
4615
			't_subcity'   => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' )
4616
		);
4617
	}
4618
	usort( $zonen, '_wp_timezone_choice_usort_callback' );
4619
4620
	$structure = array();
4621
4622
	if ( empty( $selected_zone ) ) {
4623
		$structure[] = '<option selected="selected" value="">' . __( 'Select a city' ) . '</option>';
4624
	}
4625
4626
	foreach ( $zonen as $key => $zone ) {
4627
		// Build value in an array to join later
4628
		$value = array( $zone['continent'] );
4629
4630
		if ( empty( $zone['city'] ) ) {
4631
			// It's at the continent level (generally won't happen)
4632
			$display = $zone['t_continent'];
4633
		} else {
4634
			// It's inside a continent group
4635
4636
			// Continent optgroup
4637
			if ( !isset( $zonen[$key - 1] ) || $zonen[$key - 1]['continent'] !== $zone['continent'] ) {
4638
				$label = $zone['t_continent'];
4639
				$structure[] = '<optgroup label="'. esc_attr( $label ) .'">';
4640
			}
4641
4642
			// Add the city to the value
4643
			$value[] = $zone['city'];
4644
4645
			$display = $zone['t_city'];
4646
			if ( !empty( $zone['subcity'] ) ) {
4647
				// Add the subcity to the value
4648
				$value[] = $zone['subcity'];
4649
				$display .= ' - ' . $zone['t_subcity'];
4650
			}
4651
		}
4652
4653
		// Build the value
4654
		$value = join( '/', $value );
4655
		$selected = '';
4656
		if ( $value === $selected_zone ) {
4657
			$selected = 'selected="selected" ';
4658
		}
4659
		$structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $display ) . "</option>";
4660
4661
		// Close continent optgroup
4662
		if ( !empty( $zone['city'] ) && ( !isset($zonen[$key + 1]) || (isset( $zonen[$key + 1] ) && $zonen[$key + 1]['continent'] !== $zone['continent']) ) ) {
4663
			$structure[] = '</optgroup>';
4664
		}
4665
	}
4666
4667
	// Do UTC
4668
	$structure[] = '<optgroup label="'. esc_attr__( 'UTC' ) .'">';
4669
	$selected = '';
4670
	if ( 'UTC' === $selected_zone )
4671
		$selected = 'selected="selected" ';
4672
	$structure[] = '<option ' . $selected . 'value="' . esc_attr( 'UTC' ) . '">' . __('UTC') . '</option>';
4673
	$structure[] = '</optgroup>';
4674
4675
	// Do manual UTC offsets
4676
	$structure[] = '<optgroup label="'. esc_attr__( 'Manual Offsets' ) .'">';
4677
	$offset_range = array (-12, -11.5, -11, -10.5, -10, -9.5, -9, -8.5, -8, -7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5,
4678
		0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 5.75, 6, 6.5, 7, 7.5, 8, 8.5, 8.75, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 13.75, 14);
4679
	foreach ( $offset_range as $offset ) {
4680
		if ( 0 <= $offset )
4681
			$offset_name = '+' . $offset;
4682
		else
4683
			$offset_name = (string) $offset;
4684
4685
		$offset_value = $offset_name;
4686
		$offset_name = str_replace(array('.25','.5','.75'), array(':15',':30',':45'), $offset_name);
4687
		$offset_name = 'UTC' . $offset_name;
4688
		$offset_value = 'UTC' . $offset_value;
4689
		$selected = '';
4690
		if ( $offset_value === $selected_zone )
4691
			$selected = 'selected="selected" ';
4692
		$structure[] = '<option ' . $selected . 'value="' . esc_attr( $offset_value ) . '">' . esc_html( $offset_name ) . "</option>";
4693
4694
	}
4695
	$structure[] = '</optgroup>';
4696
4697
	return join( "\n", $structure );
4698
}
4699
4700
/**
4701
 * Strip close comment and close php tags from file headers used by WP.
4702
 *
4703
 * @since 2.8.0
4704
 * @access private
4705
 *
4706
 * @see https://core.trac.wordpress.org/ticket/8497
4707
 *
4708
 * @param string $str Header comment to clean up.
4709
 * @return string
4710
 */
4711
function _cleanup_header_comment( $str ) {
4712
	return trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $str));
4713
}
4714
4715
/**
4716
 * Permanently delete comments or posts of any type that have held a status
4717
 * of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
4718
 *
4719
 * The default value of `EMPTY_TRASH_DAYS` is 30 (days).
4720
 *
4721
 * @since 2.9.0
4722
 *
4723
 * @global wpdb $wpdb WordPress database abstraction object.
4724
 */
4725
function wp_scheduled_delete() {
4726
	global $wpdb;
4727
4728
	$delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
4729
4730
	$posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
4731
4732
	foreach ( (array) $posts_to_delete as $post ) {
4733
		$post_id = (int) $post['post_id'];
4734
		if ( !$post_id )
4735
			continue;
4736
4737
		$del_post = get_post($post_id);
4738
4739
		if ( !$del_post || 'trash' != $del_post->post_status ) {
4740
			delete_post_meta($post_id, '_wp_trash_meta_status');
4741
			delete_post_meta($post_id, '_wp_trash_meta_time');
4742
		} else {
4743
			wp_delete_post($post_id);
4744
		}
4745
	}
4746
4747
	$comments_to_delete = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
4748
4749
	foreach ( (array) $comments_to_delete as $comment ) {
4750
		$comment_id = (int) $comment['comment_id'];
4751
		if ( !$comment_id )
4752
			continue;
4753
4754
		$del_comment = get_comment($comment_id);
4755
4756
		if ( !$del_comment || 'trash' != $del_comment->comment_approved ) {
4757
			delete_comment_meta($comment_id, '_wp_trash_meta_time');
4758
			delete_comment_meta($comment_id, '_wp_trash_meta_status');
4759
		} else {
4760
			wp_delete_comment( $del_comment );
4761
		}
4762
	}
4763
}
4764
4765
/**
4766
 * Retrieve metadata from a file.
4767
 *
4768
 * Searches for metadata in the first 8kiB of a file, such as a plugin or theme.
4769
 * Each piece of metadata must be on its own line. Fields can not span multiple
4770
 * lines, the value will get cut at the end of the first line.
4771
 *
4772
 * If the file data is not within that first 8kiB, then the author should correct
4773
 * their plugin file and move the data headers to the top.
4774
 *
4775
 * @link https://codex.wordpress.org/File_Header
4776
 *
4777
 * @since 2.9.0
4778
 *
4779
 * @param string $file            Path to the file.
4780
 * @param array  $default_headers List of headers, in the format array('HeaderKey' => 'Header Name').
4781
 * @param string $context         Optional. If specified adds filter hook {@see 'extra_$context_headers'}.
4782
 *                                Default empty.
4783
 * @return array Array of file headers in `HeaderKey => Header Value` format.
4784
 */
4785
function get_file_data( $file, $default_headers, $context = '' ) {
4786
	// We don't need to write to the file, so just open for reading.
4787
	$fp = fopen( $file, 'r' );
4788
4789
	// Pull only the first 8kiB of the file in.
4790
	$file_data = fread( $fp, 8192 );
4791
4792
	// PHP will close file handle, but we are good citizens.
4793
	fclose( $fp );
4794
4795
	// Make sure we catch CR-only line endings.
4796
	$file_data = str_replace( "\r", "\n", $file_data );
4797
4798
	/**
4799
	 * Filters extra file headers by context.
4800
	 *
4801
	 * The dynamic portion of the hook name, `$context`, refers to
4802
	 * the context where extra headers might be loaded.
4803
	 *
4804
	 * @since 2.9.0
4805
	 *
4806
	 * @param array $extra_context_headers Empty array by default.
4807
	 */
4808
	if ( $context && $extra_headers = apply_filters( "extra_{$context}_headers", array() ) ) {
4809
		$extra_headers = array_combine( $extra_headers, $extra_headers ); // keys equal values
4810
		$all_headers = array_merge( $extra_headers, (array) $default_headers );
4811
	} else {
4812
		$all_headers = $default_headers;
4813
	}
4814
4815
	foreach ( $all_headers as $field => $regex ) {
4816
		if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] )
4817
			$all_headers[ $field ] = _cleanup_header_comment( $match[1] );
4818
		else
4819
			$all_headers[ $field ] = '';
4820
	}
4821
4822
	return $all_headers;
4823
}
4824
4825
/**
4826
 * Returns true.
4827
 *
4828
 * Useful for returning true to filters easily.
4829
 *
4830
 * @since 3.0.0
4831
 *
4832
 * @see __return_false()
4833
 *
4834
 * @return true True.
0 ignored issues
show
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
4835
 */
4836
function __return_true() {
4837
	return true;
4838
}
4839
4840
/**
4841
 * Returns false.
4842
 *
4843
 * Useful for returning false to filters easily.
4844
 *
4845
 * @since 3.0.0
4846
 *
4847
 * @see __return_true()
4848
 *
4849
 * @return false False.
0 ignored issues
show
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
4850
 */
4851
function __return_false() {
4852
	return false;
4853
}
4854
4855
/**
4856
 * Returns 0.
4857
 *
4858
 * Useful for returning 0 to filters easily.
4859
 *
4860
 * @since 3.0.0
4861
 *
4862
 * @return int 0.
4863
 */
4864
function __return_zero() {
4865
	return 0;
4866
}
4867
4868
/**
4869
 * Returns an empty array.
4870
 *
4871
 * Useful for returning an empty array to filters easily.
4872
 *
4873
 * @since 3.0.0
4874
 *
4875
 * @return array Empty array.
4876
 */
4877
function __return_empty_array() {
4878
	return array();
4879
}
4880
4881
/**
4882
 * Returns null.
4883
 *
4884
 * Useful for returning null to filters easily.
4885
 *
4886
 * @since 3.4.0
4887
 *
4888
 * @return null Null value.
4889
 */
4890
function __return_null() {
4891
	return null;
4892
}
4893
4894
/**
4895
 * Returns an empty string.
4896
 *
4897
 * Useful for returning an empty string to filters easily.
4898
 *
4899
 * @since 3.7.0
4900
 *
4901
 * @see __return_null()
4902
 *
4903
 * @return string Empty string.
4904
 */
4905
function __return_empty_string() {
4906
	return '';
4907
}
4908
4909
/**
4910
 * Send a HTTP header to disable content type sniffing in browsers which support it.
4911
 *
4912
 * @since 3.0.0
4913
 *
4914
 * @see https://blogs.msdn.com/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx
4915
 * @see https://src.chromium.org/viewvc/chrome?view=rev&revision=6985
4916
 */
4917
function send_nosniff_header() {
4918
	@header( 'X-Content-Type-Options: nosniff' );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
4919
}
4920
4921
/**
4922
 * Return a MySQL expression for selecting the week number based on the start_of_week option.
4923
 *
4924
 * @ignore
4925
 * @since 3.0.0
4926
 *
4927
 * @param string $column Database column.
4928
 * @return string SQL clause.
4929
 */
4930
function _wp_mysql_week( $column ) {
4931
	switch ( $start_of_week = (int) get_option( 'start_of_week' ) ) {
4932
	case 1 :
4933
		return "WEEK( $column, 1 )";
4934
	case 2 :
4935
	case 3 :
4936
	case 4 :
4937
	case 5 :
4938
	case 6 :
4939
		return "WEEK( DATE_SUB( $column, INTERVAL $start_of_week DAY ), 0 )";
4940
	case 0 :
4941
	default :
4942
		return "WEEK( $column, 0 )";
4943
	}
4944
}
4945
4946
/**
4947
 * Find hierarchy loops using a callback function that maps object IDs to parent IDs.
4948
 *
4949
 * @since 3.1.0
4950
 * @access private
4951
 *
4952
 * @param callable $callback      Function that accepts ( ID, $callback_args ) and outputs parent_ID.
4953
 * @param int      $start         The ID to start the loop check at.
4954
 * @param int      $start_parent  The parent_ID of $start to use instead of calling $callback( $start ).
4955
 *                                Use null to always use $callback
4956
 * @param array    $callback_args Optional. Additional arguments to send to $callback.
4957
 * @return array IDs of all members of loop.
4958
 */
4959
function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) {
4960
	$override = is_null( $start_parent ) ? array() : array( $start => $start_parent );
4961
4962
	if ( !$arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ) )
4963
		return array();
4964
4965
	return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true );
4966
}
4967
4968
/**
4969
 * Use the "The Tortoise and the Hare" algorithm to detect loops.
4970
 *
4971
 * For every step of the algorithm, the hare takes two steps and the tortoise one.
4972
 * If the hare ever laps the tortoise, there must be a loop.
4973
 *
4974
 * @since 3.1.0
4975
 * @access private
4976
 *
4977
 * @param callable $callback      Function that accepts ( ID, callback_arg, ... ) and outputs parent_ID.
4978
 * @param int      $start         The ID to start the loop check at.
4979
 * @param array    $override      Optional. An array of ( ID => parent_ID, ... ) to use instead of $callback.
4980
 *                                Default empty array.
4981
 * @param array    $callback_args Optional. Additional arguments to send to $callback. Default empty array.
4982
 * @param bool     $_return_loop  Optional. Return loop members or just detect presence of loop? Only set
4983
 *                                to true if you already know the given $start is part of a loop (otherwise
4984
 *                                the returned array might include branches). Default false.
4985
 * @return mixed Scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if
4986
 *               $_return_loop
4987
 */
4988
function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
4989
	$tortoise = $hare = $evanescent_hare = $start;
4990
	$return = array();
4991
4992
	// Set evanescent_hare to one past hare
4993
	// Increment hare two steps
4994
	while (
4995
		$tortoise
4996
	&&
4997
		( $evanescent_hare = isset( $override[$hare] ) ? $override[$hare] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
4998
	&&
4999
		( $hare = isset( $override[$evanescent_hare] ) ? $override[$evanescent_hare] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) )
5000
	) {
5001
		if ( $_return_loop )
5002
			$return[$tortoise] = $return[$evanescent_hare] = $return[$hare] = true;
5003
5004
		// tortoise got lapped - must be a loop
5005
		if ( $tortoise == $evanescent_hare || $tortoise == $hare )
5006
			return $_return_loop ? $return : $tortoise;
5007
5008
		// Increment tortoise by one step
5009
		$tortoise = isset( $override[$tortoise] ) ? $override[$tortoise] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
5010
	}
5011
5012
	return false;
5013
}
5014
5015
/**
5016
 * Send a HTTP header to limit rendering of pages to same origin iframes.
5017
 *
5018
 * @since 3.1.3
5019
 *
5020
 * @see https://developer.mozilla.org/en/the_x-frame-options_response_header
5021
 */
5022
function send_frame_options_header() {
5023
	@header( 'X-Frame-Options: SAMEORIGIN' );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
5024
}
5025
5026
/**
5027
 * Retrieve a list of protocols to allow in HTML attributes.
5028
 *
5029
 * @since 3.3.0
5030
 * @since 4.3.0 Added 'webcal' to the protocols array.
5031
 * @since 4.7.0 Added 'urn' to the protocols array.
5032
 *
5033
 * @see wp_kses()
5034
 * @see esc_url()
5035
 *
5036
 * @staticvar array $protocols
5037
 *
5038
 * @return array Array of allowed protocols. Defaults to an array containing 'http', 'https',
5039
 *               'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet',
5040
 *               'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal', and 'urn'.
5041
 */
5042
function wp_allowed_protocols() {
5043
	static $protocols = array();
5044
5045
	if ( empty( $protocols ) ) {
5046
		$protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal', 'urn' );
5047
5048
		/**
5049
		 * Filters the list of protocols allowed in HTML attributes.
5050
		 *
5051
		 * @since 3.0.0
5052
		 *
5053
		 * @param array $protocols Array of allowed protocols e.g. 'http', 'ftp', 'tel', and more.
5054
		 */
5055
		$protocols = apply_filters( 'kses_allowed_protocols', $protocols );
5056
	}
5057
5058
	return $protocols;
5059
}
5060
5061
/**
5062
 * Return a comma-separated string of functions that have been called to get
5063
 * to the current point in code.
5064
 *
5065
 * @since 3.4.0
5066
 *
5067
 * @see https://core.trac.wordpress.org/ticket/19589
5068
 *
5069
 * @param string $ignore_class Optional. A class to ignore all function calls within - useful
5070
 *                             when you want to just give info about the callee. Default null.
5071
 * @param int    $skip_frames  Optional. A number of stack frames to skip - useful for unwinding
5072
 *                             back to the source of the issue. Default 0.
5073
 * @param bool   $pretty       Optional. Whether or not you want a comma separated string or raw
5074
 *                             array returned. Default true.
5075
 * @return string|array Either a string containing a reversed comma separated trace or an array
5076
 *                      of individual calls.
5077
 */
5078
function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pretty = true ) {
5079
	if ( version_compare( PHP_VERSION, '5.2.5', '>=' ) )
5080
		$trace = debug_backtrace( false );
5081
	else
5082
		$trace = debug_backtrace();
5083
5084
	$caller = array();
5085
	$check_class = ! is_null( $ignore_class );
5086
	$skip_frames++; // skip this function
5087
5088
	foreach ( $trace as $call ) {
5089
		if ( $skip_frames > 0 ) {
5090
			$skip_frames--;
5091
		} elseif ( isset( $call['class'] ) ) {
5092
			if ( $check_class && $ignore_class == $call['class'] )
5093
				continue; // Filter out calls
5094
5095
			$caller[] = "{$call['class']}{$call['type']}{$call['function']}";
5096
		} else {
5097
			if ( in_array( $call['function'], array( 'do_action', 'apply_filters' ) ) ) {
5098
				$caller[] = "{$call['function']}('{$call['args'][0]}')";
5099
			} elseif ( in_array( $call['function'], array( 'include', 'include_once', 'require', 'require_once' ) ) ) {
5100
				$caller[] = $call['function'] . "('" . str_replace( array( WP_CONTENT_DIR, ABSPATH ) , '', $call['args'][0] ) . "')";
5101
			} else {
5102
				$caller[] = $call['function'];
5103
			}
5104
		}
5105
	}
5106
	if ( $pretty )
5107
		return join( ', ', array_reverse( $caller ) );
5108
	else
5109
		return $caller;
5110
}
5111
5112
/**
5113
 * Retrieve ids that are not already present in the cache.
5114
 *
5115
 * @since 3.4.0
5116
 * @access private
5117
 *
5118
 * @param array  $object_ids ID list.
5119
 * @param string $cache_key  The cache bucket to check against.
5120
 *
5121
 * @return array List of ids not present in the cache.
5122
 */
5123
function _get_non_cached_ids( $object_ids, $cache_key ) {
5124
	$clean = array();
5125
	foreach ( $object_ids as $id ) {
5126
		$id = (int) $id;
5127
		if ( !wp_cache_get( $id, $cache_key ) ) {
5128
			$clean[] = $id;
5129
		}
5130
	}
5131
5132
	return $clean;
5133
}
5134
5135
/**
5136
 * Test if the current device has the capability to upload files.
5137
 *
5138
 * @since 3.4.0
5139
 * @access private
5140
 *
5141
 * @return bool Whether the device is able to upload files.
5142
 */
5143
function _device_can_upload() {
5144
	if ( ! wp_is_mobile() )
5145
		return true;
5146
5147
	$ua = $_SERVER['HTTP_USER_AGENT'];
5148
5149
	if ( strpos($ua, 'iPhone') !== false
5150
		|| strpos($ua, 'iPad') !== false
5151
		|| strpos($ua, 'iPod') !== false ) {
5152
			return preg_match( '#OS ([\d_]+) like Mac OS X#', $ua, $version ) && version_compare( $version[1], '6', '>=' );
5153
	}
5154
5155
	return true;
5156
}
5157
5158
/**
5159
 * Test if a given path is a stream URL
5160
 *
5161
 * @since 3.5.0
5162
 *
5163
 * @param string $path The resource path or URL.
5164
 * @return bool True if the path is a stream URL.
5165
 */
5166
function wp_is_stream( $path ) {
5167
	$wrappers = stream_get_wrappers();
5168
	$wrappers_re = '(' . join('|', $wrappers) . ')';
5169
5170
	return preg_match( "!^$wrappers_re://!", $path ) === 1;
5171
}
5172
5173
/**
5174
 * Test if the supplied date is valid for the Gregorian calendar.
5175
 *
5176
 * @since 3.5.0
5177
 *
5178
 * @see checkdate()
5179
 *
5180
 * @param  int    $month       Month number.
5181
 * @param  int    $day         Day number.
5182
 * @param  int    $year        Year number.
5183
 * @param  string $source_date The date to filter.
5184
 * @return bool True if valid date, false if not valid date.
5185
 */
5186
function wp_checkdate( $month, $day, $year, $source_date ) {
5187
	/**
5188
	 * Filters whether the given date is valid for the Gregorian calendar.
5189
	 *
5190
	 * @since 3.5.0
5191
	 *
5192
	 * @param bool   $checkdate   Whether the given date is valid.
5193
	 * @param string $source_date Date to check.
5194
	 */
5195
	return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date );
5196
}
5197
5198
/**
5199
 * Load the auth check for monitoring whether the user is still logged in.
5200
 *
5201
 * Can be disabled with remove_action( 'admin_enqueue_scripts', 'wp_auth_check_load' );
5202
 *
5203
 * This is disabled for certain screens where a login screen could cause an
5204
 * inconvenient interruption. A filter called {@see 'wp_auth_check_load'} can be used
5205
 * for fine-grained control.
5206
 *
5207
 * @since 3.6.0
5208
 */
5209
function wp_auth_check_load() {
5210
	if ( ! is_admin() && ! is_user_logged_in() )
5211
		return;
5212
5213
	if ( defined( 'IFRAME_REQUEST' ) )
5214
		return;
5215
5216
	$screen = get_current_screen();
5217
	$hidden = array( 'update', 'update-network', 'update-core', 'update-core-network', 'upgrade', 'upgrade-network', 'network' );
5218
	$show = ! in_array( $screen->id, $hidden );
5219
5220
	/**
5221
	 * Filters whether to load the authentication check.
5222
	 *
5223
	 * Passing a falsey value to the filter will effectively short-circuit
5224
	 * loading the authentication check.
5225
	 *
5226
	 * @since 3.6.0
5227
	 *
5228
	 * @param bool      $show   Whether to load the authentication check.
5229
	 * @param WP_Screen $screen The current screen object.
5230
	 */
5231
	if ( apply_filters( 'wp_auth_check_load', $show, $screen ) ) {
5232
		wp_enqueue_style( 'wp-auth-check' );
5233
		wp_enqueue_script( 'wp-auth-check' );
5234
5235
		add_action( 'admin_print_footer_scripts', 'wp_auth_check_html', 5 );
5236
		add_action( 'wp_print_footer_scripts', 'wp_auth_check_html', 5 );
5237
	}
5238
}
5239
5240
/**
5241
 * Output the HTML that shows the wp-login dialog when the user is no longer logged in.
5242
 *
5243
 * @since 3.6.0
5244
 */
5245
function wp_auth_check_html() {
5246
	$login_url = wp_login_url();
5247
	$current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
5248
	$same_domain = ( strpos( $login_url, $current_domain ) === 0 );
5249
5250
	/**
5251
	 * Filters whether the authentication check originated at the same domain.
5252
	 *
5253
	 * @since 3.6.0
5254
	 *
5255
	 * @param bool $same_domain Whether the authentication check originated at the same domain.
5256
	 */
5257
	$same_domain = apply_filters( 'wp_auth_check_same_domain', $same_domain );
5258
	$wrap_class = $same_domain ? 'hidden' : 'hidden fallback';
5259
5260
	?>
5261
	<div id="wp-auth-check-wrap" class="<?php echo $wrap_class; ?>">
5262
	<div id="wp-auth-check-bg"></div>
5263
	<div id="wp-auth-check">
5264
	<button type="button" class="wp-auth-check-close button-link"><span class="screen-reader-text"><?php _e( 'Close dialog' ); ?></span></button>
5265
	<?php
5266
5267
	if ( $same_domain ) {
5268
		?>
5269
		<div id="wp-auth-check-form" class="loading" data-src="<?php echo esc_url( add_query_arg( array( 'interim-login' => 1 ), $login_url ) ); ?>"></div>
5270
		<?php
5271
	}
5272
5273
	?>
5274
	<div class="wp-auth-fallback">
5275
		<p><b class="wp-auth-fallback-expired" tabindex="0"><?php _e('Session expired'); ?></b></p>
5276
		<p><a href="<?php echo esc_url( $login_url ); ?>" target="_blank"><?php _e('Please log in again.'); ?></a>
5277
		<?php _e('The login page will open in a new window. After logging in you can close it and return to this page.'); ?></p>
5278
	</div>
5279
	</div>
5280
	</div>
5281
	<?php
5282
}
5283
5284
/**
5285
 * Check whether a user is still logged in, for the heartbeat.
5286
 *
5287
 * Send a result that shows a log-in box if the user is no longer logged in,
5288
 * or if their cookie is within the grace period.
5289
 *
5290
 * @since 3.6.0
5291
 *
5292
 * @global int $login_grace_period
5293
 *
5294
 * @param array $response  The Heartbeat response.
5295
 * @return array $response The Heartbeat response with 'wp-auth-check' value set.
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,boolean>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
5296
 */
5297
function wp_auth_check( $response ) {
5298
	$response['wp-auth-check'] = is_user_logged_in() && empty( $GLOBALS['login_grace_period'] );
5299
	return $response;
5300
}
5301
5302
/**
5303
 * Return RegEx body to liberally match an opening HTML tag.
5304
 *
5305
 * Matches an opening HTML tag that:
5306
 * 1. Is self-closing or
5307
 * 2. Has no body but has a closing tag of the same name or
5308
 * 3. Contains a body and a closing tag of the same name
5309
 *
5310
 * Note: this RegEx does not balance inner tags and does not attempt
5311
 * to produce valid HTML
5312
 *
5313
 * @since 3.6.0
5314
 *
5315
 * @param string $tag An HTML tag name. Example: 'video'.
5316
 * @return string Tag RegEx.
0 ignored issues
show
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
5317
 */
5318
function get_tag_regex( $tag ) {
5319
	if ( empty( $tag ) )
5320
		return;
5321
	return sprintf( '<%1$s[^<]*(?:>[\s\S]*<\/%1$s>|\s*\/>)', tag_escape( $tag ) );
5322
}
5323
5324
/**
5325
 * Retrieve a canonical form of the provided charset appropriate for passing to PHP
5326
 * functions such as htmlspecialchars() and charset html attributes.
5327
 *
5328
 * @since 3.6.0
5329
 * @access private
5330
 *
5331
 * @see https://core.trac.wordpress.org/ticket/23688
5332
 *
5333
 * @param string $charset A charset name.
5334
 * @return string The canonical form of the charset.
5335
 */
5336
function _canonical_charset( $charset ) {
5337
	if ( 'utf-8' === strtolower( $charset ) || 'utf8' === strtolower( $charset) ) {
5338
5339
		return 'UTF-8';
5340
	}
5341
5342
	if ( 'iso-8859-1' === strtolower( $charset ) || 'iso8859-1' === strtolower( $charset ) ) {
5343
5344
		return 'ISO-8859-1';
5345
	}
5346
5347
	return $charset;
5348
}
5349
5350
/**
5351
 * Set the mbstring internal encoding to a binary safe encoding when func_overload
5352
 * is enabled.
5353
 *
5354
 * When mbstring.func_overload is in use for multi-byte encodings, the results from
5355
 * strlen() and similar functions respect the utf8 characters, causing binary data
5356
 * to return incorrect lengths.
5357
 *
5358
 * This function overrides the mbstring encoding to a binary-safe encoding, and
5359
 * resets it to the users expected encoding afterwards through the
5360
 * `reset_mbstring_encoding` function.
5361
 *
5362
 * It is safe to recursively call this function, however each
5363
 * `mbstring_binary_safe_encoding()` call must be followed up with an equal number
5364
 * of `reset_mbstring_encoding()` calls.
5365
 *
5366
 * @since 3.7.0
5367
 *
5368
 * @see reset_mbstring_encoding()
5369
 *
5370
 * @staticvar array $encodings
5371
 * @staticvar bool  $overloaded
5372
 *
5373
 * @param bool $reset Optional. Whether to reset the encoding back to a previously-set encoding.
5374
 *                    Default false.
5375
 */
5376
function mbstring_binary_safe_encoding( $reset = false ) {
5377
	static $encodings = array();
5378
	static $overloaded = null;
5379
5380
	if ( is_null( $overloaded ) )
5381
		$overloaded = function_exists( 'mb_internal_encoding' ) && ( ini_get( 'mbstring.func_overload' ) & 2 );
5382
5383
	if ( false === $overloaded )
5384
		return;
5385
5386
	if ( ! $reset ) {
5387
		$encoding = mb_internal_encoding();
5388
		array_push( $encodings, $encoding );
5389
		mb_internal_encoding( 'ISO-8859-1' );
5390
	}
5391
5392
	if ( $reset && $encodings ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $encodings of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
5393
		$encoding = array_pop( $encodings );
5394
		mb_internal_encoding( $encoding );
5395
	}
5396
}
5397
5398
/**
5399
 * Reset the mbstring internal encoding to a users previously set encoding.
5400
 *
5401
 * @see mbstring_binary_safe_encoding()
5402
 *
5403
 * @since 3.7.0
5404
 */
5405
function reset_mbstring_encoding() {
5406
	mbstring_binary_safe_encoding( true );
5407
}
5408
5409
/**
5410
 * Filter/validate a variable as a boolean.
5411
 *
5412
 * Alternative to `filter_var( $var, FILTER_VALIDATE_BOOLEAN )`.
5413
 *
5414
 * @since 4.0.0
5415
 *
5416
 * @param mixed $var Boolean value to validate.
5417
 * @return bool Whether the value is validated.
5418
 */
5419
function wp_validate_boolean( $var ) {
5420
	if ( is_bool( $var ) ) {
5421
		return $var;
5422
	}
5423
5424
	if ( is_string( $var ) && 'false' === strtolower( $var ) ) {
5425
		return false;
5426
	}
5427
5428
	return (bool) $var;
5429
}
5430
5431
/**
5432
 * Delete a file
5433
 *
5434
 * @since 4.2.0
5435
 *
5436
 * @param string $file The path to the file to delete.
5437
 */
5438
function wp_delete_file( $file ) {
5439
	/**
5440
	 * Filters the path of the file to delete.
5441
	 *
5442
	 * @since 2.1.0
5443
	 *
5444
	 * @param string $file Path to the file to delete.
5445
	 */
5446
	$delete = apply_filters( 'wp_delete_file', $file );
5447
	if ( ! empty( $delete ) ) {
5448
		@unlink( $delete );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
5449
	}
5450
}
5451
5452
/**
5453
 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
5454
 *
5455
 * This prevents reusing the same tab for a preview when the user has navigated away.
5456
 *
5457
 * @since 4.3.0
5458
 */
5459
function wp_post_preview_js() {
5460
	global $post;
5461
5462
	if ( ! is_preview() || empty( $post ) ) {
5463
		return;
5464
	}
5465
5466
	// Has to match the window name used in post_submit_meta_box()
5467
	$name = 'wp-preview-' . (int) $post->ID;
5468
5469
	?>
5470
	<script>
5471
	( function() {
5472
		var query = document.location.search;
5473
5474
		if ( query && query.indexOf( 'preview=true' ) !== -1 ) {
5475
			window.name = '<?php echo $name; ?>';
5476
		}
5477
5478
		if ( window.addEventListener ) {
5479
			window.addEventListener( 'unload', function() { window.name = ''; }, false );
5480
		}
5481
	}());
5482
	</script>
5483
	<?php
5484
}
5485
5486
/**
5487
 * Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601/RFC3339.
5488
 *
5489
 * Explicitly strips timezones, as datetimes are not saved with any timezone
5490
 * information. Including any information on the offset could be misleading.
5491
 *
5492
 * @since 4.4.0
5493
 *
5494
 * @param string $date_string Date string to parse and format.
5495
 * @return string Date formatted for ISO8601/RFC3339.
5496
 */
5497
function mysql_to_rfc3339( $date_string ) {
5498
	$formatted = mysql2date( 'c', $date_string, false );
5499
5500
	// Strip timezone information
5501
	return preg_replace( '/(?:Z|[+-]\d{2}(?::\d{2})?)$/', '', $formatted );
5502
}
5503
5504
/**
5505
 * Attempts to raise the PHP memory limit for memory intensive processes.
5506
 *
5507
 * Only allows raising the existing limit and prevents lowering it.
5508
 *
5509
 * @since 4.6.0
5510
 *
5511
 * @param string $context Optional. Context in which the function is called. Accepts either 'admin',
5512
 *                        'image', or an arbitrary other context. If an arbitrary context is passed,
5513
 *                        the similarly arbitrary {@see '{$context}_memory_limit'} filter will be
5514
 *                        invoked. Default 'admin'.
5515
 * @return bool|int|string The limit that was set or false on failure.
5516
 */
5517
function wp_raise_memory_limit( $context = 'admin' ) {
5518
	// Exit early if the limit cannot be changed.
5519
	if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
5520
		return false;
5521
	}
5522
5523
	$current_limit     = @ini_get( 'memory_limit' );
5524
	$current_limit_int = wp_convert_hr_to_bytes( $current_limit );
5525
5526
	if ( -1 === $current_limit_int ) {
5527
		return false;
5528
	}
5529
5530
	$wp_max_limit     = WP_MAX_MEMORY_LIMIT;
5531
	$wp_max_limit_int = wp_convert_hr_to_bytes( $wp_max_limit );
5532
	$filtered_limit   = $wp_max_limit;
5533
5534
	switch ( $context ) {
5535
		case 'admin':
5536
			/**
5537
			 * Filters the maximum memory limit available for administration screens.
5538
			 *
5539
			 * This only applies to administrators, who may require more memory for tasks
5540
			 * like updates. Memory limits when processing images (uploaded or edited by
5541
			 * users of any role) are handled separately.
5542
			 *
5543
			 * The `WP_MAX_MEMORY_LIMIT` constant specifically defines the maximum memory
5544
			 * limit available when in the administration back end. The default is 256M
5545
			 * (256 megabytes of memory) or the original `memory_limit` php.ini value if
5546
			 * this is higher.
5547
			 *
5548
			 * @since 3.0.0
5549
			 * @since 4.6.0 The default now takes the original `memory_limit` into account.
5550
			 *
5551
			 * @param int|string $filtered_limit The maximum WordPress memory limit. Accepts an integer
5552
			 *                                   (bytes), or a shorthand string notation, such as '256M'.
5553
			 */
5554
			$filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit );
5555
			break;
5556
5557
		case 'image':
5558
			/**
5559
			 * Filters the memory limit allocated for image manipulation.
5560
			 *
5561
			 * @since 3.5.0
5562
			 * @since 4.6.0 The default now takes the original `memory_limit` into account.
5563
			 *
5564
			 * @param int|string $filtered_limit Maximum memory limit to allocate for images.
5565
			 *                                   Default `WP_MAX_MEMORY_LIMIT` or the original
5566
			 *                                   php.ini `memory_limit`, whichever is higher.
5567
			 *                                   Accepts an integer (bytes), or a shorthand string
5568
			 *                                   notation, such as '256M'.
5569
			 */
5570
			$filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit );
5571
			break;
5572
5573
		default:
5574
			/**
5575
			 * Filters the memory limit allocated for arbitrary contexts.
5576
			 *
5577
			 * The dynamic portion of the hook name, `$context`, refers to an arbitrary
5578
			 * context passed on calling the function. This allows for plugins to define
5579
			 * their own contexts for raising the memory limit.
5580
			 *
5581
			 * @since 4.6.0
5582
			 *
5583
			 * @param int|string $filtered_limit Maximum memory limit to allocate for images.
5584
			 *                                   Default '256M' or the original php.ini `memory_limit`,
5585
			 *                                   whichever is higher. Accepts an integer (bytes), or a
5586
			 *                                   shorthand string notation, such as '256M'.
5587
			 */
5588
			$filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit );
5589
			break;
5590
	}
5591
5592
	$filtered_limit_int = wp_convert_hr_to_bytes( $filtered_limit );
5593
5594
	if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
5595
		if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) {
5596
			return $filtered_limit;
5597
		} else {
5598
			return false;
5599
		}
5600
	} elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
5601
		if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) {
5602
			return $wp_max_limit;
5603
		} else {
5604
			return false;
5605
		}
5606
	}
5607
5608
	return false;
5609
}
5610
5611
/**
5612
 * Generate a random UUID (version 4).
5613
 *
5614
 * @since 4.7.0
5615
 *
5616
 * @return string UUID.
5617
 */
5618
function wp_generate_uuid4() {
5619
	return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
5620
		mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
5621
		mt_rand( 0, 0xffff ),
5622
		mt_rand( 0, 0x0fff ) | 0x4000,
5623
		mt_rand( 0, 0x3fff ) | 0x8000,
5624
		mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
5625
	);
5626
}
5627
5628
/**
5629
 * Get last changed date for the specified cache group.
5630
 *
5631
 * @since 4.7.0
5632
 *
5633
 * @param string $group Where the cache contents are grouped.
5634
 *
5635
 * @return string $last_changed UNIX timestamp with microseconds representing when the group was last changed.
5636
 */
5637
function wp_cache_get_last_changed( $group ) {
5638
	$last_changed = wp_cache_get( 'last_changed', $group );
5639
5640
	if ( ! $last_changed ) {
5641
		$last_changed = microtime();
5642
		wp_cache_set( 'last_changed', $last_changed, $group );
5643
	}
5644
5645
	return $last_changed;
5646
}
5647