bobbingwide / oik-clone

Clone content between sites
https://www.oik-plugins.com/oik-plugins/oik-clone-clone-your-wordpress-content/
GNU General Public License v2.0
5 stars 0 forks source link

Images broken after cloning from one Multi Site site to another #60

Open bobbingwide opened 3 years ago

bobbingwide commented 3 years ago

image

Original thoughts

When I first created the Screenshots page in s.b/wp55/thisis/ it was a top level page. I moved it to below About, so the new URL became s.b/wp55/thisis/about/screenshots. But the images in the gallery were still hardcoded, so appeared broken.

Alternative explanation

The above could have been a red herring.

Looking at the source URL of the image file I can see it contains the site ID.

<li class="blocks-gallery-item">
<figure>
<a href="https://s.b/wp55/thisis/screenshots/thisis-01-theme-activation/">
<img src="https://s.b/wp55/thisis/wp-content/uploads/sites/15/2021/04/thisis-01-theme-activation-1024x549.jpg" alt="" data-id="381" data-link="https://s.b/wp55/thisis/screenshots/thisis-01-theme-activation/" class="wp-image-381"/>
</a>
</figure>
</li>

I'm cloning between two unrelated WPMS installations.

It looks like the Site ID needs to be mapped as well. Pseudo code, with these Site IDs instead of variables

$content = str_replace( '/sites/15/', '/sites/3', $content );

Question: Why aren't images broken as well? Answer: They are! But the broken image does not appear in the same way unless it's a link.

bobbingwide commented 3 years ago

Answer: They are! But...

There's some strange magic going on. For https://sneak-peek.me/2021/04/28/do-i-need-to-define-image-sizes/ the source code shows the site ID as being 15, but when I view the post it's been changed to 3.

Why doesn't this happen for the gallery?

For images there's some fancy filtering that's changing the srcset attributes.

<figure class="wp-block-image size-thumbnail"><img loading="lazy" width="150" height="150" src="https://sneak-peek.me/wp-content/uploads/sites/15/2021/04/Fuzzy-Duck-I-could-get-into-this-150x150.jpg" alt="" class="wp-image-80" srcset="https://sneak-peek.me/wp-content/uploads/sites/3/2021/04/Fuzzy-Duck-I-could-get-into-this-150x150.jpg 150w, https://sneak-peek.me/wp-content/uploads/sites/3/2021/04/Fuzzy-Duck-I-could-get-into-this-300x300.jpg 300w, https://sneak-peek.me/wp-content/uploads/sites/3/2021/04/Fuzzy-Duck-I-could-get-into-this-1024x1024.jpg 1024w, https://sneak-peek.me/wp-content/uploads/sites/3/2021/04/Fuzzy-Duck-I-could-get-into-this-768x768.jpg 768w, https://sneak-peek.me/wp-content/uploads/sites/3/2021/04/Fuzzy-Duck-I-could-get-into-this-1536x1536.jpg 1536w, https://sneak-peek.me/wp-content/uploads/sites/3/2021/04/Fuzzy-Duck-I-could-get-into-this.jpg 1936w" sizes="(max-width: 150px) 100vw, 150px" /><figcaption>Image size: Thumbnail</figcaption></figure>

It's wp_img_tag_add_srcset_and_sizes_attr() that uses the attachment ID to find the image and gets the right details.

 

bobbingwide commented 3 years ago

Proposed solution

To fix this problem it'll be necessary to pass the original site ID to the target and then apply updates to the post_content.

The target site will have to apply the changes taking into account its own site ID, obtained the same way.

Mapping is as follows:

Value of site_id Value of target's site ID Processing
1 1 This may not be Multi Site - doesn't matter though - there's nothing to change
n n This is Multi Site - -doesn't matter though - there's nothing to change
n 1 This may be Multi Site to single site. What now?
1 n This may be single site to Multi Site. What now?
n m perform the string replace

I haven't yet encountered the What now? scenario. Maybe I'll set the value to null when it's not multisite.

bobbingwide commented 3 years ago

Workaround

Use better search replace after cloning everything.