Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin/theme auto-update improvements #97

Merged
merged 10 commits into from
May 24, 2021
2 changes: 1 addition & 1 deletion bin/sizes-manifest.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions inc/RestApi/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,20 @@ public function update_item( $request ) {
update_option( 'allow_minor_auto_core_updates', $new_value );
break;
case 'autoUpdatesPlugins':
// Keep the WordPress Core setting in sync.
if ( $new_value ) {
bh_sync_plugin_update_settings();
}

$new_value = ( $new_value ) ? 'true' : 'false';
update_option( 'auto_update_plugin', $new_value );
break;
case 'autoUpdatesThemes':
// Keep the WordPress Core setting in sync.
if ( $new_value ) {
bh_sync_theme_update_settings();
}

$new_value = ( $new_value ) ? 'true' : 'false';
update_option( 'auto_update_theme', $new_value );
break;
Expand Down
152 changes: 148 additions & 4 deletions inc/updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ function bh_auto_update_configure() {
if ( ! defined( 'AUTOMATIC_UPDATER_DISABLED' ) || AUTOMATIC_UPDATER_DISABLED === false ) {
if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) {
switch ( WP_AUTO_UPDATE_CORE ) {
case true:
$settings['allow_major_auto_core_updates'] = true;
$settings['allow_minor_auto_core_updates'] = true;
break;
case false:
$settings['allow_major_auto_core_updates'] = false;
$settings['allow_minor_auto_core_updates'] = false;
Expand Down Expand Up @@ -198,6 +194,154 @@ function bh_sync_plugin_major_auto_core_update_option( $old_value, $value ) {

add_action( 'update_option_auto_update_core_major', 'bh_sync_plugin_major_auto_core_update_option', 10, 2 );


/**
* Ensures all installed plugins are set to auto-update.
*
* This makes sure that all installed plugins are listed in the WordPress Core `auto_update_plugins` option. This
* ensures that when the plugin is deactivated, WordPress will continue to auto-update plugins unless turned off.
*
* When the option to auto-update plugins is turned off in the plugin, auto-updates are managed through the default
* WordPress core UI.
*
* @return bool True if the value was updated, false otherwise.
*/
function bh_sync_plugin_update_settings() {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

return update_site_option( 'auto_update_plugins', array_keys( get_plugins() ) );
}

/**
* Ensures all installed themes are set to auto-update.
*
* This makes sure that all installed themes are listed in the WordPress Core `auto_update_themes` option. This
* ensures that when the plugin is deactivated, WordPress will continue to auto-update themes unless turned off.
*
* When the option to auto-update themes is turned off in the plugin, auto-updates are managed through the default
* WordPress core UI.
*
* @return bool True if the value was updated, false otherwise.
*/
function bh_sync_theme_update_settings() {
if ( ! function_exists( 'wp_get_themes' ) ) {
require_once ABSPATH . WPINC . '/theme.php';
}

return update_site_option( 'auto_update_themes', array_keys( wp_get_themes() ) );
}

/**
* Updates the WordPress Core plugin auto-update option when a plugin is deleted.
*
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
* @param bool $deleted Whether the plugin deletion was successful.
*/
function bh_deleted_plugin( $plugin_file, $deleted ) {
if ( ! $deleted ) {
return;
}

// When the option is disabled in the plugin, plugin auto-updates are manually managed in the WordPress Core UI.
if ( 'false' === get_site_option( 'auto_update_plugin', 'true' ) ) {
return;
}

bh_sync_plugin_update_settings();
}

add_action( 'deleted_plugin', 'bh_deleted_plugin', 10, 2 );

/**
* Updates the WordPress Core theme auto-update option when a theme is deleted.
*
* @param string $stylesheet Stylesheet of the theme to delete.
* @param bool $deleted Whether the theme deletion was successful.
*/
function bh_deleted_theme( $stylesheet, $deleted ) {
if ( ! $deleted ) {
return;
}

// When the option is disabled in the plugin, theme auto-updates are manually managed in the WordPress Core UI.
if ( 'false' === get_site_option( 'auto_update_theme', 'true' ) ) {
return;
}

bh_sync_theme_update_settings();
}

add_action( 'deleted_theme', 'bh_deleted_plugin', 10, 2 );

/**
* Updates the WordPress Core theme auto-update option when a theme is deleted.
*
* This is a backwards compatibility version of `bh_deleted_theme`, which uses the `deleted_theme` action hook that was
* added in WordPress 5.8.0.
*/
function bh_delete_site_transient_update_themes() {
global $wp_version;

// The `deleted_theme` hook was introduced in WordPress 5.8.
if ( version_compare( $wp_version, '5.8.0', '>=' ) ) {
return;
}

// When the option is disabled in the plugin, theme auto-updates are manually managed in the WordPress Core UI.
if ( 'false' === get_site_option( 'auto_update_theme', 'true' ) ) {
return;
}

bh_sync_theme_update_settings();
}

add_action( 'delete_site_transient_update_themes', 'bh_delete_site_transient_update_themes', 10, 0 );

/**
* Updates the WordPress Core options for plugin and theme auto-updates when one is updated.
*
* @param WP_Upgrader $upgrader WP_Upgrader instance. In other contexts, $this, might be a
* Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
* @param array $hook_extra {
* Array of bulk item update data.
*
* @type string $action Type of action. Default 'update'.
* @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
* @type bool $bulk Whether the update process is a bulk update. Default true.
* @type array $plugins Array of the basename paths of the plugins' main files.
* @type array $themes The theme slugs.
* @type array $translations {
* Array of translations update data.
*
* @type string $language The locale the translation is for.
* @type string $type Type of translation. Accepts 'plugin', 'theme', or 'core'.
* @type string $slug Text domain the translation is for. The slug of a theme/plugin or
* 'default' for core translations.
* @type string $version The version of a theme, plugin, or core.
* }
* }
*/
function bh_upgrader_process_complete( $upgrader, $hook_extra ) {
0aveRyan marked this conversation as resolved.
Show resolved Hide resolved
if ( ! in_array( $hook_extra['type'], array( 'plugin', 'theme' ), true ) ) {
return;
}

// When the option is disabled in the plugin, this is manually managed in the WordPress Core UI.
if ( 'false' === get_site_option( "auto_update_{$hook_extra['type']}", 'true' ) ) {
return;
}

if ( 'plugin' === $hook_extra['type'] ) {
bh_sync_plugin_update_settings();
} else {
bh_sync_theme_update_settings();
}
}

add_action( 'upgrader_process_complete', 'bh_upgrader_process_complete', 10, 2 );

/**
* When upgrading from < 5.6, sync the plugin option with Core's new option.
*
Expand Down
24 changes: 24 additions & 0 deletions inc/upgrades/2.7.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Handle updates for version 2.7.0.
*
* Sync the plugin's auto-update settings with the new, WordPress Core options.
*
* @package Bluehost
*/

if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

$auto_update_plugins = get_option( 'auto_update_plugin', 'true' );

if ( 'true' === $auto_update_plugins ) {
update_site_option( 'auto_update_plugins', array_keys( get_plugins() ) );
}

$auto_update_themes = get_option( 'auto_update_theme', 'true' );

if ( 'true' === $auto_update_themes ) {
update_site_option( 'auto_update_themes', array_keys( wp_get_themes() ) );
}
24 changes: 10 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.