short-pixel-optimizer / enable-media-replace

Enable Media Replace WordPress plugin
GNU General Public License v2.0
20 stars 28 forks source link

EMR with post-meta serialized data #51

Closed ChetCromer closed 4 years ago

ChetCromer commented 4 years ago

Bringing this over from the plugin pager now that we have round the repo. We're not PHP pro's so may not be able to contribute much, but I wanted to note it here in the event that any contributor can work on it.

Original: https://wordpress.org/support/topic/replacing-media-paths-in-serialized-wpmeta-data/#post-12146774

We utilize WooCommerce and a WooCommerce plugin called Product Documents (https://woocommerce.com/products/product-documents/) to associate PDF media files with products in my online store.

Our client uses this plugin (Enable Media Replace) to swap out media files when a new version of a PDF is released. We want all of the product document links to be updated as well.

This works fine if we allow the plugin to rename the new file to match the old file name. However, if we request that the plugin change old links to match the new file name, it only works within the product content itself, not within the links for the product documents. In fact, it wipes out the product document link data COMPLETELY.

It took some time for us to figure out the culprit, but we finally did, and I’d like to see if you can help us resolve it.

The Product Documents plugin allows us to link any number of documents to a WooCommerce plugin, and it stores the information in a meta record along with the document such as the sample that follows this post. The plugin uses a WP standard serialization method to serialize the document data into something such as this (single document):

{s:5:”label”;s:19:”WAW-2322 Spec Sheet”;s:13:”file_location”;s:75:”https://dc2willoughbyc.wpengine.com/wp-content/uploads/2018/01/WAW-2322.pdf”;}

The “Enable Media Replace” plugin, when we ask it to replace one media file with another (with a different name), DOES change the file name (in the example above, from WAW-2322.pdf to ABCD.pdf, would be an example). However, it does not update the “s:75” section of the serialized object that represents the length of the string to follow. Since the length of the file changed (reduced by 4 characters), WP can no longer deserialize the string into an object and our data is list.

Our request is that “Enable Media Replace” take into account object serialization in WPMeta fields and not only update the file name, but also the string length in the serialized object. Other search/replace plugins (such as “Better Search and Replace” https://wordpress.org/plugins/search-and-replace/) properly care for this replacement, but the UI of “Enable Media Replace” is much better for our client to use (and MUCH less dangerous!)

We would appreciate your feedback and assistance to this issue.

Full Sample data below. The plugin will replace the file name, but does not change the string length of the file name accordingly.

a:1:{i:0;a:3:{s:4:”name”;s:0:””;s:7:”default”;b:1;s:9:”documents”;a:7:{i:0;a:2:{s:5:”label”;s:19:”WAW-2322 Spec Sheet”;s:13:”file_location”;s:75:”https://dc2willoughbyc.wpengine.com/wp-content/uploads/2018/01/WAW-2322.pdf”;}i:1;a:2:{s:5:”label”;s:32:”WORD formatted CSI specification”;s:13:”file_location”;s:99:”https://dc2willoughbyc.wpengine.com/wp-content/uploads/2017/09/224233_Willoughby_Washfountains.docx”;}i:2;a:2:{s:5:”label”;s:16:”AutoCAD DWG file”;s:13:”file_location”;s:75:”https://dc2willoughbyc.wpengine.com/wp-content/uploads/2017/09/Aquasurf.dwg”;}i:3;a:2:{s:5:”label”;s:16:”DXF drawing file”;s:13:”file_location”;s:83:”https://dc2willoughbyc.wpengine.com/wp-content/uploads/2018/01/DWG_DXF/Aquasurf.dxf”;}i:4;a:2:{s:5:”label”;s:37:”Install and maintenance documentation”;s:13:”file_location”;s:86:”https://dc2willoughbyc.wpengine.com/wp-content/uploads/2018/01/WAW-2322-Series-IOM.pdf”;}i:5;a:2:{s:5:”label”;s:22:”AquaSurf® color chart”;s:13:”file_location”;s:88:”https://dc2willoughbyc.wpengine.com/wp-content/uploads/2017/05/Aqua-Surf-Color-Table.pdf”;}i:6;a:2:{s:5:”label”;s:11:”REVIT files”;s:13:”file_location”;s:114:”https://dc2willoughbyc.wpengine.com/wp-content/uploads/2018/01/AquaWave-Lavatory-2_Station-Willoughby-WAW-2322.rfa”;}}}}

bschuiling commented 4 years ago

Hi,

Yes this has been an known issue we were busy fixing, but it took a bit longer than hoped.

You have good timing however since we are testing the new version that should take into account those cases.

If you want, please download this version and test if it works for your plugin, that would help a lot! -> https://github.com/short-pixel-optimizer/enable-media-replace/tree/updates

ChetCromer commented 4 years ago

We tested 3.3.8-DEV03 on our staging site and this resolved our issue. File names in the meta table were adjusted and it appears their string length was also updated.

We won't be pushing this into our production site until you publish an official release, but we're glad to see this resolved.

Do you have a release date for the production version of 3.3.8, or its equivalent?

Chet

pdobrescu commented 4 years ago

Hello @ChetCromer and thanks for reporting this issue, can you please confirm that it is solved with the latest public releases of the plugin (we're already at 3.3.11 now)?

pdobrescu commented 4 years ago

Please comment on this issue if you still have troubles with post-meta serialized data after upgrading to the latest plugin release.