Make WordPress Core


Ignore:
Timestamp:
02/03/2023 01:48:36 AM (3 years ago)
Author:
azaozz
Message:

Filesystem API: Add directory support to WP_Filesystem_Direct::move().

Introduces:

  • New function: wp_opcache_invalidate_directory(), to recursively call wp_opcache_invalidate() after overwriting .php files.
  • New function: move_dir(), similar to copy_dir() that uses WP_Filesystem::move() followed by wp_opcache_invalidate_directory(), and has a fallback to copy_dir().

Props: costdev, afragen, peterwilsoncc, sergeybiryukov, ironprogrammer, flixos90, bronsonquick, mukesh27, azaozz.
Fixes #57375.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-filesystem-direct.php

    r53872 r55204  
    317317
    318318    /**
    319      * Moves a file.
     319     * Moves a file or directory.
     320     *
     321     * After moving files or directories, OPcache will need to be invalidated.
     322     *
     323     * If moving a directory fails, `copy_dir()` can be used for a recursive copy.
     324     *
     325     * Use `move_dir()` for moving directories with OPcache invalidation and a
     326     * fallback to `copy_dir()`.
    320327     *
    321328     * @since 2.5.0
     
    332339        }
    333340
     341        if ( $overwrite && $this->exists( $destination ) && ! $this->delete( $destination, true ) ) {
     342            // Can't overwrite if the destination couldn't be deleted.
     343            return false;
     344        }
     345
    334346        // Try using rename first. if that fails (for example, source is read only) try copy.
    335347        if ( @rename( $source, $destination ) ) {
     
    337349        }
    338350
    339         if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) {
     351        // Backward compatibility: Only fall back to `::copy()` for single files.
     352        if ( $this->is_file( $source ) && $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) {
    340353            $this->delete( $source );
    341354
Note: See TracChangeset for help on using the changeset viewer.