/**
 * Defines initial WordPress constants
 *
 * @see wp_debug_mode()
 *
 * @since 3.0.0
 *
 * @global int $blog_id
 */
function wp_initial_constants()
{
    global $blog_id;
    /**#@+
     * Constants for expressing human-readable data sizes in their respective number of bytes.
     *
     * @since 4.4.0
     */
    define('KB_IN_BYTES', 1024);
    define('MB_IN_BYTES', 1024 * KB_IN_BYTES);
    define('GB_IN_BYTES', 1024 * MB_IN_BYTES);
    define('TB_IN_BYTES', 1024 * GB_IN_BYTES);
    /**#@-*/
    $current_limit = @ini_get('memory_limit');
    $current_limit_int = wp_convert_hr_to_bytes($current_limit);
    // Define memory limits.
    if (!defined('WP_MEMORY_LIMIT')) {
        if (false === wp_is_ini_value_changeable('memory_limit')) {
            define('WP_MEMORY_LIMIT', $current_limit);
        } elseif (is_multisite()) {
            define('WP_MEMORY_LIMIT', '64M');
        } else {
            define('WP_MEMORY_LIMIT', '40M');
        }
    }
    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) {
            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)) {
        @ini_set('memory_limit', WP_MEMORY_LIMIT);
    }
    if (!isset($blog_id)) {
        $blog_id = 1;
    }
    if (!defined('WP_CONTENT_DIR')) {
        define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
    }
    // no trailing slash, full paths only - WP_CONTENT_URL is defined further down
    // Add define('WP_DEBUG', true); to wp-config.php to enable display of notices during development.
    if (!defined('WP_DEBUG')) {
        define('WP_DEBUG', false);
    }
    // Add define('WP_DEBUG_DISPLAY', null); to wp-config.php use the globally configured setting for
    // display_errors and not force errors to be displayed. Use false to force display_errors off.
    if (!defined('WP_DEBUG_DISPLAY')) {
        define('WP_DEBUG_DISPLAY', true);
    }
    // Add define('WP_DEBUG_LOG', true); to enable error logging to wp-content/debug.log.
    if (!defined('WP_DEBUG_LOG')) {
        define('WP_DEBUG_LOG', false);
    }
    if (!defined('WP_CACHE')) {
        define('WP_CACHE', false);
    }
    // Add define('SCRIPT_DEBUG', true); to wp-config.php to enable loading of non-minified,
    // non-concatenated scripts and stylesheets.
    if (!defined('SCRIPT_DEBUG')) {
        if (!empty($GLOBALS['wp_version'])) {
            $develop_src = false !== strpos($GLOBALS['wp_version'], '-src');
        } else {
            $develop_src = false;
        }
        define('SCRIPT_DEBUG', $develop_src);
    }
    /**
     * Private
     */
    if (!defined('MEDIA_TRASH')) {
        define('MEDIA_TRASH', false);
    }
    if (!defined('SHORTINIT')) {
        define('SHORTINIT', false);
    }
    // Constants for features added to WP that should short-circuit their plugin implementations
    define('WP_FEATURE_BETTER_PASSWORDS', true);
    /**#@+
     * Constants for expressing human-readable intervals
     * in their respective number of seconds.
     *
     * Please note that these values are approximate and are provided for convenience.
     * For example, MONTH_IN_SECONDS wrongly assumes every month has 30 days and
     * YEAR_IN_SECONDS does not take leap years into account.
     *
     * If you need more accuracy please consider using the DateTime class (https://secure.php.net/manual/en/class.datetime.php).
     *
     * @since 3.5.0
     * @since 4.4.0 Introduced `MONTH_IN_SECONDS`.
     */
    define('MINUTE_IN_SECONDS', 60);
    define('HOUR_IN_SECONDS', 60 * MINUTE_IN_SECONDS);
    define('DAY_IN_SECONDS', 24 * HOUR_IN_SECONDS);
    define('WEEK_IN_SECONDS', 7 * DAY_IN_SECONDS);
    define('MONTH_IN_SECONDS', 30 * DAY_IN_SECONDS);
    define('YEAR_IN_SECONDS', 365 * DAY_IN_SECONDS);
    /**#@-*/
}
Esempio n. 2
0
/**
 * Attempts to raise the PHP memory limit for memory intensive processes.
 *
 * Only allows raising the existing limit and prevents lowering it.
 *
 * @since 4.6.0
 *
 * @param string $context Optional. Context in which the function is called. Accepts either 'admin',
 *                        'image', or an arbitrary other context. If an arbitrary context is passed,
 *                        the similarly arbitrary {@see '{$context}_memory_limit'} filter will be
 *                        invoked. Default 'admin'.
 * @return bool|int|string The limit that was set or false on failure.
 */
function wp_raise_memory_limit($context = 'admin')
{
    // Exit early if the limit cannot be changed.
    if (false === wp_is_ini_value_changeable('memory_limit')) {
        return false;
    }
    $current_limit = @ini_get('memory_limit');
    $current_limit_int = wp_convert_hr_to_bytes($current_limit);
    if (-1 === $current_limit_int) {
        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;
    switch ($context) {
        case 'admin':
            /**
             * Filters the maximum memory limit available for administration screens.
             *
             * This only applies to administrators, who may require more memory for tasks
             * like updates. Memory limits when processing images (uploaded or edited by
             * users of any role) are handled separately.
             *
             * The `WP_MAX_MEMORY_LIMIT` constant specifically defines the maximum memory
             * limit available when in the administration back end. The default is 256M
             * (256 megabytes of memory) or the original `memory_limit` php.ini value if
             * this is higher.
             *
             * @since 3.0.0
             * @since 4.6.0 The default now takes the original `memory_limit` into account.
             *
             * @param int|string $filtered_limit The maximum WordPress memory limit. Accepts an integer
             *                                   (bytes), or a shorthand string notation, such as '256M'.
             */
            $filtered_limit = apply_filters('admin_memory_limit', $filtered_limit);
            break;
        case 'image':
            /**
             * Filters the memory limit allocated for image manipulation.
             *
             * @since 3.5.0
             * @since 4.6.0 The default now takes the original `memory_limit` into account.
             *
             * @param int|string $filtered_limit Maximum memory limit to allocate for images.
             *                                   Default `WP_MAX_MEMORY_LIMIT` or the original
             *                                   php.ini `memory_limit`, whichever is higher.
             *                                   Accepts an integer (bytes), or a shorthand string
             *                                   notation, such as '256M'.
             */
            $filtered_limit = apply_filters('image_memory_limit', $filtered_limit);
            break;
        default:
            /**
             * Filters the memory limit allocated for arbitrary contexts.
             *
             * The dynamic portion of the hook name, `$context`, refers to an arbitrary
             * context passed on calling the function. This allows for plugins to define
             * their own contexts for raising the memory limit.
             *
             * @since 4.6.0
             *
             * @param int|string $filtered_limit Maximum memory limit to allocate for images.
             *                                   Default '256M' or the original php.ini `memory_limit`,
             *                                   whichever is higher. Accepts an integer (bytes), or a
             *                                   shorthand string notation, such as '256M'.
             */
            $filtered_limit = apply_filters("{$context}_memory_limit", $filtered_limit);
            break;
    }
    $filtered_limit_int = wp_convert_hr_to_bytes($filtered_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;
        }
    }
    return false;
}
 /**
  * Tests the determining of the changeability of a PHP ini value.
  *
  * @ticket 32075
  *
  * @dataProvider data_wp_is_ini_value_changeable
  *
  * @param string $setting  The setting passed to wp_is_ini_value_changeable().
  * @param bool   $expected The expected output of wp_convert_hr_to_bytes().
  */
 function test_wp_is_ini_value_changeable($setting, $expected)
 {
     $this->assertSame($expected, wp_is_ini_value_changeable($setting));
 }