WordPress.org

Make WordPress Core

Ticket #36455: 36455.17.diff

File 36455.17.diff, 6.2 KB (added by mikeschroder, 7 weeks ago)

Invalidate update-core.php before update, as it is copied separately.

  • src/wp-admin/includes/class-core-upgrader.php

    diff --git src/wp-admin/includes/class-core-upgrader.php src/wp-admin/includes/class-core-upgrader.php
    index 7ef49f1f1c9..d7305589bf4 100644
    class Core_Upgrader extends WP_Upgrader { 
    161161                }
    162162                $wp_filesystem->chmod( $wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE );
    163163
     164                wp_opcache_invalidate( ABSPATH . 'wp-admin/includes/update-core.php' );
    164165                require_once ABSPATH . 'wp-admin/includes/update-core.php';
    165166
    166167                if ( ! function_exists( 'update_core' ) ) {
  • src/wp-admin/includes/file.php

    diff --git src/wp-admin/includes/file.php src/wp-admin/includes/file.php
    index 08215e5d2b2..ec01eb799a5 100644
    function wp_edit_theme_plugin_file( $args ) { 
    498498        if ( false === $written ) {
    499499                return new WP_Error( 'unable_to_write', __( 'Unable to write to file.' ) );
    500500        }
    501         if ( 'php' === $extension && function_exists( 'opcache_invalidate' ) ) {
    502                 opcache_invalidate( $real_file, true );
    503         }
     501        wp_opcache_invalidate( $real_file, true );
    504502
    505503        if ( $is_active && 'php' === $extension ) {
    506504
    function wp_edit_theme_plugin_file( $args ) { 
    608606
    609607                        // Roll-back file change.
    610608                        file_put_contents( $real_file, $previous_content );
    611                         if ( function_exists( 'opcache_invalidate' ) ) {
    612                                 opcache_invalidate( $real_file, true );
    613                         }
     609                        wp_opcache_invalidate( $real_file, true );
    614610
    615611                        if ( ! isset( $result['message'] ) ) {
    616612                                $message = __( 'Something went wrong.' );
    function copy_dir( $from, $to, $skip_list = array() ) { 
    17431739                                        return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename );
    17441740                                }
    17451741                        }
     1742                        wp_opcache_invalidate( $to . $filename );
    17461743                } elseif ( 'd' === $fileinfo['type'] ) {
    17471744                        if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
    17481745                                if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) {
    function wp_print_request_filesystem_credentials_modal() { 
    22772274        </div>
    22782275        <?php
    22792276}
     2277
     2278/**
     2279 * Attempt to clear the opcode cache for an individual PHP file.
     2280 *
     2281 * This function can be called safely without having to check the file extension
     2282 * or availability of the OPcache extension.
     2283 *
     2284 * Whether or not invalidation is possible is cached to improve performance.
     2285 *
     2286 * @since 5.5
     2287 *
     2288 * @link https://www.php.net/manual/en/function.opcache-invalidate.php
     2289 *
     2290 * @param string $filepath Path to the file, including extension, for which the opcode cache is to be cleared.
     2291 * @param bool   $force    Invalidate even if the modification time is not newer than the file in cache. Default `false`.
     2292 *
     2293 * @return bool `true` if opcache was invalidated for `$filepath`, or there was nothing to invalidate.
     2294 *              `false` if opcache invalidation is not available, or is disabled via filter.
     2295 */
     2296function wp_opcache_invalidate( $filepath, $force = false ) {
     2297        static $can_invalidate = null;
     2298
     2299        /*
     2300         * Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value.
     2301         *
     2302         * First, check to see if the function is available to call, then if the host has restricted
     2303         * the ability to run the function to avoid a PHP warning.
     2304         *
     2305         * `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`.
     2306         *
     2307         * If the host has this set, check whether the path in `opcache.restrict_api` matches
     2308         * the beginning of the path of the origin file.
     2309         *
     2310         * `$_SERVER['SCRIPT_FILENAME']` approximates the origin file's path, but
     2311         * `realpath()` is necessary because `SCRIPT_FILENAME` can be a relative path
     2312         * when run from CLI.
     2313         *
     2314         * For more details, see:
     2315         * - https://www.php.net/manual/en/opcache.configuration.php
     2316         * - https://www.php.net/manual/en/reserved.variables.server.php
     2317         * - https://core.trac.wordpress.org/ticket/36455
     2318         */
     2319        if ( $can_invalidate === null ) {
     2320                $can_invalidate = function_exists( 'opcache_invalidate' ) &&
     2321                        ( ! ini_get( 'opcache.restrict_api' ) ||
     2322                                stripos( realpath( $_SERVER['SCRIPT_FILENAME'] ), ini_get( 'opcache.restrict_api' ) ) === 0 );
     2323        }
     2324
     2325        // If invalidation is not available, return early.
     2326        if ( ! $can_invalidate ) {
     2327                return false;
     2328        }
     2329
     2330        // Verify that file to be invalidated has a PHP extension.
     2331        if ( ! preg_match( '/\.(?:php)$/i', $filepath ) ) {
     2332                return false;
     2333        }
     2334
     2335        /**
     2336         * Filters whether to invalidate a file from the opcode cache.
     2337         *
     2338         * @since 5.5
     2339         *
     2340         * @param bool   $will_invalidate Whether WordPress will invalidate `$filename`. Default `true`.
     2341         * @param string $filename        The PHP filename to invalidate.
     2342         */
     2343        if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) {
     2344                return opcache_invalidate( $filepath, $force );
     2345        }
     2346
     2347        return false;
     2348}
  • src/wp-admin/includes/update-core.php

    diff --git src/wp-admin/includes/update-core.php src/wp-admin/includes/update-core.php
    index 935f1a73036..d7af50f3ebe 100644
    function update_core( $from, $to ) { 
    13181318 *
    13191319 * Assumes that WP_Filesystem() has already been called and setup.
    13201320 *
    1321  * This is a temporary function for the 3.1 -> 3.2 upgrade, as well as for those
    1322  * upgrading to 3.7+.
     1321 * This is a standalone copy of the `copy_dir()` function that is used to
     1322 * upgrade the core files. It is placed here so that the version of this
     1323 * function from the *new* WordPress version will be called.
     1324 *
     1325 * It was initially added for the 3.1 -> 3.2 upgrade.
    13231326 *
    13241327 * @ignore
    13251328 * @since 3.2.0
    13261329 * @since 3.7.0 Updated not to use a regular expression for the skip list.
    13271330 *
    13281331 * @see copy_dir()
     1332 * @link https://core.trac.wordpress.org/ticket/17173
    13291333 *
    13301334 * @global WP_Filesystem_Base $wp_filesystem
    13311335 *
    function _copy_dir( $from, $to, $skip_list = array() ) { 
    13551359                                        return new WP_Error( 'copy_failed__copy_dir', __( 'Could not copy file.' ), $to . $filename );
    13561360                                }
    13571361                        }
     1362
     1363                        // `wp_opcache_invalidate()` only exists in WordPress 5.5, so don't run it when upgrading to 5.5.
     1364                        if ( function_exists( 'wp_opcache_invalidate') ) {
     1365                                wp_opcache_invalidate( $to . $filename );
     1366                        }
    13581367                } elseif ( 'd' === $fileinfo['type'] ) {
    13591368                        if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
    13601369                                if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) {