Skip to content

Instantly share code, notes, and snippets.

@kemenaran
Last active February 16, 2022 11:01
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kemenaran/a9a52d516ac556a57058fab7dcc8f92b to your computer and use it in GitHub Desktop.
Script to fix ColibriWP serialization errors
<?php
/**
* Plugin Name: ColibriWP Fix Options
*
* How to use:
*
* 1. Put this file inside the wp-content/plugins/ directory;
* 2. In the Wordpress Dashboard, navigate to the Extensions page;
* 3. Enable the "ColibriWP Fix Options" extension;
* 4. Load any page with a `colibriwp-try-fix-serialization` GET parameter.
* For instance, `https://www.your-wordpress-site.com/index.php?colibriwp-try-fix-serialization=true`
*/
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' );
}
}
}
});
@kemenaran
Copy link
Author

This script fixes an error related to ColibriWP extend_builder_theme setting.

The error looks like:

Warning: array_replace_recursive() [function.array-replace-recursive]: Argument #2 is not an array in /var/www/public_html/wp-content/plugins/colibri-page-builder/extend-builder/data/theme-data.php on line 150

To use:

  1. Put this file inside the wp-content/plugins/ directory;
  2. In the Wordpress Dashboard, navigate to the Extensions page;
  3. Enable the "ColibriWP Fix Options" extension.

This will fix the serialization issue.

Thanks to ColibriWP support for the details; you're the best 💚

@tealord
Copy link

tealord commented Oct 9, 2020

Hey @kemenaran,

currently I am facing the same problem and ran across your fix. Sadly the solution didn't work for me. Do you have any idea?

I placed your PHP file in the plugins directory and activated it. But as soon as I enable the colibri-page-builder afterwards, the website crashes with the same error.

Thank you for any advice.

@kemenaran
Copy link
Author

kemenaran commented Oct 11, 2020

@tealord Mmm, reading the code, I may have forgotten a step in the instructions: you may have to load a page (any page) with the GET or POST parameter colibriwp-try-fix-serialization.

For instance, does it work better if you:

  1. Enable the "ColibriWP Fix Options" extension.
  2. Load any page with a colibriwp-try-fix-serialization GET parameter; for instance www.your-wordpress-site.com/index.php?colibriwp-try-fix-serialization=true

?

@tealord
Copy link

tealord commented Oct 11, 2020

@kemenaran, thank you for your reply. I disabled the page-builder plugin and tried to load the page with the GET parameter. But the Site keeps crashing if I enable the plugin afterwards. I also tried to load the page via command line like this:

php -r '$_GET["colibriwp-try-fix-serialization"]="true"; require_once("./index.php");'

Still no success. I checked that the code is getting executed by putting a wp_die behind the if condition. Do you have another Idea what I might have to check?

@kemenaran
Copy link
Author

Hmm, in this case I'm running out of ideas :) Sorry about this.

@seasox
Copy link

seasox commented Nov 4, 2020

Hey @kemenaran,

thanks for your work on this gist. @tealord and I met some weeks ago to tackle his problem. Together, we debugged your script and came up with a solution which seems to work on WordPress 5.5.*. The issue here seems to be that get_option() does some extensive checks and reasoning about its return type and value, so the is_string($x) check fails.
I've forked your gist and implemented a workaround by adding a function get_option_raw which basically does the same as WordPress's get_option, but skips the reasoning and just returns whatever the database gives us. I also stoleborrowed @chlp's fix_str_length function from their gist.

You can check out my patch here and use it to update this gist if you like: https://gist.github.com/seasox/997df5bb6b78c918475c75fec886eebe

@kemenaran
Copy link
Author

@seasox thanks! I updated the gist, and added some usage instructions above.

@ivansammartino
Copy link

I have the same issue after migrating a website to another server.
Since I couldn't access admin in the new server due to the error appearing in every webpage, I put the fix in the old server and followed the instructions, then I copied DB and files to the new one. Still the error persists: any idea, please?

@kemenaran
Copy link
Author

@ivansammartino er, I'm not sure… If on your new server you move the wp-content/plugins/colibri directory somewhere else (like to wp-content/plugins-disabled/), this will probably allow you to regain access to the admin console on the new server. Then you can try to apply the script directly to your new server maybe?

@douce2323
Copy link

Jai presque le même problème est je n'arrive pas a le résoudre je sais que c'est le plugin colibri que pose problème:
Fatal error: Uncaught TypeError: array_replace_recursive(): Argument #2 must be of type array, null given in C:\xampp\htdocs\medic\wp-content\plugins\colibri-page-builder\extend-builder\data\theme-data.php:213 Stack trace: #0 C:\xampp\htdocs\medic\wp-content\plugins\colibri-page-builder\extend-builder\data\theme-data.php(213): array_replace_recursive(Array, NULL) #1 C:\xampp\htdocs\medic\wp-content\plugins\colibri-page-builder\extend-builder\data\theme-data.php(143): ExtendBuilder\get_theme_data() #2 C:\xampp\htdocs\medic\wp-content\plugins\colibri-page-builder\extend-builder\data\theme-data.php(41): ExtendBuilder\get_plugin_option('css_by_partials...') #3 C:\xampp\htdocs\medic\wp-content\plugins\colibri-page-builder\extend-builder\data\data.php(254): ExtendBuilder\get_colibri_options() #4 C:\xampp\htdocs\medic\wp-content\plugins\colibri-page-builder\extend-builder\data\theme-data.php(199): ExtendBuilder\get_current_data(-1, true) #5 C:\xampp\htdocs\medic\wp-content\plugins\colibri-page-builder\extend-builder\data\theme-data.php(224): ExtendBuilder\get_theme_data('colors', true, Array) #6 C:\xampp\htdocs\medic\wp-content\plugins\colibri-page-builder\extend-builder\gutenberg.php(6): ExtendBuilder\get_current_theme_data('colors', Array) #7 C:\xampp\htdocs\medic\wp-includes\class-wp-hook.php(307): ExtendBuilder{closure}('') #8 C:\xampp\htdocs\medic\wp-includes\class-wp-hook.php(331): WP_Hook->apply_filters(NULL, Array) #9 C:\xampp\htdocs\medic\wp-includes\plugin.php(474): WP_Hook->do_action(Array) #10 C:\xampp\htdocs\medic\wp-settings.php(565): do_action('after_setup_the...') #11 C:\xampp\htdocs\medic\wp-config.php(98): require_once('C:\xampp\htdocs...') #12 C:\xampp\htdocs\medic\wp-load.php(50): require_once('C:\xampp\htdocs...') #13 C:\xampp\htdocs\medic\wp-admin\admin.php(34): require_once('C:\xampp\htdocs...') #14 C:\xampp\htdocs\medic\wp-admin\index.php(10): require_once('C:\xampp\htdocs...') #15 {main} thrown in C:\xampp\htdocs\medic\wp-content\plugins\colibri-page-builder\extend-builder\data\theme-data.php on line 213
si une personne aurais une idéé

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment