Skip to content

Instantly share code, notes, and snippets.

@iamzozo
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamzozo/9668382 to your computer and use it in GitHub Desktop.
Save iamzozo/9668382 to your computer and use it in GitHub Desktop.
<?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', 'your.domain' );
define('FROM_ROOT', 'http://localhost/');
/*** When the results have been confirmed, change this to true to modify data. ***/
define( 'MAKE_CHANGES_TO_SITE', true );
/*** If your are using multisite, set the site ID ***/
define( 'SITE_ID', 1 );
/*** 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.
fix_ngg_lightbox(); // Run the fix
function fix_ngg_lightbox() {
global $wpdb;
if(is_multisite())
switch_to_blog(SITE_ID);
// Find the row in question, if it exists.
$posts = $wpdb->get_results(
"SELECT ID, post_title, post_content
FROM $wpdb->posts
WHERE post_type='lightbox_library'"
);
foreach($posts as $post) {
if ( !$post )
exit( 'Lightbox library post not found.' );
// Decode blob to proper object. Done twice to avoid shared reference.
$data_before = json_decode( base64_decode( $post->post_content ) );
$data = json_decode( base64_decode( $post->post_content ) );
foreach($data as &$v) {
if(is_object($v) || is_array($v)) {
foreach($v as &$v2) {
$v2 = str_replace(FROM_ROOT, YOUR_SITE_ROOT, $v2);
}
}else {
$v = str_replace(FROM_ROOT, YOUR_SITE_ROOT, $v);
}
}
$post_content = $post_content_filtered = base64_encode( json_encode( $data ) );
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";
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