Last active
August 26, 2024 22:57
-
-
Save boogah/8209b99cddcb5bb4024382c011745da9 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Auto-Update Enabler | |
* | |
* Decisions, not options. Inspired by Trac ticket #58389 by | |
* Jos Velasco and an offhanded comment on the fedi by Andy | |
* Fragen. | |
* | |
* @package Auto-Update Enabler | |
* @author Jason Cosper <[email protected]> | |
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ | |
* @link https://gist.github.com/boogah/8209b99cddcb5bb4024382c011745da9 | |
* | |
* @wordpress-plugin | |
* Plugin Name: Auto-Update Enabler | |
* Version: 1.0 | |
* Requires at least: 6.0 | |
* Requires PHP: 7.4 | |
* Author: Jason Cosper | |
* Author URI: https://jasoncosper.com/ | |
* License: GPL-2.0+ | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt | |
* Description: Enables auto-updates for plugins by default when plugins are installed or activated. | |
* Gist Plugin URI: https://gist.github.com/boogah/8209b99cddcb5bb4024382c011745da9 | |
*/ | |
// If this file is called directly, abort. | |
if (! defined('WPINC') ) { | |
die; | |
} | |
// Add an action hook during the plugin installation process. | |
add_action('upgrader_process_complete', 'auto_update_enabler_install_plugin', 10, 2); | |
/** | |
* upgrader_process_complete hook callback function. | |
* | |
* @param WP_Upgrader $upgrader WP_Upgrader instance. Might be a Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance. | |
* @param array $hook_extra Array of bulk item update data. | |
*/ | |
function auto_update_enabler_install_plugin($upgrader, $hook_extra) | |
{ | |
if ($hook_extra['action'] == 'install' && $hook_extra['type'] == 'plugin') { | |
$plugin_path = $upgrader->plugin_info(); | |
// Check if the auto_update_plugins option exists. | |
if (!get_option('auto_update_plugins')) { | |
add_option('auto_update_plugins', array()); | |
} | |
// Add the plugin to the auto-update list. | |
$current = get_option('auto_update_plugins', array()); | |
if (!in_array($plugin_path, $current, true)) { | |
$current[] = $plugin_path; | |
// Update the auto-update plugins option. | |
update_option('auto_update_plugins', $current); | |
} | |
} | |
} | |
// Add an action hook during the plugin activation process. | |
add_action( 'activate_plugin', 'auto_update_enabler_activate_plugin', 10, 2 ); | |
/** | |
* activate_plugin hook callback function. | |
* | |
* @param string $plugin The plugin file. | |
* @param bool $network Whether the plugin is being activated network-wide. | |
*/ | |
function auto_update_enabler_activate_plugin( $plugin, $network ) { | |
// Check if the plugin is activated in the Plugins > Add New section. | |
if ( ! $network && is_admin() && isset( $_GET['plugin'] ) && isset( $_GET['_wpnonce'] ) ) { | |
// Verify the security nonce. | |
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'activate-plugin_' . $_GET['plugin'] ) ) { | |
return; | |
} | |
// Check if the $_GET variables are set. | |
if ( ! isset( $_GET['plugin'] ) || ! isset( $_GET['_wpnonce'] ) ) { | |
return; | |
} | |
// Check if the auto_update_plugins option exists. | |
if ( ! get_option( 'auto_update_plugins' ) ) { | |
add_option( 'auto_update_plugins', array() ); | |
} | |
// Add the plugin to the auto-update list. | |
$current = get_option( 'auto_update_plugins', array() ); | |
$current[] = $plugin; | |
// Update the auto-update plugins option. | |
update_option( 'auto_update_plugins', $current ); | |
} | |
} | |
/** | |
* Enable auto-updates for a plugin. | |
* | |
* @param string $plugin The plugin file. | |
*/ | |
function auto_update_enabler_plugin( $plugin ) { | |
// Do nothing if the auto_update_plugins option does not exist. | |
if ( ! get_option( 'auto_update_plugins' ) ) { | |
return; | |
} | |
// Check if the plugin is already in the auto-update list. | |
$current = get_option( 'auto_update_plugins', array() ); | |
if ( in_array( $plugin, $current, true ) ) { | |
return; | |
} | |
// Add the plugin to the auto-update list. | |
$current[] = $plugin; | |
// Update the auto-update plugins option. | |
update_option( 'auto_update_plugins', $current ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment