refactor(mu-plugins): surface managed core version notices through HostForge platform messaging

This commit is contained in:
HostForge Systems
2026-04-18 12:38:40 +08:00
parent 854f5a2ed5
commit 141be701a8

View File

@@ -4,7 +4,6 @@ namespace HostForgeSystems\Modules\CoreUpdates;
use HostForgeSystems\Core\Module_Interface;
use HostForgeSystems\Core\Notice;
use stdClass;
if (! defined('ABSPATH')) {
exit;
@@ -29,57 +28,14 @@ class Disable_Core_Updates implements Module_Interface
add_filter('allow_minor_auto_core_updates', '__return_false');
add_filter('allow_major_auto_core_updates', '__return_false');
add_filter('pre_option__site_transient_update_core', [$this, 'force_empty_core_updates']);
add_filter('pre_site_transient_update_core', [$this, 'force_empty_core_updates'], 999);
add_filter('site_transient_update_core', [$this, 'force_empty_core_updates'], 999);
add_filter('pre_set_site_transient_update_core', [$this, 'force_empty_core_updates'], 999);
add_action('admin_init', [$this, 'clear_core_update_transient'], 1);
add_action('admin_init', [$this, 'remove_core_update_nag'], 1);
add_action('admin_init', [$this, 'block_manual_core_upgrade'], 1);
add_action('admin_init', [$this, 'maybe_redirect_updates_screen'], 1);
add_action('init', [$this, 'disable_core_version_check'], 1);
add_filter('schedule_event', [$this, 'block_core_version_check_schedule'], 1);
add_action('admin_menu', [$this, 'remove_updates_submenu'], 999);
add_action('admin_notices', [$this, 'render_admin_notice']);
}
/**
* Return a clean empty core update object.
*
* @return \stdClass
*/
public function force_empty_core_updates(): stdClass
{
$clean = new stdClass();
$clean->updates = [];
$clean->version_checked = get_bloginfo('version');
$clean->last_checked = time();
$clean->translations = [];
return $clean;
}
/**
* Clear cached core update transient during admin requests.
*
* @return void
*/
public function clear_core_update_transient(): void
{
if (! is_admin()) {
return;
}
delete_site_transient('update_core');
wp_cache_delete('update_core', 'site-transient');
wp_cache_delete('update_core', 'site-options');
wp_cache_delete('_site_transient_update_core', 'options');
wp_cache_delete('update_core', 'options');
}
/**
* Remove the default WordPress update nag.
*
@@ -91,7 +47,34 @@ class Disable_Core_Updates implements Module_Interface
}
/**
* Redirect users away from the native Updates screen.
* Block manual WordPress core upgrade attempts.
*
* @return void
*/
public function block_manual_core_upgrade(): void
{
if (! is_admin()) {
return;
}
$action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : '';
if ($action !== 'do-core-upgrade') {
return;
}
wp_die(
esc_html__(
'WordPress core updates are disabled in wp-admin. Please use the HostForge Dashboard to manage core updates.',
'hostforge-systems'
),
esc_html__('Core Updates Managed by HostForge', 'hostforge-systems'),
['response' => 403]
);
}
/**
* Redirect users away from the native WordPress Updates screen.
*
* @return void
*/
@@ -112,46 +95,20 @@ class Disable_Core_Updates implements Module_Interface
}
/**
* Disable WordPress core version checking hooks.
*
* @return void
*/
public function disable_core_version_check(): void
{
remove_action('init', 'wp_version_check');
remove_action('admin_init', '_maybe_update_core');
remove_action('wp_version_check', 'wp_version_check');
}
/**
* Block scheduling of core version checks.
*
* @param object|false $event Scheduled event object.
* @return object|false
*/
public function block_core_version_check_schedule($event)
{
if (is_object($event) && isset($event->hook) && $event->hook === 'wp_version_check') {
return false;
}
return $event;
}
/**
* Remove Updates submenu entries.
* Remove native Updates submenu entries.
*
* @return void
*/
public function remove_updates_submenu(): void
{
remove_submenu_page('index.php', 'update-core.php');
remove_submenu_page('index.php', 'update-core');
remove_submenu_page('options-general.php', 'update-core.php');
remove_submenu_page('tools.php', 'update-core.php');
}
/**
* Render HostForge notice on selected admin screens.
* Render HostForge admin notice showing the available WordPress version.
*
* @return void
*/
@@ -171,16 +128,55 @@ class Disable_Core_Updates implements Module_Interface
'dashboard',
'plugins',
'themes',
'update-core',
'index',
];
if (! in_array($screen->id, $allowed_screens, true)) {
return;
}
$available_version = $this->get_available_core_version();
if (! $available_version) {
return;
}
Notice::render(
'<strong>HostForge Notice:</strong> WordPress core updates are managed through the HostForge Dashboard.',
sprintf(
'<strong>HostForge Notice:</strong> WordPress %s is available. Please update from the HostForge Dashboard.',
esc_html($available_version)
),
'info'
);
}
/**
* Get the available WordPress core version from the update transient.
*
* @return string|null
*/
protected function get_available_core_version(): ?string
{
$update_core = get_site_transient('update_core');
if (! is_object($update_core) || empty($update_core->updates) || ! is_array($update_core->updates)) {
return null;
}
foreach ($update_core->updates as $update) {
if (! is_object($update)) {
continue;
}
if (empty($update->response) || $update->response !== 'upgrade') {
continue;
}
if (! empty($update->current) && is_string($update->current)) {
return $update->current;
}
}
return null;
}
}