Skip to content

Instantly share code, notes, and snippets.

@afragen
Last active November 7, 2022 13:28
Show Gist options
  • Save afragen/80b68a6c8826ab37025b05d4519bb4bf to your computer and use it in GitHub Desktop.
Save afragen/80b68a6c8826ab37025b05d4519bb4bf to your computer and use it in GitHub Desktop.
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.5.0
* 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 ) {
// update-core.php selected.
$plugins_selected = isset( $_GET['plugins'] ) ? explode( ',', $_GET['plugins'] ) : [];
// plugins.php selected.
$plugins_selected = isset( $_POST['plugin'] ) ? explode( ',', $_POST['plugin'] ) : $plugins_selected;
$plugin_to_fail = $plugins_selected;
unset( $plugin_to_fail[ random_int( 0, count( $plugins_selected ) - 1 ) ] );
if ( isset( $hook_extra['plugin'] ) && in_array( $hook_extra['plugin'], $plugin_to_fail, true ) ) {
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