<?php
/**
 * Name: Disable All Updates
 * Description: Completely disable all WordPress core, plugin, and theme automatic updates from any source including premium plugins to maintain full version control and stability
 * Category: Security
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Lock Production Environment",
 *     "description": "Prevent accidental updates on production sites where changes should only come through controlled deployments."
 *   },
 *   {
 *     "title": "Maintain Compatibility",
 *     "description": "Keep specific versions of WordPress, themes, and plugins that you have tested and know work together, regardless of update source."
 *   }
 * ]
 * Added: 2025-07-08
 * Tags: updates, disable, notifications, core, plugins, themes, premium
 * WhereToFind: {
 *   "location": "Works automatically",
 *   "url": "",
 *   "instructions": "All update checks and notifications disabled",
 *   "newTab": false
 * }
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

class SwitchboardDisableAllUpdates {

    public function __construct() {
        // Disable automatic updates completely
        add_filter('auto_update_core', '__return_false');
        add_filter('auto_update_plugin', '__return_false');
        add_filter('auto_update_theme', '__return_false');
        add_filter('automatic_updater_disabled', '__return_true');

        // Disable update checks by returning fake "no updates" data
        add_filter('pre_site_transient_update_core', [$this, 'disable_core_updates']);
        add_filter('pre_site_transient_update_plugins', [$this, 'disable_plugin_updates']);
        add_filter('pre_site_transient_update_themes', [$this, 'disable_theme_updates']);

        // Disable update cron events
        add_filter('wp_get_update_data', [$this, 'remove_update_counts']);

        // Block manual update capabilities
        add_filter('user_has_cap', [$this, 'disable_update_capabilities'], 10, 4);

        // Block file modifications (updates require file writes)
        add_filter('file_mod_allowed', [$this, 'disable_file_mods'], 10, 2);

        // Remove update notifications
        add_action('admin_init', [$this, 'remove_update_notifications'], 1);
        add_filter('update_footer', '__return_empty_string', 11);

        // Remove update nag
        remove_action('admin_notices', 'update_nag', 3);
        remove_action('network_admin_notices', 'update_nag', 3);

        // Hide all update UI elements globally
        add_action('admin_head', [$this, 'hide_update_ui']);
        add_action('admin_head', [$this, 'hide_all_update_buttons']);
    }

    /**
     * Register data locations for cleanup system
     *
     * @return array Data storage locations
     */
    public function register_data_locations() {
        return [
            'options' => [],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
        ];
    }

    public function disable_core_updates($transient) {
        global $wp_version;
        return (object) [
            'last_checked' => time(),
            'version_checked' => $wp_version,
            'updates' => []
        ];
    }

    public function disable_plugin_updates($transient) {
        if (is_object($transient)) {
            $transient->response = [];
        }
        return $transient;
    }

    public function disable_theme_updates($transient) {
        if (is_object($transient)) {
            $transient->response = [];
        }
        return $transient;
    }

    public function remove_update_counts() {
        return [
            'counts' => [
                'plugins' => 0,
                'themes' => 0,
                'wordpress' => 0,
                'translations' => 0,
                'total' => 0
            ],
            'title' => ''
        ];
    }

    public function disable_update_capabilities($allcaps, $caps, $args, $user) {
        // Remove update capabilities from all users
        $allcaps['update_core'] = false;
        $allcaps['update_plugins'] = false;
        $allcaps['update_themes'] = false;
        return $allcaps;
    }

    public function disable_file_mods($file_mod_allowed, $context) {
        // Block file modifications for updates, plugins, and themes
        if (in_array($context, ['update_core', 'update_plugins', 'update_themes'])) {
            return false;
        }
        return $file_mod_allowed;
    }

    public function remove_update_notifications() {
        remove_action('admin_notices', 'update_nag', 3);
        remove_action('network_admin_notices', 'update_nag', 3);
        remove_action('admin_notices', 'maintenance_nag');
    }

    public function hide_all_update_buttons() {
        // Hide update buttons globally on all admin pages
        echo '<style>
            /* Hide all update-related UI elements */
            .plugin-update-tr,
            .theme-update-tr,
            .update-message,
            .plugin-install .update-now,
            .theme-browser .update-now,
            button.update-now,
            .update-link,
            .update-available,
            .plugin-card .update-now,
            .theme-overlay .theme-actions .update-now,
            .plugin-version-author-uri .update-now,
            tr.plugin-update-tr,
            .notice.notice-warning.notice-alt.update-message {
                display: none !important;
            }
        </style>';
    }

    public function hide_update_ui() {
        // Only apply on update pages
        $screen = get_current_screen();
        if (!$screen || ($screen->id !== 'update-core' && $screen->id !== 'update-core-network')) {
            return;
        }

        echo '<style>
            /* Hide update messages and check button on Updates page */
            .update-php,
            .update-nag,
            p.update-last-checked,
            p.auto-update-status,
            #wp-version-message,
            .core-updates .button-primary,
            .wrap .error,
            .wrap .notice-error {
                display: none !important;
            }
            /* Update page title styling */
            .wrap h1 {
                display: flex;
                align-items: center;
                gap: 12px;
            }
            .wrap h1 .switchboard-badge {
                font-size: 11px;
                padding: 4px 10px;
                margin-left: 8px;
            }
        </style>';

        // Add badge to title using JavaScript
        $badge_text = esc_html__('Updates Disabled by Switchboard', 'wpswitchboard');
        echo '<script>
            jQuery(document).ready(function($) {
                var title = $(".wrap h1").first();
                if (title.length && title.text().indexOf("WordPress Updates") !== -1) {
                    title.html(title.html().replace("- Updates Disabled", "") + \' <span class="switchboard-badge">' . esc_js($badge_text) . '</span>\');
                }
            });
        </script>';
    }
}

// Initialize and register with data system
$switchboard_instance = new SwitchboardDisableAllUpdates();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
