Created
April 3, 2012 00:41
-
-
Save staylor/2288329 to your computer and use it in GitHub Desktop.
Debugs horribly-encoded currency symbols stored inside serialized arrays which won't deserialize
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 | |
require( '../wordpress/wp-load.php' ); | |
if ( !class_exists( 'IXR_Client' ) ) { | |
require( '../wordpress/wp-includes/class-IXR.php' ); | |
} | |
function repair_strlen( $ser ) { | |
return preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $ser ); | |
} | |
function get_raw_meta( $id, $key ) { | |
global $wpdb; | |
return $wpdb->get_var( "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$key}' AND post_id = {$id}" ); | |
} | |
$configs = $wpdb->get_results( "SELECT ID, post_name FROM wp_posts WHERE post_type = 'legacy_offer_config'" ); | |
foreach ( $configs as $config ) { | |
$region = array_shift( wp_get_object_terms( $config->ID, 'region' ) ); | |
$client = new IXR_Client( 'http://' . XMLRPC_ENDPOINT . '/xmlrpc.php' ); | |
$client->query( 'emusic.getOfferConfig', $config->post_name, $region->name ); | |
$response = $client->getResponse(); | |
if ( in_array( $region->name, array( 'US', 'CA' ) ) ) { | |
$serialized = get_raw_meta( $config->ID, 'reg_config' ); | |
if ( strstr( $serialized, '€' ) || strstr( $serialized, '£' ) ) { | |
$parsed = repair_strlen( str_replace( array( '€', '£' ), '$', $serialized ) ); | |
echo 'Paste this in: ', PHP_EOL, PHP_EOL, $parsed, PHP_EOL, PHP_EOL; | |
exit( "{$config->ID}:{$region->name} reg_config contains bad currencies" ); | |
} | |
$serialized = get_raw_meta( $config->ID, 'pitch_config' ); | |
if ( strstr( $serialized, '€' ) || strstr( $serialized, '£' ) ) { | |
$parsed = repair_strlen( str_replace( array( '€', '£' ), '$', $serialized ) ); | |
echo 'Paste this in: ', PHP_EOL, PHP_EOL, $parsed, PHP_EOL, PHP_EOL; | |
exit( "{$config->ID}:{$region->name} pitch_config contains bad currencies" ); | |
} | |
} | |
if ( !is_array( $response['registrationConfig'] ) ) { | |
$serialized = get_raw_meta( $config->ID, 'reg_config' ); | |
$value = unserialize( $serialized ); | |
if ( empty( $value ) ) | |
echo "reg_config won't deserialize" . PHP_EOL; | |
$fixed = repair_strlen( $serialized ); | |
$data = @unserialize( $fixed ); | |
if ( is_array( $data ) ) | |
echo "Can be fixed with script", PHP_EOL, PHP_EOL, $fixed, PHP_EOL, PHP_EOL; | |
exit( "{$config->ID}:{$region->name} has a broken reg config" ); | |
} | |
if ( !is_array( $response['pitchConfig'] ) ) { | |
$serialized = get_raw_meta( $config->ID, 'pitch_config' ); | |
$value = unserialize( $serialized ); | |
if ( empty( $value ) ) | |
echo "pitch_config won't deserialize" . PHP_EOL; | |
$fixed = repair_strlen( $serialized ); | |
$data = @unserialize( $fixed ); | |
if ( is_array( $data ) ) | |
echo "Can be fixed with script", PHP_EOL, PHP_EOL, $fixed, PHP_EOL, PHP_EOL; | |
exit( "{$config->ID}:{$region->name} has a broken pitch config" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment