WordPress.org

Make WordPress Core

Ticket #36455: 36455.12.diff

File 36455.12.diff, 3.8 KB (added by mikeschroder, 2 months ago)

Helper function and filter. Static extension checking. Modified docs.

  • src/wp-admin/includes/file.php

    diff --git src/wp-admin/includes/file.php src/wp-admin/includes/file.php
    index cca39895bab..e997b7d2b65 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() ) { 
    17411737                                        return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename );
    17421738                                }
    17431739                        }
     1740                        wp_opcache_invalidate( $to . $filename );
    17441741                } elseif ( 'd' === $fileinfo['type'] ) {
    17451742                        if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
    17461743                                if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) {
    function wp_print_request_filesystem_credentials_modal() { 
    22762273        </div>
    22772274        <?php
    22782275}
     2276
     2277/**
     2278 * Attempt to clear the opcode cache for an individual PHP file.
     2279 *
     2280 * This function can be called safely without having to check the file extension
     2281 * or availability of the OPcache extension.
     2282 *
     2283 * Whether or not invalidation is possible is cached to improve performance.
     2284 *
     2285 * See https://www.php.net/manual/en/function.opcache-invalidate.php for more details.
     2286 *
     2287 * @since 5.5
     2288 *
     2289 * @param string $filepath Path to the file, including extension, for which the opcode cache is to be cleared.
     2290 * @param bool $force Invalidate even if the modification time is not newer than the file in cache. Default false.
     2291 *
     2292 * @return bool True if opcache was invalidated for `$filepath`. Default false.
     2293 */
     2294function wp_opcache_invalidate( $filepath, $force = false ) {
     2295        static $can_invalidate = null;
     2296
     2297        // Verify that file to be invalidated has a PHP-related extension.
     2298        if ( ! preg_match( '/\.(?:php|phtml)/i', $filepath ) ) {
     2299                return false;
     2300        }
     2301
     2302        // Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value.
     2303        if ( $can_invalidate === null ) {
     2304                $can_invalidate = function_exists( 'opcache_invalidate' ) &&
     2305                        ! ini_get( 'opcache.restrict_api' ) || stripos( __FILE__, ini_get( 'opcache.restrict_api' ) ) === 0;
     2306        }
     2307
     2308        // If invalidation is not available, return early.
     2309        if ( ! $can_invalidate ) {
     2310                return false;
     2311        }
     2312
     2313        /**
     2314         * Filters whether to invalidate a file from the opcode cache.
     2315         *
     2316         * @since 5.5
     2317         *
     2318         * @param bool $will_invalidate Whether WordPress will invalidate `$filename`. Default `true`.
     2319         * @param string $filename The PHP filename to invalidate.
     2320         */
     2321        if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) {
     2322                return opcache_invalidate( $filepath, $force );
     2323        }
     2324
     2325        return false;
     2326}
  • src/wp-admin/includes/update-core.php

    diff --git src/wp-admin/includes/update-core.php src/wp-admin/includes/update-core.php
    index 595ebc053a8..0fe76de8347 100644
    function _copy_dir( $from, $to, $skip_list = array() ) { 
    13521352                                        return new WP_Error( 'copy_failed__copy_dir', __( 'Could not copy file.' ), $to . $filename );
    13531353                                }
    13541354                        }
     1355                        wp_opcache_invalidate( $to . $filename );
    13551356                } elseif ( 'd' === $fileinfo['type'] ) {
    13561357                        if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
    13571358                                if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) {