Finding the attachment_id even after site migration breaks the link between url and guid #53
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Alteration to find the attachment/post ID from the postmeta table rather than the guid held in posts table.
This (hopefully!) fixes the problem where the url differs from the guid because of site migrations i.e. where images are loaded onto one site (at which point the guid and the url are the same) but then the site is migrated and the url changes but the guid doesn't.
The problem I encountered was that picturefill-wp didn't seem to work at all when I installed it on a site that I had migrated.
Typically we will develop sites on local machines (but not using localhost as the url) and migrate them to testing and development servers where the domain name, obviously, changes.
When media is added it receives a value in the guid fields of the WordPress <wp_>Posts table. This guid is set to the url for the image. When the site is migrated, the guid stays the same e.g. http://my_local_machine/uploads/my_img.jpg but the url changes e.g. http://my_testing_server_domain/uploads/my_img.jpg.
The plugin was set to find the post (attachment) ID for the image using the guid, but because this is no longer the same as the url, it couldn't find the details and set the value of the image_attributes['attachment_id'] property of the Model_Image_Picturefill_WP class to false which meant that the standard (non-picturefill) img markup was used because the srcset array wasn't being populated by the set_serset_array() method.
Instead of using the guid in <wp_posts> to find the post/attachment ID, I used the <wp_>postmeta table finding the attachment path and file name where the meta_key field is equal to '_wp_attached_file'. So for a url 'http://my_domain/uploads/2014/12/my_image.jpg' it extracts '2014/12/my_image.jpg' finds the row for this meta_value and meta_key combination and reads the post_id column.
Disclaimers:
I've added two static 'helper' functions to perform the logic, for ease, I have just made them static functions but wouldn't be particularly happy leaving them here if it was my code.
I can't say I've tested the strip_baseurl function particularly thoroughly
I won't claim to be an expert at WordPress so can't be sure that the link from <wp_>postmeta to <wp_>posts is 100% safe but I haven't been able to break it yet.