diff --git a/package.json b/package.json index bf1584a..5272ee3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "grunt-wordpress-deploy", "description": "Deploy Wordpress without pain using Grunt.", - "version": "0.0.5", + "version": "0.0.6", "homepage": "https://github.com/webrain/grunt-wordpress-deploy", "author": { "name": "Dario Ghilardi", diff --git a/tasks/lib/util.js b/tasks/lib/util.js index b0d2887..e48b4eb 100644 --- a/tasks/lib/util.js +++ b/tasks/lib/util.js @@ -82,12 +82,17 @@ exports.init = function (grunt) { return exclusions; }; - exports.db_adapt = function(old_url, new_url, file) { + exports.db_adapt = function(old_url, new_url, file, fn_custom_filter) { grunt.log.oklns("Adapt the database: set the correct urls for the destination in the database."); var content = grunt.file.read(file); var output = exports.replace_urls(old_url, new_url, content); - + + if (fn_custom_filter) { + grunt.log.oklns("Applying custom filter to database."); + output = fn_custom_filter(output); + } + grunt.file.write(file, output); }; @@ -100,9 +105,10 @@ exports.init = function (grunt) { exports.replace_urls_in_serialized = function(search, replace, string) { var length_delta = search.length - replace.length; + var search_regexp = new RegExp(search, 'g'); // Replace for serialized data - var matches, length, delimiter, old_serialized_data, target_string, new_url; + var matches, length, delimiter, old_serialized_data, target_string, new_url, occurences; var regexp = /s:(\d+):([\\]*['"])(.*?)\2;/g; while (matches = regexp.exec(string)) { @@ -111,12 +117,13 @@ exports.init = function (grunt) { // If the string contains the url make the substitution if (target_string.indexOf(search) !== -1) { + occurences = target_string.match(search_regexp).length; length = matches[1]; delimiter = matches[2]; // Replace the url - new_url = target_string.replace(search, replace); - length -= length_delta; + new_url = target_string.replace(search_regexp, replace); + length -= length_delta * occurences; // Compose the new serialized data var new_serialized_data = 's:' + length + ':' + delimiter + new_url + delimiter + ';'; diff --git a/tasks/wordpressdeploy.js b/tasks/wordpressdeploy.js index c3ef240..afa692e 100644 --- a/tasks/wordpressdeploy.js +++ b/tasks/wordpressdeploy.js @@ -41,7 +41,7 @@ module.exports = function(grunt) { util.db_dump(local_options, local_backup_paths); // Search and Replace database refs - util.db_adapt(local_options.url, target_options.url, local_backup_paths.file); + util.db_adapt(local_options.url, target_options.url, local_backup_paths.file, target_options.filter); // Dump target DB util.db_dump(target_options, target_backup_paths); @@ -79,7 +79,7 @@ module.exports = function(grunt) { // Dump Target DB util.db_dump(target_options, target_backup_paths ); - util.db_adapt(target_options.url,local_options.url,target_backup_paths.file); + util.db_adapt(target_options.url,local_options.url,target_backup_paths.file, local_options.filter); // Backup Local DB util.db_dump(local_options, local_backup_paths);