Forked from kemenaran/colibriwp-serialization-fix.php
Created
November 4, 2020 23:47
-
-
Save seasox/997df5bb6b78c918475c75fec886eebe to your computer and use it in GitHub Desktop.
Script to fix ColibriWP serialization errors
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 | |
/** | |
* Plugin Name: ColibriWP Fix Options | |
* | |
*/ | |
function fix_str_length($matches) { | |
$string = $matches[2]; | |
$right_length = strlen($string); // yes, strlen even for UTF-8 characters, PHP wants the mem size, not the char count | |
return 's:' . $right_length . ':"' . $string . '";'; | |
} | |
function extendthemes_fix_serialized($string) | |
{ | |
// securities | |
if (!preg_match('/^[aOs]:/', $string)) return $string; | |
if (@unserialize($string) !== false) return $string; | |
$string = preg_replace("%\n%", "", $string); | |
// doublequote exploding | |
$data = preg_replace('%";%', "µµµ", $string); | |
$tab = explode("µµµ", $data); | |
$new_data = ''; | |
foreach ($tab as $line) { | |
$new_data .= preg_replace_callback('%\bs:(\d+):"(.*)%', 'fix_str_length', $line); | |
} | |
return $new_data; | |
} | |
function get_option_raw($option) { | |
global $wpdb; | |
$suppress = $wpdb->suppress_errors(); | |
$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); | |
$wpdb->suppress_errors( $suppress ); | |
return $row->option_value; | |
} | |
add_action('init', function () { | |
if (isset($_REQUEST['colibriwp-try-fix-serialization'])) { | |
$x = get_option_raw( 'extend_builder_theme'); | |
if (is_string($x)) { | |
$x = preg_replace('#\r?\n#', " ", $x); | |
$x = extendthemes_fix_serialized($x); | |
$y = unserialize($x); | |
if(is_array($y)){ | |
update_option('extend_builder_theme', $y); | |
var_dump($y); | |
} else { | |
wp_die( 'Au au' ); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment