Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter m4rcu5

    (@m4rcu5)

    Hi @tigroumeow,

    After some more digging, it seems to affect only images that are moved from the root of the uploads folder. In this site, I do not have month-year based folders enabled, meaning everything is uploaded to the root.

    I think the following is going on (all in classes/core.php);
    [line 699] $old_directory = trim( str_replace( $upload_dir['basedir'], '', $path_parts['dirname'] ), '/' ); // '2011/01'
    It will try to get the old path (/my/path/to/uploads/ => / => “”)

    [line 727] $meta['file'] = $this->str_replace( $old_directory, $new_directory, $meta['file'] );
    Calling str_replace on line 610:

    
    function str_replace( $needle, $replace, $haystack ) {                                                                                                                                                      
    ?       ?       $pos = strpos( $haystack, $needle );
    ?       ?       if ( $pos !== false )
    ?       ?       ?       $haystack = substr_replace( $haystack, $replace, $pos, strlen( $needle ) );
    ?       ?       return $haystack;
    

    But as our needle is empty (“”), because there was no date based directory, there is no substitution, and therefore the original unaltered haystack (path) is returned, and no metadata is updated.

    I came up with the following patch that works for me:

    
    --- a/classes/core.php	2021-11-13 00:47:12.000000000 +0100
    +++ b/classes/core.php	2021-11-19 13:58:35.326454709 +0100
    @@ -723,11 +723,21 @@
     		$meta = wp_get_attachment_metadata( $id );
     
     		if ( $meta ) {
    -			if ( isset( $meta['file'] ) && !empty( $meta['file'] ) )
    +			if ( isset( $meta['file'] ) && !empty( $meta['file'] ) ) {
    +			    if ( $old_directory ) {
     				$meta['file'] = $this->str_replace( $old_directory, $new_directory, $meta['file'] );
    -			if ( isset( $meta['url'] ) && !empty( $meta['url'] ) && strlen( $meta['url'] ) > 4 )
    +			    } else {
    +				$meta['file'] = trailingslashit( $new_directory ) . $meta['file'];
    +			    }
    +			}
    +			if ( isset( $meta['url'] ) && !empty( $meta['url'] ) && strlen( $meta['url'] ) > 4 ) {
    +			    if ( $old_directory ) {
     				$meta['url'] = $this->str_replace( $old_directory, $new_directory, $meta['url'] );
    -			//wp_update_attachment_metadata( $id, $meta );
    +			    } else {
    +				$meta['url'] = trailingslashit( $new_directory ) . $meta['url'];
    +			    }
    +			}
    +                        //wp_update_attachment_metadata( $id, $meta );
     		}
     
     		// Better to check like this rather than with wp_attachment_is_image
    
    • This reply was modified 3 years, 3 months ago by m4rcu5. Reason: Fixing code block formatting
Viewing 1 replies (of 1 total)