Forked from deltafactory/fix-nextgen-lightbox-path.php
Last active
August 29, 2015 14:01
-
-
Save DavidGoodwin/a6d5e0e4739505432305 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
INSTRUCTIONS: Use at your own risk. Backup your database before continuing. | |
1. Copy code to a file in the root of your website. | |
2. Change YOUR_SITE_ROOT to a value that makes sense. | |
3. Execute by visiting the page. Verify before/after results. | |
4. Change MAKE_CHANGES_TO_SITE to true and execute it again. | |
*/ | |
/*** Change this to the URL of your Wordpress installation, without the trailing slash. ***/ | |
define( 'YOUR_SITE_ROOT', 'http://new.example.domain' ); | |
/*** When the results have been confirmed, change this to true to modify data. ***/ | |
define( 'MAKE_CHANGES_TO_SITE', false); | |
/*** No need to edit below here. ***/ | |
require( 'wp-load.php' ); // Load the WordPress environment | |
header( 'Content-type: text/plain' ); // Set text output for easier debugging/readability. | |
/* ngg gallery stores settings in the wp_posts table under these titles - all of type lightbox_gallery - update them all */ | |
fix_ngg('lightbox'); | |
fix_ngg('fancybox'); | |
fix_ngg('thickbox'); | |
fix_ngg('shutter2'); | |
fix_ngg('highslide'); | |
fix_ngg('none'); | |
function _fix_url($field, $base_url) { | |
/* hopefully this matches all domain name(s). Obviously doesn't catch e.g http://foo.com/blog */ | |
return preg_replace('!http://([A-Z-90-9\.\-]+)/wp-!iU', $base_url . '/wp-', $field); | |
} | |
function fix_ngg($post_title) { | |
global $wpdb; | |
// Find the row in question, if it exists. | |
$post = $wpdb->get_row( | |
"SELECT ID, post_title, post_content | |
FROM $wpdb->posts | |
WHERE post_type='lightbox_library' AND post_title='$post_title'" | |
); | |
if ( !$post ) | |
exit( 'Lightbox library post not found.' ); | |
// Decode blob to proper object. Done twice to avoid shared reference. | |
$orig_data = $post->post_content; | |
$data_before = json_decode( base64_decode( $post->post_content ) ); | |
$data = json_decode( base64_decode( $post->post_content ) ); | |
$base_url = untrailingslashit( YOUR_SITE_ROOT ); | |
foreach($data->values as $property => $value) { | |
//(array('nextgen_lightbox_blank_img_url', 'nextgen_lightbox_close_btn_url', 'nextgen_lightbox_btn_prev_url', 'nextgen_lightbox_btn_next_url', 'nextgen_lightbox_loading_img_url') as $key) { | |
$data->values->$property = _fix_url($value, $base_url); | |
} | |
foreach(array('css_stylesheets', 'scripts') as $key) { | |
$data->$key = _fix_url($data->$key, $base_url); | |
} | |
$post_content = $post_content_filtered = base64_encode( json_encode( $data ) ); | |
$new_data = $post_content; | |
echo "\n\n"; | |
if ( MAKE_CHANGES_TO_SITE ) { | |
$wpdb->update( $wpdb->posts, compact( 'post_content', 'post_content_filtered' ), array( 'ID' => $post->ID ) ); | |
echo '*** Done.'; | |
} else { | |
echo '*** No data is being changed. Verify the results and change the constant "MAKE_CHANGES_TO_SITE" to "true" and re-run the script to commit.'; | |
} | |
echo "\n\n"; | |
if($orig_data == $new_data ) { | |
echo " No change for {$title} \n"; | |
} | |
else { | |
echo "Before:\n"; | |
print_r( $data_before ); | |
echo "\nAfter:\n"; | |
print_r( $data ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now converts more variables and does all post types.
Also only output's stuff if the data has changed.