Edit File by line
/home/barbar84/www/wp-inclu...
File: functions.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Main WordPress API
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
require ABSPATH . WPINC . '/option.php';
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Convert given MySQL date string into a different format.
[10] Fix | Delete
*
[11] Fix | Delete
* `$format` should be a PHP date format string.
[12] Fix | Delete
* 'U' and 'G' formats will return a sum of timestamp with timezone offset.
[13] Fix | Delete
* `$date` is expected to be local time in MySQL format (`Y-m-d H:i:s`).
[14] Fix | Delete
*
[15] Fix | Delete
* Historically UTC time could be passed to the function to produce Unix timestamp.
[16] Fix | Delete
*
[17] Fix | Delete
* If `$translate` is true then the given date and format string will
[18] Fix | Delete
* be passed to `wp_date()` for translation.
[19] Fix | Delete
*
[20] Fix | Delete
* @since 0.71
[21] Fix | Delete
*
[22] Fix | Delete
* @param string $format Format of the date to return.
[23] Fix | Delete
* @param string $date Date string to convert.
[24] Fix | Delete
* @param bool $translate Whether the return date should be translated. Default true.
[25] Fix | Delete
* @return string|int|false Formatted date string or sum of Unix timestamp and timezone offset.
[26] Fix | Delete
* False on failure.
[27] Fix | Delete
*/
[28] Fix | Delete
function mysql2date( $format, $date, $translate = true ) {
[29] Fix | Delete
if ( empty( $date ) ) {
[30] Fix | Delete
return false;
[31] Fix | Delete
}
[32] Fix | Delete
[33] Fix | Delete
$datetime = date_create( $date, wp_timezone() );
[34] Fix | Delete
[35] Fix | Delete
if ( false === $datetime ) {
[36] Fix | Delete
return false;
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
// Returns a sum of timestamp with timezone offset. Ideally should never be used.
[40] Fix | Delete
if ( 'G' === $format || 'U' === $format ) {
[41] Fix | Delete
return $datetime->getTimestamp() + $datetime->getOffset();
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
if ( $translate ) {
[45] Fix | Delete
return wp_date( $format, $datetime->getTimestamp() );
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
return $datetime->format( $format );
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* Retrieves the current time based on specified type.
[53] Fix | Delete
*
[54] Fix | Delete
* The 'mysql' type will return the time in the format for MySQL DATETIME field.
[55] Fix | Delete
* The 'timestamp' type will return the current timestamp or a sum of timestamp
[56] Fix | Delete
* and timezone offset, depending on `$gmt`.
[57] Fix | Delete
* Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d').
[58] Fix | Delete
*
[59] Fix | Delete
* If $gmt is set to either '1' or 'true', then both types will use GMT time.
[60] Fix | Delete
* if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
[61] Fix | Delete
*
[62] Fix | Delete
* @since 1.0.0
[63] Fix | Delete
*
[64] Fix | Delete
* @param string $type Type of time to retrieve. Accepts 'mysql', 'timestamp',
[65] Fix | Delete
* or PHP date format string (e.g. 'Y-m-d').
[66] Fix | Delete
* @param int|bool $gmt Optional. Whether to use GMT timezone. Default false.
[67] Fix | Delete
* @return int|string Integer if $type is 'timestamp', string otherwise.
[68] Fix | Delete
*/
[69] Fix | Delete
function current_time( $type, $gmt = 0 ) {
[70] Fix | Delete
// Don't use non-GMT timestamp, unless you know the difference and really need to.
[71] Fix | Delete
if ( 'timestamp' === $type || 'U' === $type ) {
[72] Fix | Delete
return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
if ( 'mysql' === $type ) {
[76] Fix | Delete
$type = 'Y-m-d H:i:s';
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
$timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone();
[80] Fix | Delete
$datetime = new DateTime( 'now', $timezone );
[81] Fix | Delete
[82] Fix | Delete
return $datetime->format( $type );
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* Retrieves the current time as an object with the timezone from settings.
[87] Fix | Delete
*
[88] Fix | Delete
* @since 5.3.0
[89] Fix | Delete
*
[90] Fix | Delete
* @return DateTimeImmutable Date and time object.
[91] Fix | Delete
*/
[92] Fix | Delete
function current_datetime() {
[93] Fix | Delete
return new DateTimeImmutable( 'now', wp_timezone() );
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
/**
[97] Fix | Delete
* Retrieves the timezone from site settings as a string.
[98] Fix | Delete
*
[99] Fix | Delete
* Uses the `timezone_string` option to get a proper timezone if available,
[100] Fix | Delete
* otherwise falls back to an offset.
[101] Fix | Delete
*
[102] Fix | Delete
* @since 5.3.0
[103] Fix | Delete
*
[104] Fix | Delete
* @return string PHP timezone string or a ±HH:MM offset.
[105] Fix | Delete
*/
[106] Fix | Delete
function wp_timezone_string() {
[107] Fix | Delete
$timezone_string = get_option( 'timezone_string' );
[108] Fix | Delete
[109] Fix | Delete
if ( $timezone_string ) {
[110] Fix | Delete
return $timezone_string;
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
$offset = (float) get_option( 'gmt_offset' );
[114] Fix | Delete
$hours = (int) $offset;
[115] Fix | Delete
$minutes = ( $offset - $hours );
[116] Fix | Delete
[117] Fix | Delete
$sign = ( $offset < 0 ) ? '-' : '+';
[118] Fix | Delete
$abs_hour = abs( $hours );
[119] Fix | Delete
$abs_mins = abs( $minutes * 60 );
[120] Fix | Delete
$tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
[121] Fix | Delete
[122] Fix | Delete
return $tz_offset;
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
/**
[126] Fix | Delete
* Retrieves the timezone from site settings as a `DateTimeZone` object.
[127] Fix | Delete
*
[128] Fix | Delete
* Timezone can be based on a PHP timezone string or a ±HH:MM offset.
[129] Fix | Delete
*
[130] Fix | Delete
* @since 5.3.0
[131] Fix | Delete
*
[132] Fix | Delete
* @return DateTimeZone Timezone object.
[133] Fix | Delete
*/
[134] Fix | Delete
function wp_timezone() {
[135] Fix | Delete
return new DateTimeZone( wp_timezone_string() );
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
/**
[139] Fix | Delete
* Retrieves the date in localized format, based on a sum of Unix timestamp and
[140] Fix | Delete
* timezone offset in seconds.
[141] Fix | Delete
*
[142] Fix | Delete
* If the locale specifies the locale month and weekday, then the locale will
[143] Fix | Delete
* take over the format for the date. If it isn't, then the date format string
[144] Fix | Delete
* will be used instead.
[145] Fix | Delete
*
[146] Fix | Delete
* Note that due to the way WP typically generates a sum of timestamp and offset
[147] Fix | Delete
* with `strtotime()`, it implies offset added at a _current_ time, not at the time
[148] Fix | Delete
* the timestamp represents. Storing such timestamps or calculating them differently
[149] Fix | Delete
* will lead to invalid output.
[150] Fix | Delete
*
[151] Fix | Delete
* @since 0.71
[152] Fix | Delete
* @since 5.3.0 Converted into a wrapper for wp_date().
[153] Fix | Delete
*
[154] Fix | Delete
* @global WP_Locale $wp_locale WordPress date and time locale object.
[155] Fix | Delete
*
[156] Fix | Delete
* @param string $format Format to display the date.
[157] Fix | Delete
* @param int|bool $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset
[158] Fix | Delete
* in seconds. Default false.
[159] Fix | Delete
* @param bool $gmt Optional. Whether to use GMT timezone. Only applies
[160] Fix | Delete
* if timestamp is not provided. Default false.
[161] Fix | Delete
* @return string The date, translated if locale specifies it.
[162] Fix | Delete
*/
[163] Fix | Delete
function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) {
[164] Fix | Delete
$timestamp = $timestamp_with_offset;
[165] Fix | Delete
[166] Fix | Delete
// If timestamp is omitted it should be current time (summed with offset, unless `$gmt` is true).
[167] Fix | Delete
if ( ! is_numeric( $timestamp ) ) {
[168] Fix | Delete
$timestamp = current_time( 'timestamp', $gmt );
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
/*
[172] Fix | Delete
* This is a legacy implementation quirk that the returned timestamp is also with offset.
[173] Fix | Delete
* Ideally this function should never be used to produce a timestamp.
[174] Fix | Delete
*/
[175] Fix | Delete
if ( 'U' === $format ) {
[176] Fix | Delete
$date = $timestamp;
[177] Fix | Delete
} elseif ( $gmt && false === $timestamp_with_offset ) { // Current time in UTC.
[178] Fix | Delete
$date = wp_date( $format, null, new DateTimeZone( 'UTC' ) );
[179] Fix | Delete
} elseif ( false === $timestamp_with_offset ) { // Current time in site's timezone.
[180] Fix | Delete
$date = wp_date( $format );
[181] Fix | Delete
} else {
[182] Fix | Delete
/*
[183] Fix | Delete
* Timestamp with offset is typically produced by a UTC `strtotime()` call on an input without timezone.
[184] Fix | Delete
* This is the best attempt to reverse that operation into a local time to use.
[185] Fix | Delete
*/
[186] Fix | Delete
$local_time = gmdate( 'Y-m-d H:i:s', $timestamp );
[187] Fix | Delete
$timezone = wp_timezone();
[188] Fix | Delete
$datetime = date_create( $local_time, $timezone );
[189] Fix | Delete
$date = wp_date( $format, $datetime->getTimestamp(), $timezone );
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
/**
[193] Fix | Delete
* Filters the date formatted based on the locale.
[194] Fix | Delete
*
[195] Fix | Delete
* @since 2.8.0
[196] Fix | Delete
*
[197] Fix | Delete
* @param string $date Formatted date string.
[198] Fix | Delete
* @param string $format Format to display the date.
[199] Fix | Delete
* @param int $timestamp A sum of Unix timestamp and timezone offset in seconds.
[200] Fix | Delete
* Might be without offset if input omitted timestamp but requested GMT.
[201] Fix | Delete
* @param bool $gmt Whether to use GMT timezone. Only applies if timestamp was not provided.
[202] Fix | Delete
* Default false.
[203] Fix | Delete
*/
[204] Fix | Delete
$date = apply_filters( 'date_i18n', $date, $format, $timestamp, $gmt );
[205] Fix | Delete
[206] Fix | Delete
return $date;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
/**
[210] Fix | Delete
* Retrieves the date, in localized format.
[211] Fix | Delete
*
[212] Fix | Delete
* This is a newer function, intended to replace `date_i18n()` without legacy quirks in it.
[213] Fix | Delete
*
[214] Fix | Delete
* Note that, unlike `date_i18n()`, this function accepts a true Unix timestamp, not summed
[215] Fix | Delete
* with timezone offset.
[216] Fix | Delete
*
[217] Fix | Delete
* @since 5.3.0
[218] Fix | Delete
*
[219] Fix | Delete
* @param string $format PHP date format.
[220] Fix | Delete
* @param int $timestamp Optional. Unix timestamp. Defaults to current time.
[221] Fix | Delete
* @param DateTimeZone $timezone Optional. Timezone to output result in. Defaults to timezone
[222] Fix | Delete
* from site settings.
[223] Fix | Delete
* @return string|false The date, translated if locale specifies it. False on invalid timestamp input.
[224] Fix | Delete
*/
[225] Fix | Delete
function wp_date( $format, $timestamp = null, $timezone = null ) {
[226] Fix | Delete
global $wp_locale;
[227] Fix | Delete
[228] Fix | Delete
if ( null === $timestamp ) {
[229] Fix | Delete
$timestamp = time();
[230] Fix | Delete
} elseif ( ! is_numeric( $timestamp ) ) {
[231] Fix | Delete
return false;
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
if ( ! $timezone ) {
[235] Fix | Delete
$timezone = wp_timezone();
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
$datetime = date_create( '@' . $timestamp );
[239] Fix | Delete
$datetime->setTimezone( $timezone );
[240] Fix | Delete
[241] Fix | Delete
if ( empty( $wp_locale->month ) || empty( $wp_locale->weekday ) ) {
[242] Fix | Delete
$date = $datetime->format( $format );
[243] Fix | Delete
} else {
[244] Fix | Delete
// We need to unpack shorthand `r` format because it has parts that might be localized.
[245] Fix | Delete
$format = preg_replace( '/(?<!\\\\)r/', DATE_RFC2822, $format );
[246] Fix | Delete
[247] Fix | Delete
$new_format = '';
[248] Fix | Delete
$format_length = strlen( $format );
[249] Fix | Delete
$month = $wp_locale->get_month( $datetime->format( 'm' ) );
[250] Fix | Delete
$weekday = $wp_locale->get_weekday( $datetime->format( 'w' ) );
[251] Fix | Delete
[252] Fix | Delete
for ( $i = 0; $i < $format_length; $i ++ ) {
[253] Fix | Delete
switch ( $format[ $i ] ) {
[254] Fix | Delete
case 'D':
[255] Fix | Delete
$new_format .= addcslashes( $wp_locale->get_weekday_abbrev( $weekday ), '\\A..Za..z' );
[256] Fix | Delete
break;
[257] Fix | Delete
case 'F':
[258] Fix | Delete
$new_format .= addcslashes( $month, '\\A..Za..z' );
[259] Fix | Delete
break;
[260] Fix | Delete
case 'l':
[261] Fix | Delete
$new_format .= addcslashes( $weekday, '\\A..Za..z' );
[262] Fix | Delete
break;
[263] Fix | Delete
case 'M':
[264] Fix | Delete
$new_format .= addcslashes( $wp_locale->get_month_abbrev( $month ), '\\A..Za..z' );
[265] Fix | Delete
break;
[266] Fix | Delete
case 'a':
[267] Fix | Delete
$new_format .= addcslashes( $wp_locale->get_meridiem( $datetime->format( 'a' ) ), '\\A..Za..z' );
[268] Fix | Delete
break;
[269] Fix | Delete
case 'A':
[270] Fix | Delete
$new_format .= addcslashes( $wp_locale->get_meridiem( $datetime->format( 'A' ) ), '\\A..Za..z' );
[271] Fix | Delete
break;
[272] Fix | Delete
case '\\':
[273] Fix | Delete
$new_format .= $format[ $i ];
[274] Fix | Delete
[275] Fix | Delete
// If character follows a slash, we add it without translating.
[276] Fix | Delete
if ( $i < $format_length ) {
[277] Fix | Delete
$new_format .= $format[ ++$i ];
[278] Fix | Delete
}
[279] Fix | Delete
break;
[280] Fix | Delete
default:
[281] Fix | Delete
$new_format .= $format[ $i ];
[282] Fix | Delete
break;
[283] Fix | Delete
}
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
$date = $datetime->format( $new_format );
[287] Fix | Delete
$date = wp_maybe_decline_date( $date, $format );
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
/**
[291] Fix | Delete
* Filters the date formatted based on the locale.
[292] Fix | Delete
*
[293] Fix | Delete
* @since 5.3.0
[294] Fix | Delete
*
[295] Fix | Delete
* @param string $date Formatted date string.
[296] Fix | Delete
* @param string $format Format to display the date.
[297] Fix | Delete
* @param int $timestamp Unix timestamp.
[298] Fix | Delete
* @param DateTimeZone $timezone Timezone.
[299] Fix | Delete
*/
[300] Fix | Delete
$date = apply_filters( 'wp_date', $date, $format, $timestamp, $timezone );
[301] Fix | Delete
[302] Fix | Delete
return $date;
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
/**
[306] Fix | Delete
* Determines if the date should be declined.
[307] Fix | Delete
*
[308] Fix | Delete
* If the locale specifies that month names require a genitive case in certain
[309] Fix | Delete
* formats (like 'j F Y'), the month name will be replaced with a correct form.
[310] Fix | Delete
*
[311] Fix | Delete
* @since 4.4.0
[312] Fix | Delete
* @since 5.4.0 The `$format` parameter was added.
[313] Fix | Delete
*
[314] Fix | Delete
* @global WP_Locale $wp_locale WordPress date and time locale object.
[315] Fix | Delete
*
[316] Fix | Delete
* @param string $date Formatted date string.
[317] Fix | Delete
* @param string $format Optional. Date format to check. Default empty string.
[318] Fix | Delete
* @return string The date, declined if locale specifies it.
[319] Fix | Delete
*/
[320] Fix | Delete
function wp_maybe_decline_date( $date, $format = '' ) {
[321] Fix | Delete
global $wp_locale;
[322] Fix | Delete
[323] Fix | Delete
// i18n functions are not available in SHORTINIT mode.
[324] Fix | Delete
if ( ! function_exists( '_x' ) ) {
[325] Fix | Delete
return $date;
[326] Fix | Delete
}
[327] Fix | Delete
[328] Fix | Delete
/*
[329] Fix | Delete
* translators: If months in your language require a genitive case,
[330] Fix | Delete
* translate this to 'on'. Do not translate into your own language.
[331] Fix | Delete
*/
[332] Fix | Delete
if ( 'on' === _x( 'off', 'decline months names: on or off' ) ) {
[333] Fix | Delete
[334] Fix | Delete
$months = $wp_locale->month;
[335] Fix | Delete
$months_genitive = $wp_locale->month_genitive;
[336] Fix | Delete
[337] Fix | Delete
/*
[338] Fix | Delete
* Match a format like 'j F Y' or 'j. F' (day of the month, followed by month name)
[339] Fix | Delete
* and decline the month.
[340] Fix | Delete
*/
[341] Fix | Delete
if ( $format ) {
[342] Fix | Delete
$decline = preg_match( '#[dj]\.? F#', $format );
[343] Fix | Delete
} else {
[344] Fix | Delete
// If the format is not passed, try to guess it from the date string.
[345] Fix | Delete
$decline = preg_match( '#\b\d{1,2}\.? [^\d ]+\b#u', $date );
[346] Fix | Delete
}
[347] Fix | Delete
[348] Fix | Delete
if ( $decline ) {
[349] Fix | Delete
foreach ( $months as $key => $month ) {
[350] Fix | Delete
$months[ $key ] = '# ' . preg_quote( $month, '#' ) . '\b#u';
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
foreach ( $months_genitive as $key => $month ) {
[354] Fix | Delete
$months_genitive[ $key ] = ' ' . $month;
[355] Fix | Delete
}
[356] Fix | Delete
[357] Fix | Delete
$date = preg_replace( $months, $months_genitive, $date );
[358] Fix | Delete
}
[359] Fix | Delete
[360] Fix | Delete
/*
[361] Fix | Delete
* Match a format like 'F jS' or 'F j' (month name, followed by day with an optional ordinal suffix)
[362] Fix | Delete
* and change it to declined 'j F'.
[363] Fix | Delete
*/
[364] Fix | Delete
if ( $format ) {
[365] Fix | Delete
$decline = preg_match( '#F [dj]#', $format );
[366] Fix | Delete
} else {
[367] Fix | Delete
// If the format is not passed, try to guess it from the date string.
[368] Fix | Delete
$decline = preg_match( '#\b[^\d ]+ \d{1,2}(st|nd|rd|th)?\b#u', trim( $date ) );
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
if ( $decline ) {
[372] Fix | Delete
foreach ( $months as $key => $month ) {
[373] Fix | Delete
$months[ $key ] = '#\b' . preg_quote( $month, '#' ) . ' (\d{1,2})(st|nd|rd|th)?([-–]\d{1,2})?(st|nd|rd|th)?\b#u';
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
foreach ( $months_genitive as $key => $month ) {
[377] Fix | Delete
$months_genitive[ $key ] = '$1$3 ' . $month;
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
$date = preg_replace( $months, $months_genitive, $date );
[381] Fix | Delete
}
[382] Fix | Delete
}
[383] Fix | Delete
[384] Fix | Delete
// Used for locale-specific rules.
[385] Fix | Delete
$locale = get_locale();
[386] Fix | Delete
[387] Fix | Delete
if ( 'ca' === $locale ) {
[388] Fix | Delete
// " de abril| de agost| de octubre..." -> " d'abril| d'agost| d'octubre..."
[389] Fix | Delete
$date = preg_replace( '# de ([ao])#i', " d'\\1", $date );
[390] Fix | Delete
}
[391] Fix | Delete
[392] Fix | Delete
return $date;
[393] Fix | Delete
}
[394] Fix | Delete
[395] Fix | Delete
/**
[396] Fix | Delete
* Convert float number to format based on the locale.
[397] Fix | Delete
*
[398] Fix | Delete
* @since 2.3.0
[399] Fix | Delete
*
[400] Fix | Delete
* @global WP_Locale $wp_locale WordPress date and time locale object.
[401] Fix | Delete
*
[402] Fix | Delete
* @param float $number The number to convert based on locale.
[403] Fix | Delete
* @param int $decimals Optional. Precision of the number of decimal places. Default 0.
[404] Fix | Delete
* @return string Converted number in string format.
[405] Fix | Delete
*/
[406] Fix | Delete
function number_format_i18n( $number, $decimals = 0 ) {
[407] Fix | Delete
global $wp_locale;
[408] Fix | Delete
[409] Fix | Delete
if ( isset( $wp_locale ) ) {
[410] Fix | Delete
$formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
[411] Fix | Delete
} else {
[412] Fix | Delete
$formatted = number_format( $number, absint( $decimals ) );
[413] Fix | Delete
}
[414] Fix | Delete
[415] Fix | Delete
/**
[416] Fix | Delete
* Filters the number formatted based on the locale.
[417] Fix | Delete
*
[418] Fix | Delete
* @since 2.8.0
[419] Fix | Delete
* @since 4.9.0 The `$number` and `$decimals` parameters were added.
[420] Fix | Delete
*
[421] Fix | Delete
* @param string $formatted Converted number in string format.
[422] Fix | Delete
* @param float $number The number to convert based on locale.
[423] Fix | Delete
* @param int $decimals Precision of the number of decimal places.
[424] Fix | Delete
*/
[425] Fix | Delete
return apply_filters( 'number_format_i18n', $formatted, $number, $decimals );
[426] Fix | Delete
}
[427] Fix | Delete
[428] Fix | Delete
/**
[429] Fix | Delete
* Convert number of bytes largest unit bytes will fit into.
[430] Fix | Delete
*
[431] Fix | Delete
* It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts
[432] Fix | Delete
* number of bytes to human readable number by taking the number of that unit
[433] Fix | Delete
* that the bytes will go into it. Supports TB value.
[434] Fix | Delete
*
[435] Fix | Delete
* Please note that integers in PHP are limited to 32 bits, unless they are on
[436] Fix | Delete
* 64 bit architecture, then they have 64 bit size. If you need to place the
[437] Fix | Delete
* larger size then what PHP integer type will hold, then use a string. It will
[438] Fix | Delete
* be converted to a double, which should always have 64 bit length.
[439] Fix | Delete
*
[440] Fix | Delete
* Technically the correct unit names for powers of 1024 are KiB, MiB etc.
[441] Fix | Delete
*
[442] Fix | Delete
* @since 2.3.0
[443] Fix | Delete
*
[444] Fix | Delete
* @param int|string $bytes Number of bytes. Note max integer size for integers.
[445] Fix | Delete
* @param int $decimals Optional. Precision of number of decimal places. Default 0.
[446] Fix | Delete
* @return string|false Number string on success, false on failure.
[447] Fix | Delete
*/
[448] Fix | Delete
function size_format( $bytes, $decimals = 0 ) {
[449] Fix | Delete
$quant = array(
[450] Fix | Delete
/* translators: Unit symbol for terabyte. */
[451] Fix | Delete
_x( 'TB', 'unit symbol' ) => TB_IN_BYTES,
[452] Fix | Delete
/* translators: Unit symbol for gigabyte. */
[453] Fix | Delete
_x( 'GB', 'unit symbol' ) => GB_IN_BYTES,
[454] Fix | Delete
/* translators: Unit symbol for megabyte. */
[455] Fix | Delete
_x( 'MB', 'unit symbol' ) => MB_IN_BYTES,
[456] Fix | Delete
/* translators: Unit symbol for kilobyte. */
[457] Fix | Delete
_x( 'KB', 'unit symbol' ) => KB_IN_BYTES,
[458] Fix | Delete
/* translators: Unit symbol for byte. */
[459] Fix | Delete
_x( 'B', 'unit symbol' ) => 1,
[460] Fix | Delete
);
[461] Fix | Delete
[462] Fix | Delete
if ( 0 === $bytes ) {
[463] Fix | Delete
/* translators: Unit symbol for byte. */
[464] Fix | Delete
return number_format_i18n( 0, $decimals ) . ' ' . _x( 'B', 'unit symbol' );
[465] Fix | Delete
}
[466] Fix | Delete
[467] Fix | Delete
foreach ( $quant as $unit => $mag ) {
[468] Fix | Delete
if ( (float) $bytes >= $mag ) {
[469] Fix | Delete
return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
[470] Fix | Delete
}
[471] Fix | Delete
}
[472] Fix | Delete
[473] Fix | Delete
return false;
[474] Fix | Delete
}
[475] Fix | Delete
[476] Fix | Delete
/**
[477] Fix | Delete
* Convert a duration to human readable format.
[478] Fix | Delete
*
[479] Fix | Delete
* @since 5.1.0
[480] Fix | Delete
*
[481] Fix | Delete
* @param string $duration Duration will be in string format (HH:ii:ss) OR (ii:ss),
[482] Fix | Delete
* with a possible prepended negative sign (-).
[483] Fix | Delete
* @return string|false A human readable duration string, false on failure.
[484] Fix | Delete
*/
[485] Fix | Delete
function human_readable_duration( $duration = '' ) {
[486] Fix | Delete
if ( ( empty( $duration ) || ! is_string( $duration ) ) ) {
[487] Fix | Delete
return false;
[488] Fix | Delete
}
[489] Fix | Delete
[490] Fix | Delete
$duration = trim( $duration );
[491] Fix | Delete
[492] Fix | Delete
// Remove prepended negative sign.
[493] Fix | Delete
if ( '-' === substr( $duration, 0, 1 ) ) {
[494] Fix | Delete
$duration = substr( $duration, 1 );
[495] Fix | Delete
}
[496] Fix | Delete
[497] Fix | Delete
// Extract duration parts.
[498] Fix | Delete
$duration_parts = array_reverse( explode( ':', $duration ) );
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function