Skip to content

Add wp_ini_parse_quantity() to report numeric php.ini directive values #2660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-admin/includes/class-wp-debug-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ public static function debug_data() {
$post_max_size = ini_get( 'post_max_size' );
$upload_max_filesize = ini_get( 'upload_max_filesize' );
$max_file_uploads = ini_get( 'max_file_uploads' );
$effective = min( wp_convert_hr_to_bytes( $post_max_size ), wp_convert_hr_to_bytes( $upload_max_filesize ) );
$effective = wp_ini_lesser_quantity( $post_max_size, $upload_max_filesize );

// Add info in Media section.
$info['wp-media']['fields']['file_uploads'] = array(
Expand Down
4 changes: 2 additions & 2 deletions src/wp-admin/includes/class-wp-site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,7 @@ public function get_test_file_uploads() {
$post_max_size = ini_get( 'post_max_size' );
$upload_max_filesize = ini_get( 'upload_max_filesize' );

if ( wp_convert_hr_to_bytes( $post_max_size ) < wp_convert_hr_to_bytes( $upload_max_filesize ) ) {
if ( wp_ini_quantity_cmp( $post_max_size, $upload_max_filesize ) < 0 ) {
$result['label'] = sprintf(
/* translators: 1: post_max_size, 2: upload_max_filesize */
__( 'The "%1$s" value is smaller than "%2$s"' ),
Expand All @@ -2172,7 +2172,7 @@ public function get_test_file_uploads() {
);
$result['status'] = 'recommended';

if ( 0 === wp_convert_hr_to_bytes( $post_max_size ) ) {
if ( wp_ini_parse_quantity( $post_max_size ) <= 0 ) {
$result['description'] = sprintf(
'<p>%s</p>',
sprintf(
Expand Down
8 changes: 3 additions & 5 deletions src/wp-includes/default-constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ function wp_initial_constants() {
define( 'WP_START_TIMESTAMP', microtime( true ) );
}

$current_limit = ini_get( 'memory_limit' );
$current_limit_int = wp_convert_hr_to_bytes( $current_limit );
$current_limit = ini_get( 'memory_limit' );

// Define memory limits.
if ( ! defined( 'WP_MEMORY_LIMIT' ) ) {
Expand All @@ -56,16 +55,15 @@ function wp_initial_constants() {
if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) {
if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
} elseif ( -1 === $current_limit_int || $current_limit_int > 268435456 /* = 256M */ ) {
} elseif ( wp_ini_quantity_cmp( $current_limit, '256M' ) > 0 ) {
define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
} else {
define( 'WP_MAX_MEMORY_LIMIT', '256M' );
}
}

// Set memory limits.
$wp_limit_int = wp_convert_hr_to_bytes( WP_MEMORY_LIMIT );
if ( -1 !== $current_limit_int && ( -1 === $wp_limit_int || $wp_limit_int > $current_limit_int ) ) {
if ( wp_ini_quantity_cmp( WP_MEMORY_LIMIT, $current_limit ) > 0 ) {
ini_set( 'memory_limit', WP_MEMORY_LIMIT );
}

Expand Down
35 changes: 15 additions & 20 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7454,16 +7454,15 @@ function wp_raise_memory_limit( $context = 'admin' ) {
return false;
}

$current_limit = ini_get( 'memory_limit' );
$current_limit_int = wp_convert_hr_to_bytes( $current_limit );
$current_limit = ini_get( 'memory_limit' );

if ( -1 === $current_limit_int ) {
// If we're already set to an unlimited value there's no higher limit to set.
if ( wp_ini_parse_quantity( $current_limit ) <= 0 ) {
return false;
}

$wp_max_limit = WP_MAX_MEMORY_LIMIT;
$wp_max_limit_int = wp_convert_hr_to_bytes( $wp_max_limit );
$filtered_limit = $wp_max_limit;
$wp_max_limit = WP_MAX_MEMORY_LIMIT;
$filtered_limit = $wp_max_limit;

switch ( $context ) {
case 'admin':
Expand Down Expand Up @@ -7523,23 +7522,19 @@ function wp_raise_memory_limit( $context = 'admin' ) {
break;
}

$filtered_limit_int = wp_convert_hr_to_bytes( $filtered_limit );
// Set the memory limit to the greatest of all the filtered value, the MAX limit, and the current limit.
$new_limit = wp_ini_greater_quantity( $current_limit, WP_MAX_MEMORY_LIMIT );
$new_limit = wp_ini_greater_quantity( $filtered_limit, $new_limit );

if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
if ( false !== ini_set( 'memory_limit', $filtered_limit ) ) {
return $filtered_limit;
} else {
return false;
}
} elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
if ( false !== ini_set( 'memory_limit', $wp_max_limit ) ) {
return $wp_max_limit;
} else {
return false;
}
// If we're already set at the greatest limit we don't need to change it.
if ( 0 === wp_ini_quantity_cmp( $new_limit, $current_limit ) ) {
return false;
}

return false;
// Otherwise attempt to set the new limit and return the new value if it succeeded.
return false !== ini_set( 'memory_limit', $new_limit )
? $new_limit
: false;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/wp-includes/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package WordPress
*/

require_once __DIR__ . '/php-compat.php';

/**
* Return the HTTP protocol sent by the server.
*
Expand Down Expand Up @@ -1465,6 +1467,7 @@ function is_ssl() {
*
* @since 2.3.0
* @since 4.6.0 Moved from media.php to load.php.
* @deprecated 6.1.0 Use wp_ini_parse_quantity() or wp_hr_bytes() instead.
*
* @link https://www.php.net/manual/en/function.ini-get.php
* @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
Expand All @@ -1473,6 +1476,19 @@ function is_ssl() {
* @return int An integer byte value.
*/
function wp_convert_hr_to_bytes( $value ) {
_deprecated_function( __FUNCTION__, '6.1.0', 'wp_ini_parse_quantity' );
return wp_hr_bytes( $value );
}

/**
* Parses a "human-readable" byte value into an integer.
*
* @since 6.1.0
*
* @param string $value Human-readable description of a byte size
* @return int An integer byte value.
*/
function wp_hr_bytes( $value ) {
$value = strtolower( trim( $value ) );
$bytes = (int) $value;

Expand Down
12 changes: 9 additions & 3 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -3787,8 +3787,9 @@ function wp_expand_dimensions( $example_width, $example_height, $max_width, $max
* @return int Allowed upload size.
*/
function wp_max_upload_size() {
$u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
$p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) );
$upload_max_filesize = ini_get( 'upload_max_filesize' );
$post_max_size = ini_get( 'post_max_size' );
$max_upload = wp_ini_lesser_quantity( $upload_max_filesize, $post_max_size );

/**
* Filters the maximum upload size allowed in php.ini.
Expand All @@ -3799,7 +3800,12 @@ function wp_max_upload_size() {
* @param int $u_bytes Maximum upload filesize in bytes.
* @param int $p_bytes Maximum size of POST data in bytes.
*/
return apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
return apply_filters(
'upload_size_limit',
wp_ini_parse_quantity( $max_upload ),
wp_ini_parse_quantity( $upload_max_filesize ),
wp_ini_parse_quantity( $post_max_size )
);
}

/**
Expand Down
Loading