webrain/grunt-wordpress-deploy — pull request #9 by i11ume (2014-03-30 02:05:54+00:00)
Title: Fixed bug with replacing url in serialized data

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.