Skip to content

Instantly share code, notes, and snippets.

@antonlukin
Created May 12, 2022 15:10
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 antonlukin/56f04f17f73732eadba3008c83ad6218 to your computer and use it in GitHub Desktop.
Save antonlukin/56f04f17f73732eadba3008c83ad6218 to your computer and use it in GitHub Desktop.
Fix a race condition in alloptions caching
<?php
/**
* Fix a race condition in alloptions caching
*
* @link https://core.trac.wordpress.org/ticket/31245
* @link https://github.com/Automattic/vip-go-mu-plugins-built/blob/master/misc.php#L75
*/
function _knife_maybe_clear_alloptions_cache( $option ) {
if ( ! wp_installing() ) {
$alloptions = wp_load_alloptions(); //alloptions should be cached at this point
if ( isset( $alloptions[ $option ] ) ) { //only if option is among alloptions
wp_cache_delete( 'alloptions', 'options' );
}
}
}
add_action( 'added_option', '_knife_maybe_clear_alloptions_cache' );
add_action( 'updated_option', '_knife_maybe_clear_alloptions_cache' );
add_action( 'deleted_option', '_knife_maybe_clear_alloptions_cache' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment