Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Plugin to simulate plugin/theme update failure for testing Rollback Update Failure feature plugin.
<?php
/**
* Rollback Update Testing
*
* @package rollback-update-testing
* @author Andy Fragen <andy@thefragens.com>
* @license MIT
*/
/**
* Plugin Name: Rollback Update Testing
* Plugin URI: https://gist.github.com/afragen/80b68a6c8826ab37025b05d4519bb4bf
* Description: This plugin is used for Rollback Update Failure feature plugin testing to randomly simulate an update failure.
* Version: 0.4.1
* Author: WordPress Core Contributors
* License: MIT
* Requires at least: 5.2
* Requires PHP: 5.6
* Gist Plugin URI: https://gist.github.com/afragen/80b68a6c8826ab37025b05d4519bb4bf
*/
// add_filter( 'upgrader_install_package_result', function() {
// return new WP_Error( 'simulated_error', 'Simulated Error' );
// });
add_filter(
'upgrader_install_package_result',
function( $result, $hook_extra ) {
$transient = get_site_transient( 'update_plugins' );
if ( isset( $transient->response ) && is_array( $transient->response ) && count( $transient->response ) >= 1 ) {
$plugin_to_fail = array_keys( $transient->response )[ random_int( 0, count( $transient->response ) - 1 ) ];
if ( isset( $hook_extra['plugin' ] ) && $plugin_to_fail === $hook_extra['plugin'] ) {
return new WP_Error( 'simulated_error', 'Simulated Error' );
}
}
return $result;
},
10,
2
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment