last_error = ''; $old = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", $key ) ); if ( '' !== $wpdb->last_error ) { throw new \RuntimeException( 'Cannot read plugin state: ' . $key ); } $current = null === $old ? $default : maybe_unserialize( $old ); $next = $transform( $current ); $value = maybe_serialize( $next ); if ( null !== $old && (string) $value === $old ) { self::clear_cache( $key ); return $next; } if ( null === $old ) { $changed = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->options} (option_name, option_value, autoload) VALUES (%s, %s, 'no')", $key, $value ) ); } else { $changed = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->options} SET option_value = %s WHERE option_name = %s AND BINARY option_value = %s", $value, $key, $old ) ); } if ( false === $changed ) { throw new \RuntimeException( 'Cannot persist plugin state: ' . $key ); } if ( 1 === $changed ) { self::clear_cache( $key ); return $next; } } throw new \RuntimeException( 'Plugin state is busy; retry this operation: ' . $key ); } }