Created
August 31, 2022 07:25
-
-
Save pramodjodhani/121601cea82f1748fe5106e7ac0b98f0 to your computer and use it in GitHub Desktop.
WordPress force perform plugin updates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
This function check for plugin udpates, if there are any present then | |
will automatically update the plugins. | |
*/ | |
function perform_updates() { | |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php'; | |
require_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php'; | |
require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php'; | |
require_once ABSPATH . 'wp-admin/includes/file.php'; | |
wp_clean_update_cache(); | |
wp_update_plugins(); | |
$plugin_updates = get_site_transient( 'update_plugins' ); | |
if ( ! $plugin_updates || empty( $plugin_updates->response ) ) { | |
// No updates. | |
return; | |
} | |
$plugins_updated = array(); | |
$plugins = get_plugins(); | |
$skin = new Automatic_Upgrader_Skin; | |
$upgrader = new Plugin_Upgrader( $skin ); | |
$context = WP_PLUGIN_DIR; // We don't support custom Plugin directories, or updates for WPMU_PLUGIN_DIR. | |
foreach ( $plugin_updates->response as $plugin => $item ) { | |
$upgrader_item = $item->plugin; | |
$plugins_updated[] = $item->plugin; | |
$plugin_data = get_plugin_data( $context . '/' . $upgrader_item ); | |
$item_name = $plugin_data['Name']; | |
// Add the current version so that it can be reported in the notification email. | |
$item->current_version = $plugin_data['Version']; | |
if ( empty( $item->current_version ) ) { | |
$item->current_version = false; | |
} | |
/* translators: %s: Plugin name. */ | |
$skin->feedback( __( 'Updating plugin: %s' ), $item_name ); | |
$allow_relaxed_file_ownership = false; | |
$upgrade_result = $upgrader->upgrade( | |
$upgrader_item, | |
array( | |
'clear_update_cache' => false, | |
// Always use partial builds if possible for core updates. | |
'pre_check_md5' => false, | |
// Only available for core updates. | |
'attempt_rollback' => true, | |
// Allow relaxed file ownership in some scenarios. | |
'allow_relaxed_file_ownership' => $allow_relaxed_file_ownership, | |
) | |
); | |
// Activate the plugin. | |
$current = get_option( 'active_plugins', array() ); | |
$current[] = $upgrader_item; | |
sort( $current ); | |
update_option( 'active_plugins', $current ); | |
} | |
return $plugins_updated; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment