aubreypwd / WordPress

[DEPRECATED] See https://github.com/aubreypwd/wordpress-develop
https://github.com/WordPress/WordPress
Other
0 stars 0 forks source link

#49335 — On Multisite, wp_get_attachment_image_url returns URL with current site path even though switch_to_blog() is on another #6

Open aubreypwd opened 4 years ago

aubreypwd commented 4 years ago

...even though switch_to_blog() is on another.

Example

        switch_to_blog( 2 );

        $attachment = get_post( $attachment_id, ARRAY_A );
        $mime_type  = get_post_mime_type( $attachment_id );

        $source_urls = [];
        if ( stristr( $mime_type, 'image' ) ) {
            foreach ( array_merge( get_intermediate_image_sizes(), [ 'full' ] ) as $size ) {
                $source_urls[ $size ] = wp_get_attachment_image_url( $attachment_id, $size );
            }
        }

        restore_current_blog();

In the above example $source_urls all have paths appended instead of the path of switch_to_blog().

richaber commented 4 years ago

Have you checked WP Core Trac to see if this is a known issue, or reported it?

aubreypwd commented 4 years ago

@richaber Did not find on Trac: https://core.trac.wordpress.org/search?q=wp_get_attachment_image_url going to confirm this and report if I can verify on fresh install.

aubreypwd commented 4 years ago

So in my example, I have a sub-dir site with three sites:

1) The root site (ID: 1) 2) site-2 (ID: 2) 3) site-3 (ID: 3)

Then I load up site 2 wp-admin/ and let the following code run in an mu-plugin/:

add_action( 'plugins_loaded', function() {
    switch_to_blog( 3 );
        error_log( print_r( (object) [ __FILE__ => __LINE__,
            wp_get_attachment_image_url( 4, 'full' ),
            get_site( get_current_blog_id() ),
        ], true ) );
    restore_current_blog();
} );

This switches to site 3 and gets an attachment URL using wp_get_attachment_image_url and outputs the WP_Site of site 3, but in the output:

(
    [/app/public/wp-content/mu-plugins/debug.php] => 9
    [0] => http://wordpress-subdir.test/site-2/wp-content/uploads/sites/3/2020/01/Database.png
    [1] => WP_Site Object
        (
            [blog_id] => 3
            [domain] => wordpress-subdir.test
            [path] => /site-3/
            [site_id] => 1
            [registered] => 2020-01-31 20:43:10
            [last_updated] => 2020-01-31 20:43:10
            [public] => 1
            [archived] => 0
            [mature] => 0
            [spam] => 0
            [deleted] => 0
            [lang_id] => 0
        )
)

Notice that the URL [0] => http://wordpress-subdir.test/site-2/wp-content/uploads/sites/3/2020/01/Database.png contains the site slug site-2 of site 2 even though we are actively on site 3 via switch_to_blog().

aubreypwd commented 4 years ago

Oddly, on sub-domain I get:

(
    [/app/public/wp-content/mu-plugins/debug.php] => 9
    [0] => http://site2.wordpress-subdomain.test/wp-content/uploads/sites/3/2020/01/terminal-gnome.ong_.png
    [1] => WP_Site Object
        (
            [blog_id] => 3
            [domain] => site3.wordpress-subdomain.test
            [path] => /
            [site_id] => 1
            [registered] => 2020-01-31 21:28:57
            [last_updated] => 2020-01-31 21:28:57
            [public] => 1
            [archived] => 0
            [mature] => 0
            [spam] => 0
            [deleted] => 0
            [lang_id] => 0
        )

)

Which does NOT contain the site slug of the admin dashboard.

aubreypwd commented 4 years ago

On Trac: https://core.trac.wordpress.org/ticket/49335#ticket

aubreypwd commented 4 years ago

Thought I would back this up with a little video explaining my findings: http://aubrey.pw/d/2019/1580507121.mp4