webrain / grunt-wordpress-deploy

A Grunt plugin to quickly deploy Wordpress websites.
MIT License
104 stars 37 forks source link

Fixed bug with replacing url in serialized data #9

Closed jamwise closed 10 years ago

jamwise commented 10 years ago

Background: The script first finds all serialized data and replaces the urls there, then it going through all the data and does a replace again, assuming the data in the serialized strings has been replaced.

Problem: The script was using the javascript .replace() method using a standard string which only replaces the first occurrence of a url. Some data in the database has several occurrences though, so though it would properly change the length number on the serialized object, it would only replace one occurrence. The other occurrences would then be replaced by the regular find and replace which doesn’t update the length of the serialized object and therefore breaks that entire data object. So as an example, if you have two links in your widgets, which wordpress stores all in one serialized data object, you’ll break all your widgets because that whole data object is now corrupted.

Solution: Instead of using the generic .replace(), we create a regex object based on the search key and use the /g flag which replaces all occurrences. Then we calculate the amount of occurrences of the url in that particular object and multiply the length_delta by that so we can properly change the length of the object.

darioghilardi commented 10 years ago

Sorry for the delay, I am going to look the code and write the appropriate tests before merging the pull request. It will be released in a few days. Thanks again for pointing out the issue.

jamwise commented 10 years ago

I've updated the tests with a news test for the replace_urls_in_serialized method. It checks for the issue I added the fix for. Sorry for the delay, I never saw your reply until now.

Thanks again for this great tool

davidthou commented 10 years ago

Thank you for your contribution, a new release of the plugin including your fix will be released shortly. I have added some minor fixes to your pull request you can see here #17.