Is it possible to disable caching of an option when using w3 total cache?

a plugin i’m working on has a conflict with w3 total cache. To be more specific, in my code i have

static function get_instance()
{
    if( self::$_instance === NULL ) {
        // get the settings from the database
        self::$_instance = get_option( 'ai1ec_settings' );

        // if there are no settings in the database
        // save default values for the settings
        if( ! self::$_instance ) {
            self::$_instance = new self();
            delete_option( 'ai1ec_settings' );
            add_option( 'ai1ec_settings', self::$_instance );
        } else {
            self::$_instance->set_defaults(); // set default settings
        }
    }

    return self::$_instance;
}

The problem lies in self::$_instance = get_option( 'ai1ec_settings' ); because un incomplete object is returned (this happens only when using w3 total cache, i have declared an autoloader, but doing some debugging it seems that the incomplete object is stored in the cache)

This is the error message

Ai1ec_Settings::get_instance(): The script tried to execute a method
or access a property of an incomplete object. Please ensure that the
class definition “Ai1ec_Settings” of the object you are trying to
operate on was loaded before unserialize() gets called or provide a
__autoload() function to load the class definition

1) Is there a way to tell w3 total cache not to cache that option?
2) If not, it would be correct to handle things like this?

try {
    self::$_instance = self::getOptionsFromDb();
} catch (Exception $e) {
    //Show a friendly error page
}

private static function getOptionsFromDb(){
    $ai1ec_settings =  get_option( 'ai1ec_settings' );
    if ( ! $ai1ec_settings instanceof self ) {
        if (false === $ai1ec_settings) {
            return false; //option was not set in the db
        } elseif( class_exists('W3_Plugin_TotalCacheAdmin') ){
            //let's clear thew cache and try again
            $plugin_totalcacheadmin = & w3_instance('W3_Plugin_TotalCacheAdmin');
            $plugin_totalcacheadmin->flush_all();
            $ai1ec_settings =  get_option( 'ai1ec_settings' );
            if ( $ai1ec_settings instanceof self ) {
                return $ai1ec_settings;
            } else {
                throw new Exception();
            }
        } else {
            throw new Exception();
        }
    }   
}

2 s
2

okay, here we go. try this: in your functions.php modify the behaviour of ai1ec_options:

$ai1ec_options = get_option('ai1ec_options');
delete_option('ai1ec_options');
add_option('ai1ec_options', $ai1ec_options, '', 'no'); // thanks for your suggestion :)

be careful though, as you might lose your set options, so be sure to get them from the database first. or create a backup option. afterwards, clear the caches and try again.

Leave a Comment