wp/wp-includes/functions.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     6  */
     6  */
     7 
     7 
     8 require( ABSPATH . WPINC . '/option.php' );
     8 require( ABSPATH . WPINC . '/option.php' );
     9 
     9 
    10 /**
    10 /**
    11  * Converts given date string into a different format.
    11  * Convert given date string into a different format.
    12  *
    12  *
    13  * $format should be either a PHP date format string, e.g. 'U' for a Unix
    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.
    14  * timestamp, or 'G' for a Unix timestamp assuming that $date is GMT.
    15  *
    15  *
    16  * If $translate is true then the given date and format string will
    16  * If $translate is true then the given date and format string will
    17  * be passed to date_i18n() for translation.
    17  * be passed to date_i18n() for translation.
    18  *
    18  *
    19  * @since 0.71
    19  * @since 0.71
    20  *
    20  *
    21  * @param string $format Format of the date to return.
    21  * @param string $format    Format of the date to return.
    22  * @param string $date Date string to convert.
    22  * @param string $date      Date string to convert.
    23  * @param bool $translate Whether the return date should be translated. Default is true.
    23  * @param bool   $translate Whether the return date should be translated. Default true.
    24  * @return string|int Formatted date string, or Unix timestamp.
    24  * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty.
    25  */
    25  */
    26 function mysql2date( $format, $date, $translate = true ) {
    26 function mysql2date( $format, $date, $translate = true ) {
    27 	if ( empty( $date ) )
    27 	if ( empty( $date ) )
    28 		return false;
    28 		return false;
    29 
    29 
    44 /**
    44 /**
    45  * Retrieve the current time based on specified type.
    45  * Retrieve the current time based on specified type.
    46  *
    46  *
    47  * The 'mysql' type will return the time in the format for MySQL DATETIME field.
    47  * The 'mysql' type will return the time in the format for MySQL DATETIME field.
    48  * The 'timestamp' type will return the current timestamp.
    48  * The 'timestamp' type will return the current timestamp.
       
    49  * Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d').
    49  *
    50  *
    50  * If $gmt is set to either '1' or 'true', then both types will use GMT time.
    51  * If $gmt is set to either '1' or 'true', then both types will use GMT time.
    51  * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
    52  * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
    52  *
    53  *
    53  * @since 1.0.0
    54  * @since 1.0.0
    54  *
    55  *
    55  * @param string $type Either 'mysql' or 'timestamp'.
    56  * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date
    56  * @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false.
    57  *                       format string (e.g. 'Y-m-d').
    57  * @return int|string String if $type is 'gmt', int if $type is 'timestamp'.
    58  * @param int|bool $gmt  Optional. Whether to use GMT timezone. Default false.
       
    59  * @return int|string Integer if $type is 'timestamp', string otherwise.
    58  */
    60  */
    59 function current_time( $type, $gmt = 0 ) {
    61 function current_time( $type, $gmt = 0 ) {
    60 	switch ( $type ) {
    62 	switch ( $type ) {
    61 		case 'mysql':
    63 		case 'mysql':
    62 			return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
    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 ) ) );
    63 			break;
       
    64 		case 'timestamp':
    65 		case 'timestamp':
    65 			return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
    66 			return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
    66 			break;
    67 		default:
       
    68 			return ( $gmt ) ? date( $type ) : date( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
    67 	}
    69 	}
    68 }
    70 }
    69 
    71 
    70 /**
    72 /**
    71  * Retrieve the date in localized format, based on timestamp.
    73  * Retrieve the date in localized format, based on timestamp.
    74  * take over the format for the date. If it isn't, then the date format string
    76  * take over the format for the date. If it isn't, then the date format string
    75  * will be used instead.
    77  * will be used instead.
    76  *
    78  *
    77  * @since 0.71
    79  * @since 0.71
    78  *
    80  *
    79  * @param string $dateformatstring Format to display the date.
    81  * @param string   $dateformatstring Format to display the date.
    80  * @param int $unixtimestamp Optional. Unix timestamp.
    82  * @param bool|int $unixtimestamp    Optional. Unix timestamp. Default false.
    81  * @param bool $gmt Optional, default is false. Whether to convert to GMT for time.
    83  * @param bool     $gmt              Optional. Whether to use GMT timezone. Default false.
       
    84  *
    82  * @return string The date, translated if locale specifies it.
    85  * @return string The date, translated if locale specifies it.
    83  */
    86  */
    84 function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
    87 function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
    85 	global $wp_locale;
    88 	global $wp_locale;
    86 	$i = $unixtimestamp;
    89 	$i = $unixtimestamp;
    93 		// we should not let date() interfere with our
    96 		// we should not let date() interfere with our
    94 		// specially computed timestamp
    97 		// specially computed timestamp
    95 		$gmt = true;
    98 		$gmt = true;
    96 	}
    99 	}
    97 
   100 
    98 	// store original value for language with untypical grammars
   101 	/*
    99 	// see http://core.trac.wordpress.org/ticket/9396
   102 	 * Store original value for language with untypical grammars.
       
   103 	 * See https://core.trac.wordpress.org/ticket/9396
       
   104 	 */
   100 	$req_format = $dateformatstring;
   105 	$req_format = $dateformatstring;
   101 
   106 
   102 	$datefunc = $gmt? 'gmdate' : 'date';
   107 	$datefunc = $gmt? 'gmdate' : 'date';
   103 
   108 
   104 	if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
   109 	if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
   134 				}
   139 				}
   135 			}
   140 			}
   136 		}
   141 		}
   137 	}
   142 	}
   138 	$j = @$datefunc( $dateformatstring, $i );
   143 	$j = @$datefunc( $dateformatstring, $i );
   139 	// allow plugins to redo this entirely for languages with untypical grammars
   144 
   140 	$j = apply_filters('date_i18n', $j, $req_format, $i, $gmt);
   145 	/**
       
   146 	 * Filter the date formatted based on the locale.
       
   147 	 *
       
   148 	 * @since 2.8.0
       
   149 	 *
       
   150 	 * @param string $j          Formatted date string.
       
   151 	 * @param string $req_format Format to display the date.
       
   152 	 * @param int    $i          Unix timestamp.
       
   153 	 * @param bool   $gmt        Whether to convert to GMT for time. Default false.
       
   154 	 */
       
   155 	$j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt );
   141 	return $j;
   156 	return $j;
   142 }
   157 }
   143 
   158 
   144 /**
   159 /**
   145  * Convert integer number to format based on the locale.
   160  * Convert integer number to format based on the locale.
   146  *
   161  *
   147  * @since 2.3.0
   162  * @since 2.3.0
   148  *
   163  *
   149  * @param int $number The number to convert based on locale.
   164  * @param int $number   The number to convert based on locale.
   150  * @param int $decimals Precision of the number of decimal places.
   165  * @param int $decimals Optional. Precision of the number of decimal places. Default 0.
   151  * @return string Converted number in string format.
   166  * @return string Converted number in string format.
   152  */
   167  */
   153 function number_format_i18n( $number, $decimals = 0 ) {
   168 function number_format_i18n( $number, $decimals = 0 ) {
   154 	global $wp_locale;
   169 	global $wp_locale;
   155 	$formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
   170 	$formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
       
   171 
       
   172 	/**
       
   173 	 * Filter the number formatted based on the locale.
       
   174 	 *
       
   175 	 * @since  2.8.0
       
   176 	 *
       
   177 	 * @param string $formatted Converted number in string format.
       
   178 	 */
   156 	return apply_filters( 'number_format_i18n', $formatted );
   179 	return apply_filters( 'number_format_i18n', $formatted );
   157 }
   180 }
   158 
   181 
   159 /**
   182 /**
   160  * Convert number of bytes largest unit bytes will fit into.
   183  * Convert number of bytes largest unit bytes will fit into.
   161  *
   184  *
   162  * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts
   185  * It is easier to read 1 kB than 1024 bytes and 1 MB than 1048576 bytes. Converts
   163  * number of bytes to human readable number by taking the number of that unit
   186  * number of bytes to human readable number by taking the number of that unit
   164  * that the bytes will go into it. Supports TB value.
   187  * that the bytes will go into it. Supports TB value.
   165  *
   188  *
   166  * Please note that integers in PHP are limited to 32 bits, unless they are on
   189  * Please note that integers in PHP are limited to 32 bits, unless they are on
   167  * 64 bit architecture, then they have 64 bit size. If you need to place the
   190  * 64 bit architecture, then they have 64 bit size. If you need to place the
   168  * larger size then what PHP integer type will hold, then use a string. It will
   191  * larger size then what PHP integer type will hold, then use a string. It will
   169  * be converted to a double, which should always have 64 bit length.
   192  * be converted to a double, which should always have 64 bit length.
   170  *
   193  *
   171  * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
   194  * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
   172  * @link http://en.wikipedia.org/wiki/Byte
       
   173  *
   195  *
   174  * @since 2.3.0
   196  * @since 2.3.0
   175  *
   197  *
   176  * @param int|string $bytes Number of bytes. Note max integer size for integers.
   198  * @param int|string $bytes    Number of bytes. Note max integer size for integers.
   177  * @param int $decimals Precision of number of decimal places. Deprecated.
   199  * @param int        $decimals Optional. Precision of number of decimal places. Default 0.
   178  * @return bool|string False on failure. Number string on success.
   200  * @return string|false False on failure. Number string on success.
   179  */
   201  */
   180 function size_format( $bytes, $decimals = 0 ) {
   202 function size_format( $bytes, $decimals = 0 ) {
   181 	$quant = array(
   203 	$quant = array(
   182 		// ========================= Origin ====
   204 		// ========================= Origin ====
   183 		'TB' => 1099511627776,  // pow( 1024, 4)
   205 		'TB' => 1099511627776,  // pow( 1024, 4)
   184 		'GB' => 1073741824,     // pow( 1024, 3)
   206 		'GB' => 1073741824,     // pow( 1024, 3)
   185 		'MB' => 1048576,        // pow( 1024, 2)
   207 		'MB' => 1048576,        // pow( 1024, 2)
   186 		'kB' => 1024,           // pow( 1024, 1)
   208 		'kB' => 1024,           // pow( 1024, 1)
   187 		'B ' => 1,              // pow( 1024, 0)
   209 		'B'  => 1,              // pow( 1024, 0)
   188 	);
   210 	);
   189 	foreach ( $quant as $unit => $mag )
   211 
   190 		if ( doubleval($bytes) >= $mag )
   212 	foreach ( $quant as $unit => $mag ) {
       
   213 		if ( doubleval( $bytes ) >= $mag ) {
   191 			return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
   214 			return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
       
   215 		}
       
   216 	}
   192 
   217 
   193 	return false;
   218 	return false;
   194 }
   219 }
   195 
   220 
   196 /**
   221 /**
   197  * Get the week start and end from the datetime or date string from mysql.
   222  * Get the week start and end from the datetime or date string from MySQL.
   198  *
   223  *
   199  * @since 0.71
   224  * @since 0.71
   200  *
   225  *
   201  * @param string $mysqlstring Date or datetime field type from mysql.
   226  * @param string     $mysqlstring   Date or datetime field type from MySQL.
   202  * @param int $start_of_week Optional. Start of the week as an integer.
   227  * @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string.
   203  * @return array Keys are 'start' and 'end'.
   228  * @return array Keys are 'start' and 'end'.
   204  */
   229  */
   205 function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
   230 function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
   206 	$my = substr( $mysqlstring, 0, 4 ); // Mysql string Year
   231 	// MySQL string year.
   207 	$mm = substr( $mysqlstring, 8, 2 ); // Mysql string Month
   232 	$my = substr( $mysqlstring, 0, 4 );
   208 	$md = substr( $mysqlstring, 5, 2 ); // Mysql string day
   233 
   209 	$day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day.
   234 	// MySQL string month.
   210 	$weekday = date( 'w', $day ); // The day of the week from the timestamp
   235 	$mm = substr( $mysqlstring, 8, 2 );
       
   236 
       
   237 	// MySQL string day.
       
   238 	$md = substr( $mysqlstring, 5, 2 );
       
   239 
       
   240 	// The timestamp for MySQL string day.
       
   241 	$day = mktime( 0, 0, 0, $md, $mm, $my );
       
   242 
       
   243 	// The day of the week from the timestamp.
       
   244 	$weekday = date( 'w', $day );
       
   245 
   211 	if ( !is_numeric($start_of_week) )
   246 	if ( !is_numeric($start_of_week) )
   212 		$start_of_week = get_option( 'start_of_week' );
   247 		$start_of_week = get_option( 'start_of_week' );
   213 
   248 
   214 	if ( $weekday < $start_of_week )
   249 	if ( $weekday < $start_of_week )
   215 		$weekday += 7;
   250 		$weekday += 7;
   216 
   251 
   217 	$start = $day - DAY_IN_SECONDS * ( $weekday - $start_of_week ); // The most recent week start day on or before $day
   252 	// The most recent week start day on or before $day.
   218 	$end = $start + 7 * DAY_IN_SECONDS - 1; // $start + 7 days - 1 second
   253 	$start = $day - DAY_IN_SECONDS * ( $weekday - $start_of_week );
       
   254 
       
   255 	// $start + 7 days - 1 second.
       
   256 	$end = $start + 7 * DAY_IN_SECONDS - 1;
   219 	return compact( 'start', 'end' );
   257 	return compact( 'start', 'end' );
   220 }
   258 }
   221 
   259 
   222 /**
   260 /**
   223  * Unserialize value only if it was serialized.
   261  * Unserialize value only if it was serialized.
   239  * If $data is not an string, then returned value will always be false.
   277  * If $data is not an string, then returned value will always be false.
   240  * Serialized data is always a string.
   278  * Serialized data is always a string.
   241  *
   279  *
   242  * @since 2.0.5
   280  * @since 2.0.5
   243  *
   281  *
   244  * @param mixed $data Value to check to see if was serialized.
   282  * @param string $data   Value to check to see if was serialized.
   245  * @param bool $strict Optional. Whether to be strict about the end of the string. Defaults true.
   283  * @param bool   $strict Optional. Whether to be strict about the end of the string. Default true.
   246  * @return bool False if not serialized and true if it was.
   284  * @return bool False if not serialized and true if it was.
   247  */
   285  */
   248 function is_serialized( $data, $strict = true ) {
   286 function is_serialized( $data, $strict = true ) {
   249 	// if it isn't a string, it isn't serialized
   287 	// if it isn't a string, it isn't serialized.
   250 	if ( ! is_string( $data ) )
   288 	if ( ! is_string( $data ) ) {
   251 		return false;
   289 		return false;
       
   290 	}
   252 	$data = trim( $data );
   291 	$data = trim( $data );
   253  	if ( 'N;' == $data )
   292  	if ( 'N;' == $data ) {
   254 		return true;
   293 		return true;
   255 	$length = strlen( $data );
   294 	}
   256 	if ( $length < 4 )
   295 	if ( strlen( $data ) < 4 ) {
   257 		return false;
   296 		return false;
   258 	if ( ':' !== $data[1] )
   297 	}
       
   298 	if ( ':' !== $data[1] ) {
   259 		return false;
   299 		return false;
       
   300 	}
   260 	if ( $strict ) {
   301 	if ( $strict ) {
   261 		$lastc = $data[ $length - 1 ];
   302 		$lastc = substr( $data, -1 );
   262 		if ( ';' !== $lastc && '}' !== $lastc )
   303 		if ( ';' !== $lastc && '}' !== $lastc ) {
   263 			return false;
   304 			return false;
       
   305 		}
   264 	} else {
   306 	} else {
   265 		$semicolon = strpos( $data, ';' );
   307 		$semicolon = strpos( $data, ';' );
   266 		$brace     = strpos( $data, '}' );
   308 		$brace     = strpos( $data, '}' );
   267 		// Either ; or } must exist.
   309 		// Either ; or } must exist.
   268 		if ( false === $semicolon && false === $brace )
   310 		if ( false === $semicolon && false === $brace )
   275 	}
   317 	}
   276 	$token = $data[0];
   318 	$token = $data[0];
   277 	switch ( $token ) {
   319 	switch ( $token ) {
   278 		case 's' :
   320 		case 's' :
   279 			if ( $strict ) {
   321 			if ( $strict ) {
   280 				if ( '"' !== $data[ $length - 2 ] )
   322 				if ( '"' !== substr( $data, -2, 1 ) ) {
   281 					return false;
   323 					return false;
       
   324 				}
   282 			} elseif ( false === strpos( $data, '"' ) ) {
   325 			} elseif ( false === strpos( $data, '"' ) ) {
   283 				return false;
   326 				return false;
   284 			}
   327 			}
   285 			// or else fall through
   328 			// or else fall through
   286 		case 'a' :
   329 		case 'a' :
   298 /**
   341 /**
   299  * Check whether serialized data is of string type.
   342  * Check whether serialized data is of string type.
   300  *
   343  *
   301  * @since 2.0.5
   344  * @since 2.0.5
   302  *
   345  *
   303  * @param mixed $data Serialized data
   346  * @param string $data Serialized data.
   304  * @return bool False if not a serialized string, true if it is.
   347  * @return bool False if not a serialized string, true if it is.
   305  */
   348  */
   306 function is_serialized_string( $data ) {
   349 function is_serialized_string( $data ) {
   307 	// if it isn't a string, it isn't a serialized string
   350 	// if it isn't a string, it isn't a serialized string.
   308 	if ( !is_string( $data ) )
   351 	if ( ! is_string( $data ) ) {
   309 		return false;
   352 		return false;
       
   353 	}
   310 	$data = trim( $data );
   354 	$data = trim( $data );
   311 	$length = strlen( $data );
   355 	if ( strlen( $data ) < 4 ) {
   312 	if ( $length < 4 )
       
   313 		return false;
   356 		return false;
   314 	elseif ( ':' !== $data[1] )
   357 	} elseif ( ':' !== $data[1] ) {
   315 		return false;
   358 		return false;
   316 	elseif ( ';' !== $data[$length-1] )
   359 	} elseif ( ';' !== substr( $data, -1 ) ) {
   317 		return false;
   360 		return false;
   318 	elseif ( $data[0] !== 's' )
   361 	} elseif ( $data[0] !== 's' ) {
   319 		return false;
   362 		return false;
   320 	elseif ( '"' !== $data[$length-2] )
   363 	} elseif ( '"' !== substr( $data, -2, 1 ) ) {
   321 		return false;
   364 		return false;
   322 	else
   365 	} else {
   323 		return true;
   366 		return true;
       
   367 	}
   324 }
   368 }
   325 
   369 
   326 /**
   370 /**
   327  * Serialize data, if needed.
   371  * Serialize data, if needed.
   328  *
   372  *
   329  * @since 2.0.5
   373  * @since 2.0.5
   330  *
   374  *
   331  * @param mixed $data Data that might be serialized.
   375  * @param string|array|object $data Data that might be serialized.
   332  * @return mixed A scalar data
   376  * @return mixed A scalar data
   333  */
   377  */
   334 function maybe_serialize( $data ) {
   378 function maybe_serialize( $data ) {
   335 	if ( is_array( $data ) || is_object( $data ) )
   379 	if ( is_array( $data ) || is_object( $data ) )
   336 		return serialize( $data );
   380 		return serialize( $data );
   337 
   381 
   338 	// Double serialization is required for backward compatibility.
   382 	// Double serialization is required for backward compatibility.
   339 	// See http://core.trac.wordpress.org/ticket/12930
   383 	// See https://core.trac.wordpress.org/ticket/12930
   340 	if ( is_serialized( $data, false ) )
   384 	if ( is_serialized( $data, false ) )
   341 		return serialize( $data );
   385 		return serialize( $data );
   342 
   386 
   343 	return $data;
   387 	return $data;
   344 }
   388 }
   347  * Retrieve post title from XMLRPC XML.
   391  * Retrieve post title from XMLRPC XML.
   348  *
   392  *
   349  * If the title element is not part of the XML, then the default post title from
   393  * If the title element is not part of the XML, then the default post title from
   350  * the $post_default_title will be used instead.
   394  * the $post_default_title will be used instead.
   351  *
   395  *
   352  * @package WordPress
       
   353  * @subpackage XMLRPC
       
   354  * @since 0.71
   396  * @since 0.71
   355  *
   397  *
   356  * @global string $post_default_title Default XMLRPC post title.
   398  * @global string $post_default_title Default XML-RPC post title.
   357  *
   399  *
   358  * @param string $content XMLRPC XML Request content
   400  * @param string $content XMLRPC XML Request content
   359  * @return string Post title
   401  * @return string Post title
   360  */
   402  */
   361 function xmlrpc_getposttitle( $content ) {
   403 function xmlrpc_getposttitle( $content ) {
   373  *
   415  *
   374  * If the category element is not found, then the default post category will be
   416  * If the category element is not found, then the default post category will be
   375  * used. The return type then would be what $post_default_category. If the
   417  * used. The return type then would be what $post_default_category. If the
   376  * category is found, then it will always be an array.
   418  * category is found, then it will always be an array.
   377  *
   419  *
   378  * @package WordPress
       
   379  * @subpackage XMLRPC
       
   380  * @since 0.71
   420  * @since 0.71
   381  *
   421  *
   382  * @global string $post_default_category Default XMLRPC post category.
   422  * @global string $post_default_category Default XML-RPC post category.
   383  *
   423  *
   384  * @param string $content XMLRPC XML Request content
   424  * @param string $content XMLRPC XML Request content
   385  * @return string|array List of categories or category name.
   425  * @return string|array List of categories or category name.
   386  */
   426  */
   387 function xmlrpc_getpostcategory( $content ) {
   427 function xmlrpc_getpostcategory( $content ) {
   396 }
   436 }
   397 
   437 
   398 /**
   438 /**
   399  * XMLRPC XML content without title and category elements.
   439  * XMLRPC XML content without title and category elements.
   400  *
   440  *
   401  * @package WordPress
       
   402  * @subpackage XMLRPC
       
   403  * @since 0.71
   441  * @since 0.71
   404  *
   442  *
   405  * @param string $content XMLRPC XML Request content
   443  * @param string $content XML-RPC XML Request content.
   406  * @return string XMLRPC XML Request content without title and category elements.
   444  * @return string XMLRPC XML Request content without title and category elements.
   407  */
   445  */
   408 function xmlrpc_removepostdata( $content ) {
   446 function xmlrpc_removepostdata( $content ) {
   409 	$content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content );
   447 	$content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content );
   410 	$content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content );
   448 	$content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content );
   411 	$content = trim( $content );
   449 	$content = trim( $content );
   412 	return $content;
   450 	return $content;
   413 }
   451 }
   414 
   452 
   415 /**
   453 /**
   416  * Use RegEx to extract URLs from arbitrary content
   454  * Use RegEx to extract URLs from arbitrary content.
   417  *
   455  *
   418  * @since 3.7.0
   456  * @since 3.7.0
   419  *
   457  *
   420  * @param string $content
   458  * @param string $content Content to extract URLs from.
   421  * @return array URLs found in passed string
   459  * @return array URLs found in passed string.
   422  */
   460  */
   423 function wp_extract_urls( $content ) {
   461 function wp_extract_urls( $content ) {
   424 	preg_match_all(
   462 	preg_match_all(
   425 		"#((?:[\w-]+://?|[\w\d]+[.])[^\s()<>]+[.](?:\([\w\d]+\)|(?:[^`!()\[\]{};:'\".,<>?«»“”‘’\s]|(?:[:]\d+)?/?)+))#",
   463 		"#([\"']?)("
       
   464 			. "(?:([\w-]+:)?//?)"
       
   465 			. "[^\s()<>]+"
       
   466 			. "[.]"
       
   467 			. "(?:"
       
   468 				. "\([\w\d]+\)|"
       
   469 				. "(?:"
       
   470 					. "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|"
       
   471 					. "(?:[:]\d+)?/?"
       
   472 				. ")+"
       
   473 			. ")"
       
   474 		. ")\\1#",
   426 		$content,
   475 		$content,
   427 		$post_links
   476 		$post_links
   428 	);
   477 	);
   429 
   478 
   430 	$post_links = array_unique( array_map( 'html_entity_decode', $post_links[0] ) );
   479 	$post_links = array_unique( array_map( 'html_entity_decode', $post_links[2] ) );
   431 
   480 
   432 	return array_values( $post_links );
   481 	return array_values( $post_links );
   433 }
   482 }
   434 
   483 
   435 /**
   484 /**
   437  *
   486  *
   438  * Will not add enclosures that have already been added and will
   487  * Will not add enclosures that have already been added and will
   439  * remove enclosures that are no longer in the post. This is called as
   488  * remove enclosures that are no longer in the post. This is called as
   440  * pingbacks and trackbacks.
   489  * pingbacks and trackbacks.
   441  *
   490  *
   442  * @package WordPress
       
   443  * @since 1.5.0
   491  * @since 1.5.0
   444  *
   492  *
   445  * @uses $wpdb
   493  * @see $wpdb
   446  *
   494  *
   447  * @param string $content Post Content
   495  * @param string $content Post Content.
   448  * @param int $post_ID Post ID
   496  * @param int $post_ID Post ID.
   449  */
   497  */
   450 function do_enclose( $content, $post_ID ) {
   498 function do_enclose( $content, $post_ID ) {
   451 	global $wpdb;
   499 	global $wpdb;
   452 
   500 
   453 	//TODO: Tidy this ghetto code up and make the debug code optional
   501 	//TODO: Tidy this ghetto code up and make the debug code optional
   459 
   507 
   460 	$post_links_temp = wp_extract_urls( $content );
   508 	$post_links_temp = wp_extract_urls( $content );
   461 
   509 
   462 	foreach ( $pung as $link_test ) {
   510 	foreach ( $pung as $link_test ) {
   463 		if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post
   511 		if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post
   464 			$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, like_escape( $link_test ) . '%') );
   512 			$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 ) . '%') );
   465 			foreach ( $mids as $mid )
   513 			foreach ( $mids as $mid )
   466 				delete_metadata_by_mid( 'post', $mid );
   514 				delete_metadata_by_mid( 'post', $mid );
   467 		}
   515 		}
   468 	}
   516 	}
   469 
   517 
   478 				$post_links[] = $link_test;
   526 				$post_links[] = $link_test;
   479 		}
   527 		}
   480 	}
   528 	}
   481 
   529 
   482 	foreach ( (array) $post_links as $url ) {
   530 	foreach ( (array) $post_links as $url ) {
   483 		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, like_escape( $url ) . '%' ) ) ) {
   531 		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 ) . '%' ) ) ) {
   484 
   532 
   485 			if ( $headers = wp_get_http_headers( $url) ) {
   533 			if ( $headers = wp_get_http_headers( $url) ) {
   486 				$len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0;
   534 				$len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0;
   487 				$type = isset( $headers['content-type'] ) ? $headers['content-type'] : '';
   535 				$type = isset( $headers['content-type'] ) ? $headers['content-type'] : '';
   488 				$allowed_types = array( 'video', 'audio' );
   536 				$allowed_types = array( 'video', 'audio' );
   516  * If $file_path is a writable filename, this will do a GET request and write
   564  * If $file_path is a writable filename, this will do a GET request and write
   517  * the file to that path.
   565  * the file to that path.
   518  *
   566  *
   519  * @since 2.5.0
   567  * @since 2.5.0
   520  *
   568  *
   521  * @param string $url URL to fetch.
   569  * @param string      $url       URL to fetch.
   522  * @param string|bool $file_path Optional. File path to write request to.
   570  * @param string|bool $file_path Optional. File path to write request to. Default false.
   523  * @param int $red (private) The number of Redirects followed, Upon 5 being hit, returns false.
   571  * @param int         $red       Optional. The number of Redirects followed, Upon 5 being hit,
       
   572  *                               returns false. Default 1.
   524  * @return bool|string False on failure and string of headers if HEAD request.
   573  * @return bool|string False on failure and string of headers if HEAD request.
   525  */
   574  */
   526 function wp_get_http( $url, $file_path = false, $red = 1 ) {
   575 function wp_get_http( $url, $file_path = false, $red = 1 ) {
   527 	@set_time_limit( 60 );
   576 	@set_time_limit( 60 );
   528 
   577 
   568 /**
   617 /**
   569  * Retrieve HTTP Headers from URL.
   618  * Retrieve HTTP Headers from URL.
   570  *
   619  *
   571  * @since 1.5.1
   620  * @since 1.5.1
   572  *
   621  *
   573  * @param string $url
   622  * @param string $url        URL to retrieve HTTP headers from.
   574  * @param bool $deprecated Not Used.
   623  * @param bool   $deprecated Not Used.
   575  * @return bool|string False on failure, headers on success.
   624  * @return bool|string False on failure, headers on success.
   576  */
   625  */
   577 function wp_get_http_headers( $url, $deprecated = false ) {
   626 function wp_get_http_headers( $url, $deprecated = false ) {
   578 	if ( !empty( $deprecated ) )
   627 	if ( !empty( $deprecated ) )
   579 		_deprecated_argument( __FUNCTION__, '2.7' );
   628 		_deprecated_argument( __FUNCTION__, '2.7' );
   585 
   634 
   586 	return wp_remote_retrieve_headers( $response );
   635 	return wp_remote_retrieve_headers( $response );
   587 }
   636 }
   588 
   637 
   589 /**
   638 /**
   590  * Whether today is a new day.
   639  * Whether the publish date of the current post in the loop is different from the
       
   640  * publish date of the previous post in the loop.
   591  *
   641  *
   592  * @since 0.71
   642  * @since 0.71
   593  * @uses $day Today
   643  *
   594  * @uses $previousday Previous day
   644  * @global string $currentday  The day of the current post in the loop.
       
   645  * @global string $previousday The day of the previous post in the loop.
   595  *
   646  *
   596  * @return int 1 when new day, 0 if not a new day.
   647  * @return int 1 when new day, 0 if not a new day.
   597  */
   648  */
   598 function is_new_day() {
   649 function is_new_day() {
   599 	global $currentday, $previousday;
   650 	global $currentday, $previousday;
   607  * Build URL query based on an associative and, or indexed array.
   658  * Build URL query based on an associative and, or indexed array.
   608  *
   659  *
   609  * This is a convenient function for easily building url queries. It sets the
   660  * This is a convenient function for easily building url queries. It sets the
   610  * separator to '&' and uses _http_build_query() function.
   661  * separator to '&' and uses _http_build_query() function.
   611  *
   662  *
       
   663  * @since 2.3.0
       
   664  *
   612  * @see _http_build_query() Used to build the query
   665  * @see _http_build_query() Used to build the query
   613  * @link http://us2.php.net/manual/en/function.http-build-query.php more on what
   666  * @see http://us2.php.net/manual/en/function.http-build-query.php for more on what
   614  *		http_build_query() does.
   667  *		http_build_query() does.
   615  *
   668  *
   616  * @since 2.3.0
       
   617  *
       
   618  * @param array $data URL-encode key/value pairs.
   669  * @param array $data URL-encode key/value pairs.
   619  * @return string URL encoded string
   670  * @return string URL-encoded string.
   620  */
   671  */
   621 function build_query( $data ) {
   672 function build_query( $data ) {
   622 	return _http_build_query( $data, null, '&', '', false );
   673 	return _http_build_query( $data, null, '&', '', false );
   623 }
   674 }
   624 
   675 
   625 // from php.net (modified by Mark Jaquith to behave like the native PHP5 function)
   676 /**
   626 function _http_build_query($data, $prefix=null, $sep=null, $key='', $urlencode=true) {
   677  * From php.net (modified by Mark Jaquith to behave like the native PHP5 function).
       
   678  *
       
   679  * @since 3.2.0
       
   680  * @access private
       
   681  *
       
   682  * @see http://us1.php.net/manual/en/function.http-build-query.php
       
   683  *
       
   684  * @param array|object  $data       An array or object of data. Converted to array.
       
   685  * @param string        $prefix     Optional. Numeric index. If set, start parameter numbering with it.
       
   686  *                                  Default null.
       
   687  * @param string        $sep        Optional. Argument separator; defaults to 'arg_separator.output'.
       
   688  *                                  Default null.
       
   689  * @param string        $key        Optional. Used to prefix key name. Default empty.
       
   690  * @param bool          $urlencode  Optional. Whether to use urlencode() in the result. Default true.
       
   691  *
       
   692  * @return string The query string.
       
   693  */
       
   694 function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) {
   627 	$ret = array();
   695 	$ret = array();
   628 
   696 
   629 	foreach ( (array) $data as $k => $v ) {
   697 	foreach ( (array) $data as $k => $v ) {
   630 		if ( $urlencode)
   698 		if ( $urlencode)
   631 			$k = urlencode($k);
   699 			$k = urlencode($k);
   633 			$k = $prefix.$k;
   701 			$k = $prefix.$k;
   634 		if ( !empty($key) )
   702 		if ( !empty($key) )
   635 			$k = $key . '%5B' . $k . '%5D';
   703 			$k = $key . '%5B' . $k . '%5D';
   636 		if ( $v === null )
   704 		if ( $v === null )
   637 			continue;
   705 			continue;
   638 		elseif ( $v === FALSE )
   706 		elseif ( $v === false )
   639 			$v = '0';
   707 			$v = '0';
   640 
   708 
   641 		if ( is_array($v) || is_object($v) )
   709 		if ( is_array($v) || is_object($v) )
   642 			array_push($ret,_http_build_query($v, '', $sep, $k, $urlencode));
   710 			array_push($ret,_http_build_query($v, '', $sep, $k, $urlencode));
   643 		elseif ( $urlencode )
   711 		elseif ( $urlencode )
   663  * value. Additional values provided are expected to be encoded appropriately
   731  * value. Additional values provided are expected to be encoded appropriately
   664  * with urlencode() or rawurlencode().
   732  * with urlencode() or rawurlencode().
   665  *
   733  *
   666  * @since 1.5.0
   734  * @since 1.5.0
   667  *
   735  *
   668  * @param mixed $param1 Either newkey or an associative_array
   736  * @param string|array $param1 Either newkey or an associative_array.
   669  * @param mixed $param2 Either newvalue or oldquery or uri
   737  * @param string       $param2 Either newvalue or oldquery or URI.
   670  * @param mixed $param3 Optional. Old query or uri
   738  * @param string       $param3 Optional. Old query or URI.
   671  * @return string New URL query string.
   739  * @return string New URL query string.
   672  */
   740  */
   673 function add_query_arg() {
   741 function add_query_arg() {
   674 	$ret = '';
       
   675 	$args = func_get_args();
   742 	$args = func_get_args();
   676 	if ( is_array( $args[0] ) ) {
   743 	if ( is_array( $args[0] ) ) {
   677 		if ( count( $args ) < 2 || false === $args[1] )
   744 		if ( count( $args ) < 2 || false === $args[1] )
   678 			$uri = $_SERVER['REQUEST_URI'];
   745 			$uri = $_SERVER['REQUEST_URI'];
   679 		else
   746 		else
   712 	}
   779 	}
   713 
   780 
   714 	wp_parse_str( $query, $qs );
   781 	wp_parse_str( $query, $qs );
   715 	$qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
   782 	$qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
   716 	if ( is_array( $args[0] ) ) {
   783 	if ( is_array( $args[0] ) ) {
   717 		$kayvees = $args[0];
   784 		foreach ( $args[0] as $k => $v ) {
   718 		$qs = array_merge( $qs, $kayvees );
   785 			$qs[ $k ] = $v;
       
   786 		}
   719 	} else {
   787 	} else {
   720 		$qs[ $args[0] ] = $args[1];
   788 		$qs[ $args[0] ] = $args[1];
   721 	}
   789 	}
   722 
   790 
   723 	foreach ( $qs as $k => $v ) {
   791 	foreach ( $qs as $k => $v ) {
   736 /**
   804 /**
   737  * Removes an item or list from the query string.
   805  * Removes an item or list from the query string.
   738  *
   806  *
   739  * @since 1.5.0
   807  * @since 1.5.0
   740  *
   808  *
   741  * @param string|array $key Query key or keys to remove.
   809  * @param string|array $key   Query key or keys to remove.
   742  * @param bool $query When false uses the $_SERVER value.
   810  * @param bool|string  $query Optional. When false uses the $_SERVER value. Default false.
   743  * @return string New URL query string.
   811  * @return string New URL query string.
   744  */
   812  */
   745 function remove_query_arg( $key, $query=false ) {
   813 function remove_query_arg( $key, $query = false ) {
   746 	if ( is_array( $key ) ) { // removing multiple keys
   814 	if ( is_array( $key ) ) { // removing multiple keys
   747 		foreach ( $key as $k )
   815 		foreach ( $key as $k )
   748 			$query = add_query_arg( $k, false, $query );
   816 			$query = add_query_arg( $k, false, $query );
   749 		return $query;
   817 		return $query;
   750 	}
   818 	}
   772 
   840 
   773 /**
   841 /**
   774  * HTTP request for URI to retrieve content.
   842  * HTTP request for URI to retrieve content.
   775  *
   843  *
   776  * @since 1.5.1
   844  * @since 1.5.1
   777  * @uses wp_remote_get()
   845  *
       
   846  * @see wp_safe_remote_get()
   778  *
   847  *
   779  * @param string $uri URI/URL of web page to retrieve.
   848  * @param string $uri URI/URL of web page to retrieve.
   780  * @return bool|string HTTP content. False on failure.
   849  * @return false|string HTTP content. False on failure.
   781  */
   850  */
   782 function wp_remote_fopen( $uri ) {
   851 function wp_remote_fopen( $uri ) {
   783 	$parsed_url = @parse_url( $uri );
   852 	$parsed_url = @parse_url( $uri );
   784 
   853 
   785 	if ( !$parsed_url || !is_array( $parsed_url ) )
   854 	if ( !$parsed_url || !is_array( $parsed_url ) )
   799 /**
   868 /**
   800  * Set up the WordPress query.
   869  * Set up the WordPress query.
   801  *
   870  *
   802  * @since 2.0.0
   871  * @since 2.0.0
   803  *
   872  *
   804  * @param string $query_vars Default WP_Query arguments.
   873  * @param string|array $query_vars Default WP_Query arguments.
   805  */
   874  */
   806 function wp( $query_vars = '' ) {
   875 function wp( $query_vars = '' ) {
   807 	global $wp, $wp_query, $wp_the_query;
   876 	global $wp, $wp_query, $wp_the_query;
   808 	$wp->main( $query_vars );
   877 	$wp->main( $query_vars );
   809 
   878 
   865 			413 => 'Request Entity Too Large',
   934 			413 => 'Request Entity Too Large',
   866 			414 => 'Request-URI Too Long',
   935 			414 => 'Request-URI Too Long',
   867 			415 => 'Unsupported Media Type',
   936 			415 => 'Unsupported Media Type',
   868 			416 => 'Requested Range Not Satisfiable',
   937 			416 => 'Requested Range Not Satisfiable',
   869 			417 => 'Expectation Failed',
   938 			417 => 'Expectation Failed',
       
   939 			418 => 'I\'m a teapot',
   870 			422 => 'Unprocessable Entity',
   940 			422 => 'Unprocessable Entity',
   871 			423 => 'Locked',
   941 			423 => 'Locked',
   872 			424 => 'Failed Dependency',
   942 			424 => 'Failed Dependency',
   873 			426 => 'Upgrade Required',
   943 			426 => 'Upgrade Required',
       
   944 			428 => 'Precondition Required',
       
   945 			429 => 'Too Many Requests',
       
   946 			431 => 'Request Header Fields Too Large',
   874 
   947 
   875 			500 => 'Internal Server Error',
   948 			500 => 'Internal Server Error',
   876 			501 => 'Not Implemented',
   949 			501 => 'Not Implemented',
   877 			502 => 'Bad Gateway',
   950 			502 => 'Bad Gateway',
   878 			503 => 'Service Unavailable',
   951 			503 => 'Service Unavailable',
   879 			504 => 'Gateway Timeout',
   952 			504 => 'Gateway Timeout',
   880 			505 => 'HTTP Version Not Supported',
   953 			505 => 'HTTP Version Not Supported',
   881 			506 => 'Variant Also Negotiates',
   954 			506 => 'Variant Also Negotiates',
   882 			507 => 'Insufficient Storage',
   955 			507 => 'Insufficient Storage',
   883 			510 => 'Not Extended'
   956 			510 => 'Not Extended',
       
   957 			511 => 'Network Authentication Required',
   884 		);
   958 		);
   885 	}
   959 	}
   886 
   960 
   887 	if ( isset( $wp_header_to_desc[$code] ) )
   961 	if ( isset( $wp_header_to_desc[$code] ) )
   888 		return $wp_header_to_desc[$code];
   962 		return $wp_header_to_desc[$code];
   892 
   966 
   893 /**
   967 /**
   894  * Set HTTP status header.
   968  * Set HTTP status header.
   895  *
   969  *
   896  * @since 2.0.0
   970  * @since 2.0.0
   897  * @uses apply_filters() Calls 'status_header' on status header string, HTTP
   971  *
   898  *		HTTP code, HTTP code description, and protocol string as separate
   972  * @see get_status_header_desc()
   899  *		parameters.
   973  *
   900  *
   974  * @param int $code HTTP status code.
   901  * @param int $header HTTP status code
   975  */
   902  * @return unknown
   976 function status_header( $code ) {
   903  */
   977 	$description = get_status_header_desc( $code );
   904 function status_header( $header ) {
   978 
   905 	$text = get_status_header_desc( $header );
   979 	if ( empty( $description ) )
   906 
   980 		return;
   907 	if ( empty( $text ) )
   981 
   908 		return false;
   982 	$protocol = $_SERVER['SERVER_PROTOCOL'];
   909 
       
   910 	$protocol = $_SERVER["SERVER_PROTOCOL"];
       
   911 	if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
   983 	if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
   912 		$protocol = 'HTTP/1.0';
   984 		$protocol = 'HTTP/1.0';
   913 	$status_header = "$protocol $header $text";
   985 	$status_header = "$protocol $code $description";
   914 	if ( function_exists( 'apply_filters' ) )
   986 	if ( function_exists( 'apply_filters' ) )
   915 		$status_header = apply_filters( 'status_header', $status_header, $header, $text, $protocol );
   987 
   916 
   988 		/**
   917 	return @header( $status_header, true, $header );
   989 		 * Filter an HTTP status header.
   918 }
   990 		 *
   919 
   991 		 * @since 2.2.0
   920 /**
   992 		 *
   921  * Gets the header information to prevent caching.
   993 		 * @param string $status_header HTTP status header.
   922  *
   994 		 * @param int    $code          HTTP status code.
   923  * The several different headers cover the different ways cache prevention is handled
   995 		 * @param string $description   Description for the status code.
   924  * by different browsers
   996 		 * @param string $protocol      Server protocol.
       
   997 		 */
       
   998 		$status_header = apply_filters( 'status_header', $status_header, $code, $description, $protocol );
       
   999 
       
  1000 	@header( $status_header, true, $code );
       
  1001 }
       
  1002 
       
  1003 /**
       
  1004  * Get the header information to prevent caching.
       
  1005  *
       
  1006  * The several different headers cover the different ways cache prevention
       
  1007  * is handled by different browsers
   925  *
  1008  *
   926  * @since 2.8.0
  1009  * @since 2.8.0
   927  *
  1010  *
   928  * @uses apply_filters()
       
   929  * @return array The associative array of header names and field values.
  1011  * @return array The associative array of header names and field values.
   930  */
  1012  */
   931 function wp_get_nocache_headers() {
  1013 function wp_get_nocache_headers() {
   932 	$headers = array(
  1014 	$headers = array(
   933 		'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
  1015 		'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
   934 		'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
  1016 		'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
   935 		'Pragma' => 'no-cache',
  1017 		'Pragma' => 'no-cache',
   936 	);
  1018 	);
   937 
  1019 
   938 	if ( function_exists('apply_filters') ) {
  1020 	if ( function_exists('apply_filters') ) {
   939 		$headers = (array) apply_filters('nocache_headers', $headers);
  1021 		/**
       
  1022 		 * Filter the cache-controlling headers.
       
  1023 		 *
       
  1024 		 * @since 2.8.0
       
  1025 		 *
       
  1026 		 * @see wp_get_nocache_headers()
       
  1027 		 *
       
  1028 		 * @param array $headers {
       
  1029 		 *     Header names and field values.
       
  1030 		 *
       
  1031 		 *     @type string $Expires       Expires header.
       
  1032 		 *     @type string $Cache-Control Cache-Control header.
       
  1033 		 *     @type string $Pragma        Pragma header.
       
  1034 		 * }
       
  1035 		 */
       
  1036 		$headers = (array) apply_filters( 'nocache_headers', $headers );
   940 	}
  1037 	}
   941 	$headers['Last-Modified'] = false;
  1038 	$headers['Last-Modified'] = false;
   942 	return $headers;
  1039 	return $headers;
   943 }
  1040 }
   944 
  1041 
   945 /**
  1042 /**
   946  * Sets the headers to prevent caching for the different browsers.
  1043  * Set the headers to prevent caching for the different browsers.
   947  *
  1044  *
   948  * Different browsers support different nocache headers, so several headers must
  1045  * Different browsers support different nocache headers, so several
   949  * be sent so that all of them get the point that no caching should occur.
  1046  * headers must be sent so that all of them get the point that no
       
  1047  * caching should occur.
   950  *
  1048  *
   951  * @since 2.0.0
  1049  * @since 2.0.0
   952  * @uses wp_get_nocache_headers()
  1050  *
       
  1051  * @see wp_get_nocache_headers()
   953  */
  1052  */
   954 function nocache_headers() {
  1053 function nocache_headers() {
   955 	$headers = wp_get_nocache_headers();
  1054 	$headers = wp_get_nocache_headers();
   956 
  1055 
   957 	unset( $headers['Last-Modified'] );
  1056 	unset( $headers['Last-Modified'] );
   979  *
  1078  *
   980  * @since 2.1.0
  1079  * @since 2.1.0
   981  */
  1080  */
   982 function cache_javascript_headers() {
  1081 function cache_javascript_headers() {
   983 	$expiresOffset = 10 * DAY_IN_SECONDS;
  1082 	$expiresOffset = 10 * DAY_IN_SECONDS;
       
  1083 
   984 	header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
  1084 	header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
   985 	header( "Vary: Accept-Encoding" ); // Handle proxies
  1085 	header( "Vary: Accept-Encoding" ); // Handle proxies
   986 	header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
  1086 	header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
   987 }
  1087 }
   988 
  1088 
   989 /**
  1089 /**
   990  * Retrieve the number of database queries during the WordPress execution.
  1090  * Retrieve the number of database queries during the WordPress execution.
   991  *
  1091  *
   992  * @since 2.0.0
  1092  * @since 2.0.0
   993  *
  1093  *
   994  * @return int Number of database queries
  1094  * @global wpdb $wpdb WordPress database abstraction object.
       
  1095  *
       
  1096  * @return int Number of database queries.
   995  */
  1097  */
   996 function get_num_queries() {
  1098 function get_num_queries() {
   997 	global $wpdb;
  1099 	global $wpdb;
   998 	return $wpdb->num_queries;
  1100 	return $wpdb->num_queries;
   999 }
  1101 }
  1000 
  1102 
  1001 /**
  1103 /**
  1002  * Whether input is yes or no. Must be 'y' to be true.
  1104  * Whether input is yes or no.
       
  1105  *
       
  1106  * Must be 'y' to be true.
  1003  *
  1107  *
  1004  * @since 1.0.0
  1108  * @since 1.0.0
  1005  *
  1109  *
  1006  * @param string $yn Character string containing either 'y' or 'n'
  1110  * @param string $yn Character string containing either 'y' (yes) or 'n' (no).
  1007  * @return bool True if yes, false on anything else
  1111  * @return bool True if yes, false on anything else.
  1008  */
  1112  */
  1009 function bool_from_yn( $yn ) {
  1113 function bool_from_yn( $yn ) {
  1010 	return ( strtolower( $yn ) == 'y' );
  1114 	return ( strtolower( $yn ) == 'y' );
  1011 }
  1115 }
  1012 
  1116 
  1013 /**
  1117 /**
  1014  * Loads the feed template from the use of an action hook.
  1118  * Load the feed template from the use of an action hook.
  1015  *
  1119  *
  1016  * If the feed action does not have a hook, then the function will die with a
  1120  * If the feed action does not have a hook, then the function will die with a
  1017  * message telling the visitor that the feed is not valid.
  1121  * message telling the visitor that the feed is not valid.
  1018  *
  1122  *
  1019  * It is better to only have one hook for each feed.
  1123  * It is better to only have one hook for each feed.
  1020  *
  1124  *
  1021  * @since 2.1.0
  1125  * @since 2.1.0
       
  1126  *
  1022  * @uses $wp_query Used to tell if the use a comment feed.
  1127  * @uses $wp_query Used to tell if the use a comment feed.
  1023  * @uses do_action() Calls 'do_feed_$feed' hook, if a hook exists for the feed.
       
  1024  */
  1128  */
  1025 function do_feed() {
  1129 function do_feed() {
  1026 	global $wp_query;
  1130 	global $wp_query;
  1027 
  1131 
  1028 	$feed = get_query_var( 'feed' );
  1132 	$feed = get_query_var( 'feed' );
  1035 
  1139 
  1036 	$hook = 'do_feed_' . $feed;
  1140 	$hook = 'do_feed_' . $feed;
  1037 	if ( ! has_action( $hook ) )
  1141 	if ( ! has_action( $hook ) )
  1038 		wp_die( __( 'ERROR: This is not a valid feed template.' ), '', array( 'response' => 404 ) );
  1142 		wp_die( __( 'ERROR: This is not a valid feed template.' ), '', array( 'response' => 404 ) );
  1039 
  1143 
       
  1144 	/**
       
  1145 	 * Fires once the given feed is loaded.
       
  1146 	 *
       
  1147 	 * The dynamic hook name, $hook, refers to the feed name.
       
  1148 	 *
       
  1149 	 * @since 2.1.0
       
  1150 	 *
       
  1151 	 * @param bool $is_comment_feed Whether the feed is a comment feed.
       
  1152 	 */
  1040 	do_action( $hook, $wp_query->is_comment_feed );
  1153 	do_action( $hook, $wp_query->is_comment_feed );
  1041 }
  1154 }
  1042 
  1155 
  1043 /**
  1156 /**
  1044  * Load the RDF RSS 0.91 Feed template.
  1157  * Load the RDF RSS 0.91 Feed template.
  1045  *
  1158  *
  1046  * @since 2.1.0
  1159  * @since 2.1.0
       
  1160  *
       
  1161  * @see load_template()
  1047  */
  1162  */
  1048 function do_feed_rdf() {
  1163 function do_feed_rdf() {
  1049 	load_template( ABSPATH . WPINC . '/feed-rdf.php' );
  1164 	load_template( ABSPATH . WPINC . '/feed-rdf.php' );
  1050 }
  1165 }
  1051 
  1166 
  1052 /**
  1167 /**
  1053  * Load the RSS 1.0 Feed Template.
  1168  * Load the RSS 1.0 Feed Template.
  1054  *
  1169  *
  1055  * @since 2.1.0
  1170  * @since 2.1.0
       
  1171  *
       
  1172  * @see load_template()
  1056  */
  1173  */
  1057 function do_feed_rss() {
  1174 function do_feed_rss() {
  1058 	load_template( ABSPATH . WPINC . '/feed-rss.php' );
  1175 	load_template( ABSPATH . WPINC . '/feed-rss.php' );
  1059 }
  1176 }
  1060 
  1177 
  1061 /**
  1178 /**
  1062  * Load either the RSS2 comment feed or the RSS2 posts feed.
  1179  * Load either the RSS2 comment feed or the RSS2 posts feed.
  1063  *
  1180  *
  1064  * @since 2.1.0
  1181  * @since 2.1.0
       
  1182  *
       
  1183  * @see load_template()
  1065  *
  1184  *
  1066  * @param bool $for_comments True for the comment feed, false for normal feed.
  1185  * @param bool $for_comments True for the comment feed, false for normal feed.
  1067  */
  1186  */
  1068 function do_feed_rss2( $for_comments ) {
  1187 function do_feed_rss2( $for_comments ) {
  1069 	if ( $for_comments )
  1188 	if ( $for_comments )
  1075 /**
  1194 /**
  1076  * Load either Atom comment feed or Atom posts feed.
  1195  * Load either Atom comment feed or Atom posts feed.
  1077  *
  1196  *
  1078  * @since 2.1.0
  1197  * @since 2.1.0
  1079  *
  1198  *
       
  1199  * @see load_template()
       
  1200  *
  1080  * @param bool $for_comments True for the comment feed, false for normal feed.
  1201  * @param bool $for_comments True for the comment feed, false for normal feed.
  1081  */
  1202  */
  1082 function do_feed_atom( $for_comments ) {
  1203 function do_feed_atom( $for_comments ) {
  1083 	if ($for_comments)
  1204 	if ($for_comments)
  1084 		load_template( ABSPATH . WPINC . '/feed-atom-comments.php');
  1205 		load_template( ABSPATH . WPINC . '/feed-atom-comments.php');
  1091  *
  1212  *
  1092  * The echo content should be with usage of the permalinks or for creating the
  1213  * The echo content should be with usage of the permalinks or for creating the
  1093  * robots.txt file.
  1214  * robots.txt file.
  1094  *
  1215  *
  1095  * @since 2.1.0
  1216  * @since 2.1.0
  1096  * @uses do_action() Calls 'do_robotstxt' hook for displaying robots.txt rules.
       
  1097  */
  1217  */
  1098 function do_robots() {
  1218 function do_robots() {
  1099 	header( 'Content-Type: text/plain; charset=utf-8' );
  1219 	header( 'Content-Type: text/plain; charset=utf-8' );
  1100 
  1220 
       
  1221 	/**
       
  1222 	 * Fires when displaying the robots.txt file.
       
  1223 	 *
       
  1224 	 * @since 2.1.0
       
  1225 	 */
  1101 	do_action( 'do_robotstxt' );
  1226 	do_action( 'do_robotstxt' );
  1102 
  1227 
  1103 	$output = "User-agent: *\n";
  1228 	$output = "User-agent: *\n";
  1104 	$public = get_option( 'blog_public' );
  1229 	$public = get_option( 'blog_public' );
  1105 	if ( '0' == $public ) {
  1230 	if ( '0' == $public ) {
  1106 		$output .= "Disallow: /\n";
  1231 		$output .= "Disallow: /\n";
  1107 	} else {
  1232 	} else {
  1108 		$site_url = parse_url( site_url() );
  1233 		$site_url = parse_url( site_url() );
  1109 		$path = ( !empty( $site_url['path'] ) ) ? $site_url['path'] : '';
  1234 		$path = ( !empty( $site_url['path'] ) ) ? $site_url['path'] : '';
  1110 		$output .= "Disallow: $path/wp-admin/\n";
  1235 		$output .= "Disallow: $path/wp-admin/\n";
  1111 		$output .= "Disallow: $path/wp-includes/\n";
  1236 	}
  1112 	}
  1237 
  1113 
  1238 	/**
  1114 	echo apply_filters('robots_txt', $output, $public);
  1239 	 * Filter the robots.txt output.
       
  1240 	 *
       
  1241 	 * @since 3.0.0
       
  1242 	 *
       
  1243 	 * @param string $output Robots.txt output.
       
  1244 	 * @param bool   $public Whether the site is considered "public".
       
  1245 	 */
       
  1246 	echo apply_filters( 'robots_txt', $output, $public );
  1115 }
  1247 }
  1116 
  1248 
  1117 /**
  1249 /**
  1118  * Test whether blog is already installed.
  1250  * Test whether blog is already installed.
  1119  *
  1251  *
  1120  * The cache will be checked first. If you have a cache plugin, which saves the
  1252  * The cache will be checked first. If you have a cache plugin, which saves
  1121  * cache values, then this will work. If you use the default WordPress cache,
  1253  * the cache values, then this will work. If you use the default WordPress
  1122  * and the database goes away, then you might have problems.
  1254  * cache, and the database goes away, then you might have problems.
  1123  *
  1255  *
  1124  * Checks for the option siteurl for whether WordPress is installed.
  1256  * Checks for the 'siteurl' option for whether WordPress is installed.
  1125  *
  1257  *
  1126  * @since 2.1.0
  1258  * @since 2.1.0
  1127  * @uses $wpdb
  1259  *
  1128  *
  1260  * @global wpdb $wpdb WordPress database abstraction object.
  1129  * @return bool Whether blog is already installed.
  1261  *
       
  1262  * @return bool Whether the blog is already installed.
  1130  */
  1263  */
  1131 function is_blog_installed() {
  1264 function is_blog_installed() {
  1132 	global $wpdb;
  1265 	global $wpdb;
  1133 
  1266 
  1134 	// Check cache first. If options table goes away and we have true cached, oh well.
  1267 	/*
       
  1268 	 * Check cache first. If options table goes away and we have true
       
  1269 	 * cached, oh well.
       
  1270 	 */
  1135 	if ( wp_cache_get( 'is_blog_installed' ) )
  1271 	if ( wp_cache_get( 'is_blog_installed' ) )
  1136 		return true;
  1272 		return true;
  1137 
  1273 
  1138 	$suppress = $wpdb->suppress_errors();
  1274 	$suppress = $wpdb->suppress_errors();
  1139 	if ( ! defined( 'WP_INSTALLING' ) ) {
  1275 	if ( ! defined( 'WP_INSTALLING' ) ) {
  1156 	if ( defined( 'WP_REPAIRING' ) )
  1292 	if ( defined( 'WP_REPAIRING' ) )
  1157 		return true;
  1293 		return true;
  1158 
  1294 
  1159 	$suppress = $wpdb->suppress_errors();
  1295 	$suppress = $wpdb->suppress_errors();
  1160 
  1296 
  1161 	// Loop over the WP tables. If none exist, then scratch install is allowed.
  1297 	/*
  1162 	// If one or more exist, suggest table repair since we got here because the options
  1298 	 * Loop over the WP tables. If none exist, then scratch install is allowed.
  1163 	// table could not be accessed.
  1299 	 * If one or more exist, suggest table repair since we got here because the
       
  1300 	 * options table could not be accessed.
       
  1301 	 */
  1164 	$wp_tables = $wpdb->tables();
  1302 	$wp_tables = $wpdb->tables();
  1165 	foreach ( $wp_tables as $table ) {
  1303 	foreach ( $wp_tables as $table ) {
  1166 		// The existence of custom user tables shouldn't suggest an insane state or prevent a clean install.
  1304 		// The existence of custom user tables shouldn't suggest an insane state or prevent a clean install.
  1167 		if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE == $table )
  1305 		if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE == $table )
  1168 			continue;
  1306 			continue;
  1189 }
  1327 }
  1190 
  1328 
  1191 /**
  1329 /**
  1192  * Retrieve URL with nonce added to URL query.
  1330  * Retrieve URL with nonce added to URL query.
  1193  *
  1331  *
  1194  * @package WordPress
       
  1195  * @subpackage Security
       
  1196  * @since 2.0.4
  1332  * @since 2.0.4
  1197  *
  1333  *
  1198  * @param string $actionurl URL to add nonce action.
  1334  * @param string     $actionurl URL to add nonce action.
  1199  * @param string $action Optional. Nonce action name.
  1335  * @param int|string $action    Optional. Nonce action name. Default -1.
  1200  * @param string $name Optional. Nonce name.
  1336  * @param string     $name      Optional. Nonce name. Default '_wpnonce'.
  1201  * @return string URL with nonce action added.
  1337  * @return string Escaped URL with nonce action added.
  1202  */
  1338  */
  1203 function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) {
  1339 function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) {
  1204 	$actionurl = str_replace( '&amp;', '&', $actionurl );
  1340 	$actionurl = str_replace( '&amp;', '&', $actionurl );
  1205 	return esc_html( add_query_arg( $name, wp_create_nonce( $action ), $actionurl ) );
  1341 	return esc_html( add_query_arg( $name, wp_create_nonce( $action ), $actionurl ) );
  1206 }
  1342 }
  1221  * damage.
  1357  * damage.
  1222  *
  1358  *
  1223  * The input name will be whatever $name value you gave. The input value will be
  1359  * The input name will be whatever $name value you gave. The input value will be
  1224  * the nonce creation value.
  1360  * the nonce creation value.
  1225  *
  1361  *
  1226  * @package WordPress
       
  1227  * @subpackage Security
       
  1228  * @since 2.0.4
  1362  * @since 2.0.4
  1229  *
  1363  *
  1230  * @param string $action Optional. Action name.
  1364  * @param int|string $action  Optional. Action name. Default -1.
  1231  * @param string $name Optional. Nonce name.
  1365  * @param string     $name    Optional. Nonce name. Default '_wpnonce'.
  1232  * @param bool $referer Optional, default true. Whether to set the referer field for validation.
  1366  * @param bool       $referer Optional. Whether to set the referer field for validation. Default true.
  1233  * @param bool $echo Optional, default true. Whether to display or return hidden form field.
  1367  * @param bool       $echo    Optional. Whether to display or return hidden form field. Default true.
  1234  * @return string Nonce field.
  1368  * @return string Nonce field HTML markup.
  1235  */
  1369  */
  1236 function wp_nonce_field( $action = -1, $name = "_wpnonce", $referer = true , $echo = true ) {
  1370 function wp_nonce_field( $action = -1, $name = "_wpnonce", $referer = true , $echo = true ) {
  1237 	$name = esc_attr( $name );
  1371 	$name = esc_attr( $name );
  1238 	$nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />';
  1372 	$nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />';
  1239 
  1373 
  1250  * Retrieve or display referer hidden field for forms.
  1384  * Retrieve or display referer hidden field for forms.
  1251  *
  1385  *
  1252  * The referer link is the current Request URI from the server super global. The
  1386  * The referer link is the current Request URI from the server super global. The
  1253  * input name is '_wp_http_referer', in case you wanted to check manually.
  1387  * input name is '_wp_http_referer', in case you wanted to check manually.
  1254  *
  1388  *
  1255  * @package WordPress
       
  1256  * @subpackage Security
       
  1257  * @since 2.0.4
  1389  * @since 2.0.4
  1258  *
  1390  *
  1259  * @param bool $echo Whether to echo or return the referer field.
  1391  * @param bool $echo Optional. Whether to echo or return the referer field. Default true.
  1260  * @return string Referer field.
  1392  * @return string Referer field HTML markup.
  1261  */
  1393  */
  1262 function wp_referer_field( $echo = true ) {
  1394 function wp_referer_field( $echo = true ) {
  1263 	$referer_field = '<input type="hidden" name="_wp_http_referer" value="'. esc_attr( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . '" />';
  1395 	$referer_field = '<input type="hidden" name="_wp_http_referer" value="'. esc_attr( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . '" />';
  1264 
  1396 
  1265 	if ( $echo )
  1397 	if ( $echo )
  1269 
  1401 
  1270 /**
  1402 /**
  1271  * Retrieve or display original referer hidden field for forms.
  1403  * Retrieve or display original referer hidden field for forms.
  1272  *
  1404  *
  1273  * The input name is '_wp_original_http_referer' and will be either the same
  1405  * The input name is '_wp_original_http_referer' and will be either the same
  1274  * value of {@link wp_referer_field()}, if that was posted already or it will
  1406  * value of wp_referer_field(), if that was posted already or it will be the
  1275  * be the current page, if it doesn't exist.
  1407  * current page, if it doesn't exist.
  1276  *
  1408  *
  1277  * @package WordPress
       
  1278  * @subpackage Security
       
  1279  * @since 2.0.4
  1409  * @since 2.0.4
  1280  *
  1410  *
  1281  * @param bool $echo Whether to echo the original http referer
  1411  * @param bool   $echo         Optional. Whether to echo the original http referer. Default true.
  1282  * @param string $jump_back_to Optional, default is 'current'. Can be 'previous' or page you want to jump back to.
  1412  * @param string $jump_back_to Optional. Can be 'previous' or page you want to jump back to.
       
  1413  *                             Default 'current'.
  1283  * @return string Original referer field.
  1414  * @return string Original referer field.
  1284  */
  1415  */
  1285 function wp_original_referer_field( $echo = true, $jump_back_to = 'current' ) {
  1416 function wp_original_referer_field( $echo = true, $jump_back_to = 'current' ) {
  1286 	if ( ! $ref = wp_get_original_referer() ) {
  1417 	if ( ! $ref = wp_get_original_referer() ) {
  1287 		$ref = 'previous' == $jump_back_to ? wp_get_referer() : wp_unslash( $_SERVER['REQUEST_URI'] );
  1418 		$ref = 'previous' == $jump_back_to ? wp_get_referer() : wp_unslash( $_SERVER['REQUEST_URI'] );
  1291 		echo $orig_referer_field;
  1422 		echo $orig_referer_field;
  1292 	return $orig_referer_field;
  1423 	return $orig_referer_field;
  1293 }
  1424 }
  1294 
  1425 
  1295 /**
  1426 /**
  1296  * Retrieve referer from '_wp_http_referer' or HTTP referer. If it's the same
  1427  * Retrieve referer from '_wp_http_referer' or HTTP referer.
  1297  * as the current request URL, will return false.
  1428  *
  1298  *
  1429  * If it's the same as the current request URL, will return false.
  1299  * @package WordPress
  1430  *
  1300  * @subpackage Security
       
  1301  * @since 2.0.4
  1431  * @since 2.0.4
  1302  *
  1432  *
  1303  * @return string|bool False on failure. Referer URL on success.
  1433  * @return false|string False on failure. Referer URL on success.
  1304  */
  1434  */
  1305 function wp_get_referer() {
  1435 function wp_get_referer() {
  1306 	if ( ! function_exists( 'wp_validate_redirect' ) )
  1436 	if ( ! function_exists( 'wp_validate_redirect' ) )
  1307 		return false;
  1437 		return false;
  1308 	$ref = false;
  1438 	$ref = false;
  1309 	if ( ! empty( $_REQUEST['_wp_http_referer'] ) )
  1439 	if ( ! empty( $_REQUEST['_wp_http_referer'] ) )
  1310 		$ref = wp_unslash( $_REQUEST['_wp_http_referer'] );
  1440 		$ref = wp_unslash( $_REQUEST['_wp_http_referer'] );
  1311 	else if ( ! empty( $_SERVER['HTTP_REFERER'] ) )
  1441 	elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) )
  1312 		$ref = wp_unslash( $_SERVER['HTTP_REFERER'] );
  1442 		$ref = wp_unslash( $_SERVER['HTTP_REFERER'] );
  1313 
  1443 
  1314 	if ( $ref && $ref !== wp_unslash( $_SERVER['REQUEST_URI'] ) )
  1444 	if ( $ref && $ref !== wp_unslash( $_SERVER['REQUEST_URI'] ) )
  1315 		return wp_validate_redirect( $ref, false );
  1445 		return wp_validate_redirect( $ref, false );
  1316 	return false;
  1446 	return false;
  1317 }
  1447 }
  1318 
  1448 
  1319 /**
  1449 /**
  1320  * Retrieve original referer that was posted, if it exists.
  1450  * Retrieve original referer that was posted, if it exists.
  1321  *
  1451  *
  1322  * @package WordPress
       
  1323  * @subpackage Security
       
  1324  * @since 2.0.4
  1452  * @since 2.0.4
  1325  *
  1453  *
  1326  * @return string|bool False if no original referer or original referer if set.
  1454  * @return string|false False if no original referer or original referer if set.
  1327  */
  1455  */
  1328 function wp_get_original_referer() {
  1456 function wp_get_original_referer() {
  1329 	if ( ! empty( $_REQUEST['_wp_original_http_referer'] ) && function_exists( 'wp_validate_redirect' ) )
  1457 	if ( ! empty( $_REQUEST['_wp_original_http_referer'] ) && function_exists( 'wp_validate_redirect' ) )
  1330 		return wp_validate_redirect( wp_unslash( $_REQUEST['_wp_original_http_referer'] ), false );
  1458 		return wp_validate_redirect( wp_unslash( $_REQUEST['_wp_original_http_referer'] ), false );
  1331 	return false;
  1459 	return false;
  1342  * @return bool Whether the path was created. True if path already exists.
  1470  * @return bool Whether the path was created. True if path already exists.
  1343  */
  1471  */
  1344 function wp_mkdir_p( $target ) {
  1472 function wp_mkdir_p( $target ) {
  1345 	$wrapper = null;
  1473 	$wrapper = null;
  1346 
  1474 
  1347 	// strip the protocol
  1475 	// Strip the protocol.
  1348 	if( wp_is_stream( $target ) ) {
  1476 	if( wp_is_stream( $target ) ) {
  1349 		list( $wrapper, $target ) = explode( '://', $target, 2 );
  1477 		list( $wrapper, $target ) = explode( '://', $target, 2 );
  1350 	}
  1478 	}
  1351 
  1479 
  1352 	// from php.net/mkdir user contributed notes
  1480 	// From php.net/mkdir user contributed notes.
  1353 	$target = str_replace( '//', '/', $target );
  1481 	$target = str_replace( '//', '/', $target );
  1354 
  1482 
  1355 	// put the wrapper back on the target
  1483 	// Put the wrapper back on the target.
  1356 	if( $wrapper !== null ) {
  1484 	if( $wrapper !== null ) {
  1357 		$target = $wrapper . '://' . $target;
  1485 		$target = $wrapper . '://' . $target;
  1358 	}
  1486 	}
  1359 
  1487 
  1360 	// safe mode fails with a trailing slash under certain PHP versions.
  1488 	/*
  1361 	$target = rtrim($target, '/'); // Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.
  1489 	 * Safe mode fails with a trailing slash under certain PHP versions.
       
  1490 	 * Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.
       
  1491 	 */
       
  1492 	$target = rtrim($target, '/');
  1362 	if ( empty($target) )
  1493 	if ( empty($target) )
  1363 		$target = '/';
  1494 		$target = '/';
  1364 
  1495 
  1365 	if ( file_exists( $target ) )
  1496 	if ( file_exists( $target ) )
  1366 		return @is_dir( $target );
  1497 		return @is_dir( $target );
  1370 	while ( '.' != $target_parent && ! is_dir( $target_parent ) ) {
  1501 	while ( '.' != $target_parent && ! is_dir( $target_parent ) ) {
  1371 		$target_parent = dirname( $target_parent );
  1502 		$target_parent = dirname( $target_parent );
  1372 	}
  1503 	}
  1373 
  1504 
  1374 	// Get the permission bits.
  1505 	// Get the permission bits.
  1375 	if ( $target_parent && '.' != $target_parent ) {
  1506 	if ( $stat = @stat( $target_parent ) ) {
  1376 		$stat = @stat( $target_parent );
       
  1377 		$dir_perms = $stat['mode'] & 0007777;
  1507 		$dir_perms = $stat['mode'] & 0007777;
  1378 	} else {
  1508 	} else {
  1379 		$dir_perms = 0777;
  1509 		$dir_perms = 0777;
  1380 	}
  1510 	}
  1381 
  1511 
  1382 	if ( @mkdir( $target, $dir_perms, true ) ) {
  1512 	if ( @mkdir( $target, $dir_perms, true ) ) {
       
  1513 
       
  1514 		/*
       
  1515 		 * If a umask is set that modifies $dir_perms, we'll have to re-set
       
  1516 		 * the $dir_perms correctly with chmod()
       
  1517 		 */
       
  1518 		if ( $dir_perms != ( $dir_perms & ~umask() ) ) {
       
  1519 			$folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) );
       
  1520 			for ( $i = 1, $c = count( $folder_parts ); $i <= $c; $i++ ) {
       
  1521 				@chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms );
       
  1522 			}
       
  1523 		}
       
  1524 
  1383 		return true;
  1525 		return true;
  1384 	}
  1526 	}
  1385 
  1527 
  1386 	return false;
  1528 	return false;
  1387 }
  1529 }
  1388 
  1530 
  1389 /**
  1531 /**
  1390  * Test if a give filesystem path is absolute ('/foo/bar', 'c:\windows').
  1532  * Test if a give filesystem path is absolute.
       
  1533  *
       
  1534  * For example, '/foo/bar', or 'c:\windows'.
  1391  *
  1535  *
  1392  * @since 2.5.0
  1536  * @since 2.5.0
  1393  *
  1537  *
  1394  * @param string $path File path
  1538  * @param string $path File path.
  1395  * @return bool True if path is absolute, false is not absolute.
  1539  * @return bool True if path is absolute, false is not absolute.
  1396  */
  1540  */
  1397 function path_is_absolute( $path ) {
  1541 function path_is_absolute( $path ) {
  1398 	// this is definitive if true but fails if $path does not exist or contains a symbolic link
  1542 	/*
       
  1543 	 * This is definitive if true but fails if $path does not exist or contains
       
  1544 	 * a symbolic link.
       
  1545 	 */
  1399 	if ( realpath($path) == $path )
  1546 	if ( realpath($path) == $path )
  1400 		return true;
  1547 		return true;
  1401 
  1548 
  1402 	if ( strlen($path) == 0 || $path[0] == '.' )
  1549 	if ( strlen($path) == 0 || $path[0] == '.' )
  1403 		return false;
  1550 		return false;
  1404 
  1551 
  1405 	// windows allows absolute paths like this
  1552 	// Windows allows absolute paths like this.
  1406 	if ( preg_match('#^[a-zA-Z]:\\\\#', $path) )
  1553 	if ( preg_match('#^[a-zA-Z]:\\\\#', $path) )
  1407 		return true;
  1554 		return true;
  1408 
  1555 
  1409 	// a path starting with / or \ is absolute; anything else is relative
  1556 	// A path starting with / or \ is absolute; anything else is relative.
  1410 	return ( $path[0] == '/' || $path[0] == '\\' );
  1557 	return ( $path[0] == '/' || $path[0] == '\\' );
  1411 }
  1558 }
  1412 
  1559 
  1413 /**
  1560 /**
  1414  * Join two filesystem paths together (e.g. 'give me $path relative to $base').
  1561  * Join two filesystem paths together.
  1415  *
  1562  *
  1416  * If the $path is absolute, then it the full path is returned.
  1563  * For example, 'give me $path relative to $base'. If the $path is absolute,
       
  1564  * then it the full path is returned.
  1417  *
  1565  *
  1418  * @since 2.5.0
  1566  * @since 2.5.0
  1419  *
  1567  *
  1420  * @param string $base
  1568  * @param string $base Base path.
  1421  * @param string $path
  1569  * @param string $path Path relative to $base.
  1422  * @return string The path with the base or absolute path.
  1570  * @return string The path with the base or absolute path.
  1423  */
  1571  */
  1424 function path_join( $base, $path ) {
  1572 function path_join( $base, $path ) {
  1425 	if ( path_is_absolute($path) )
  1573 	if ( path_is_absolute($path) )
  1426 		return $path;
  1574 		return $path;
  1427 
  1575 
  1428 	return rtrim($base, '/') . '/' . ltrim($path, '/');
  1576 	return rtrim($base, '/') . '/' . ltrim($path, '/');
  1429 }
  1577 }
  1430 
  1578 
  1431 /**
  1579 /**
  1432  * Determines a writable directory for temporary files.
  1580  * Normalize a filesystem path.
  1433  * Function's preference is the return value of <code>sys_get_temp_dir()</code>,
  1581  *
       
  1582  * Replaces backslashes with forward slashes for Windows systems, and ensures
       
  1583  * no duplicate slashes exist.
       
  1584  *
       
  1585  * @since 3.9.0
       
  1586  *
       
  1587  * @param string $path Path to normalize.
       
  1588  * @return string Normalized path.
       
  1589  */
       
  1590 function wp_normalize_path( $path ) {
       
  1591 	$path = str_replace( '\\', '/', $path );
       
  1592 	$path = preg_replace( '|/+|','/', $path );
       
  1593 	return $path;
       
  1594 }
       
  1595 
       
  1596 /**
       
  1597  * Determine a writable directory for temporary files.
       
  1598  *
       
  1599  * Function's preference is the return value of sys_get_temp_dir(),
  1434  * followed by your PHP temporary upload directory, followed by WP_CONTENT_DIR,
  1600  * followed by your PHP temporary upload directory, followed by WP_CONTENT_DIR,
  1435  * before finally defaulting to /tmp/
  1601  * before finally defaulting to /tmp/
  1436  *
  1602  *
  1437  * In the event that this function does not find a writable location,
  1603  * In the event that this function does not find a writable location,
  1438  * It may be overridden by the <code>WP_TEMP_DIR</code> constant in
  1604  * It may be overridden by the WP_TEMP_DIR constant in your wp-config.php file.
  1439  * your <code>wp-config.php</code> file.
       
  1440  *
  1605  *
  1441  * @since 2.5.0
  1606  * @since 2.5.0
  1442  *
  1607  *
  1443  * @return string Writable temporary directory
  1608  * @return string Writable temporary directory.
  1444  */
  1609  */
  1445 function get_temp_dir() {
  1610 function get_temp_dir() {
  1446 	static $temp;
  1611 	static $temp;
  1447 	if ( defined('WP_TEMP_DIR') )
  1612 	if ( defined('WP_TEMP_DIR') )
  1448 		return trailingslashit(WP_TEMP_DIR);
  1613 		return trailingslashit(WP_TEMP_DIR);
  1449 
  1614 
  1450 	if ( $temp )
  1615 	if ( $temp )
  1451 		return trailingslashit( rtrim( $temp, '\\' ) );
  1616 		return trailingslashit( $temp );
  1452 
  1617 
  1453 	if ( function_exists('sys_get_temp_dir') ) {
  1618 	if ( function_exists('sys_get_temp_dir') ) {
  1454 		$temp = sys_get_temp_dir();
  1619 		$temp = sys_get_temp_dir();
  1455 		if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
  1620 		if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
  1456 			return trailingslashit( rtrim( $temp, '\\' ) );
  1621 			return trailingslashit( $temp );
  1457 	}
  1622 	}
  1458 
  1623 
  1459 	$temp = ini_get('upload_tmp_dir');
  1624 	$temp = ini_get('upload_tmp_dir');
  1460 	if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
  1625 	if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
  1461 		return trailingslashit( rtrim( $temp, '\\' ) );
  1626 		return trailingslashit( $temp );
  1462 
  1627 
  1463 	$temp = WP_CONTENT_DIR . '/';
  1628 	$temp = WP_CONTENT_DIR . '/';
  1464 	if ( is_dir( $temp ) && wp_is_writable( $temp ) )
  1629 	if ( is_dir( $temp ) && wp_is_writable( $temp ) )
  1465 		return $temp;
  1630 		return $temp;
  1466 
  1631 
  1469 }
  1634 }
  1470 
  1635 
  1471 /**
  1636 /**
  1472  * Determine if a directory is writable.
  1637  * Determine if a directory is writable.
  1473  *
  1638  *
  1474  * This function is used to work around certain ACL issues
  1639  * This function is used to work around certain ACL issues in PHP primarily
  1475  * in PHP primarily affecting Windows Servers.
  1640  * affecting Windows Servers.
       
  1641  *
       
  1642  * @since 3.6.0
  1476  *
  1643  *
  1477  * @see win_is_writable()
  1644  * @see win_is_writable()
  1478  *
  1645  *
  1479  * @since 3.6.0
  1646  * @param string $path Path to check for write-ability.
  1480  *
  1647  * @return bool Whether the path is writable.
  1481  * @param string $path
       
  1482  * @return bool
       
  1483  */
  1648  */
  1484 function wp_is_writable( $path ) {
  1649 function wp_is_writable( $path ) {
  1485 	if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) )
  1650 	if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) )
  1486 		return win_is_writable( $path );
  1651 		return win_is_writable( $path );
  1487 	else
  1652 	else
  1494  * PHP has issues with Windows ACL's for determine if a
  1659  * PHP has issues with Windows ACL's for determine if a
  1495  * directory is writable or not, this works around them by
  1660  * directory is writable or not, this works around them by
  1496  * checking the ability to open files rather than relying
  1661  * checking the ability to open files rather than relying
  1497  * upon PHP to interprate the OS ACL.
  1662  * upon PHP to interprate the OS ACL.
  1498  *
  1663  *
  1499  * @link http://bugs.php.net/bug.php?id=27609
       
  1500  * @link http://bugs.php.net/bug.php?id=30931
       
  1501  *
       
  1502  * @since 2.8.0
  1664  * @since 2.8.0
  1503  *
  1665  *
  1504  * @param string $path
  1666  * @see http://bugs.php.net/bug.php?id=27609
  1505  * @return bool
  1667  * @see http://bugs.php.net/bug.php?id=30931
       
  1668  *
       
  1669  * @param string $path Windows path to check for write-ability.
       
  1670  * @return bool Whether the path is writable.
  1506  */
  1671  */
  1507 function win_is_writable( $path ) {
  1672 function win_is_writable( $path ) {
  1508 
  1673 
  1509 	if ( $path[strlen( $path ) - 1] == '/' ) // if it looks like a directory, check a random file within the directory
  1674 	if ( $path[strlen( $path ) - 1] == '/' ) { // if it looks like a directory, check a random file within the directory
  1510 		return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp');
  1675 		return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp');
  1511 	else if ( is_dir( $path ) ) // If it's a directory (and not a file) check a random file within the directory
  1676 	} elseif ( is_dir( $path ) ) { // If it's a directory (and not a file) check a random file within the directory
  1512 		return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' );
  1677 		return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' );
  1513 
  1678 	}
  1514 	// check tmp file for read/write capabilities
  1679 	// check tmp file for read/write capabilities
  1515 	$should_delete_tmp_file = !file_exists( $path );
  1680 	$should_delete_tmp_file = !file_exists( $path );
  1516 	$f = @fopen( $path, 'a' );
  1681 	$f = @fopen( $path, 'a' );
  1517 	if ( $f === false )
  1682 	if ( $f === false )
  1518 		return false;
  1683 		return false;
  1548  * 'basedir' - path without subdir.
  1713  * 'basedir' - path without subdir.
  1549  * 'baseurl' - URL path without subdir.
  1714  * 'baseurl' - URL path without subdir.
  1550  * 'error' - set to false.
  1715  * 'error' - set to false.
  1551  *
  1716  *
  1552  * @since 2.0.0
  1717  * @since 2.0.0
  1553  * @uses apply_filters() Calls 'upload_dir' on returned array.
  1718  *
  1554  *
  1719  * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
  1555  * @param string $time Optional. Time formatted in 'yyyy/mm'.
       
  1556  * @return array See above for description.
  1720  * @return array See above for description.
  1557  */
  1721  */
  1558 function wp_upload_dir( $time = null ) {
  1722 function wp_upload_dir( $time = null ) {
  1559 	$siteurl = get_option( 'siteurl' );
  1723 	$siteurl = get_option( 'siteurl' );
  1560 	$upload_path = trim( get_option( 'upload_path' ) );
  1724 	$upload_path = trim( get_option( 'upload_path' ) );
  1573 			$url = WP_CONTENT_URL . '/uploads';
  1737 			$url = WP_CONTENT_URL . '/uploads';
  1574 		else
  1738 		else
  1575 			$url = trailingslashit( $siteurl ) . $upload_path;
  1739 			$url = trailingslashit( $siteurl ) . $upload_path;
  1576 	}
  1740 	}
  1577 
  1741 
  1578 	// Obey the value of UPLOADS. This happens as long as ms-files rewriting is disabled.
  1742 	/*
  1579 	// We also sometimes obey UPLOADS when rewriting is enabled -- see the next block.
  1743 	 * Honor the value of UPLOADS. This happens as long as ms-files rewriting is disabled.
       
  1744 	 * We also sometimes obey UPLOADS when rewriting is enabled -- see the next block.
       
  1745 	 */
  1580 	if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) {
  1746 	if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) {
  1581 		$dir = ABSPATH . UPLOADS;
  1747 		$dir = ABSPATH . UPLOADS;
  1582 		$url = trailingslashit( $siteurl ) . UPLOADS;
  1748 		$url = trailingslashit( $siteurl ) . UPLOADS;
  1583 	}
  1749 	}
  1584 
  1750 
  1585 	// If multisite (and if not the main site in a post-MU network)
  1751 	// If multisite (and if not the main site in a post-MU network)
  1586 	if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
  1752 	if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
  1587 
  1753 
  1588 		if ( ! get_site_option( 'ms_files_rewriting' ) ) {
  1754 		if ( ! get_site_option( 'ms_files_rewriting' ) ) {
  1589 			// If ms-files rewriting is disabled (networks created post-3.5), it is fairly straightforward:
  1755 			/*
  1590 			// Append sites/%d if we're not on the main site (for post-MU networks). (The extra directory
  1756 			 * If ms-files rewriting is disabled (networks created post-3.5), it is fairly
  1591 			// prevents a four-digit ID from conflicting with a year-based directory for the main site.
  1757 			 * straightforward: Append sites/%d if we're not on the main site (for post-MU
  1592 			// But if a MU-era network has disabled ms-files rewriting manually, they don't need the extra
  1758 			 * networks). (The extra directory prevents a four-digit ID from conflicting with
  1593 			// directory, as they never had wp-content/uploads for the main site.)
  1759 			 * a year-based directory for the main site. But if a MU-era network has disabled
       
  1760 			 * ms-files rewriting manually, they don't need the extra directory, as they never
       
  1761 			 * had wp-content/uploads for the main site.)
       
  1762 			 */
  1594 
  1763 
  1595 			if ( defined( 'MULTISITE' ) )
  1764 			if ( defined( 'MULTISITE' ) )
  1596 				$ms_dir = '/sites/' . get_current_blog_id();
  1765 				$ms_dir = '/sites/' . get_current_blog_id();
  1597 			else
  1766 			else
  1598 				$ms_dir = '/' . get_current_blog_id();
  1767 				$ms_dir = '/' . get_current_blog_id();
  1599 
  1768 
  1600 			$dir .= $ms_dir;
  1769 			$dir .= $ms_dir;
  1601 			$url .= $ms_dir;
  1770 			$url .= $ms_dir;
  1602 
  1771 
  1603 		} elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) {
  1772 		} elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) {
  1604 			// Handle the old-form ms-files.php rewriting if the network still has that enabled.
  1773 			/*
  1605 			// When ms-files rewriting is enabled, then we only listen to UPLOADS when:
  1774 			 * Handle the old-form ms-files.php rewriting if the network still has that enabled.
  1606 			//   1) we are not on the main site in a post-MU network,
  1775 			 * When ms-files rewriting is enabled, then we only listen to UPLOADS when:
  1607 			//      as wp-content/uploads is used there, and
  1776 			 * 1) We are not on the main site in a post-MU network, as wp-content/uploads is used
  1608 			//   2) we are not switched, as ms_upload_constants() hardcodes
  1777 			 *    there, and
  1609 			//      these constants to reflect the original blog ID.
  1778 			 * 2) We are not switched, as ms_upload_constants() hardcodes these constants to reflect
  1610 			//
  1779 			 *    the original blog ID.
  1611 			// Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute.
  1780 			 *
  1612 			// (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as
  1781 			 * Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute.
  1613 			// as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files
  1782 			 * (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as
  1614 			// rewriting in multisite, the resulting URL is /files. (#WP22702 for background.)
  1783 			 * as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files
       
  1784 			 * rewriting in multisite, the resulting URL is /files. (#WP22702 for background.)
       
  1785 			 */
  1615 
  1786 
  1616 			if ( defined( 'BLOGUPLOADDIR' ) )
  1787 			if ( defined( 'BLOGUPLOADDIR' ) )
  1617 				$dir = untrailingslashit( BLOGUPLOADDIR );
  1788 				$dir = untrailingslashit( BLOGUPLOADDIR );
  1618 			else
  1789 			else
  1619 				$dir = ABSPATH . UPLOADS;
  1790 				$dir = ABSPATH . UPLOADS;
  1635 	}
  1806 	}
  1636 
  1807 
  1637 	$dir .= $subdir;
  1808 	$dir .= $subdir;
  1638 	$url .= $subdir;
  1809 	$url .= $subdir;
  1639 
  1810 
       
  1811 	/**
       
  1812 	 * Filter the uploads directory data.
       
  1813 	 *
       
  1814 	 * @since 2.0.0
       
  1815 	 *
       
  1816 	 * @param array $uploads Array of upload directory data with keys of 'path',
       
  1817 	 *                       'url', 'subdir, 'basedir', and 'error'.
       
  1818 	 */
  1640 	$uploads = apply_filters( 'upload_dir',
  1819 	$uploads = apply_filters( 'upload_dir',
  1641 		array(
  1820 		array(
  1642 			'path'    => $dir,
  1821 			'path'    => $dir,
  1643 			'url'     => $url,
  1822 			'url'     => $url,
  1644 			'subdir'  => $subdir,
  1823 			'subdir'  => $subdir,
  1645 			'basedir' => $basedir,
  1824 			'basedir' => $basedir,
  1646 			'baseurl' => $baseurl,
  1825 			'baseurl' => $baseurl,
  1647 			'error'   => false,
  1826 			'error'   => false,
  1648 		) );
  1827 		) );
  1649 
  1828 
  1650 	// Make sure we have an uploads dir
  1829 	// Make sure we have an uploads directory.
  1651 	if ( ! wp_mkdir_p( $uploads['path'] ) ) {
  1830 	if ( ! wp_mkdir_p( $uploads['path'] ) ) {
  1652 		if ( 0 === strpos( $uploads['basedir'], ABSPATH ) )
  1831 		if ( 0 === strpos( $uploads['basedir'], ABSPATH ) )
  1653 			$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
  1832 			$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
  1654 		else
  1833 		else
  1655 			$error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
  1834 			$error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
  1671  * The callback is passed three parameters, the first one is the directory, the
  1850  * The callback is passed three parameters, the first one is the directory, the
  1672  * second is the filename, and the third is the extension.
  1851  * second is the filename, and the third is the extension.
  1673  *
  1852  *
  1674  * @since 2.5.0
  1853  * @since 2.5.0
  1675  *
  1854  *
  1676  * @param string $dir
  1855  * @param string   $dir                      Directory.
  1677  * @param string $filename
  1856  * @param string   $filename                 File name.
  1678  * @param mixed $unique_filename_callback Callback.
  1857  * @param callback $unique_filename_callback Callback. Default null.
  1679  * @return string New filename, if given wasn't unique.
  1858  * @return string New filename, if given wasn't unique.
  1680  */
  1859  */
  1681 function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) {
  1860 function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) {
  1682 	// sanitize the file name before we begin processing
  1861 	// Sanitize the file name before we begin processing.
  1683 	$filename = sanitize_file_name($filename);
  1862 	$filename = sanitize_file_name($filename);
  1684 
  1863 
  1685 	// separate the filename into a name and extension
  1864 	// Separate the filename into a name and extension.
  1686 	$info = pathinfo($filename);
  1865 	$info = pathinfo($filename);
  1687 	$ext = !empty($info['extension']) ? '.' . $info['extension'] : '';
  1866 	$ext = !empty($info['extension']) ? '.' . $info['extension'] : '';
  1688 	$name = basename($filename, $ext);
  1867 	$name = basename($filename, $ext);
  1689 
  1868 
  1690 	// edge case: if file is named '.ext', treat as an empty name
  1869 	// Edge case: if file is named '.ext', treat as an empty name.
  1691 	if ( $name === $ext )
  1870 	if ( $name === $ext )
  1692 		$name = '';
  1871 		$name = '';
  1693 
  1872 
  1694 	// Increment the file number until we have a unique file to save in $dir. Use callback if supplied.
  1873 	/*
       
  1874 	 * Increment the file number until we have a unique file to save in $dir.
       
  1875 	 * Use callback if supplied.
       
  1876 	 */
  1695 	if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) {
  1877 	if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) {
  1696 		$filename = call_user_func( $unique_filename_callback, $dir, $name, $ext );
  1878 		$filename = call_user_func( $unique_filename_callback, $dir, $name, $ext );
  1697 	} else {
  1879 	} else {
  1698 		$number = '';
  1880 		$number = '';
  1699 
  1881 
  1700 		// change '.ext' to lower case
  1882 		// Change '.ext' to lower case.
  1701 		if ( $ext && strtolower($ext) != $ext ) {
  1883 		if ( $ext && strtolower($ext) != $ext ) {
  1702 			$ext2 = strtolower($ext);
  1884 			$ext2 = strtolower($ext);
  1703 			$filename2 = preg_replace( '|' . preg_quote($ext) . '$|', $ext2, $filename );
  1885 			$filename2 = preg_replace( '|' . preg_quote($ext) . '$|', $ext2, $filename );
  1704 
  1886 
  1705 			// check for both lower and upper case extension or image sub-sizes may be overwritten
  1887 			// Check for both lower and upper case extension or image sub-sizes may be overwritten.
  1706 			while ( file_exists($dir . "/$filename") || file_exists($dir . "/$filename2") ) {
  1888 			while ( file_exists($dir . "/$filename") || file_exists($dir . "/$filename2") ) {
  1707 				$new_number = $number + 1;
  1889 				$new_number = $number + 1;
  1708 				$filename = str_replace( "$number$ext", "$new_number$ext", $filename );
  1890 				$filename = str_replace( "$number$ext", "$new_number$ext", $filename );
  1709 				$filename2 = str_replace( "$number$ext2", "$new_number$ext2", $filename2 );
  1891 				$filename2 = str_replace( "$number$ext2", "$new_number$ext2", $filename2 );
  1710 				$number = $new_number;
  1892 				$number = $new_number;
  1738  *
  1920  *
  1739  * The permissions will be set on the new file automatically by this function.
  1921  * The permissions will be set on the new file automatically by this function.
  1740  *
  1922  *
  1741  * @since 2.0.0
  1923  * @since 2.0.0
  1742  *
  1924  *
  1743  * @param string $name
  1925  * @param string       $name       Filename.
  1744  * @param null $deprecated Never used. Set to null.
  1926  * @param null|string  $deprecated Never used. Set to null.
  1745  * @param mixed $bits File content
  1927  * @param mixed        $bits       File content
  1746  * @param string $time Optional. Time formatted in 'yyyy/mm'.
  1928  * @param string       $time       Optional. Time formatted in 'yyyy/mm'. Default null.
  1747  * @return array
  1929  * @return array
  1748  */
  1930  */
  1749 function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {
  1931 function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {
  1750 	if ( !empty( $deprecated ) )
  1932 	if ( !empty( $deprecated ) )
  1751 		_deprecated_argument( __FUNCTION__, '2.0' );
  1933 		_deprecated_argument( __FUNCTION__, '2.0' );
  1760 	$upload = wp_upload_dir( $time );
  1942 	$upload = wp_upload_dir( $time );
  1761 
  1943 
  1762 	if ( $upload['error'] !== false )
  1944 	if ( $upload['error'] !== false )
  1763 		return $upload;
  1945 		return $upload;
  1764 
  1946 
       
  1947 	/**
       
  1948 	 * Filter whether to treat the upload bits as an error.
       
  1949 	 *
       
  1950 	 * Passing a non-array to the filter will effectively short-circuit preparing
       
  1951 	 * the upload bits, returning that value instead.
       
  1952 	 *
       
  1953 	 * @since 3.0.0
       
  1954 	 *
       
  1955 	 * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
       
  1956 	 */
  1765 	$upload_bits_error = apply_filters( 'wp_upload_bits', array( 'name' => $name, 'bits' => $bits, 'time' => $time ) );
  1957 	$upload_bits_error = apply_filters( 'wp_upload_bits', array( 'name' => $name, 'bits' => $bits, 'time' => $time ) );
  1766 	if ( !is_array( $upload_bits_error ) ) {
  1958 	if ( !is_array( $upload_bits_error ) ) {
  1767 		$upload[ 'error' ] = $upload_bits_error;
  1959 		$upload[ 'error' ] = $upload_bits_error;
  1768 		return $upload;
  1960 		return $upload;
  1769 	}
  1961 	}
  1803 }
  1995 }
  1804 
  1996 
  1805 /**
  1997 /**
  1806  * Retrieve the file type based on the extension name.
  1998  * Retrieve the file type based on the extension name.
  1807  *
  1999  *
  1808  * @package WordPress
       
  1809  * @since 2.5.0
  2000  * @since 2.5.0
  1810  * @uses apply_filters() Calls 'ext2type' hook on default supported types.
       
  1811  *
  2001  *
  1812  * @param string $ext The extension to search.
  2002  * @param string $ext The extension to search.
  1813  * @return string|null The file type, example: audio, video, document, spreadsheet, etc. Null if not found.
  2003  * @return string|null The file type, example: audio, video, document, spreadsheet, etc.
       
  2004  *                     Null if not found.
  1814  */
  2005  */
  1815 function wp_ext2type( $ext ) {
  2006 function wp_ext2type( $ext ) {
  1816 	$ext = strtolower( $ext );
  2007 	$ext = strtolower( $ext );
       
  2008 
       
  2009 	/**
       
  2010 	 * Filter file type based on the extension name.
       
  2011 	 *
       
  2012 	 * @since 2.5.0
       
  2013 	 *
       
  2014 	 * @see wp_ext2type()
       
  2015 	 *
       
  2016 	 * @param array $ext2type Multi-dimensional array with extensions for a default set
       
  2017 	 *                        of file types.
       
  2018 	 */
  1817 	$ext2type = apply_filters( 'ext2type', array(
  2019 	$ext2type = apply_filters( 'ext2type', array(
  1818 		'image'       => array( 'jpg', 'jpeg', 'jpe',  'gif',  'png',  'bmp',   'tif',  'tiff', 'ico' ),
  2020 		'image'       => array( 'jpg', 'jpeg', 'jpe',  'gif',  'png',  'bmp',   'tif',  'tiff', 'ico' ),
  1819 		'audio'       => array( 'aac', 'ac3',  'aif',  'aiff', 'm3a',  'm4a',   'm4b',  'mka',  'mp1',  'mp2',  'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
  2021 		'audio'       => array( 'aac', 'ac3',  'aif',  'aiff', 'm3a',  'm4a',   'm4b',  'mka',  'mp1',  'mp2',  'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
  1820 		'video'       => array( 'asf', 'avi',  'divx', 'dv',   'flv',  'm4v',   'mkv',  'mov',  'mp4',  'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt',  'rm', 'vob', 'wmv' ),
  2022 		'video'       => array( '3g2',  '3gp', '3gpp', 'asf', 'avi',  'divx', 'dv',   'flv',  'm4v',   'mkv',  'mov',  'mp4',  'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt',  'rm', 'vob', 'wmv' ),
  1821 		'document'    => array( 'doc', 'docx', 'docm', 'dotm', 'odt',  'pages', 'pdf',  'rtf',  'wp',   'wpd' ),
  2023 		'document'    => array( 'doc', 'docx', 'docm', 'dotm', 'odt',  'pages', 'pdf',  'xps',  'oxps', 'rtf',  'wp', 'wpd', 'psd', 'xcf' ),
  1822 		'spreadsheet' => array( 'numbers',     'ods',  'xls',  'xlsx', 'xlsm',  'xlsb' ),
  2024 		'spreadsheet' => array( 'numbers',     'ods',  'xls',  'xlsx', 'xlsm',  'xlsb' ),
  1823 		'interactive' => array( 'swf', 'key',  'ppt',  'pptx', 'pptm', 'pps',   'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ),
  2025 		'interactive' => array( 'swf', 'key',  'ppt',  'pptx', 'pptm', 'pps',   'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ),
  1824 		'text'        => array( 'asc', 'csv',  'tsv',  'txt' ),
  2026 		'text'        => array( 'asc', 'csv',  'tsv',  'txt' ),
  1825 		'archive'     => array( 'bz2', 'cab',  'dmg',  'gz',   'rar',  'sea',   'sit',  'sqx',  'tar',  'tgz',  'zip', '7z' ),
  2027 		'archive'     => array( 'bz2', 'cab',  'dmg',  'gz',   'rar',  'sea',   'sit',  'sqx',  'tar',  'tgz',  'zip', '7z' ),
  1826 		'code'        => array( 'css', 'htm',  'html', 'php',  'js' ),
  2028 		'code'        => array( 'css', 'htm',  'html', 'php',  'js' ),
  1839  * You can optionally define the mime array, if needed.
  2041  * You can optionally define the mime array, if needed.
  1840  *
  2042  *
  1841  * @since 2.0.4
  2043  * @since 2.0.4
  1842  *
  2044  *
  1843  * @param string $filename File name or path.
  2045  * @param string $filename File name or path.
  1844  * @param array $mimes Optional. Key is the file extension with value as the mime type.
  2046  * @param array  $mimes    Optional. Key is the file extension with value as the mime type.
  1845  * @return array Values with extension first and mime type.
  2047  * @return array Values with extension first and mime type.
  1846  */
  2048  */
  1847 function wp_check_filetype( $filename, $mimes = null ) {
  2049 function wp_check_filetype( $filename, $mimes = null ) {
  1848 	if ( empty($mimes) )
  2050 	if ( empty($mimes) )
  1849 		$mimes = get_allowed_mime_types();
  2051 		$mimes = get_allowed_mime_types();
  1862 	return compact( 'ext', 'type' );
  2064 	return compact( 'ext', 'type' );
  1863 }
  2065 }
  1864 
  2066 
  1865 /**
  2067 /**
  1866  * Attempt to determine the real file type of a file.
  2068  * Attempt to determine the real file type of a file.
       
  2069  *
  1867  * If unable to, the file name extension will be used to determine type.
  2070  * If unable to, the file name extension will be used to determine type.
  1868  *
  2071  *
  1869  * If it's determined that the extension does not match the file's real type,
  2072  * If it's determined that the extension does not match the file's real type,
  1870  * then the "proper_filename" value will be set with a proper filename and extension.
  2073  * then the "proper_filename" value will be set with a proper filename and extension.
  1871  *
  2074  *
  1872  * Currently this function only supports validating images known to getimagesize().
  2075  * Currently this function only supports validating images known to getimagesize().
  1873  *
  2076  *
  1874  * @since 3.0.0
  2077  * @since 3.0.0
  1875  *
  2078  *
  1876  * @param string $file Full path to the file.
  2079  * @param string $file     Full path to the file.
  1877  * @param string $filename The name of the file (may differ from $file due to $file being in a tmp directory)
  2080  * @param string $filename The name of the file (may differ from $file due to $file being
  1878  * @param array $mimes Optional. Key is the file extension with value as the mime type.
  2081  *                         in a tmp directory).
  1879  * @return array Values for the extension, MIME, and either a corrected filename or false if original $filename is valid
  2082  * @param array   $mimes   Optional. Key is the file extension with value as the mime type.
       
  2083  * @return array Values for the extension, MIME, and either a corrected filename or false
       
  2084  *               if original $filename is valid.
  1880  */
  2085  */
  1881 function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
  2086 function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
  1882 
  2087 
  1883 	$proper_filename = false;
  2088 	$proper_filename = false;
  1884 
  2089 
  1885 	// Do basic extension validation and MIME mapping
  2090 	// Do basic extension validation and MIME mapping
  1886 	$wp_filetype = wp_check_filetype( $filename, $mimes );
  2091 	$wp_filetype = wp_check_filetype( $filename, $mimes );
  1887 	extract( $wp_filetype );
  2092 	$ext = $wp_filetype['ext'];
       
  2093 	$type = $wp_filetype['type'];
  1888 
  2094 
  1889 	// We can't do any further validation without a file to work with
  2095 	// We can't do any further validation without a file to work with
  1890 	if ( ! file_exists( $file ) )
  2096 	if ( ! file_exists( $file ) ) {
  1891 		return compact( 'ext', 'type', 'proper_filename' );
  2097 		return compact( 'ext', 'type', 'proper_filename' );
       
  2098 	}
  1892 
  2099 
  1893 	// We're able to validate images using GD
  2100 	// We're able to validate images using GD
  1894 	if ( $type && 0 === strpos( $type, 'image/' ) && function_exists('getimagesize') ) {
  2101 	if ( $type && 0 === strpos( $type, 'image/' ) && function_exists('getimagesize') ) {
  1895 
  2102 
  1896 		// Attempt to figure out what type of image it actually is
  2103 		// Attempt to figure out what type of image it actually is
  1897 		$imgstats = @getimagesize( $file );
  2104 		$imgstats = @getimagesize( $file );
  1898 
  2105 
  1899 		// If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME
  2106 		// If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME
  1900 		if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) {
  2107 		if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) {
  1901 			// This is a simplified array of MIMEs that getimagesize() can detect and their extensions
  2108 			/**
  1902 			// You shouldn't need to use this filter, but it's here just in case
  2109 			 * Filter the list mapping image mime types to their respective extensions.
       
  2110 			 *
       
  2111 			 * @since 3.0.0
       
  2112 			 *
       
  2113 			 * @param  array $mime_to_ext Array of image mime types and their matching extensions.
       
  2114 			 */
  1903 			$mime_to_ext = apply_filters( 'getimagesize_mimes_to_exts', array(
  2115 			$mime_to_ext = apply_filters( 'getimagesize_mimes_to_exts', array(
  1904 				'image/jpeg' => 'jpg',
  2116 				'image/jpeg' => 'jpg',
  1905 				'image/png'  => 'png',
  2117 				'image/png'  => 'png',
  1906 				'image/gif'  => 'gif',
  2118 				'image/gif'  => 'gif',
  1907 				'image/bmp'  => 'bmp',
  2119 				'image/bmp'  => 'bmp',
  1913 				$filename_parts = explode( '.', $filename );
  2125 				$filename_parts = explode( '.', $filename );
  1914 				array_pop( $filename_parts );
  2126 				array_pop( $filename_parts );
  1915 				$filename_parts[] = $mime_to_ext[ $imgstats['mime'] ];
  2127 				$filename_parts[] = $mime_to_ext[ $imgstats['mime'] ];
  1916 				$new_filename = implode( '.', $filename_parts );
  2128 				$new_filename = implode( '.', $filename_parts );
  1917 
  2129 
  1918 				if ( $new_filename != $filename )
  2130 				if ( $new_filename != $filename ) {
  1919 					$proper_filename = $new_filename; // Mark that it changed
  2131 					$proper_filename = $new_filename; // Mark that it changed
  1920 
  2132 				}
  1921 				// Redefine the extension / MIME
  2133 				// Redefine the extension / MIME
  1922 				$wp_filetype = wp_check_filetype( $new_filename, $mimes );
  2134 				$wp_filetype = wp_check_filetype( $new_filename, $mimes );
  1923 				extract( $wp_filetype );
  2135 				$ext = $wp_filetype['ext'];
       
  2136 				$type = $wp_filetype['type'];
  1924 			}
  2137 			}
  1925 		}
  2138 		}
  1926 	}
  2139 	}
  1927 
  2140 
  1928 	// Let plugins try and validate other types of files
  2141 	/**
  1929 	// Should return an array in the style of array( 'ext' => $ext, 'type' => $type, 'proper_filename' => $proper_filename )
  2142 	 * Filter the "real" file type of the given file.
       
  2143 	 *
       
  2144 	 * @since 3.0.0
       
  2145 	 *
       
  2146 	 * @param array  $wp_check_filetype_and_ext File data array containing 'ext', 'type', and
       
  2147 	 *                                          'proper_filename' keys.
       
  2148 	 * @param string $file                      Full path to the file.
       
  2149 	 * @param string $filename                  The name of the file (may differ from $file due to
       
  2150 	 *                                          $file being in a tmp directory).
       
  2151 	 * @param array  $mimes                     Key is the file extension with value as the mime type.
       
  2152 	 */
  1930 	return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );
  2153 	return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );
  1931 }
  2154 }
  1932 
  2155 
  1933 /**
  2156 /**
  1934  * Retrieve list of mime types and file extensions.
  2157  * Retrieve list of mime types and file extensions.
  1935  *
  2158  *
  1936  * @since 3.5.0
  2159  * @since 3.5.0
  1937  *
  2160  * @since 4.2.0 Support was added for GIMP (xcf) files.
  1938  * @uses apply_filters() Calls 'mime_types' on returned array. This filter should
       
  1939  * be used to add types, not remove them. To remove types use the upload_mimes filter.
       
  1940  *
  2161  *
  1941  * @return array Array of mime types keyed by the file extension regex corresponding to those types.
  2162  * @return array Array of mime types keyed by the file extension regex corresponding to those types.
  1942  */
  2163  */
  1943 function wp_get_mime_types() {
  2164 function wp_get_mime_types() {
  1944 	// Accepted MIME types are set here as PCRE unless provided.
  2165 	/**
       
  2166 	 * Filter the list of mime types and file extensions.
       
  2167 	 *
       
  2168 	 * This filter should be used to add, not remove, mime types. To remove
       
  2169 	 * mime types, use the 'upload_mimes' filter.
       
  2170 	 *
       
  2171 	 * @since 3.5.0
       
  2172 	 *
       
  2173 	 * @param array $wp_get_mime_types Mime types keyed by the file extension regex
       
  2174 	 *                                 corresponding to those types.
       
  2175 	 */
  1945 	return apply_filters( 'mime_types', array(
  2176 	return apply_filters( 'mime_types', array(
  1946 	// Image formats
  2177 	// Image formats.
  1947 	'jpg|jpeg|jpe' => 'image/jpeg',
  2178 	'jpg|jpeg|jpe' => 'image/jpeg',
  1948 	'gif' => 'image/gif',
  2179 	'gif' => 'image/gif',
  1949 	'png' => 'image/png',
  2180 	'png' => 'image/png',
  1950 	'bmp' => 'image/bmp',
  2181 	'bmp' => 'image/bmp',
  1951 	'tif|tiff' => 'image/tiff',
  2182 	'tiff|tif' => 'image/tiff',
  1952 	'ico' => 'image/x-icon',
  2183 	'ico' => 'image/x-icon',
  1953 	// Video formats
  2184 	// Video formats.
  1954 	'asf|asx' => 'video/x-ms-asf',
  2185 	'asf|asx' => 'video/x-ms-asf',
  1955 	'wmv' => 'video/x-ms-wmv',
  2186 	'wmv' => 'video/x-ms-wmv',
  1956 	'wmx' => 'video/x-ms-wmx',
  2187 	'wmx' => 'video/x-ms-wmx',
  1957 	'wm' => 'video/x-ms-wm',
  2188 	'wm' => 'video/x-ms-wm',
  1958 	'avi' => 'video/avi',
  2189 	'avi' => 'video/avi',
  1962 	'mpeg|mpg|mpe' => 'video/mpeg',
  2193 	'mpeg|mpg|mpe' => 'video/mpeg',
  1963 	'mp4|m4v' => 'video/mp4',
  2194 	'mp4|m4v' => 'video/mp4',
  1964 	'ogv' => 'video/ogg',
  2195 	'ogv' => 'video/ogg',
  1965 	'webm' => 'video/webm',
  2196 	'webm' => 'video/webm',
  1966 	'mkv' => 'video/x-matroska',
  2197 	'mkv' => 'video/x-matroska',
  1967 	// Text formats
  2198 	'3gp|3gpp' => 'video/3gpp', // Can also be audio
  1968 	'txt|asc|c|cc|h' => 'text/plain',
  2199 	'3g2|3gp2' => 'video/3gpp2', // Can also be audio
       
  2200 	// Text formats.
       
  2201 	'txt|asc|c|cc|h|srt' => 'text/plain',
  1969 	'csv' => 'text/csv',
  2202 	'csv' => 'text/csv',
  1970 	'tsv' => 'text/tab-separated-values',
  2203 	'tsv' => 'text/tab-separated-values',
  1971 	'ics' => 'text/calendar',
  2204 	'ics' => 'text/calendar',
  1972 	'rtx' => 'text/richtext',
  2205 	'rtx' => 'text/richtext',
  1973 	'css' => 'text/css',
  2206 	'css' => 'text/css',
  1974 	'htm|html' => 'text/html',
  2207 	'htm|html' => 'text/html',
  1975 	// Audio formats
  2208 	'vtt' => 'text/vtt',
       
  2209 	'dfxp' => 'application/ttaf+xml',
       
  2210 	// Audio formats.
  1976 	'mp3|m4a|m4b' => 'audio/mpeg',
  2211 	'mp3|m4a|m4b' => 'audio/mpeg',
  1977 	'ra|ram' => 'audio/x-realaudio',
  2212 	'ra|ram' => 'audio/x-realaudio',
  1978 	'wav' => 'audio/wav',
  2213 	'wav' => 'audio/wav',
  1979 	'ogg|oga' => 'audio/ogg',
  2214 	'ogg|oga' => 'audio/ogg',
  1980 	'mid|midi' => 'audio/midi',
  2215 	'mid|midi' => 'audio/midi',
  1981 	'wma' => 'audio/x-ms-wma',
  2216 	'wma' => 'audio/x-ms-wma',
  1982 	'wax' => 'audio/x-ms-wax',
  2217 	'wax' => 'audio/x-ms-wax',
  1983 	'mka' => 'audio/x-matroska',
  2218 	'mka' => 'audio/x-matroska',
  1984 	// Misc application formats
  2219 	// Misc application formats.
  1985 	'rtf' => 'application/rtf',
  2220 	'rtf' => 'application/rtf',
  1986 	'js' => 'application/javascript',
  2221 	'js' => 'application/javascript',
  1987 	'pdf' => 'application/pdf',
  2222 	'pdf' => 'application/pdf',
  1988 	'swf' => 'application/x-shockwave-flash',
  2223 	'swf' => 'application/x-shockwave-flash',
  1989 	'class' => 'application/java',
  2224 	'class' => 'application/java',
  1991 	'zip' => 'application/zip',
  2226 	'zip' => 'application/zip',
  1992 	'gz|gzip' => 'application/x-gzip',
  2227 	'gz|gzip' => 'application/x-gzip',
  1993 	'rar' => 'application/rar',
  2228 	'rar' => 'application/rar',
  1994 	'7z' => 'application/x-7z-compressed',
  2229 	'7z' => 'application/x-7z-compressed',
  1995 	'exe' => 'application/x-msdownload',
  2230 	'exe' => 'application/x-msdownload',
  1996 	// MS Office formats
  2231 	'psd' => 'application/octet-stream',
       
  2232 	'xcf' => 'application/octet-stream',
       
  2233 	// MS Office formats.
  1997 	'doc' => 'application/msword',
  2234 	'doc' => 'application/msword',
  1998 	'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
  2235 	'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
  1999 	'wri' => 'application/vnd.ms-write',
  2236 	'wri' => 'application/vnd.ms-write',
  2000 	'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
  2237 	'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
  2001 	'mdb' => 'application/vnd.ms-access',
  2238 	'mdb' => 'application/vnd.ms-access',
  2018 	'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  2255 	'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  2019 	'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  2256 	'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  2020 	'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  2257 	'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  2021 	'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  2258 	'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  2022 	'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote',
  2259 	'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote',
  2023 	// OpenOffice formats
  2260 	'oxps' => 'application/oxps',
       
  2261 	'xps' => 'application/vnd.ms-xpsdocument',
       
  2262 	// OpenOffice formats.
  2024 	'odt' => 'application/vnd.oasis.opendocument.text',
  2263 	'odt' => 'application/vnd.oasis.opendocument.text',
  2025 	'odp' => 'application/vnd.oasis.opendocument.presentation',
  2264 	'odp' => 'application/vnd.oasis.opendocument.presentation',
  2026 	'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  2265 	'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  2027 	'odg' => 'application/vnd.oasis.opendocument.graphics',
  2266 	'odg' => 'application/vnd.oasis.opendocument.graphics',
  2028 	'odc' => 'application/vnd.oasis.opendocument.chart',
  2267 	'odc' => 'application/vnd.oasis.opendocument.chart',
  2029 	'odb' => 'application/vnd.oasis.opendocument.database',
  2268 	'odb' => 'application/vnd.oasis.opendocument.database',
  2030 	'odf' => 'application/vnd.oasis.opendocument.formula',
  2269 	'odf' => 'application/vnd.oasis.opendocument.formula',
  2031 	// WordPerfect formats
  2270 	// WordPerfect formats.
  2032 	'wp|wpd' => 'application/wordperfect',
  2271 	'wp|wpd' => 'application/wordperfect',
  2033 	// iWork formats
  2272 	// iWork formats.
  2034 	'key' => 'application/vnd.apple.keynote',
  2273 	'key' => 'application/vnd.apple.keynote',
  2035 	'numbers' => 'application/vnd.apple.numbers',
  2274 	'numbers' => 'application/vnd.apple.numbers',
  2036 	'pages' => 'application/vnd.apple.pages',
  2275 	'pages' => 'application/vnd.apple.pages',
  2037 	) );
  2276 	) );
  2038 }
  2277 }
  2039 /**
  2278 /**
  2040  * Retrieve list of allowed mime types and file extensions.
  2279  * Retrieve list of allowed mime types and file extensions.
  2041  *
  2280  *
  2042  * @since 2.8.6
  2281  * @since 2.8.6
  2043  *
  2282  *
  2044  * @uses apply_filters() Calls 'upload_mimes' on returned array
       
  2045  * @uses wp_get_upload_mime_types() to fetch the list of mime types
       
  2046  *
       
  2047  * @param int|WP_User $user Optional. User to check. Defaults to current user.
  2283  * @param int|WP_User $user Optional. User to check. Defaults to current user.
  2048  * @return array Array of mime types keyed by the file extension regex corresponding to those types.
  2284  * @return array Array of mime types keyed by the file extension regex corresponding
       
  2285  *               to those types.
  2049  */
  2286  */
  2050 function get_allowed_mime_types( $user = null ) {
  2287 function get_allowed_mime_types( $user = null ) {
  2051 	$t = wp_get_mime_types();
  2288 	$t = wp_get_mime_types();
  2052 
  2289 
  2053 	unset( $t['swf'], $t['exe'] );
  2290 	unset( $t['swf'], $t['exe'] );
  2055 		$unfiltered = $user ? user_can( $user, 'unfiltered_html' ) : current_user_can( 'unfiltered_html' );
  2292 		$unfiltered = $user ? user_can( $user, 'unfiltered_html' ) : current_user_can( 'unfiltered_html' );
  2056 
  2293 
  2057 	if ( empty( $unfiltered ) )
  2294 	if ( empty( $unfiltered ) )
  2058 		unset( $t['htm|html'] );
  2295 		unset( $t['htm|html'] );
  2059 
  2296 
       
  2297 	/**
       
  2298 	 * Filter list of allowed mime types and file extensions.
       
  2299 	 *
       
  2300 	 * @since 2.0.0
       
  2301 	 *
       
  2302 	 * @param array            $t    Mime types keyed by the file extension regex corresponding to
       
  2303 	 *                               those types. 'swf' and 'exe' removed from full list. 'htm|html' also
       
  2304 	 *                               removed depending on '$user' capabilities.
       
  2305 	 * @param int|WP_User|null $user User ID, User object or null if not provided (indicates current user).
       
  2306 	 */
  2060 	return apply_filters( 'upload_mimes', $t, $user );
  2307 	return apply_filters( 'upload_mimes', $t, $user );
  2061 }
  2308 }
  2062 
  2309 
  2063 /**
  2310 /**
  2064  * Display "Are You Sure" message to confirm the action being taken.
  2311  * Display "Are You Sure" message to confirm the action being taken.
  2065  *
  2312  *
  2066  * If the action has the nonce explain message, then it will be displayed along
  2313  * If the action has the nonce explain message, then it will be displayed
  2067  * with the "Are you sure?" message.
  2314  * along with the "Are you sure?" message.
  2068  *
  2315  *
  2069  * @package WordPress
       
  2070  * @subpackage Security
       
  2071  * @since 2.0.4
  2316  * @since 2.0.4
  2072  *
  2317  *
  2073  * @param string $action The nonce action.
  2318  * @param string $action The nonce action.
  2074  */
  2319  */
  2075 function wp_nonce_ays( $action ) {
  2320 function wp_nonce_ays( $action ) {
  2076 	$title = __( 'WordPress Failure Notice' );
       
  2077 	if ( 'log-out' == $action ) {
  2321 	if ( 'log-out' == $action ) {
  2078 		$html = sprintf( __( 'You are attempting to log out of %s' ), get_bloginfo( 'name' ) ) . '</p><p>';
  2322 		$html = sprintf( __( 'You are attempting to log out of %s' ), get_bloginfo( 'name' ) ) . '</p><p>';
  2079 		$html .= sprintf( __( "Do you really want to <a href='%s'>log out</a>?"), wp_logout_url() );
  2323 		$redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
       
  2324 		$html .= sprintf( __( "Do you really want to <a href='%s'>log out</a>?"), wp_logout_url( $redirect_to ) );
  2080 	} else {
  2325 	} else {
  2081 		$html = __( 'Are you sure you want to do this?' );
  2326 		$html = __( 'Are you sure you want to do this?' );
  2082 		if ( wp_get_referer() )
  2327 		if ( wp_get_referer() )
  2083 			$html .= "</p><p><a href='" . esc_url( remove_query_arg( 'updated', wp_get_referer() ) ) . "'>" . __( 'Please try again.' ) . "</a>";
  2328 			$html .= "</p><p><a href='" . esc_url( remove_query_arg( 'updated', wp_get_referer() ) ) . "'>" . __( 'Please try again.' ) . "</a>";
  2084 	}
  2329 	}
  2085 
  2330 
  2086 	wp_die( $html, $title, array('response' => 403) );
  2331 	wp_die( $html, __( 'WordPress Failure Notice' ), 403 );
  2087 }
  2332 }
  2088 
  2333 
  2089 /**
  2334 /**
  2090  * Kill WordPress execution and display HTML message with error message.
  2335  * Kill WordPress execution and display HTML message with error message.
  2091  *
  2336  *
  2092  * This function complements the die() PHP function. The difference is that
  2337  * This function complements the `die()` PHP function. The difference is that
  2093  * HTML will be displayed to the user. It is recommended to use this function
  2338  * HTML will be displayed to the user. It is recommended to use this function
  2094  * only, when the execution should not continue any further. It is not
  2339  * only when the execution should not continue any further. It is not recommended
  2095  * recommended to call this function very often and try to handle as many errors
  2340  * to call this function very often, and try to handle as many errors as possible
  2096  * as possible silently.
  2341  * silently or more gracefully.
       
  2342  *
       
  2343  * As a shorthand, the desired HTTP response code may be passed as an integer to
       
  2344  * the `$title` parameter (the default title would apply) or the `$args` parameter.
  2097  *
  2345  *
  2098  * @since 2.0.4
  2346  * @since 2.0.4
  2099  *
  2347  * @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept
  2100  * @param string $message Error message.
  2348  *              an integer to be used as the response code.
  2101  * @param string $title Error title.
  2349  *
  2102  * @param string|array $args Optional arguments to control behavior.
  2350  * @param string|WP_Error  $message Optional. Error message. If this is a {@see WP_Error} object,
       
  2351  *                                  the error's messages are used. Default empty.
       
  2352  * @param string|int       $title   Optional. Error title. If `$message` is a `WP_Error` object,
       
  2353  *                                  error data with the key 'title' may be used to specify the title.
       
  2354  *                                  If `$title` is an integer, then it is treated as the response
       
  2355  *                                  code. Default empty.
       
  2356  * @param string|array|int $args {
       
  2357  *     Optional. Arguments to control behavior. If `$args` is an integer, then it is treated
       
  2358  *     as the response code. Default empty array.
       
  2359  *
       
  2360  *     @type int    $response       The HTTP response code. Default 500.
       
  2361  *     @type bool   $back_link      Whether to include a link to go back. Default false.
       
  2362  *     @type string $text_direction The text direction. This is only useful internally, when WordPress
       
  2363  *                                  is still loading and the site's locale is not set up yet. Accepts 'rtl'.
       
  2364  *                                  Default is the value of {@see is_rtl()}.
       
  2365  * }
  2103  */
  2366  */
  2104 function wp_die( $message = '', $title = '', $args = array() ) {
  2367 function wp_die( $message = '', $title = '', $args = array() ) {
  2105 	if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  2368 
       
  2369 	if ( is_int( $args ) ) {
       
  2370 		$args = array( 'response' => $args );
       
  2371 	} elseif ( is_int( $title ) ) {
       
  2372 		$args  = array( 'response' => $title );
       
  2373 		$title = '';
       
  2374 	}
       
  2375 
       
  2376 	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
       
  2377 		/**
       
  2378 		 * Filter callback for killing WordPress execution for AJAX requests.
       
  2379 		 *
       
  2380 		 * @since 3.4.0
       
  2381 		 *
       
  2382 		 * @param callback $function Callback function name.
       
  2383 		 */
  2106 		$function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
  2384 		$function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
  2107 	elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
  2385 	} elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
       
  2386 		/**
       
  2387 		 * Filter callback for killing WordPress execution for XML-RPC requests.
       
  2388 		 *
       
  2389 		 * @since 3.4.0
       
  2390 		 *
       
  2391 		 * @param callback $function Callback function name.
       
  2392 		 */
  2108 		$function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
  2393 		$function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
  2109 	else
  2394 	} else {
       
  2395 		/**
       
  2396 		 * Filter callback for killing WordPress execution for all non-AJAX, non-XML-RPC requests.
       
  2397 		 *
       
  2398 		 * @since 3.0.0
       
  2399 		 *
       
  2400 		 * @param callback $function Callback function name.
       
  2401 		 */
  2110 		$function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
  2402 		$function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
       
  2403 	}
  2111 
  2404 
  2112 	call_user_func( $function, $message, $title, $args );
  2405 	call_user_func( $function, $message, $title, $args );
  2113 }
  2406 }
  2114 
  2407 
  2115 /**
  2408 /**
  2119  * site then you can overload using the wp_die_handler filter in wp_die
  2412  * site then you can overload using the wp_die_handler filter in wp_die
  2120  *
  2413  *
  2121  * @since 3.0.0
  2414  * @since 3.0.0
  2122  * @access private
  2415  * @access private
  2123  *
  2416  *
  2124  * @param string $message Error message.
  2417  * @param string       $message Error message.
  2125  * @param string $title Error title.
  2418  * @param string       $title   Optional. Error title. Default empty.
  2126  * @param string|array $args Optional arguments to control behavior.
  2419  * @param string|array $args    Optional. Arguments to control behavior. Default empty array.
  2127  */
  2420  */
  2128 function _default_wp_die_handler( $message, $title = '', $args = array() ) {
  2421 function _default_wp_die_handler( $message, $title = '', $args = array() ) {
  2129 	$defaults = array( 'response' => 500 );
  2422 	$defaults = array( 'response' => 500 );
  2130 	$r = wp_parse_args($args, $defaults);
  2423 	$r = wp_parse_args($args, $defaults);
  2131 
  2424 
  2136 			$error_data = $message->get_error_data();
  2429 			$error_data = $message->get_error_data();
  2137 			if ( is_array( $error_data ) && isset( $error_data['title'] ) )
  2430 			if ( is_array( $error_data ) && isset( $error_data['title'] ) )
  2138 				$title = $error_data['title'];
  2431 				$title = $error_data['title'];
  2139 		}
  2432 		}
  2140 		$errors = $message->get_error_messages();
  2433 		$errors = $message->get_error_messages();
  2141 		switch ( count( $errors ) ) :
  2434 		switch ( count( $errors ) ) {
  2142 		case 0 :
  2435 		case 0 :
  2143 			$message = '';
  2436 			$message = '';
  2144 			break;
  2437 			break;
  2145 		case 1 :
  2438 		case 1 :
  2146 			$message = "<p>{$errors[0]}</p>";
  2439 			$message = "<p>{$errors[0]}</p>";
  2147 			break;
  2440 			break;
  2148 		default :
  2441 		default :
  2149 			$message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $errors ) . "</li>\n\t</ul>";
  2442 			$message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $errors ) . "</li>\n\t</ul>";
  2150 			break;
  2443 			break;
  2151 		endswitch;
  2444 		}
  2152 	} elseif ( is_string( $message ) ) {
  2445 	} elseif ( is_string( $message ) ) {
  2153 		$message = "<p>$message</p>";
  2446 		$message = "<p>$message</p>";
  2154 	}
  2447 	}
  2155 
  2448 
  2156 	if ( isset( $r['back_link'] ) && $r['back_link'] ) {
  2449 	if ( isset( $r['back_link'] ) && $r['back_link'] ) {
  2181 <head>
  2474 <head>
  2182 	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2475 	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2183 	<title><?php echo $title ?></title>
  2476 	<title><?php echo $title ?></title>
  2184 	<style type="text/css">
  2477 	<style type="text/css">
  2185 		html {
  2478 		html {
  2186 			background: #f9f9f9;
  2479 			background: #f1f1f1;
  2187 		}
  2480 		}
  2188 		body {
  2481 		body {
  2189 			background: #fff;
  2482 			background: #fff;
  2190 			color: #333;
  2483 			color: #444;
  2191 			font-family: sans-serif;
  2484 			font-family: "Open Sans", sans-serif;
  2192 			margin: 2em auto;
  2485 			margin: 2em auto;
  2193 			padding: 1em 2em;
  2486 			padding: 1em 2em;
  2194 			-webkit-border-radius: 3px;
       
  2195 			border-radius: 3px;
       
  2196 			border: 1px solid #dfdfdf;
       
  2197 			max-width: 700px;
  2487 			max-width: 700px;
       
  2488 			-webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);
       
  2489 			box-shadow: 0 1px 3px rgba(0,0,0,0.13);
  2198 		}
  2490 		}
  2199 		h1 {
  2491 		h1 {
  2200 			border-bottom: 1px solid #dadada;
  2492 			border-bottom: 1px solid #dadada;
  2201 			clear: both;
  2493 			clear: both;
  2202 			color: #666;
  2494 			color: #666;
  2203 			font: 24px Georgia, "Times New Roman", Times, serif;
  2495 			font: 24px "Open Sans", sans-serif;
  2204 			margin: 30px 0 0 0;
  2496 			margin: 30px 0 0 0;
  2205 			padding: 0;
  2497 			padding: 0;
  2206 			padding-bottom: 7px;
  2498 			padding-bottom: 7px;
  2207 		}
  2499 		}
  2208 		#error-page {
  2500 		#error-page {
  2226 		}
  2518 		}
  2227 		a:hover {
  2519 		a:hover {
  2228 			color: #D54E21;
  2520 			color: #D54E21;
  2229 		}
  2521 		}
  2230 		.button {
  2522 		.button {
       
  2523 			background: #f7f7f7;
       
  2524 			border: 1px solid #cccccc;
       
  2525 			color: #555;
  2231 			display: inline-block;
  2526 			display: inline-block;
  2232 			text-decoration: none;
  2527 			text-decoration: none;
  2233 			font-size: 14px;
  2528 			font-size: 13px;
  2234 			line-height: 23px;
  2529 			line-height: 26px;
  2235 			height: 24px;
  2530 			height: 28px;
  2236 			margin: 0;
  2531 			margin: 0;
  2237 			padding: 0 10px 1px;
  2532 			padding: 0 10px 1px;
  2238 			cursor: pointer;
  2533 			cursor: pointer;
  2239 			border-width: 1px;
       
  2240 			border-style: solid;
       
  2241 			-webkit-border-radius: 3px;
  2534 			-webkit-border-radius: 3px;
       
  2535 			-webkit-appearance: none;
  2242 			border-radius: 3px;
  2536 			border-radius: 3px;
  2243 			white-space: nowrap;
  2537 			white-space: nowrap;
  2244 			-webkit-box-sizing: border-box;
  2538 			-webkit-box-sizing: border-box;
  2245 			-moz-box-sizing:    border-box;
  2539 			-moz-box-sizing:    border-box;
  2246 			box-sizing:         border-box;
  2540 			box-sizing:         border-box;
  2247 			background: #f3f3f3;
  2541 
  2248 			background-image: -webkit-gradient(linear, left top, left bottom, from(#fefefe), to(#f4f4f4));
  2542 			-webkit-box-shadow: inset 0 1px 0 #fff, 0 1px 0 rgba(0,0,0,.08);
  2249 			background-image: -webkit-linear-gradient(top, #fefefe, #f4f4f4);
  2543 			box-shadow: inset 0 1px 0 #fff, 0 1px 0 rgba(0,0,0,.08);
  2250 			background-image:    -moz-linear-gradient(top, #fefefe, #f4f4f4);
  2544 		 	vertical-align: top;
  2251 			background-image:      -o-linear-gradient(top, #fefefe, #f4f4f4);
       
  2252 			background-image:   linear-gradient(to bottom, #fefefe, #f4f4f4);
       
  2253 			border-color: #bbb;
       
  2254 		 	color: #333;
       
  2255 			text-shadow: 0 1px 0 #fff;
       
  2256 		}
  2545 		}
  2257 
  2546 
  2258 		.button.button-large {
  2547 		.button.button-large {
  2259 			height: 29px;
  2548 			height: 29px;
  2260 			line-height: 28px;
  2549 			line-height: 28px;
  2261 			padding: 0 12px;
  2550 			padding: 0 12px;
  2262 		}
  2551 		}
  2263 
  2552 
  2264 		.button:hover,
  2553 		.button:hover,
  2265 		.button:focus {
  2554 		.button:focus {
  2266 			background: #f3f3f3;
  2555 			background: #fafafa;
  2267 			background-image: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#f3f3f3));
       
  2268 			background-image: -webkit-linear-gradient(top, #fff, #f3f3f3);
       
  2269 			background-image:    -moz-linear-gradient(top, #fff, #f3f3f3);
       
  2270 			background-image:     -ms-linear-gradient(top, #fff, #f3f3f3);
       
  2271 			background-image:      -o-linear-gradient(top, #fff, #f3f3f3);
       
  2272 			background-image:   linear-gradient(to bottom, #fff, #f3f3f3);
       
  2273 			border-color: #999;
  2556 			border-color: #999;
  2274 			color: #222;
  2557 			color: #222;
  2275 		}
  2558 		}
  2276 
  2559 
  2277 		.button:focus  {
  2560 		.button:focus  {
  2278 			-webkit-box-shadow: 1px 1px 1px rgba(0,0,0,.2);
  2561 			-webkit-box-shadow: 1px 1px 1px rgba(0,0,0,.2);
  2279 			box-shadow: 1px 1px 1px rgba(0,0,0,.2);
  2562 			box-shadow: 1px 1px 1px rgba(0,0,0,.2);
  2280 		}
  2563 		}
  2281 
  2564 
  2282 		.button:active {
  2565 		.button:active {
  2283 			outline: none;
       
  2284 			background: #eee;
  2566 			background: #eee;
  2285 			background-image: -webkit-gradient(linear, left top, left bottom, from(#f4f4f4), to(#fefefe));
       
  2286 			background-image: -webkit-linear-gradient(top, #f4f4f4, #fefefe);
       
  2287 			background-image:    -moz-linear-gradient(top, #f4f4f4, #fefefe);
       
  2288 			background-image:     -ms-linear-gradient(top, #f4f4f4, #fefefe);
       
  2289 			background-image:      -o-linear-gradient(top, #f4f4f4, #fefefe);
       
  2290 			background-image:   linear-gradient(to bottom, #f4f4f4, #fefefe);
       
  2291 			border-color: #999;
  2567 			border-color: #999;
  2292 			color: #333;
  2568 			color: #333;
  2293 			text-shadow: 0 -1px 0 #fff;
       
  2294 			-webkit-box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
  2569 			-webkit-box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
  2295 		 	box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
  2570 		 	box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
  2296 		}
  2571 		}
  2297 
  2572 
  2298 		<?php if ( 'rtl' == $text_direction ) : ?>
  2573 		<?php if ( 'rtl' == $text_direction ) : ?>
  2315  * This is the handler for wp_die when processing XMLRPC requests.
  2590  * This is the handler for wp_die when processing XMLRPC requests.
  2316  *
  2591  *
  2317  * @since 3.2.0
  2592  * @since 3.2.0
  2318  * @access private
  2593  * @access private
  2319  *
  2594  *
  2320  * @param string $message Error message.
  2595  * @param string       $message Error message.
  2321  * @param string $title Error title.
  2596  * @param string       $title   Optional. Error title. Default empty.
  2322  * @param string|array $args Optional arguments to control behavior.
  2597  * @param string|array $args    Optional. Arguments to control behavior. Default empty array.
  2323  */
  2598  */
  2324 function _xmlrpc_wp_die_handler( $message, $title = '', $args = array() ) {
  2599 function _xmlrpc_wp_die_handler( $message, $title = '', $args = array() ) {
  2325 	global $wp_xmlrpc_server;
  2600 	global $wp_xmlrpc_server;
  2326 	$defaults = array( 'response' => 500 );
  2601 	$defaults = array( 'response' => 500 );
  2327 
  2602 
  2340  * This is the handler for wp_die when processing Ajax requests.
  2615  * This is the handler for wp_die when processing Ajax requests.
  2341  *
  2616  *
  2342  * @since 3.4.0
  2617  * @since 3.4.0
  2343  * @access private
  2618  * @access private
  2344  *
  2619  *
  2345  * @param string $message Optional. Response to print.
  2620  * @param string $message Optional. Response to print. Default empty.
  2346  */
  2621  */
  2347 function _ajax_wp_die_handler( $message = '' ) {
  2622 function _ajax_wp_die_handler( $message = '' ) {
  2348 	if ( is_scalar( $message ) )
  2623 	if ( is_scalar( $message ) )
  2349 		die( (string) $message );
  2624 		die( (string) $message );
  2350 	die( '0' );
  2625 	die( '0' );
  2356  * This is the handler for wp_die when processing APP requests.
  2631  * This is the handler for wp_die when processing APP requests.
  2357  *
  2632  *
  2358  * @since 3.4.0
  2633  * @since 3.4.0
  2359  * @access private
  2634  * @access private
  2360  *
  2635  *
  2361  * @param string $message Optional. Response to print.
  2636  * @param string $message Optional. Response to print. Default empty.
  2362  */
  2637  */
  2363 function _scalar_wp_die_handler( $message = '' ) {
  2638 function _scalar_wp_die_handler( $message = '' ) {
  2364 	if ( is_scalar( $message ) )
  2639 	if ( is_scalar( $message ) )
  2365 		die( (string) $message );
  2640 		die( (string) $message );
  2366 	die();
  2641 	die();
  2367 }
  2642 }
  2368 
  2643 
  2369 /**
  2644 /**
       
  2645  * Encode a variable into JSON, with some sanity checks.
       
  2646  *
       
  2647  * @since 4.1.0
       
  2648  *
       
  2649  * @param mixed $data    Variable (usually an array or object) to encode as JSON.
       
  2650  * @param int   $options Optional. Options to be passed to json_encode(). Default 0.
       
  2651  * @param int   $depth   Optional. Maximum depth to walk through $data. Must be
       
  2652  *                       greater than 0. Default 512.
       
  2653  * @return bool|string The JSON encoded string, or false if it cannot be encoded.
       
  2654  */
       
  2655 function wp_json_encode( $data, $options = 0, $depth = 512 ) {
       
  2656 	/*
       
  2657 	 * json_encode() has had extra params added over the years.
       
  2658 	 * $options was added in 5.3, and $depth in 5.5.
       
  2659 	 * We need to make sure we call it with the correct arguments.
       
  2660 	 */
       
  2661 	if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) {
       
  2662 		$args = array( $data, $options, $depth );
       
  2663 	} elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
       
  2664 		$args = array( $data, $options );
       
  2665 	} else {
       
  2666 		$args = array( $data );
       
  2667 	}
       
  2668 
       
  2669 	$json = call_user_func_array( 'json_encode', $args );
       
  2670 
       
  2671 	// If json_encode() was successful, no need to do more sanity checking.
       
  2672 	// ... unless we're in an old version of PHP, and json_encode() returned
       
  2673 	// a string containing 'null'. Then we need to do more sanity checking.
       
  2674 	if ( false !== $json && ( version_compare( PHP_VERSION, '5.5', '>=' ) || false === strpos( $json, 'null' ) ) )  {
       
  2675 		return $json;
       
  2676 	}
       
  2677 
       
  2678 	try {
       
  2679 		$args[0] = _wp_json_sanity_check( $data, $depth );
       
  2680 	} catch ( Exception $e ) {
       
  2681 		return false;
       
  2682 	}
       
  2683 
       
  2684 	return call_user_func_array( 'json_encode', $args );
       
  2685 }
       
  2686 
       
  2687 /**
       
  2688  * Perform sanity checks on data that shall be encoded to JSON.
       
  2689  *
       
  2690  * @ignore
       
  2691  * @since 4.1.0
       
  2692  * @access private
       
  2693  *
       
  2694  * @see wp_json_encode()
       
  2695  *
       
  2696  * @param mixed $data  Variable (usually an array or object) to encode as JSON.
       
  2697  * @param int   $depth Maximum depth to walk through $data. Must be greater than 0.
       
  2698  * @return mixed The sanitized data that shall be encoded to JSON.
       
  2699  */
       
  2700 function _wp_json_sanity_check( $data, $depth ) {
       
  2701 	if ( $depth < 0 ) {
       
  2702 		throw new Exception( 'Reached depth limit' );
       
  2703 	}
       
  2704 
       
  2705 	if ( is_array( $data ) ) {
       
  2706 		$output = array();
       
  2707 		foreach ( $data as $id => $el ) {
       
  2708 			// Don't forget to sanitize the ID!
       
  2709 			if ( is_string( $id ) ) {
       
  2710 				$clean_id = _wp_json_convert_string( $id );
       
  2711 			} else {
       
  2712 				$clean_id = $id;
       
  2713 			}
       
  2714 
       
  2715 			// Check the element type, so that we're only recursing if we really have to.
       
  2716 			if ( is_array( $el ) || is_object( $el ) ) {
       
  2717 				$output[ $clean_id ] = _wp_json_sanity_check( $el, $depth - 1 );
       
  2718 			} elseif ( is_string( $el ) ) {
       
  2719 				$output[ $clean_id ] = _wp_json_convert_string( $el );
       
  2720 			} else {
       
  2721 				$output[ $clean_id ] = $el;
       
  2722 			}
       
  2723 		}
       
  2724 	} elseif ( is_object( $data ) ) {
       
  2725 		$output = new stdClass;
       
  2726 		foreach ( $data as $id => $el ) {
       
  2727 			if ( is_string( $id ) ) {
       
  2728 				$clean_id = _wp_json_convert_string( $id );
       
  2729 			} else {
       
  2730 				$clean_id = $id;
       
  2731 			}
       
  2732 
       
  2733 			if ( is_array( $el ) || is_object( $el ) ) {
       
  2734 				$output->$clean_id = _wp_json_sanity_check( $el, $depth - 1 );
       
  2735 			} elseif ( is_string( $el ) ) {
       
  2736 				$output->$clean_id = _wp_json_convert_string( $el );
       
  2737 			} else {
       
  2738 				$output->$clean_id = $el;
       
  2739 			}
       
  2740 		}
       
  2741 	} elseif ( is_string( $data ) ) {
       
  2742 		return _wp_json_convert_string( $data );
       
  2743 	} else {
       
  2744 		return $data;
       
  2745 	}
       
  2746 
       
  2747 	return $output;
       
  2748 }
       
  2749 
       
  2750 /**
       
  2751  * Convert a string to UTF-8, so that it can be safely encoded to JSON.
       
  2752  *
       
  2753  * @ignore
       
  2754  * @since 4.1.0
       
  2755  * @access private
       
  2756  *
       
  2757  * @see _wp_json_sanity_check()
       
  2758  *
       
  2759  * @param string $string The string which is to be converted.
       
  2760  * @return string The checked string.
       
  2761  */
       
  2762 function _wp_json_convert_string( $string ) {
       
  2763 	static $use_mb = null;
       
  2764 	if ( is_null( $use_mb ) ) {
       
  2765 		$use_mb = function_exists( 'mb_convert_encoding' );
       
  2766 	}
       
  2767 
       
  2768 	if ( $use_mb ) {
       
  2769 		$encoding = mb_detect_encoding( $string, mb_detect_order(), true );
       
  2770 		if ( $encoding ) {
       
  2771 			return mb_convert_encoding( $string, 'UTF-8', $encoding );
       
  2772 		} else {
       
  2773 			return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' );
       
  2774 		}
       
  2775 	} else {
       
  2776 		return wp_check_invalid_utf8( $string, true );
       
  2777 	}
       
  2778 }
       
  2779 
       
  2780 /**
  2370  * Send a JSON response back to an Ajax request.
  2781  * Send a JSON response back to an Ajax request.
  2371  *
  2782  *
  2372  * @since 3.5.0
  2783  * @since 3.5.0
  2373  *
  2784  *
  2374  * @param mixed $response Variable (usually an array or object) to encode as JSON, then print and die.
  2785  * @param mixed $response Variable (usually an array or object) to encode as JSON,
       
  2786  *                        then print and die.
  2375  */
  2787  */
  2376 function wp_send_json( $response ) {
  2788 function wp_send_json( $response ) {
  2377 	@header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
  2789 	@header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
  2378 	echo json_encode( $response );
  2790 	echo wp_json_encode( $response );
  2379 	if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  2791 	if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  2380 		wp_die();
  2792 		wp_die();
  2381 	else
  2793 	else
  2382 		die;
  2794 		die;
  2383 }
  2795 }
  2399 }
  2811 }
  2400 
  2812 
  2401 /**
  2813 /**
  2402  * Send a JSON response back to an Ajax request, indicating failure.
  2814  * Send a JSON response back to an Ajax request, indicating failure.
  2403  *
  2815  *
       
  2816  * If the `$data` parameter is a {@see WP_Error} object, the errors
       
  2817  * within the object are processed and output as an array of error
       
  2818  * codes and corresponding messages. All other types are output
       
  2819  * without further processing.
       
  2820  *
  2404  * @since 3.5.0
  2821  * @since 3.5.0
       
  2822  * @since 4.1.0 The `$data` parameter is now processed if a {@see WP_Error}
       
  2823  *              object is passed in.
  2405  *
  2824  *
  2406  * @param mixed $data Data to encode as JSON, then print and die.
  2825  * @param mixed $data Data to encode as JSON, then print and die.
  2407  */
  2826  */
  2408 function wp_send_json_error( $data = null ) {
  2827 function wp_send_json_error( $data = null ) {
  2409 	$response = array( 'success' => false );
  2828 	$response = array( 'success' => false );
  2410 
  2829 
  2411 	if ( isset( $data ) )
  2830 	if ( isset( $data ) ) {
  2412 		$response['data'] = $data;
  2831 		if ( is_wp_error( $data ) ) {
       
  2832 			$result = array();
       
  2833 			foreach ( $data->errors as $code => $messages ) {
       
  2834 				foreach ( $messages as $message ) {
       
  2835 					$result[] = array( 'code' => $code, 'message' => $message );
       
  2836 				}
       
  2837 			}
       
  2838 
       
  2839 			$response['data'] = $result;
       
  2840 		} else {
       
  2841 			$response['data'] = $data;
       
  2842 		}
       
  2843 	}
  2413 
  2844 
  2414 	wp_send_json( $response );
  2845 	wp_send_json( $response );
  2415 }
  2846 }
  2416 
  2847 
  2417 /**
  2848 /**
  2418  * Retrieve the WordPress home page URL.
  2849  * Retrieve the WordPress home page URL.
  2419  *
  2850  *
  2420  * If the constant named 'WP_HOME' exists, then it will be used and returned by
  2851  * If the constant named 'WP_HOME' exists, then it will be used and returned
  2421  * the function. This can be used to counter the redirection on your local
  2852  * by the function. This can be used to counter the redirection on your local
  2422  * development environment.
  2853  * development environment.
  2423  *
  2854  *
       
  2855  * @since 2.2.0
  2424  * @access private
  2856  * @access private
  2425  * @package WordPress
  2857  *
  2426  * @since 2.2.0
  2858  * @see WP_HOME
  2427  *
  2859  *
  2428  * @param string $url URL for the home location
  2860  * @param string $url URL for the home location.
  2429  * @return string Homepage location.
  2861  * @return string Homepage location.
  2430  */
  2862  */
  2431 function _config_wp_home( $url = '' ) {
  2863 function _config_wp_home( $url = '' ) {
  2432 	if ( defined( 'WP_HOME' ) )
  2864 	if ( defined( 'WP_HOME' ) )
  2433 		return untrailingslashit( WP_HOME );
  2865 		return untrailingslashit( WP_HOME );
  2436 
  2868 
  2437 /**
  2869 /**
  2438  * Retrieve the WordPress site URL.
  2870  * Retrieve the WordPress site URL.
  2439  *
  2871  *
  2440  * If the constant named 'WP_SITEURL' is defined, then the value in that
  2872  * If the constant named 'WP_SITEURL' is defined, then the value in that
  2441  * constant will always be returned. This can be used for debugging a site on
  2873  * constant will always be returned. This can be used for debugging a site
  2442  * your localhost while not having to change the database to your URL.
  2874  * on your localhost while not having to change the database to your URL.
  2443  *
  2875  *
       
  2876  * @since 2.2.0
  2444  * @access private
  2877  * @access private
  2445  * @package WordPress
  2878  *
  2446  * @since 2.2.0
  2879  * @see WP_SITEURL
  2447  *
  2880  *
  2448  * @param string $url URL to set the WordPress site location.
  2881  * @param string $url URL to set the WordPress site location.
  2449  * @return string The WordPress Site URL
  2882  * @return string The WordPress Site URL.
  2450  */
  2883  */
  2451 function _config_wp_siteurl( $url = '' ) {
  2884 function _config_wp_siteurl( $url = '' ) {
  2452 	if ( defined( 'WP_SITEURL' ) )
  2885 	if ( defined( 'WP_SITEURL' ) )
  2453 		return untrailingslashit( WP_SITEURL );
  2886 		return untrailingslashit( WP_SITEURL );
  2454 	return $url;
  2887 	return $url;
  2455 }
  2888 }
  2456 
  2889 
  2457 /**
  2890 /**
  2458  * Set the localized direction for MCE plugin.
  2891  * Set the localized direction for MCE plugin.
  2459  *
  2892  *
  2460  * Will only set the direction to 'rtl', if the WordPress locale has the text
  2893  * Will only set the direction to 'rtl', if the WordPress locale has
  2461  * direction set to 'rtl'.
  2894  * the text direction set to 'rtl'.
  2462  *
  2895  *
  2463  * Fills in the 'directionality', 'plugins', and 'theme_advanced_button1' array
  2896  * Fills in the 'directionality' setting, enables the 'directionality'
  2464  * keys. These keys are then returned in the $input array.
  2897  * plugin, and adds the 'ltr' button to 'toolbar1', formerly
  2465  *
  2898  * 'theme_advanced_buttons1' array keys. These keys are then returned
       
  2899  * in the $input (TinyMCE settings) array.
       
  2900  *
       
  2901  * @since 2.1.0
  2466  * @access private
  2902  * @access private
  2467  * @package WordPress
  2903  *
  2468  * @subpackage MCE
  2904  * @param array $input MCE settings array.
  2469  * @since 2.1.0
       
  2470  *
       
  2471  * @param array $input MCE plugin array.
       
  2472  * @return array Direction set for 'rtl', if needed by locale.
  2905  * @return array Direction set for 'rtl', if needed by locale.
  2473  */
  2906  */
  2474 function _mce_set_direction( $input ) {
  2907 function _mce_set_direction( $input ) {
  2475 	if ( is_rtl() ) {
  2908 	if ( is_rtl() ) {
  2476 		$input['directionality'] = 'rtl';
  2909 		$input['directionality'] = 'rtl';
  2477 		$input['plugins'] .= ',directionality';
  2910 
  2478 		$input['theme_advanced_buttons1'] .= ',ltr';
  2911 		if ( ! empty( $input['plugins'] ) && strpos( $input['plugins'], 'directionality' ) === false ) {
       
  2912 			$input['plugins'] .= ',directionality';
       
  2913 		}
       
  2914 
       
  2915 		if ( ! empty( $input['toolbar1'] ) && ! preg_match( '/\bltr\b/', $input['toolbar1'] ) ) {
       
  2916 			$input['toolbar1'] .= ',ltr';
       
  2917 		}
  2479 	}
  2918 	}
  2480 
  2919 
  2481 	return $input;
  2920 	return $input;
  2482 }
  2921 }
       
  2922 
  2483 
  2923 
  2484 /**
  2924 /**
  2485  * Convert smiley code to the icon graphic file equivalent.
  2925  * Convert smiley code to the icon graphic file equivalent.
  2486  *
  2926  *
  2487  * You can turn off smilies, by going to the write setting screen and unchecking
  2927  * You can turn off smilies, by going to the write setting screen and unchecking
  2498  * the description. Probably should create a Codex page for it, so that it is
  2938  * the description. Probably should create a Codex page for it, so that it is
  2499  * available.
  2939  * available.
  2500  *
  2940  *
  2501  * @global array $wpsmiliestrans
  2941  * @global array $wpsmiliestrans
  2502  * @global array $wp_smiliessearch
  2942  * @global array $wp_smiliessearch
       
  2943  *
  2503  * @since 2.2.0
  2944  * @since 2.2.0
  2504  */
  2945  */
  2505 function smilies_init() {
  2946 function smilies_init() {
  2506 	global $wpsmiliestrans, $wp_smiliessearch;
  2947 	global $wpsmiliestrans, $wp_smiliessearch;
  2507 
  2948 
  2509 	if ( !get_option( 'use_smilies' ) )
  2950 	if ( !get_option( 'use_smilies' ) )
  2510 		return;
  2951 		return;
  2511 
  2952 
  2512 	if ( !isset( $wpsmiliestrans ) ) {
  2953 	if ( !isset( $wpsmiliestrans ) ) {
  2513 		$wpsmiliestrans = array(
  2954 		$wpsmiliestrans = array(
  2514 		':mrgreen:' => 'icon_mrgreen.gif',
  2955 		':mrgreen:' => 'mrgreen.png',
  2515 		':neutral:' => 'icon_neutral.gif',
  2956 		':neutral:' => "\xf0\x9f\x98\x90",
  2516 		':twisted:' => 'icon_twisted.gif',
  2957 		':twisted:' => "\xf0\x9f\x98\x88",
  2517 		  ':arrow:' => 'icon_arrow.gif',
  2958 		  ':arrow:' => "\xe2\x9e\xa1",
  2518 		  ':shock:' => 'icon_eek.gif',
  2959 		  ':shock:' => "\xf0\x9f\x98\xaf",
  2519 		  ':smile:' => 'icon_smile.gif',
  2960 		  ':smile:' => 'simple-smile.png',
  2520 		    ':???:' => 'icon_confused.gif',
  2961 		    ':???:' => "\xf0\x9f\x98\x95",
  2521 		   ':cool:' => 'icon_cool.gif',
  2962 		   ':cool:' => "\xf0\x9f\x98\x8e",
  2522 		   ':evil:' => 'icon_evil.gif',
  2963 		   ':evil:' => "\xf0\x9f\x91\xbf",
  2523 		   ':grin:' => 'icon_biggrin.gif',
  2964 		   ':grin:' => "\xf0\x9f\x98\x80",
  2524 		   ':idea:' => 'icon_idea.gif',
  2965 		   ':idea:' => "\xf0\x9f\x92\xa1",
  2525 		   ':oops:' => 'icon_redface.gif',
  2966 		   ':oops:' => "\xf0\x9f\x98\xb3",
  2526 		   ':razz:' => 'icon_razz.gif',
  2967 		   ':razz:' => "\xf0\x9f\x98\x9b",
  2527 		   ':roll:' => 'icon_rolleyes.gif',
  2968 		   ':roll:' => 'rolleyes.png',
  2528 		   ':wink:' => 'icon_wink.gif',
  2969 		   ':wink:' => "\xf0\x9f\x98\x89",
  2529 		    ':cry:' => 'icon_cry.gif',
  2970 		    ':cry:' => "\xf0\x9f\x98\xa5",
  2530 		    ':eek:' => 'icon_surprised.gif',
  2971 		    ':eek:' => "\xf0\x9f\x98\xae",
  2531 		    ':lol:' => 'icon_lol.gif',
  2972 		    ':lol:' => "\xf0\x9f\x98\x86",
  2532 		    ':mad:' => 'icon_mad.gif',
  2973 		    ':mad:' => "\xf0\x9f\x98\xa1",
  2533 		    ':sad:' => 'icon_sad.gif',
  2974 		    ':sad:' => 'frownie.png',
  2534 		      '8-)' => 'icon_cool.gif',
  2975 		      '8-)' => "\xf0\x9f\x98\x8e",
  2535 		      '8-O' => 'icon_eek.gif',
  2976 		      '8-O' => "\xf0\x9f\x98\xaf",
  2536 		      ':-(' => 'icon_sad.gif',
  2977 		      ':-(' => 'frownie.png',
  2537 		      ':-)' => 'icon_smile.gif',
  2978 		      ':-)' => 'simple-smile.png',
  2538 		      ':-?' => 'icon_confused.gif',
  2979 		      ':-?' => "\xf0\x9f\x98\x95",
  2539 		      ':-D' => 'icon_biggrin.gif',
  2980 		      ':-D' => "\xf0\x9f\x98\x80",
  2540 		      ':-P' => 'icon_razz.gif',
  2981 		      ':-P' => "\xf0\x9f\x98\x9b",
  2541 		      ':-o' => 'icon_surprised.gif',
  2982 		      ':-o' => "\xf0\x9f\x98\xae",
  2542 		      ':-x' => 'icon_mad.gif',
  2983 		      ':-x' => "\xf0\x9f\x98\xa1",
  2543 		      ':-|' => 'icon_neutral.gif',
  2984 		      ':-|' => "\xf0\x9f\x98\x90",
  2544 		      ';-)' => 'icon_wink.gif',
  2985 		      ';-)' => "\xf0\x9f\x98\x89",
  2545 		// This one transformation breaks regular text with frequency.
  2986 		// This one transformation breaks regular text with frequency.
  2546 		//     '8)' => 'icon_cool.gif',
  2987 		//     '8)' => "\xf0\x9f\x98\x8e",
  2547 		       '8O' => 'icon_eek.gif',
  2988 		       '8O' => "\xf0\x9f\x98\xaf",
  2548 		       ':(' => 'icon_sad.gif',
  2989 		       ':(' => 'frownie.png',
  2549 		       ':)' => 'icon_smile.gif',
  2990 		       ':)' => 'simple-smile.png',
  2550 		       ':?' => 'icon_confused.gif',
  2991 		       ':?' => "\xf0\x9f\x98\x95",
  2551 		       ':D' => 'icon_biggrin.gif',
  2992 		       ':D' => "\xf0\x9f\x98\x80",
  2552 		       ':P' => 'icon_razz.gif',
  2993 		       ':P' => "\xf0\x9f\x98\x9b",
  2553 		       ':o' => 'icon_surprised.gif',
  2994 		       ':o' => "\xf0\x9f\x98\xae",
  2554 		       ':x' => 'icon_mad.gif',
  2995 		       ':x' => "\xf0\x9f\x98\xa1",
  2555 		       ':|' => 'icon_neutral.gif',
  2996 		       ':|' => "\xf0\x9f\x98\x90",
  2556 		       ';)' => 'icon_wink.gif',
  2997 		       ';)' => "\xf0\x9f\x98\x89",
  2557 		      ':!:' => 'icon_exclaim.gif',
  2998 		      ':!:' => "\xe2\x9d\x97",
  2558 		      ':?:' => 'icon_question.gif',
  2999 		      ':?:' => "\xe2\x9d\x93",
  2559 		);
  3000 		);
  2560 	}
  3001 	}
  2561 
  3002 
  2562 	if (count($wpsmiliestrans) == 0) {
  3003 	if (count($wpsmiliestrans) == 0) {
  2563 		return;
  3004 		return;
  2568 	 * we match the longest possible smilie (:???: vs :?) as the regular
  3009 	 * we match the longest possible smilie (:???: vs :?) as the regular
  2569 	 * expression used below is first-match
  3010 	 * expression used below is first-match
  2570 	 */
  3011 	 */
  2571 	krsort($wpsmiliestrans);
  3012 	krsort($wpsmiliestrans);
  2572 
  3013 
  2573 	$wp_smiliessearch = '/(?:\s|^)';
  3014 	$spaces = wp_spaces_regexp();
       
  3015 
       
  3016 	// Begin first "subpattern"
       
  3017 	$wp_smiliessearch = '/(?<=' . $spaces . '|^)';
  2574 
  3018 
  2575 	$subchar = '';
  3019 	$subchar = '';
  2576 	foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
  3020 	foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
  2577 		$firstchar = substr($smiley, 0, 1);
  3021 		$firstchar = substr($smiley, 0, 1);
  2578 		$rest = substr($smiley, 1);
  3022 		$rest = substr($smiley, 1);
  2579 
  3023 
  2580 		// new subpattern?
  3024 		// new subpattern?
  2581 		if ($firstchar != $subchar) {
  3025 		if ($firstchar != $subchar) {
  2582 			if ($subchar != '') {
  3026 			if ($subchar != '') {
  2583 				$wp_smiliessearch .= ')|(?:\s|^)';
  3027 				$wp_smiliessearch .= ')(?=' . $spaces . '|$)';  // End previous "subpattern"
       
  3028 				$wp_smiliessearch .= '|(?<=' . $spaces . '|^)'; // Begin another "subpattern"
  2584 			}
  3029 			}
  2585 			$subchar = $firstchar;
  3030 			$subchar = $firstchar;
  2586 			$wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:';
  3031 			$wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:';
  2587 		} else {
  3032 		} else {
  2588 			$wp_smiliessearch .= '|';
  3033 			$wp_smiliessearch .= '|';
  2589 		}
  3034 		}
  2590 		$wp_smiliessearch .= preg_quote($rest, '/');
  3035 		$wp_smiliessearch .= preg_quote($rest, '/');
  2591 	}
  3036 	}
  2592 
  3037 
  2593 	$wp_smiliessearch .= ')(?:\s|$)/m';
  3038 	$wp_smiliessearch .= ')(?=' . $spaces . '|$)/m';
       
  3039 
  2594 }
  3040 }
  2595 
  3041 
  2596 /**
  3042 /**
  2597  * Merge user defined arguments into defaults array.
  3043  * Merge user defined arguments into defaults array.
  2598  *
  3044  *
  2599  * This function is used throughout WordPress to allow for both string or array
  3045  * This function is used throughout WordPress to allow for both string or array
  2600  * to be merged into another array.
  3046  * to be merged into another array.
  2601  *
  3047  *
  2602  * @since 2.2.0
  3048  * @since 2.2.0
  2603  *
  3049  *
  2604  * @param string|array $args Value to merge with $defaults
  3050  * @param string|array $args     Value to merge with $defaults
  2605  * @param array $defaults Array that serves as the defaults.
  3051  * @param array        $defaults Optional. Array that serves as the defaults. Default empty.
  2606  * @return array Merged user defined values with defaults.
  3052  * @return array Merged user defined values with defaults.
  2607  */
  3053  */
  2608 function wp_parse_args( $args, $defaults = '' ) {
  3054 function wp_parse_args( $args, $defaults = '' ) {
  2609 	if ( is_object( $args ) )
  3055 	if ( is_object( $args ) )
  2610 		$r = get_object_vars( $args );
  3056 		$r = get_object_vars( $args );
  2621 /**
  3067 /**
  2622  * Clean up an array, comma- or space-separated list of IDs.
  3068  * Clean up an array, comma- or space-separated list of IDs.
  2623  *
  3069  *
  2624  * @since 3.0.0
  3070  * @since 3.0.0
  2625  *
  3071  *
  2626  * @param array|string $list
  3072  * @param array|string $list List of ids.
  2627  * @return array Sanitized array of IDs
  3073  * @return array Sanitized array of IDs.
  2628  */
  3074  */
  2629 function wp_parse_id_list( $list ) {
  3075 function wp_parse_id_list( $list ) {
  2630 	if ( !is_array($list) )
  3076 	if ( !is_array($list) )
  2631 		$list = preg_split('/[\s,]+/', $list);
  3077 		$list = preg_split('/[\s,]+/', $list);
  2632 
  3078 
  2636 /**
  3082 /**
  2637  * Extract a slice of an array, given a list of keys.
  3083  * Extract a slice of an array, given a list of keys.
  2638  *
  3084  *
  2639  * @since 3.1.0
  3085  * @since 3.1.0
  2640  *
  3086  *
  2641  * @param array $array The original array
  3087  * @param array $array The original array.
  2642  * @param array $keys The list of keys
  3088  * @param array $keys  The list of keys.
  2643  * @return array The array slice
  3089  * @return array The array slice.
  2644  */
  3090  */
  2645 function wp_array_slice_assoc( $array, $keys ) {
  3091 function wp_array_slice_assoc( $array, $keys ) {
  2646 	$slice = array();
  3092 	$slice = array();
  2647 	foreach ( $keys as $key )
  3093 	foreach ( $keys as $key )
  2648 		if ( isset( $array[ $key ] ) )
  3094 		if ( isset( $array[ $key ] ) )
  2654 /**
  3100 /**
  2655  * Filters a list of objects, based on a set of key => value arguments.
  3101  * Filters a list of objects, based on a set of key => value arguments.
  2656  *
  3102  *
  2657  * @since 3.0.0
  3103  * @since 3.0.0
  2658  *
  3104  *
  2659  * @param array $list An array of objects to filter
  3105  * @param array       $list     An array of objects to filter
  2660  * @param array $args An array of key => value arguments to match against each object
  3106  * @param array       $args     Optional. An array of key => value arguments to match
  2661  * @param string $operator The logical operation to perform. 'or' means only one element
  3107  *                              against each object. Default empty array.
  2662  *	from the array needs to match; 'and' means all elements must match. The default is 'and'.
  3108  * @param string      $operator Optional. The logical operation to perform. 'or' means
  2663  * @param bool|string $field A field from the object to place instead of the entire object
  3109  *                              only one element from the array needs to match; 'and'
  2664  * @return array A list of objects or object fields
  3110  *                              means all elements must match. Default 'and'.
       
  3111  * @param bool|string $field    A field from the object to place instead of the entire object.
       
  3112  *                              Default false.
       
  3113  * @return array A list of objects or object fields.
  2665  */
  3114  */
  2666 function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
  3115 function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
  2667 	if ( ! is_array( $list ) )
  3116 	if ( ! is_array( $list ) )
  2668 		return array();
  3117 		return array();
  2669 
  3118 
  2678 /**
  3127 /**
  2679  * Filters a list of objects, based on a set of key => value arguments.
  3128  * Filters a list of objects, based on a set of key => value arguments.
  2680  *
  3129  *
  2681  * @since 3.1.0
  3130  * @since 3.1.0
  2682  *
  3131  *
  2683  * @param array $list An array of objects to filter
  3132  * @param array  $list     An array of objects to filter.
  2684  * @param array $args An array of key => value arguments to match against each object
  3133  * @param array  $args     Optional. An array of key => value arguments to match
  2685  * @param string $operator The logical operation to perform:
  3134  *                         against each object. Default empty array.
  2686  *    'AND' means all elements from the array must match;
  3135  * @param string $operator Optional. The logical operation to perform. 'AND' means
  2687  *    'OR' means only one element needs to match;
  3136  *                         all elements from the array must match. 'OR' means only
  2688  *    'NOT' means no elements may match.
  3137  *                         one element needs to match. 'NOT' means no elements may
  2689  *   The default is 'AND'.
  3138  *                         match. Default 'AND'.
  2690  * @return array
  3139  * @return array Array of found values.
  2691  */
  3140  */
  2692 function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
  3141 function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
  2693 	if ( ! is_array( $list ) )
  3142 	if ( ! is_array( $list ) )
  2694 		return array();
  3143 		return array();
  2695 
  3144 
  2720 }
  3169 }
  2721 
  3170 
  2722 /**
  3171 /**
  2723  * Pluck a certain field out of each object in a list.
  3172  * Pluck a certain field out of each object in a list.
  2724  *
  3173  *
       
  3174  * This has the same functionality and prototype of
       
  3175  * array_column() (PHP 5.5) but also supports objects.
       
  3176  *
  2725  * @since 3.1.0
  3177  * @since 3.1.0
  2726  *
  3178  * @since 4.0.0 $index_key parameter added.
  2727  * @param array $list A list of objects or arrays
  3179  *
  2728  * @param int|string $field A field from the object to place instead of the entire object
  3180  * @param array      $list      List of objects or arrays
  2729  * @return array
  3181  * @param int|string $field     Field from the object to place instead of the entire object
  2730  */
  3182  * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
  2731 function wp_list_pluck( $list, $field ) {
  3183  *                              Default null.
  2732 	foreach ( $list as $key => $value ) {
  3184  * @return array Array of found values. If `$index_key` is set, an array of found values with keys
  2733 		if ( is_object( $value ) )
  3185  *               corresponding to `$index_key`. If `$index_key` is null, array keys from the original
  2734 			$list[ $key ] = $value->$field;
  3186  *               `$list` will be preserved in the results.
  2735 		else
  3187  */
  2736 			$list[ $key ] = $value[ $field ];
  3188 function wp_list_pluck( $list, $field, $index_key = null ) {
  2737 	}
  3189 	if ( ! $index_key ) {
  2738 
  3190 		/*
  2739 	return $list;
  3191 		 * This is simple. Could at some point wrap array_column()
       
  3192 		 * if we knew we had an array of arrays.
       
  3193 		 */
       
  3194 		foreach ( $list as $key => $value ) {
       
  3195 			if ( is_object( $value ) ) {
       
  3196 				$list[ $key ] = $value->$field;
       
  3197 			} else {
       
  3198 				$list[ $key ] = $value[ $field ];
       
  3199 			}
       
  3200 		}
       
  3201 		return $list;
       
  3202 	}
       
  3203 
       
  3204 	/*
       
  3205 	 * When index_key is not set for a particular item, push the value
       
  3206 	 * to the end of the stack. This is how array_column() behaves.
       
  3207 	 */
       
  3208 	$newlist = array();
       
  3209 	foreach ( $list as $value ) {
       
  3210 		if ( is_object( $value ) ) {
       
  3211 			if ( isset( $value->$index_key ) ) {
       
  3212 				$newlist[ $value->$index_key ] = $value->$field;
       
  3213 			} else {
       
  3214 				$newlist[] = $value->$field;
       
  3215 			}
       
  3216 		} else {
       
  3217 			if ( isset( $value[ $index_key ] ) ) {
       
  3218 				$newlist[ $value[ $index_key ] ] = $value[ $field ];
       
  3219 			} else {
       
  3220 				$newlist[] = $value[ $field ];
       
  3221 			}
       
  3222 		}
       
  3223 	}
       
  3224 
       
  3225 	return $newlist;
  2740 }
  3226 }
  2741 
  3227 
  2742 /**
  3228 /**
  2743  * Determines if Widgets library should be loaded.
  3229  * Determines if Widgets library should be loaded.
  2744  *
  3230  *
  2745  * Checks to make sure that the widgets library hasn't already been loaded. If
  3231  * Checks to make sure that the widgets library hasn't already been loaded.
  2746  * it hasn't, then it will load the widgets library and run an action hook.
  3232  * If it hasn't, then it will load the widgets library and run an action hook.
  2747  *
  3233  *
  2748  * @since 2.2.0
  3234  * @since 2.2.0
  2749  * @uses add_action() Calls '_admin_menu' hook with 'wp_widgets_add_menu' value.
       
  2750  */
  3235  */
  2751 function wp_maybe_load_widgets() {
  3236 function wp_maybe_load_widgets() {
  2752 	if ( ! apply_filters('load_default_widgets', true) )
  3237 	/**
       
  3238 	 * Filter whether to load the Widgets library.
       
  3239 	 *
       
  3240 	 * Passing a falsey value to the filter will effectively short-circuit
       
  3241 	 * the Widgets library from loading.
       
  3242 	 *
       
  3243 	 * @since 2.8.0
       
  3244 	 *
       
  3245 	 * @param bool $wp_maybe_load_widgets Whether to load the Widgets library.
       
  3246 	 *                                    Default true.
       
  3247 	 */
       
  3248 	if ( ! apply_filters( 'load_default_widgets', true ) ) {
  2753 		return;
  3249 		return;
       
  3250 	}
       
  3251 
  2754 	require_once( ABSPATH . WPINC . '/default-widgets.php' );
  3252 	require_once( ABSPATH . WPINC . '/default-widgets.php' );
       
  3253 
  2755 	add_action( '_admin_menu', 'wp_widgets_add_menu' );
  3254 	add_action( '_admin_menu', 'wp_widgets_add_menu' );
  2756 }
  3255 }
  2757 
  3256 
  2758 /**
  3257 /**
  2759  * Append the Widgets menu to the themes main menu.
  3258  * Append the Widgets menu to the themes main menu.
  2760  *
  3259  *
  2761  * @since 2.2.0
  3260  * @since 2.2.0
  2762  * @uses $submenu The administration submenu list.
       
  2763  */
  3261  */
  2764 function wp_widgets_add_menu() {
  3262 function wp_widgets_add_menu() {
  2765 	global $submenu;
  3263 	global $submenu;
  2766 
  3264 
  2767 	if ( ! current_theme_supports( 'widgets' ) )
  3265 	if ( ! current_theme_supports( 'widgets' ) )
  2772 }
  3270 }
  2773 
  3271 
  2774 /**
  3272 /**
  2775  * Flush all output buffers for PHP 5.2.
  3273  * Flush all output buffers for PHP 5.2.
  2776  *
  3274  *
  2777  * Make sure all output buffers are flushed before our singletons our destroyed.
  3275  * Make sure all output buffers are flushed before our singletons are destroyed.
  2778  *
  3276  *
  2779  * @since 2.2.0
  3277  * @since 2.2.0
  2780  */
  3278  */
  2781 function wp_ob_end_flush_all() {
  3279 function wp_ob_end_flush_all() {
  2782 	$levels = ob_get_level();
  3280 	$levels = ob_get_level();
  2797  *
  3295  *
  2798  * This function was backported to WordPress 2.3.2, but originally was added
  3296  * This function was backported to WordPress 2.3.2, but originally was added
  2799  * in WordPress 2.5.0.
  3297  * in WordPress 2.5.0.
  2800  *
  3298  *
  2801  * @since 2.3.2
  3299  * @since 2.3.2
  2802  * @uses $wpdb
  3300  *
       
  3301  * @global wpdb $wpdb WordPress database abstraction object.
  2803  */
  3302  */
  2804 function dead_db() {
  3303 function dead_db() {
  2805 	global $wpdb;
  3304 	global $wpdb;
       
  3305 
       
  3306 	wp_load_translations_early();
  2806 
  3307 
  2807 	// Load custom DB error template, if present.
  3308 	// Load custom DB error template, if present.
  2808 	if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
  3309 	if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
  2809 		require_once( WP_CONTENT_DIR . '/db-error.php' );
  3310 		require_once( WP_CONTENT_DIR . '/db-error.php' );
  2810 		die();
  3311 		die();
  2816 
  3317 
  2817 	// Otherwise, be terse.
  3318 	// Otherwise, be terse.
  2818 	status_header( 500 );
  3319 	status_header( 500 );
  2819 	nocache_headers();
  3320 	nocache_headers();
  2820 	header( 'Content-Type: text/html; charset=utf-8' );
  3321 	header( 'Content-Type: text/html; charset=utf-8' );
  2821 
       
  2822 	wp_load_translations_early();
       
  2823 ?>
  3322 ?>
  2824 <!DOCTYPE html>
  3323 <!DOCTYPE html>
  2825 <html xmlns="http://www.w3.org/1999/xhtml"<?php if ( is_rtl() ) echo ' dir="rtl"'; ?>>
  3324 <html xmlns="http://www.w3.org/1999/xhtml"<?php if ( is_rtl() ) echo ' dir="rtl"'; ?>>
  2826 <head>
  3325 <head>
  2827 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  3326 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2835 <?php
  3334 <?php
  2836 	die();
  3335 	die();
  2837 }
  3336 }
  2838 
  3337 
  2839 /**
  3338 /**
  2840  * Converts value to nonnegative integer.
  3339  * Convert a value to non-negative integer.
  2841  *
  3340  *
  2842  * @since 2.5.0
  3341  * @since 2.5.0
  2843  *
  3342  *
  2844  * @param mixed $maybeint Data you wish to have converted to a nonnegative integer
  3343  * @param mixed $maybeint Data you wish to have converted to a non-negative integer.
  2845  * @return int An nonnegative integer
  3344  * @return int A non-negative integer.
  2846  */
  3345  */
  2847 function absint( $maybeint ) {
  3346 function absint( $maybeint ) {
  2848 	return abs( intval( $maybeint ) );
  3347 	return abs( intval( $maybeint ) );
  2849 }
  3348 }
  2850 
  3349 
  2851 /**
  3350 /**
  2852  * Determines if the blog can be accessed over SSL.
  3351  * Mark a function as deprecated and inform when it has been used.
  2853  *
       
  2854  * Determines if blog can be accessed over SSL by using cURL to access the site
       
  2855  * using the https in the siteurl. Requires cURL extension to work correctly.
       
  2856  *
       
  2857  * @since 2.5.0
       
  2858  *
       
  2859  * @param string $url
       
  2860  * @return bool Whether SSL access is available
       
  2861  */
       
  2862 function url_is_accessable_via_ssl($url)
       
  2863 {
       
  2864 	if ( in_array( 'curl', get_loaded_extensions() ) ) {
       
  2865 		$ssl = set_url_scheme( $url, 'https' );
       
  2866 
       
  2867 		$ch = curl_init();
       
  2868 		curl_setopt($ch, CURLOPT_URL, $ssl);
       
  2869 		curl_setopt($ch, CURLOPT_FAILONERROR, true);
       
  2870 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       
  2871 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       
  2872 		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
       
  2873 
       
  2874 		curl_exec($ch);
       
  2875 
       
  2876 		$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
       
  2877 		curl_close ($ch);
       
  2878 
       
  2879 		if ($status == 200 || $status == 401) {
       
  2880 			return true;
       
  2881 		}
       
  2882 	}
       
  2883 	return false;
       
  2884 }
       
  2885 
       
  2886 /**
       
  2887  * Marks a function as deprecated and informs when it has been used.
       
  2888  *
  3352  *
  2889  * There is a hook deprecated_function_run that will be called that can be used
  3353  * There is a hook deprecated_function_run that will be called that can be used
  2890  * to get the backtrace up to what file and function called the deprecated
  3354  * to get the backtrace up to what file and function called the deprecated
  2891  * function.
  3355  * function.
  2892  *
  3356  *
  2893  * The current behavior is to trigger a user error if WP_DEBUG is true.
  3357  * The current behavior is to trigger a user error if WP_DEBUG is true.
  2894  *
  3358  *
  2895  * This function is to be used in every function that is deprecated.
  3359  * This function is to be used in every function that is deprecated.
  2896  *
  3360  *
  2897  * @package WordPress
       
  2898  * @subpackage Debug
       
  2899  * @since 2.5.0
  3361  * @since 2.5.0
  2900  * @access private
  3362  * @access private
  2901  *
  3363  *
  2902  * @uses do_action() Calls 'deprecated_function_run' and passes the function name, what to use instead,
  3364  * @param string $function    The function that was called.
  2903  *   and the version the function was deprecated in.
  3365  * @param string $version     The version of WordPress that deprecated the function.
  2904  * @uses apply_filters() Calls 'deprecated_function_trigger_error' and expects boolean value of true to do
  3366  * @param string $replacement Optional. The function that should have been called. Default null.
  2905  *   trigger or false to not trigger error.
       
  2906  *
       
  2907  * @param string $function The function that was called
       
  2908  * @param string $version The version of WordPress that deprecated the function
       
  2909  * @param string $replacement Optional. The function that should have been called
       
  2910  */
  3367  */
  2911 function _deprecated_function( $function, $version, $replacement = null ) {
  3368 function _deprecated_function( $function, $version, $replacement = null ) {
  2912 
  3369 
       
  3370 	/**
       
  3371 	 * Fires when a deprecated function is called.
       
  3372 	 *
       
  3373 	 * @since 2.5.0
       
  3374 	 *
       
  3375 	 * @param string $function    The function that was called.
       
  3376 	 * @param string $replacement The function that should have been called.
       
  3377 	 * @param string $version     The version of WordPress that deprecated the function.
       
  3378 	 */
  2913 	do_action( 'deprecated_function_run', $function, $replacement, $version );
  3379 	do_action( 'deprecated_function_run', $function, $replacement, $version );
  2914 
  3380 
  2915 	// Allow plugin to filter the output error trigger
  3381 	/**
       
  3382 	 * Filter whether to trigger an error for deprecated functions.
       
  3383 	 *
       
  3384 	 * @since 2.5.0
       
  3385 	 *
       
  3386 	 * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
       
  3387 	 */
  2916 	if ( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true ) ) {
  3388 	if ( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true ) ) {
  2917 		if ( function_exists( '__' ) ) {
  3389 		if ( function_exists( '__' ) ) {
  2918 			if ( ! is_null( $replacement ) )
  3390 			if ( ! is_null( $replacement ) )
  2919 				trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $function, $version, $replacement ) );
  3391 				trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $function, $version, $replacement ) );
  2920 			else
  3392 			else
  2927 		}
  3399 		}
  2928 	}
  3400 	}
  2929 }
  3401 }
  2930 
  3402 
  2931 /**
  3403 /**
  2932  * Marks a file as deprecated and informs when it has been used.
  3404  * Mark a file as deprecated and inform when it has been used.
  2933  *
  3405  *
  2934  * There is a hook deprecated_file_included that will be called that can be used
  3406  * There is a hook deprecated_file_included that will be called that can be used
  2935  * to get the backtrace up to what file and function included the deprecated
  3407  * to get the backtrace up to what file and function included the deprecated
  2936  * file.
  3408  * file.
  2937  *
  3409  *
  2938  * The current behavior is to trigger a user error if WP_DEBUG is true.
  3410  * The current behavior is to trigger a user error if WP_DEBUG is true.
  2939  *
  3411  *
  2940  * This function is to be used in every file that is deprecated.
  3412  * This function is to be used in every file that is deprecated.
  2941  *
  3413  *
  2942  * @package WordPress
       
  2943  * @subpackage Debug
       
  2944  * @since 2.5.0
  3414  * @since 2.5.0
  2945  * @access private
  3415  * @access private
  2946  *
  3416  *
  2947  * @uses do_action() Calls 'deprecated_file_included' and passes the file name, what to use instead,
  3417  * @param string $file        The file that was included.
  2948  *   the version in which the file was deprecated, and any message regarding the change.
  3418  * @param string $version     The version of WordPress that deprecated the file.
  2949  * @uses apply_filters() Calls 'deprecated_file_trigger_error' and expects boolean value of true to do
  3419  * @param string $replacement Optional. The file that should have been included based on ABSPATH.
  2950  *   trigger or false to not trigger error.
  3420  *                            Default null.
  2951  *
  3421  * @param string $message     Optional. A message regarding the change. Default empty.
  2952  * @param string $file The file that was included
       
  2953  * @param string $version The version of WordPress that deprecated the file
       
  2954  * @param string $replacement Optional. The file that should have been included based on ABSPATH
       
  2955  * @param string $message Optional. A message regarding the change
       
  2956  */
  3422  */
  2957 function _deprecated_file( $file, $version, $replacement = null, $message = '' ) {
  3423 function _deprecated_file( $file, $version, $replacement = null, $message = '' ) {
  2958 
  3424 
       
  3425 	/**
       
  3426 	 * Fires when a deprecated file is called.
       
  3427 	 *
       
  3428 	 * @since 2.5.0
       
  3429 	 *
       
  3430 	 * @param string $file        The file that was called.
       
  3431 	 * @param string $replacement The file that should have been included based on ABSPATH.
       
  3432 	 * @param string $version     The version of WordPress that deprecated the file.
       
  3433 	 * @param string $message     A message regarding the change.
       
  3434 	 */
  2959 	do_action( 'deprecated_file_included', $file, $replacement, $version, $message );
  3435 	do_action( 'deprecated_file_included', $file, $replacement, $version, $message );
  2960 
  3436 
  2961 	// Allow plugin to filter the output error trigger
  3437 	/**
       
  3438 	 * Filter whether to trigger an error for deprecated files.
       
  3439 	 *
       
  3440 	 * @since 2.5.0
       
  3441 	 *
       
  3442 	 * @param bool $trigger Whether to trigger the error for deprecated files. Default true.
       
  3443 	 */
  2962 	if ( WP_DEBUG && apply_filters( 'deprecated_file_trigger_error', true ) ) {
  3444 	if ( WP_DEBUG && apply_filters( 'deprecated_file_trigger_error', true ) ) {
  2963 		$message = empty( $message ) ? '' : ' ' . $message;
  3445 		$message = empty( $message ) ? '' : ' ' . $message;
  2964 		if ( function_exists( '__' ) ) {
  3446 		if ( function_exists( '__' ) ) {
  2965 			if ( ! is_null( $replacement ) )
  3447 			if ( ! is_null( $replacement ) )
  2966 				trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $file, $version, $replacement ) . $message );
  3448 				trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $file, $version, $replacement ) . $message );
  2973 				trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $file, $version ) . $message );
  3455 				trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $file, $version ) . $message );
  2974 		}
  3456 		}
  2975 	}
  3457 	}
  2976 }
  3458 }
  2977 /**
  3459 /**
  2978  * Marks a function argument as deprecated and informs when it has been used.
  3460  * Mark a function argument as deprecated and inform when it has been used.
  2979  *
  3461  *
  2980  * This function is to be used whenever a deprecated function argument is used.
  3462  * This function is to be used whenever a deprecated function argument is used.
  2981  * Before this function is called, the argument must be checked for whether it was
  3463  * Before this function is called, the argument must be checked for whether it was
  2982  * used by comparing it to its default value or evaluating whether it is empty.
  3464  * used by comparing it to its default value or evaluating whether it is empty.
  2983  * For example:
  3465  * For example:
  2984  * <code>
  3466  *
  2985  * if ( !empty($deprecated) )
  3467  *     if ( ! empty( $deprecated ) ) {
  2986  * 	_deprecated_argument( __FUNCTION__, '3.0' );
  3468  *         _deprecated_argument( __FUNCTION__, '3.0' );
  2987  * </code>
  3469  *     }
       
  3470  *
  2988  *
  3471  *
  2989  * There is a hook deprecated_argument_run that will be called that can be used
  3472  * There is a hook deprecated_argument_run that will be called that can be used
  2990  * to get the backtrace up to what file and function used the deprecated
  3473  * to get the backtrace up to what file and function used the deprecated
  2991  * argument.
  3474  * argument.
  2992  *
  3475  *
  2993  * The current behavior is to trigger a user error if WP_DEBUG is true.
  3476  * The current behavior is to trigger a user error if WP_DEBUG is true.
  2994  *
  3477  *
  2995  * @package WordPress
       
  2996  * @subpackage Debug
       
  2997  * @since 3.0.0
  3478  * @since 3.0.0
  2998  * @access private
  3479  * @access private
  2999  *
  3480  *
  3000  * @uses do_action() Calls 'deprecated_argument_run' and passes the function name, a message on the change,
  3481  * @param string $function The function that was called.
  3001  *   and the version in which the argument was deprecated.
  3482  * @param string $version  The version of WordPress that deprecated the argument used.
  3002  * @uses apply_filters() Calls 'deprecated_argument_trigger_error' and expects boolean value of true to do
  3483  * @param string $message  Optional. A message regarding the change. Default null.
  3003  *   trigger or false to not trigger error.
       
  3004  *
       
  3005  * @param string $function The function that was called
       
  3006  * @param string $version The version of WordPress that deprecated the argument used
       
  3007  * @param string $message Optional. A message regarding the change.
       
  3008  */
  3484  */
  3009 function _deprecated_argument( $function, $version, $message = null ) {
  3485 function _deprecated_argument( $function, $version, $message = null ) {
  3010 
  3486 
       
  3487 	/**
       
  3488 	 * Fires when a deprecated argument is called.
       
  3489 	 *
       
  3490 	 * @since 3.0.0
       
  3491 	 *
       
  3492 	 * @param string $function The function that was called.
       
  3493 	 * @param string $message  A message regarding the change.
       
  3494 	 * @param string $version  The version of WordPress that deprecated the argument used.
       
  3495 	 */
  3011 	do_action( 'deprecated_argument_run', $function, $message, $version );
  3496 	do_action( 'deprecated_argument_run', $function, $message, $version );
  3012 
  3497 
  3013 	// Allow plugin to filter the output error trigger
  3498 	/**
       
  3499 	 * Filter whether to trigger an error for deprecated arguments.
       
  3500 	 *
       
  3501 	 * @since 3.0.0
       
  3502 	 *
       
  3503 	 * @param bool $trigger Whether to trigger the error for deprecated arguments. Default true.
       
  3504 	 */
  3014 	if ( WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) {
  3505 	if ( WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) {
  3015 		if ( function_exists( '__' ) ) {
  3506 		if ( function_exists( '__' ) ) {
  3016 			if ( ! is_null( $message ) )
  3507 			if ( ! is_null( $message ) )
  3017 				trigger_error( sprintf( __('%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s'), $function, $version, $message ) );
  3508 				trigger_error( sprintf( __('%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s'), $function, $version, $message ) );
  3018 			else
  3509 			else
  3025 		}
  3516 		}
  3026 	}
  3517 	}
  3027 }
  3518 }
  3028 
  3519 
  3029 /**
  3520 /**
  3030  * Marks something as being incorrectly called.
  3521  * Mark something as being incorrectly called.
  3031  *
  3522  *
  3032  * There is a hook doing_it_wrong_run that will be called that can be used
  3523  * There is a hook doing_it_wrong_run that will be called that can be used
  3033  * to get the backtrace up to what file and function called the deprecated
  3524  * to get the backtrace up to what file and function called the deprecated
  3034  * function.
  3525  * function.
  3035  *
  3526  *
  3036  * The current behavior is to trigger a user error if WP_DEBUG is true.
  3527  * The current behavior is to trigger a user error if WP_DEBUG is true.
  3037  *
  3528  *
  3038  * @package WordPress
       
  3039  * @subpackage Debug
       
  3040  * @since 3.1.0
  3529  * @since 3.1.0
  3041  * @access private
  3530  * @access private
  3042  *
  3531  *
  3043  * @uses do_action() Calls 'doing_it_wrong_run' and passes the function arguments.
       
  3044  * @uses apply_filters() Calls 'doing_it_wrong_trigger_error' and expects boolean value of true to do
       
  3045  *   trigger or false to not trigger error.
       
  3046  *
       
  3047  * @param string $function The function that was called.
  3532  * @param string $function The function that was called.
  3048  * @param string $message A message explaining what has been done incorrectly.
  3533  * @param string $message  A message explaining what has been done incorrectly.
  3049  * @param string $version The version of WordPress where the message was added.
  3534  * @param string $version  The version of WordPress where the message was added.
  3050  */
  3535  */
  3051 function _doing_it_wrong( $function, $message, $version ) {
  3536 function _doing_it_wrong( $function, $message, $version ) {
  3052 
  3537 
       
  3538 	/**
       
  3539 	 * Fires when the given function is being used incorrectly.
       
  3540 	 *
       
  3541 	 * @since 3.1.0
       
  3542 	 *
       
  3543 	 * @param string $function The function that was called.
       
  3544 	 * @param string $message  A message explaining what has been done incorrectly.
       
  3545 	 * @param string $version  The version of WordPress where the message was added.
       
  3546 	 */
  3053 	do_action( 'doing_it_wrong_run', $function, $message, $version );
  3547 	do_action( 'doing_it_wrong_run', $function, $message, $version );
  3054 
  3548 
  3055 	// Allow plugin to filter the output error trigger
  3549 	/**
       
  3550 	 * Filter whether to trigger an error for _doing_it_wrong() calls.
       
  3551 	 *
       
  3552 	 * @since 3.1.0
       
  3553 	 *
       
  3554 	 * @param bool $trigger Whether to trigger the error for _doing_it_wrong() calls. Default true.
       
  3555 	 */
  3056 	if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
  3556 	if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
  3057 		if ( function_exists( '__' ) ) {
  3557 		if ( function_exists( '__' ) ) {
  3058 			$version = is_null( $version ) ? '' : sprintf( __( '(This message was added in version %s.)' ), $version );
  3558 			$version = is_null( $version ) ? '' : sprintf( __( '(This message was added in version %s.)' ), $version );
  3059 			$message .= ' ' . __( 'Please see <a href="http://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information.' );
  3559 			$message .= ' ' . __( 'Please see <a href="https://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information.' );
  3060 			trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) );
  3560 			trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) );
  3061 		} else {
  3561 		} else {
  3062 			$version = is_null( $version ) ? '' : sprintf( '(This message was added in version %s.)', $version );
  3562 			$version = is_null( $version ) ? '' : sprintf( '(This message was added in version %s.)', $version );
  3063 			$message .= ' Please see <a href="http://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information.';
  3563 			$message .= ' Please see <a href="https://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information.';
  3064 			trigger_error( sprintf( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', $function, $message, $version ) );
  3564 			trigger_error( sprintf( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', $function, $message, $version ) );
  3065 		}
  3565 		}
  3066 	}
  3566 	}
  3067 }
  3567 }
  3068 
  3568 
  3069 /**
  3569 /**
  3070  * Is the server running earlier than 1.5.0 version of lighttpd?
  3570  * Is the server running earlier than 1.5.0 version of lighttpd?
  3071  *
  3571  *
  3072  * @since 2.5.0
  3572  * @since 2.5.0
  3073  *
  3573  *
  3074  * @return bool Whether the server is running lighttpd < 1.5.0
  3574  * @return bool Whether the server is running lighttpd < 1.5.0.
  3075  */
  3575  */
  3076 function is_lighttpd_before_150() {
  3576 function is_lighttpd_before_150() {
  3077 	$server_parts = explode( '/', isset( $_SERVER['SERVER_SOFTWARE'] )? $_SERVER['SERVER_SOFTWARE'] : '' );
  3577 	$server_parts = explode( '/', isset( $_SERVER['SERVER_SOFTWARE'] )? $_SERVER['SERVER_SOFTWARE'] : '' );
  3078 	$server_parts[1] = isset( $server_parts[1] )? $server_parts[1] : '';
  3578 	$server_parts[1] = isset( $server_parts[1] )? $server_parts[1] : '';
  3079 	return  'lighttpd' == $server_parts[0] && -1 == version_compare( $server_parts[1], '1.5.0' );
  3579 	return  'lighttpd' == $server_parts[0] && -1 == version_compare( $server_parts[1], '1.5.0' );
  3082 /**
  3582 /**
  3083  * Does the specified module exist in the Apache config?
  3583  * Does the specified module exist in the Apache config?
  3084  *
  3584  *
  3085  * @since 2.5.0
  3585  * @since 2.5.0
  3086  *
  3586  *
  3087  * @param string $mod e.g. mod_rewrite
  3587  * @param string $mod     The module, e.g. mod_rewrite.
  3088  * @param bool $default The default return value if the module is not found
  3588  * @param bool   $default Optional. The default return value if the module is not found. Default false.
  3089  * @return bool
  3589  * @return bool Whether the specified module is loaded.
  3090  */
  3590  */
  3091 function apache_mod_loaded($mod, $default = false) {
  3591 function apache_mod_loaded($mod, $default = false) {
  3092 	global $is_apache;
  3592 	global $is_apache;
  3093 
  3593 
  3094 	if ( !$is_apache )
  3594 	if ( !$is_apache )
  3095 		return false;
  3595 		return false;
  3096 
  3596 
  3097 	if ( function_exists('apache_get_modules') ) {
  3597 	if ( function_exists( 'apache_get_modules' ) ) {
  3098 		$mods = apache_get_modules();
  3598 		$mods = apache_get_modules();
  3099 		if ( in_array($mod, $mods) )
  3599 		if ( in_array($mod, $mods) )
  3100 			return true;
  3600 			return true;
  3101 	} elseif ( function_exists('phpinfo') ) {
  3601 	} elseif ( function_exists( 'phpinfo' ) && false === strpos( ini_get( 'disable_functions' ), 'phpinfo' ) ) {
  3102 			ob_start();
  3602 			ob_start();
  3103 			phpinfo(8);
  3603 			phpinfo(8);
  3104 			$phpinfo = ob_get_clean();
  3604 			$phpinfo = ob_get_clean();
  3105 			if ( false !== strpos($phpinfo, $mod) )
  3605 			if ( false !== strpos($phpinfo, $mod) )
  3106 				return true;
  3606 				return true;
  3111 /**
  3611 /**
  3112  * Check if IIS 7+ supports pretty permalinks.
  3612  * Check if IIS 7+ supports pretty permalinks.
  3113  *
  3613  *
  3114  * @since 2.8.0
  3614  * @since 2.8.0
  3115  *
  3615  *
  3116  * @return bool
  3616  * @return bool Whether IIS7 supports permalinks.
  3117  */
  3617  */
  3118 function iis7_supports_permalinks() {
  3618 function iis7_supports_permalinks() {
  3119 	global $is_iis7;
  3619 	global $is_iis7;
  3120 
  3620 
  3121 	$supports_permalinks = false;
  3621 	$supports_permalinks = false;
  3127 		 * Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When
  3627 		 * Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When
  3128 		 * URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'.
  3628 		 * URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'.
  3129 		 * Lastly we make sure that PHP is running via FastCGI. This is important because if it runs
  3629 		 * Lastly we make sure that PHP is running via FastCGI. This is important because if it runs
  3130 		 * via ISAPI then pretty permalinks will not work.
  3630 		 * via ISAPI then pretty permalinks will not work.
  3131 		 */
  3631 		 */
  3132 		$supports_permalinks = class_exists('DOMDocument') && isset($_SERVER['IIS_UrlRewriteModule']) && ( php_sapi_name() == 'cgi-fcgi' );
  3632 		$supports_permalinks = class_exists('DOMDocument') && isset($_SERVER['IIS_UrlRewriteModule']) && ( PHP_SAPI == 'cgi-fcgi' );
  3133 	}
  3633 	}
  3134 
  3634 
  3135 	return apply_filters('iis7_supports_permalinks', $supports_permalinks);
  3635 	/**
       
  3636 	 * Filter whether IIS 7+ supports pretty permalinks.
       
  3637 	 *
       
  3638 	 * @since 2.8.0
       
  3639 	 *
       
  3640 	 * @param bool $supports_permalinks Whether IIS7 supports permalinks. Default false.
       
  3641 	 */
       
  3642 	return apply_filters( 'iis7_supports_permalinks', $supports_permalinks );
  3136 }
  3643 }
  3137 
  3644 
  3138 /**
  3645 /**
  3139  * File validates against allowed set of defined rules.
  3646  * File validates against allowed set of defined rules.
  3140  *
  3647  *
  3187 /**
  3694 /**
  3188  * Whether SSL login should be forced.
  3695  * Whether SSL login should be forced.
  3189  *
  3696  *
  3190  * @since 2.6.0
  3697  * @since 2.6.0
  3191  *
  3698  *
  3192  * @param string|bool $force Optional.
  3699  * @see force_ssl_admin()
       
  3700  *
       
  3701  * @param string|bool $force Optional Whether to force SSL login. Default null.
  3193  * @return bool True if forced, false if not forced.
  3702  * @return bool True if forced, false if not forced.
  3194  */
  3703  */
  3195 function force_ssl_login( $force = null ) {
  3704 function force_ssl_login( $force = null ) {
       
  3705 	return force_ssl_admin( $force );
       
  3706 }
       
  3707 
       
  3708 /**
       
  3709  * Whether to force SSL used for the Administration Screens.
       
  3710  *
       
  3711  * @since 2.6.0
       
  3712  *
       
  3713  * @param string|bool $force Optional. Whether to force SSL in admin screens. Default null.
       
  3714  * @return bool True if forced, false if not forced.
       
  3715  */
       
  3716 function force_ssl_admin( $force = null ) {
  3196 	static $forced = false;
  3717 	static $forced = false;
  3197 
  3718 
  3198 	if ( !is_null( $force ) ) {
  3719 	if ( !is_null( $force ) ) {
  3199 		$old_forced = $forced;
  3720 		$old_forced = $forced;
  3200 		$forced = $force;
  3721 		$forced = $force;
  3203 
  3724 
  3204 	return $forced;
  3725 	return $forced;
  3205 }
  3726 }
  3206 
  3727 
  3207 /**
  3728 /**
  3208  * Whether to force SSL used for the Administration Screens.
       
  3209  *
       
  3210  * @since 2.6.0
       
  3211  *
       
  3212  * @param string|bool $force
       
  3213  * @return bool True if forced, false if not forced.
       
  3214  */
       
  3215 function force_ssl_admin( $force = null ) {
       
  3216 	static $forced = false;
       
  3217 
       
  3218 	if ( !is_null( $force ) ) {
       
  3219 		$old_forced = $forced;
       
  3220 		$forced = $force;
       
  3221 		return $old_forced;
       
  3222 	}
       
  3223 
       
  3224 	return $forced;
       
  3225 }
       
  3226 
       
  3227 /**
       
  3228  * Guess the URL for the site.
  3729  * Guess the URL for the site.
  3229  *
  3730  *
  3230  * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
  3731  * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
  3231  * directory.
  3732  * directory.
  3232  *
  3733  *
  3233  * @since 2.6.0
  3734  * @since 2.6.0
  3234  *
  3735  *
  3235  * @return string
  3736  * @return string The guessed URL.
  3236  */
  3737  */
  3237 function wp_guess_url() {
  3738 function wp_guess_url() {
  3238 	if ( defined('WP_SITEURL') && '' != WP_SITEURL ) {
  3739 	if ( defined('WP_SITEURL') && '' != WP_SITEURL ) {
  3239 		$url = WP_SITEURL;
  3740 		$url = WP_SITEURL;
  3240 	} else {
  3741 	} else {
  3256 				$directory = str_replace( ABSPATH, '', $script_filename_dir );
  3757 				$directory = str_replace( ABSPATH, '', $script_filename_dir );
  3257 				// Strip off the sub directory, and any file/query paramss
  3758 				// Strip off the sub directory, and any file/query paramss
  3258 				$path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] );
  3759 				$path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] );
  3259 			} elseif ( false !== strpos( $abspath_fix, $script_filename_dir ) ) {
  3760 			} elseif ( false !== strpos( $abspath_fix, $script_filename_dir ) ) {
  3260 				// Request is hitting a file above ABSPATH
  3761 				// Request is hitting a file above ABSPATH
  3261 				$subdirectory = str_replace( $script_filename_dir, '', $abspath_fix );
  3762 				$subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) );
  3262 				// Strip off any file/query params from the path, appending the sub directory to the install
  3763 				// Strip off any file/query params from the path, appending the sub directory to the install
  3263 				$path = preg_replace( '#/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] ) . $subdirectory;
  3764 				$path = preg_replace( '#/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] ) . $subdirectory;
  3264 			} else {
  3765 			} else {
  3265 				$path = $_SERVER['REQUEST_URI'];
  3766 				$path = $_SERVER['REQUEST_URI'];
  3266 			}
  3767 			}
  3298 }
  3799 }
  3299 
  3800 
  3300 /**
  3801 /**
  3301  * Suspend cache invalidation.
  3802  * Suspend cache invalidation.
  3302  *
  3803  *
  3303  * Turns cache invalidation on and off. Useful during imports where you don't wont to do invalidations
  3804  * Turns cache invalidation on and off. Useful during imports where you don't wont to do
  3304  * every time a post is inserted. Callers must be sure that what they are doing won't lead to an inconsistent
  3805  * invalidations every time a post is inserted. Callers must be sure that what they are
  3305  * cache when invalidation is suspended.
  3806  * doing won't lead to an inconsistent cache when invalidation is suspended.
  3306  *
  3807  *
  3307  * @since 2.7.0
  3808  * @since 2.7.0
  3308  *
  3809  *
  3309  * @param bool $suspend Whether to suspend or enable cache invalidation
  3810  * @param bool $suspend Optional. Whether to suspend or enable cache invalidation. Default true.
  3310  * @return bool The current suspend setting
  3811  * @return bool The current suspend setting.
  3311  */
  3812  */
  3312 function wp_suspend_cache_invalidation($suspend = true) {
  3813 function wp_suspend_cache_invalidation( $suspend = true ) {
  3313 	global $_wp_suspend_cache_invalidation;
  3814 	global $_wp_suspend_cache_invalidation;
  3314 
  3815 
  3315 	$current_suspend = $_wp_suspend_cache_invalidation;
  3816 	$current_suspend = $_wp_suspend_cache_invalidation;
  3316 	$_wp_suspend_cache_invalidation = $suspend;
  3817 	$_wp_suspend_cache_invalidation = $suspend;
  3317 	return $current_suspend;
  3818 	return $current_suspend;
  3318 }
  3819 }
  3319 
  3820 
  3320 /**
  3821 /**
  3321  * Whether a site is the main site of the current network.
  3822  * Determine whether a site is the main site of the current network.
  3322  *
  3823  *
  3323  * @since 3.0.0
  3824  * @since 3.0.0
  3324  *
  3825  *
  3325  * @param int $site_id Optional. Site ID to test. Defaults to current site.
  3826  * @param int $site_id Optional. Site ID to test. Defaults to current site.
  3326  * @return bool True if $site_id is the main site of the network, or if not running multisite.
  3827  *                     Defaults to current site.
       
  3828  * @return bool True if $site_id is the main site of the network, or if not
       
  3829  *              running Multisite.
  3327  */
  3830  */
  3328 function is_main_site( $site_id = null ) {
  3831 function is_main_site( $site_id = null ) {
  3329 	// This is the current network's information; 'site' is old terminology.
  3832 	// This is the current network's information; 'site' is old terminology.
  3330 	global $current_site;
  3833 	global $current_site;
  3331 
  3834 
  3337 
  3840 
  3338 	return (int) $site_id === (int) $current_site->blog_id;
  3841 	return (int) $site_id === (int) $current_site->blog_id;
  3339 }
  3842 }
  3340 
  3843 
  3341 /**
  3844 /**
  3342  * Whether a network is the main network of the multisite install.
  3845  * Determine whether a network is the main network of the Multisite install.
  3343  *
  3846  *
  3344  * @since 3.7.0
  3847  * @since 3.7.0
  3345  *
  3848  *
  3346  * @param int $network_id Optional. Network ID to test. Defaults to current network.
  3849  * @param int $network_id Optional. Network ID to test. Defaults to current network.
  3347  * @return bool True if $network_id is the main network, or if not running multisite.
  3850  * @return bool True if $network_id is the main network, or if not running Multisite.
  3348  */
  3851  */
  3349 function is_main_network( $network_id = null ) {
  3852 function is_main_network( $network_id = null ) {
  3350 	global $current_site, $wpdb;
  3853 	global $wpdb;
  3351 
  3854 
  3352 	if ( ! is_multisite() )
  3855 	if ( ! is_multisite() )
  3353 		return true;
  3856 		return true;
  3354 
  3857 
  3355 	$current_network_id = (int) $current_site->id;
  3858 	$current_network_id = (int) get_current_site()->id;
  3356 
  3859 
  3357 	if ( ! $network_id )
  3860 	if ( ! $network_id )
  3358 		$network_id = $current_network_id;
  3861 		$network_id = $current_network_id;
  3359 	$network_id = (int) $network_id;
  3862 	$network_id = (int) $network_id;
  3360 
  3863 
  3374 
  3877 
  3375 	return $network_id === $primary_network_id;
  3878 	return $network_id === $primary_network_id;
  3376 }
  3879 }
  3377 
  3880 
  3378 /**
  3881 /**
  3379  * Whether global terms are enabled.
  3882  * Determine whether global terms are enabled.
  3380  *
       
  3381  *
  3883  *
  3382  * @since 3.0.0
  3884  * @since 3.0.0
  3383  * @package WordPress
  3885  *
  3384  *
  3886  * @return bool True if multisite and global terms enabled.
  3385  * @return bool True if multisite and global terms enabled
       
  3386  */
  3887  */
  3387 function global_terms_enabled() {
  3888 function global_terms_enabled() {
  3388 	if ( ! is_multisite() )
  3889 	if ( ! is_multisite() )
  3389 		return false;
  3890 		return false;
  3390 
  3891 
  3391 	static $global_terms = null;
  3892 	static $global_terms = null;
  3392 	if ( is_null( $global_terms ) ) {
  3893 	if ( is_null( $global_terms ) ) {
       
  3894 
       
  3895 		/**
       
  3896 		 * Filter whether global terms are enabled.
       
  3897 		 *
       
  3898 		 * Passing a non-null value to the filter will effectively short-circuit the function,
       
  3899 		 * returning the value of the 'global_terms_enabled' site option instead.
       
  3900 		 *
       
  3901 		 * @since 3.0.0
       
  3902 		 *
       
  3903 		 * @param null $anbled Whether global terms are enabled.
       
  3904 		 */
  3393 		$filter = apply_filters( 'global_terms_enabled', null );
  3905 		$filter = apply_filters( 'global_terms_enabled', null );
  3394 		if ( ! is_null( $filter ) )
  3906 		if ( ! is_null( $filter ) )
  3395 			$global_terms = (bool) $filter;
  3907 			$global_terms = (bool) $filter;
  3396 		else
  3908 		else
  3397 			$global_terms = (bool) get_site_option( 'global_terms_enabled', false );
  3909 			$global_terms = (bool) get_site_option( 'global_terms_enabled', false );
  3404  *
  3916  *
  3405  * Overrides the gmt_offset option if we have a timezone_string available.
  3917  * Overrides the gmt_offset option if we have a timezone_string available.
  3406  *
  3918  *
  3407  * @since 2.8.0
  3919  * @since 2.8.0
  3408  *
  3920  *
  3409  * @return float|bool
  3921  * @return float|bool Timezone GMT offset, false otherwise.
  3410  */
  3922  */
  3411 function wp_timezone_override_offset() {
  3923 function wp_timezone_override_offset() {
  3412 	if ( !$timezone_string = get_option( 'timezone_string' ) ) {
  3924 	if ( !$timezone_string = get_option( 'timezone_string' ) ) {
  3413 		return false;
  3925 		return false;
  3414 	}
  3926 	}
  3423 
  3935 
  3424 /**
  3936 /**
  3425  * Sort-helper for timezones.
  3937  * Sort-helper for timezones.
  3426  *
  3938  *
  3427  * @since 2.9.0
  3939  * @since 2.9.0
       
  3940  * @access private
  3428  *
  3941  *
  3429  * @param array $a
  3942  * @param array $a
  3430  * @param array $b
  3943  * @param array $b
  3431  * @return int
  3944  * @return int
  3432  */
  3945  */
  3467 		return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
  3980 		return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
  3468 	}
  3981 	}
  3469 }
  3982 }
  3470 
  3983 
  3471 /**
  3984 /**
  3472  * Gives a nicely formatted list of timezone strings.
  3985  * Gives a nicely-formatted list of timezone strings.
  3473  *
  3986  *
  3474  * @since 2.9.0
  3987  * @since 2.9.0
  3475  *
  3988  *
  3476  * @param string $selected_zone Selected Zone
  3989  * @param string $selected_zone Selected timezone.
  3477  * @return string
  3990  * @return string
  3478  */
  3991  */
  3479 function wp_timezone_choice( $selected_zone ) {
  3992 function wp_timezone_choice( $selected_zone ) {
  3480 	static $mo_loaded = false;
  3993 	static $mo_loaded = false;
  3481 
  3994 
  3597 	return join( "\n", $structure );
  4110 	return join( "\n", $structure );
  3598 }
  4111 }
  3599 
  4112 
  3600 /**
  4113 /**
  3601  * Strip close comment and close php tags from file headers used by WP.
  4114  * Strip close comment and close php tags from file headers used by WP.
  3602  * See http://core.trac.wordpress.org/ticket/8497
       
  3603  *
  4115  *
  3604  * @since 2.8.0
  4116  * @since 2.8.0
  3605  *
  4117  * @access private
  3606  * @param string $str
  4118  *
       
  4119  * @see https://core.trac.wordpress.org/ticket/8497
       
  4120  *
       
  4121  * @param string $str Header comment to clean up.
  3607  * @return string
  4122  * @return string
  3608  */
  4123  */
  3609 function _cleanup_header_comment($str) {
  4124 function _cleanup_header_comment( $str ) {
  3610 	return trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $str));
  4125 	return trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $str));
  3611 }
  4126 }
  3612 
  4127 
  3613 /**
  4128 /**
  3614  * Permanently deletes posts, pages, attachments, and comments which have been in the trash for EMPTY_TRASH_DAYS.
  4129  * Permanently delete comments or posts of any type that have held a status
       
  4130  * of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
       
  4131  *
       
  4132  * The default value of `EMPTY_TRASH_DAYS` is 30 (days).
  3615  *
  4133  *
  3616  * @since 2.9.0
  4134  * @since 2.9.0
  3617  */
  4135  */
  3618 function wp_scheduled_delete() {
  4136 function wp_scheduled_delete() {
  3619 	global $wpdb;
  4137 	global $wpdb;
  3663  * lines, the value will get cut at the end of the first line.
  4181  * lines, the value will get cut at the end of the first line.
  3664  *
  4182  *
  3665  * If the file data is not within that first 8kiB, then the author should correct
  4183  * If the file data is not within that first 8kiB, then the author should correct
  3666  * their plugin file and move the data headers to the top.
  4184  * their plugin file and move the data headers to the top.
  3667  *
  4185  *
  3668  * @see http://codex.wordpress.org/File_Header
  4186  * @link https://codex.wordpress.org/File_Header
  3669  *
  4187  *
  3670  * @since 2.9.0
  4188  * @since 2.9.0
  3671  * @param string $file Path to the file
  4189  *
  3672  * @param array $default_headers List of headers, in the format array('HeaderKey' => 'Header Name')
  4190  * @param string $file            Path to the file.
  3673  * @param string $context If specified adds filter hook "extra_{$context}_headers"
  4191  * @param array  $default_headers List of headers, in the format array('HeaderKey' => 'Header Name').
       
  4192  * @param string $context         Optional. If specified adds filter hook "extra_{$context}_headers".
       
  4193  *                                Default empty.
       
  4194  * @return array Array of file headers in `HeaderKey => Header Value` format.
  3674  */
  4195  */
  3675 function get_file_data( $file, $default_headers, $context = '' ) {
  4196 function get_file_data( $file, $default_headers, $context = '' ) {
  3676 	// We don't need to write to the file, so just open for reading.
  4197 	// We don't need to write to the file, so just open for reading.
  3677 	$fp = fopen( $file, 'r' );
  4198 	$fp = fopen( $file, 'r' );
  3678 
  4199 
  3683 	fclose( $fp );
  4204 	fclose( $fp );
  3684 
  4205 
  3685 	// Make sure we catch CR-only line endings.
  4206 	// Make sure we catch CR-only line endings.
  3686 	$file_data = str_replace( "\r", "\n", $file_data );
  4207 	$file_data = str_replace( "\r", "\n", $file_data );
  3687 
  4208 
       
  4209 	/**
       
  4210 	 * Filter extra file headers by context.
       
  4211 	 *
       
  4212 	 * The dynamic portion of the hook name, `$context`, refers to
       
  4213 	 * the context where extra headers might be loaded.
       
  4214 	 *
       
  4215 	 * @since 2.9.0
       
  4216 	 *
       
  4217 	 * @param array $extra_context_headers Empty array by default.
       
  4218 	 */
  3688 	if ( $context && $extra_headers = apply_filters( "extra_{$context}_headers", array() ) ) {
  4219 	if ( $context && $extra_headers = apply_filters( "extra_{$context}_headers", array() ) ) {
  3689 		$extra_headers = array_combine( $extra_headers, $extra_headers ); // keys equal values
  4220 		$extra_headers = array_combine( $extra_headers, $extra_headers ); // keys equal values
  3690 		$all_headers = array_merge( $extra_headers, (array) $default_headers );
  4221 		$all_headers = array_merge( $extra_headers, (array) $default_headers );
  3691 	} else {
  4222 	} else {
  3692 		$all_headers = $default_headers;
  4223 		$all_headers = $default_headers;
  3706  * Returns true.
  4237  * Returns true.
  3707  *
  4238  *
  3708  * Useful for returning true to filters easily.
  4239  * Useful for returning true to filters easily.
  3709  *
  4240  *
  3710  * @since 3.0.0
  4241  * @since 3.0.0
       
  4242  *
  3711  * @see __return_false()
  4243  * @see __return_false()
  3712  * @return bool true
  4244  *
       
  4245  * @return bool True.
  3713  */
  4246  */
  3714 function __return_true() {
  4247 function __return_true() {
  3715 	return true;
  4248 	return true;
  3716 }
  4249 }
  3717 
  4250 
  3719  * Returns false.
  4252  * Returns false.
  3720  *
  4253  *
  3721  * Useful for returning false to filters easily.
  4254  * Useful for returning false to filters easily.
  3722  *
  4255  *
  3723  * @since 3.0.0
  4256  * @since 3.0.0
       
  4257  *
  3724  * @see __return_true()
  4258  * @see __return_true()
  3725  * @return bool false
  4259  *
       
  4260  * @return bool False.
  3726  */
  4261  */
  3727 function __return_false() {
  4262 function __return_false() {
  3728 	return false;
  4263 	return false;
  3729 }
  4264 }
  3730 
  4265 
  3732  * Returns 0.
  4267  * Returns 0.
  3733  *
  4268  *
  3734  * Useful for returning 0 to filters easily.
  4269  * Useful for returning 0 to filters easily.
  3735  *
  4270  *
  3736  * @since 3.0.0
  4271  * @since 3.0.0
  3737  * @see __return_zero()
  4272  *
  3738  * @return int 0
  4273  * @return int 0.
  3739  */
  4274  */
  3740 function __return_zero() {
  4275 function __return_zero() {
  3741 	return 0;
  4276 	return 0;
  3742 }
  4277 }
  3743 
  4278 
  3745  * Returns an empty array.
  4280  * Returns an empty array.
  3746  *
  4281  *
  3747  * Useful for returning an empty array to filters easily.
  4282  * Useful for returning an empty array to filters easily.
  3748  *
  4283  *
  3749  * @since 3.0.0
  4284  * @since 3.0.0
  3750  * @see __return_zero()
  4285  *
  3751  * @return array Empty array
  4286  * @return array Empty array.
  3752  */
  4287  */
  3753 function __return_empty_array() {
  4288 function __return_empty_array() {
  3754 	return array();
  4289 	return array();
  3755 }
  4290 }
  3756 
  4291 
  3758  * Returns null.
  4293  * Returns null.
  3759  *
  4294  *
  3760  * Useful for returning null to filters easily.
  4295  * Useful for returning null to filters easily.
  3761  *
  4296  *
  3762  * @since 3.4.0
  4297  * @since 3.4.0
  3763  * @return null
  4298  *
       
  4299  * @return null Null value.
  3764  */
  4300  */
  3765 function __return_null() {
  4301 function __return_null() {
  3766 	return null;
  4302 	return null;
  3767 }
  4303 }
  3768 
  4304 
  3770  * Returns an empty string.
  4306  * Returns an empty string.
  3771  *
  4307  *
  3772  * Useful for returning an empty string to filters easily.
  4308  * Useful for returning an empty string to filters easily.
  3773  *
  4309  *
  3774  * @since 3.7.0
  4310  * @since 3.7.0
       
  4311  *
  3775  * @see __return_null()
  4312  * @see __return_null()
  3776  * @return string Empty string
  4313  *
       
  4314  * @return string Empty string.
  3777  */
  4315  */
  3778 function __return_empty_string() {
  4316 function __return_empty_string() {
  3779 	return '';
  4317 	return '';
  3780 }
  4318 }
  3781 
  4319 
  3782 /**
  4320 /**
  3783  * Send a HTTP header to disable content type sniffing in browsers which support it.
  4321  * Send a HTTP header to disable content type sniffing in browsers which support it.
  3784  *
  4322  *
  3785  * @link http://blogs.msdn.com/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx
       
  3786  * @link http://src.chromium.org/viewvc/chrome?view=rev&revision=6985
       
  3787  *
       
  3788  * @since 3.0.0
  4323  * @since 3.0.0
  3789  * @return none
  4324  *
       
  4325  * @see http://blogs.msdn.com/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx
       
  4326  * @see http://src.chromium.org/viewvc/chrome?view=rev&revision=6985
  3790  */
  4327  */
  3791 function send_nosniff_header() {
  4328 function send_nosniff_header() {
  3792 	@header( 'X-Content-Type-Options: nosniff' );
  4329 	@header( 'X-Content-Type-Options: nosniff' );
  3793 }
  4330 }
  3794 
  4331 
  3795 /**
  4332 /**
  3796  * Returns a MySQL expression for selecting the week number based on the start_of_week option.
  4333  * Return a MySQL expression for selecting the week number based on the start_of_week option.
  3797  *
  4334  *
  3798  * @internal
  4335  * @ignore
  3799  * @since 3.0.0
  4336  * @since 3.0.0
  3800  * @param string $column
  4337  *
  3801  * @return string
  4338  * @param string $column Database column.
       
  4339  * @return string SQL clause.
  3802  */
  4340  */
  3803 function _wp_mysql_week( $column ) {
  4341 function _wp_mysql_week( $column ) {
  3804 	switch ( $start_of_week = (int) get_option( 'start_of_week' ) ) {
  4342 	switch ( $start_of_week = (int) get_option( 'start_of_week' ) ) {
  3805 	default :
       
  3806 	case 0 :
       
  3807 		return "WEEK( $column, 0 )";
       
  3808 	case 1 :
  4343 	case 1 :
  3809 		return "WEEK( $column, 1 )";
  4344 		return "WEEK( $column, 1 )";
  3810 	case 2 :
  4345 	case 2 :
  3811 	case 3 :
  4346 	case 3 :
  3812 	case 4 :
  4347 	case 4 :
  3813 	case 5 :
  4348 	case 5 :
  3814 	case 6 :
  4349 	case 6 :
  3815 		return "WEEK( DATE_SUB( $column, INTERVAL $start_of_week DAY ), 0 )";
  4350 		return "WEEK( DATE_SUB( $column, INTERVAL $start_of_week DAY ), 0 )";
  3816 	}
  4351 	case 0 :
  3817 }
  4352 	default :
  3818 
  4353 		return "WEEK( $column, 0 )";
  3819 /**
  4354 	}
  3820  * Finds hierarchy loops using a callback function that maps object IDs to parent IDs.
  4355 }
       
  4356 
       
  4357 /**
       
  4358  * Find hierarchy loops using a callback function that maps object IDs to parent IDs.
  3821  *
  4359  *
  3822  * @since 3.1.0
  4360  * @since 3.1.0
  3823  * @access private
  4361  * @access private
  3824  *
  4362  *
  3825  * @param callback $callback function that accepts ( ID, $callback_args ) and outputs parent_ID
  4363  * @param callback $callback      Function that accepts ( ID, $callback_args ) and outputs parent_ID.
  3826  * @param int $start The ID to start the loop check at
  4364  * @param int      $start         The ID to start the loop check at.
  3827  * @param int $start_parent the parent_ID of $start to use instead of calling $callback( $start ). Use null to always use $callback
  4365  * @param int      $start_parent  The parent_ID of $start to use instead of calling $callback( $start ).
  3828  * @param array $callback_args optional additional arguments to send to $callback
  4366  *                                Use null to always use $callback
  3829  * @return array IDs of all members of loop
  4367  * @param array    $callback_args Optional. Additional arguments to send to $callback.
       
  4368  * @return array IDs of all members of loop.
  3830  */
  4369  */
  3831 function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) {
  4370 function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) {
  3832 	$override = is_null( $start_parent ) ? array() : array( $start => $start_parent );
  4371 	$override = is_null( $start_parent ) ? array() : array( $start => $start_parent );
  3833 
  4372 
  3834 	if ( !$arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ) )
  4373 	if ( !$arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ) )
  3836 
  4375 
  3837 	return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true );
  4376 	return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true );
  3838 }
  4377 }
  3839 
  4378 
  3840 /**
  4379 /**
  3841  * Uses the "The Tortoise and the Hare" algorithm to detect loops.
  4380  * Use the "The Tortoise and the Hare" algorithm to detect loops.
  3842  *
  4381  *
  3843  * For every step of the algorithm, the hare takes two steps and the tortoise one.
  4382  * For every step of the algorithm, the hare takes two steps and the tortoise one.
  3844  * If the hare ever laps the tortoise, there must be a loop.
  4383  * If the hare ever laps the tortoise, there must be a loop.
  3845  *
  4384  *
  3846  * @since 3.1.0
  4385  * @since 3.1.0
  3847  * @access private
  4386  * @access private
  3848  *
  4387  *
  3849  * @param callback $callback function that accepts ( ID, callback_arg, ... ) and outputs parent_ID
  4388  * @param callback $callback      Function that accepts ( ID, callback_arg, ... ) and outputs parent_ID.
  3850  * @param int $start The ID to start the loop check at
  4389  * @param int      $start         The ID to start the loop check at.
  3851  * @param array $override an array of ( ID => parent_ID, ... ) to use instead of $callback
  4390  * @param array    $override      Optional. An array of ( ID => parent_ID, ... ) to use instead of $callback.
  3852  * @param array $callback_args optional additional arguments to send to $callback
  4391  *                                Default empty array.
  3853  * @param bool $_return_loop Return loop members or just detect presence of loop?
  4392  * @param array    $callback_args Optional. Additional arguments to send to $callback. Default empty array.
  3854  *             Only set to true if you already know the given $start is part of a loop
  4393  * @param bool     $_return_loop  Optional. Return loop members or just detect presence of loop? Only set
  3855  *             (otherwise the returned array might include branches)
  4394  *                                to true if you already know the given $start is part of a loop (otherwise
  3856  * @return mixed scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if $_return_loop
  4395  *                                the returned array might include branches). Default false.
       
  4396  * @return mixed Scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if
       
  4397  *               $_return_loop
  3857  */
  4398  */
  3858 function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
  4399 function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
  3859 	$tortoise = $hare = $evanescent_hare = $start;
  4400 	$tortoise = $hare = $evanescent_hare = $start;
  3860 	$return = array();
  4401 	$return = array();
  3861 
  4402 
  3883 }
  4424 }
  3884 
  4425 
  3885 /**
  4426 /**
  3886  * Send a HTTP header to limit rendering of pages to same origin iframes.
  4427  * Send a HTTP header to limit rendering of pages to same origin iframes.
  3887  *
  4428  *
  3888  * @link https://developer.mozilla.org/en/the_x-frame-options_response_header
       
  3889  *
       
  3890  * @since 3.1.3
  4429  * @since 3.1.3
  3891  * @return none
  4430  *
       
  4431  * @see https://developer.mozilla.org/en/the_x-frame-options_response_header
  3892  */
  4432  */
  3893 function send_frame_options_header() {
  4433 function send_frame_options_header() {
  3894 	@header( 'X-Frame-Options: SAMEORIGIN' );
  4434 	@header( 'X-Frame-Options: SAMEORIGIN' );
  3895 }
  4435 }
  3896 
  4436 
  3897 /**
  4437 /**
  3898  * Retrieve a list of protocols to allow in HTML attributes.
  4438  * Retrieve a list of protocols to allow in HTML attributes.
  3899  *
  4439  *
  3900  * @since 3.3.0
  4440  * @since 3.3.0
       
  4441  *
  3901  * @see wp_kses()
  4442  * @see wp_kses()
  3902  * @see esc_url()
  4443  * @see esc_url()
  3903  *
  4444  *
  3904  * @return array Array of allowed protocols
  4445  * @return array Array of allowed protocols.
  3905  */
  4446  */
  3906 function wp_allowed_protocols() {
  4447 function wp_allowed_protocols() {
  3907 	static $protocols;
  4448 	static $protocols;
  3908 
  4449 
  3909 	if ( empty( $protocols ) ) {
  4450 	if ( empty( $protocols ) ) {
  3910 		$protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp' );
  4451 		$protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp' );
       
  4452 
       
  4453 		/**
       
  4454 		 * Filter the list of protocols allowed in HTML attributes.
       
  4455 		 *
       
  4456 		 * @since 3.0.0
       
  4457 		 *
       
  4458 		 * @param array $protocols Array of allowed protocols e.g. 'http', 'ftp', 'tel', and more.
       
  4459 		 */
  3911 		$protocols = apply_filters( 'kses_allowed_protocols', $protocols );
  4460 		$protocols = apply_filters( 'kses_allowed_protocols', $protocols );
  3912 	}
  4461 	}
  3913 
  4462 
  3914 	return $protocols;
  4463 	return $protocols;
  3915 }
  4464 }
  3916 
  4465 
  3917 /**
  4466 /**
  3918  * Return a comma separated string of functions that have been called to get to the current point in code.
  4467  * Return a comma-separated string of functions that have been called to get
  3919  *
  4468  * to the current point in code.
  3920  * @link http://core.trac.wordpress.org/ticket/19589
  4469  *
  3921  * @since 3.4
  4470  * @since 3.4.0
  3922  *
  4471  *
  3923  * @param string $ignore_class A class to ignore all function calls within - useful when you want to just give info about the callee
  4472  * @see https://core.trac.wordpress.org/ticket/19589
  3924  * @param int $skip_frames A number of stack frames to skip - useful for unwinding back to the source of the issue
  4473  *
  3925  * @param bool $pretty Whether or not you want a comma separated string or raw array returned
  4474  * @param string $ignore_class Optional. A class to ignore all function calls within - useful
  3926  * @return string|array Either a string containing a reversed comma separated trace or an array of individual calls.
  4475  *                             when you want to just give info about the callee. Default null.
       
  4476  * @param int    $skip_frames  Optional. A number of stack frames to skip - useful for unwinding
       
  4477  *                             back to the source of the issue. Default 0.
       
  4478  * @param bool   $pretty       Optional. Whether or not you want a comma separated string or raw
       
  4479  *                             array returned. Default true.
       
  4480  * @return string|array Either a string containing a reversed comma separated trace or an array
       
  4481  *                      of individual calls.
  3927  */
  4482  */
  3928 function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pretty = true ) {
  4483 function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pretty = true ) {
  3929 	if ( version_compare( PHP_VERSION, '5.2.5', '>=' ) )
  4484 	if ( version_compare( PHP_VERSION, '5.2.5', '>=' ) )
  3930 		$trace = debug_backtrace( false );
  4485 		$trace = debug_backtrace( false );
  3931 	else
  4486 	else
  3958 	else
  4513 	else
  3959 		return $caller;
  4514 		return $caller;
  3960 }
  4515 }
  3961 
  4516 
  3962 /**
  4517 /**
  3963  * Retrieve ids that are not already present in the cache
  4518  * Retrieve ids that are not already present in the cache.
  3964  *
  4519  *
  3965  * @since 3.4.0
  4520  * @since 3.4.0
  3966  *
  4521  * @access private
  3967  * @param array $object_ids ID list
  4522  *
  3968  * @param string $cache_key The cache bucket to check against
  4523  * @param array  $object_ids ID list.
  3969  *
  4524  * @param string $cache_key  The cache bucket to check against.
  3970  * @return array
  4525  *
       
  4526  * @return array List of ids not present in the cache.
  3971  */
  4527  */
  3972 function _get_non_cached_ids( $object_ids, $cache_key ) {
  4528 function _get_non_cached_ids( $object_ids, $cache_key ) {
  3973 	$clean = array();
  4529 	$clean = array();
  3974 	foreach ( $object_ids as $id ) {
  4530 	foreach ( $object_ids as $id ) {
  3975 		$id = (int) $id;
  4531 		$id = (int) $id;
  3985  * Test if the current device has the capability to upload files.
  4541  * Test if the current device has the capability to upload files.
  3986  *
  4542  *
  3987  * @since 3.4.0
  4543  * @since 3.4.0
  3988  * @access private
  4544  * @access private
  3989  *
  4545  *
  3990  * @return bool true|false
  4546  * @return bool true|false Whether the device is able to upload files.
  3991  */
  4547  */
  3992 function _device_can_upload() {
  4548 function _device_can_upload() {
  3993 	if ( ! wp_is_mobile() )
  4549 	if ( ! wp_is_mobile() )
  3994 		return true;
  4550 		return true;
  3995 
  4551 
  4005 }
  4561 }
  4006 
  4562 
  4007 /**
  4563 /**
  4008  * Test if a given path is a stream URL
  4564  * Test if a given path is a stream URL
  4009  *
  4565  *
  4010  * @param string $path The resource path or URL
  4566  * @param string $path The resource path or URL.
  4011  * @return bool True if the path is a stream URL
  4567  * @return bool True if the path is a stream URL.
  4012  */
  4568  */
  4013 function wp_is_stream( $path ) {
  4569 function wp_is_stream( $path ) {
  4014 	$wrappers = stream_get_wrappers();
  4570 	$wrappers = stream_get_wrappers();
  4015 	$wrappers_re = '(' . join('|', $wrappers) . ')';
  4571 	$wrappers_re = '(' . join('|', $wrappers) . ')';
  4016 
  4572 
  4017 	return preg_match( "!^$wrappers_re://!", $path ) === 1;
  4573 	return preg_match( "!^$wrappers_re://!", $path ) === 1;
  4018 }
  4574 }
  4019 
  4575 
  4020 /**
  4576 /**
  4021  * Test if the supplied date is valid for the Gregorian calendar
  4577  * Test if the supplied date is valid for the Gregorian calendar.
  4022  *
  4578  *
  4023  * @since 3.5.0
  4579  * @since 3.5.0
  4024  *
  4580  *
  4025  * @return bool true|false
  4581  * @see checkdate()
       
  4582  *
       
  4583  * @param  int    $month       Month number.
       
  4584  * @param  int    $day         Day number.
       
  4585  * @param  int    $year        Year number.
       
  4586  * @param  string $source_date The date to filter.
       
  4587  * @return bool True if valid date, false if not valid date.
  4026  */
  4588  */
  4027 function wp_checkdate( $month, $day, $year, $source_date ) {
  4589 function wp_checkdate( $month, $day, $year, $source_date ) {
       
  4590 	/**
       
  4591 	 * Filter whether the given date is valid for the Gregorian calendar.
       
  4592 	 *
       
  4593 	 * @since 3.5.0
       
  4594 	 *
       
  4595 	 * @param bool   $checkdate   Whether the given date is valid.
       
  4596 	 * @param string $source_date Date to check.
       
  4597 	 */
  4028 	return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date );
  4598 	return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date );
  4029 }
  4599 }
  4030 
  4600 
  4031 /**
  4601 /**
  4032  * Load the auth check for monitoring whether the user is still logged in.
  4602  * Load the auth check for monitoring whether the user is still logged in.
  4048 
  4618 
  4049 	$screen = get_current_screen();
  4619 	$screen = get_current_screen();
  4050 	$hidden = array( 'update', 'update-network', 'update-core', 'update-core-network', 'upgrade', 'upgrade-network', 'network' );
  4620 	$hidden = array( 'update', 'update-network', 'update-core', 'update-core-network', 'upgrade', 'upgrade-network', 'network' );
  4051 	$show = ! in_array( $screen->id, $hidden );
  4621 	$show = ! in_array( $screen->id, $hidden );
  4052 
  4622 
       
  4623 	/**
       
  4624 	 * Filter whether to load the authentication check.
       
  4625 	 *
       
  4626 	 * Passing a falsey value to the filter will effectively short-circuit
       
  4627 	 * loading the authentication check.
       
  4628 	 *
       
  4629 	 * @since 3.6.0
       
  4630 	 *
       
  4631 	 * @param bool      $show   Whether to load the authentication check.
       
  4632 	 * @param WP_Screen $screen The current screen object.
       
  4633 	 */
  4053 	if ( apply_filters( 'wp_auth_check_load', $show, $screen ) ) {
  4634 	if ( apply_filters( 'wp_auth_check_load', $show, $screen ) ) {
  4054 		wp_enqueue_style( 'wp-auth-check' );
  4635 		wp_enqueue_style( 'wp-auth-check' );
  4055 		wp_enqueue_script( 'wp-auth-check' );
  4636 		wp_enqueue_script( 'wp-auth-check' );
  4056 
  4637 
  4057 		add_action( 'admin_print_footer_scripts', 'wp_auth_check_html', 5 );
  4638 		add_action( 'admin_print_footer_scripts', 'wp_auth_check_html', 5 );
  4067 function wp_auth_check_html() {
  4648 function wp_auth_check_html() {
  4068 	$login_url = wp_login_url();
  4649 	$login_url = wp_login_url();
  4069 	$current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
  4650 	$current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
  4070 	$same_domain = ( strpos( $login_url, $current_domain ) === 0 );
  4651 	$same_domain = ( strpos( $login_url, $current_domain ) === 0 );
  4071 
  4652 
  4072 	if ( $same_domain && force_ssl_login() && ! force_ssl_admin() )
  4653 	/**
  4073 		$same_domain = false;
  4654 	 * Filter whether the authentication check originated at the same domain.
  4074 
  4655 	 *
  4075 	// Let plugins change this if they know better.
  4656 	 * @since 3.6.0
       
  4657 	 *
       
  4658 	 * @param bool $same_domain Whether the authentication check originated at the same domain.
       
  4659 	 */
  4076 	$same_domain = apply_filters( 'wp_auth_check_same_domain', $same_domain );
  4660 	$same_domain = apply_filters( 'wp_auth_check_same_domain', $same_domain );
  4077 	$wrap_class = $same_domain ? 'hidden' : 'hidden fallback';
  4661 	$wrap_class = $same_domain ? 'hidden' : 'hidden fallback';
  4078 
  4662 
  4079 	?>
  4663 	?>
  4080 	<div id="wp-auth-check-wrap" class="<?php echo $wrap_class; ?>">
  4664 	<div id="wp-auth-check-wrap" class="<?php echo $wrap_class; ?>">
  4105  *
  4689  *
  4106  * Send a result that shows a log-in box if the user is no longer logged in,
  4690  * Send a result that shows a log-in box if the user is no longer logged in,
  4107  * or if their cookie is within the grace period.
  4691  * or if their cookie is within the grace period.
  4108  *
  4692  *
  4109  * @since 3.6.0
  4693  * @since 3.6.0
  4110  */
  4694  *
  4111 function wp_auth_check( $response, $data ) {
  4695  * @param array|object $response  The Heartbeat response object or array.
       
  4696  * @return array|object $response The Heartbeat response object or array with 'wp-auth-check'
       
  4697  *                                value set.
       
  4698  */
       
  4699 function wp_auth_check( $response ) {
  4112 	$response['wp-auth-check'] = is_user_logged_in() && empty( $GLOBALS['login_grace_period'] );
  4700 	$response['wp-auth-check'] = is_user_logged_in() && empty( $GLOBALS['login_grace_period'] );
  4113 	return $response;
  4701 	return $response;
  4114 }
  4702 }
  4115 
  4703 
  4116 /**
  4704 /**
  4117  * Return RegEx body to liberally match an opening HTML tag that:
  4705  * Return RegEx body to liberally match an opening HTML tag.
       
  4706  *
       
  4707  * Matches an opening HTML tag that:
  4118  * 1. Is self-closing or
  4708  * 1. Is self-closing or
  4119  * 2. Has no body but has a closing tag of the same name or
  4709  * 2. Has no body but has a closing tag of the same name or
  4120  * 3. Contains a body and a closing tag of the same name
  4710  * 3. Contains a body and a closing tag of the same name
  4121  *
  4711  *
  4122  * Note: this RegEx does not balance inner tags and does not attempt to produce valid HTML
  4712  * Note: this RegEx does not balance inner tags and does not attempt
       
  4713  * to produce valid HTML
  4123  *
  4714  *
  4124  * @since 3.6.0
  4715  * @since 3.6.0
  4125  *
  4716  *
  4126  * @param string $tag An HTML tag name. Example: 'video'
  4717  * @param string $tag An HTML tag name. Example: 'video'.
  4127  * @return string
  4718  * @return string Tag RegEx.
  4128  */
  4719  */
  4129 function get_tag_regex( $tag ) {
  4720 function get_tag_regex( $tag ) {
  4130 	if ( empty( $tag ) )
  4721 	if ( empty( $tag ) )
  4131 		return;
  4722 		return;
  4132 	return sprintf( '<%1$s[^<]*(?:>[\s\S]*<\/%1$s>|\s*\/>)', tag_escape( $tag ) );
  4723 	return sprintf( '<%1$s[^<]*(?:>[\s\S]*<\/%1$s>|\s*\/>)', tag_escape( $tag ) );
  4133 }
  4724 }
  4134 
  4725 
  4135 /**
  4726 /**
  4136  * Return a canonical form of the provided charset appropriate for passing to PHP
  4727  * Retrieve a canonical form of the provided charset appropriate for passing to PHP
  4137  * functions such as htmlspecialchars() and charset html attributes.
  4728  * functions such as htmlspecialchars() and charset html attributes.
  4138  *
  4729  *
  4139  * @link http://core.trac.wordpress.org/ticket/23688
       
  4140  * @since 3.6.0
  4730  * @since 3.6.0
  4141  *
  4731  * @access private
  4142  * @param string A charset name
  4732  *
  4143  * @return string The canonical form of the charset
  4733  * @see https://core.trac.wordpress.org/ticket/23688
       
  4734  *
       
  4735  * @param string $charset A charset name.
       
  4736  * @return string The canonical form of the charset.
  4144  */
  4737  */
  4145 function _canonical_charset( $charset ) {
  4738 function _canonical_charset( $charset ) {
  4146 	if ( 'UTF-8' === $charset || 'utf-8' === $charset || 'utf8' === $charset ||
  4739 	if ( 'UTF-8' === $charset || 'utf-8' === $charset || 'utf8' === $charset ||
  4147 		'UTF8' === $charset )
  4740 		'UTF8' === $charset )
  4148 		return 'UTF-8';
  4741 		return 'UTF-8';
  4153 
  4746 
  4154 	return $charset;
  4747 	return $charset;
  4155 }
  4748 }
  4156 
  4749 
  4157 /**
  4750 /**
  4158  * Sets the mbstring internal encoding to a binary safe encoding whne func_overload is enabled.
  4751  * Set the mbstring internal encoding to a binary safe encoding when func_overload
  4159  *
  4752  * is enabled.
  4160  * When mbstring.func_overload is in use for multi-byte encodings, the results from strlen() and
  4753  *
  4161  * similar functions respect the utf8 characters, causing binary data to return incorrect lengths.
  4754  * When mbstring.func_overload is in use for multi-byte encodings, the results from
  4162  *
  4755  * strlen() and similar functions respect the utf8 characters, causing binary data
  4163  * This function overrides the mbstring encoding to a binary-safe encoding, and resets it to the
  4756  * to return incorrect lengths.
  4164  * users expected encoding afterwards through the `reset_mbstring_encoding` function.
  4757  *
  4165  *
  4758  * This function overrides the mbstring encoding to a binary-safe encoding, and
  4166  * It is safe to recursively call this function, however each `mbstring_binary_safe_encoding()`
  4759  * resets it to the users expected encoding afterwards through the
  4167  * call must be followed up with an equal number of `reset_mbstring_encoding()` calls.
  4760  * `reset_mbstring_encoding` function.
       
  4761  *
       
  4762  * It is safe to recursively call this function, however each
       
  4763  * `mbstring_binary_safe_encoding()` call must be followed up with an equal number
       
  4764  * of `reset_mbstring_encoding()` calls.
       
  4765  *
       
  4766  * @since 3.7.0
  4168  *
  4767  *
  4169  * @see reset_mbstring_encoding()
  4768  * @see reset_mbstring_encoding()
  4170  *
  4769  *
  4171  * @since 3.7.0
  4770  * @param bool $reset Optional. Whether to reset the encoding back to a previously-set encoding.
  4172  *
  4771  *                    Default false.
  4173  * @param bool $reset Whether to reset the encoding back to a previously-set encoding.
       
  4174  */
  4772  */
  4175 function mbstring_binary_safe_encoding( $reset = false ) {
  4773 function mbstring_binary_safe_encoding( $reset = false ) {
  4176 	static $encodings = array();
  4774 	static $encodings = array();
  4177 	static $overloaded = null;
  4775 	static $overloaded = null;
  4178 
  4776 
  4193 		mb_internal_encoding( $encoding );
  4791 		mb_internal_encoding( $encoding );
  4194 	}
  4792 	}
  4195 }
  4793 }
  4196 
  4794 
  4197 /**
  4795 /**
  4198  * Resets the mbstring internal encoding to a users previously set encoding.
  4796  * Reset the mbstring internal encoding to a users previously set encoding.
  4199  *
  4797  *
  4200  * @see mbstring_binary_safe_encoding()
  4798  * @see mbstring_binary_safe_encoding()
  4201  *
  4799  *
  4202  * @since 3.7.0
  4800  * @since 3.7.0
  4203  */
  4801  */
  4204 function reset_mbstring_encoding() {
  4802 function reset_mbstring_encoding() {
  4205 	mbstring_binary_safe_encoding( true );
  4803 	mbstring_binary_safe_encoding( true );
  4206 }
  4804 }
       
  4805 
       
  4806 /**
       
  4807  * Filter/validate a variable as a boolean.
       
  4808  *
       
  4809  * Alternative to `filter_var( $var, FILTER_VALIDATE_BOOLEAN )`.
       
  4810  *
       
  4811  * @since 4.0.0
       
  4812  *
       
  4813  * @param mixed $var Boolean value to validate.
       
  4814  * @return bool Whether the value is validated.
       
  4815  */
       
  4816 function wp_validate_boolean( $var ) {
       
  4817 	if ( is_bool( $var ) ) {
       
  4818 		return $var;
       
  4819 	}
       
  4820 
       
  4821 	if ( is_string( $var ) && 'false' === strtolower( $var ) ) {
       
  4822 		return false;
       
  4823 	}
       
  4824 
       
  4825 	return (bool) $var;
       
  4826 }
       
  4827 
       
  4828 /**
       
  4829  * Delete a file
       
  4830  *
       
  4831  * @since 4.2.0
       
  4832  *
       
  4833  * @param string $file The path to the file to delete.
       
  4834  */
       
  4835 function wp_delete_file( $file ) {
       
  4836 	/**
       
  4837 	 * Filter the path of the file to delete.
       
  4838 	 *
       
  4839 	 * @since 2.1.0
       
  4840 	 *
       
  4841 	 * @param string $medium Path to the file to delete.
       
  4842 	 */
       
  4843 	$delete = apply_filters( 'wp_delete_file', $file );
       
  4844 	if ( ! empty( $delete ) ) {
       
  4845 		@unlink( $delete );
       
  4846 	}
       
  4847 }