<?php
/**
 * Name: Display Active Plugins First
 * Description: Show active plugins at the top of plugins list separated from inactive plugins for easier management and quick access to enabled plugins needing updates
 * Category: Admin
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Managing 50+ Plugin Installation",
 *     "description": "On a multisite network with dozens of plugins, quickly locate active plugins without scrolling through a long list of inactive test plugins."
 *   },
 *   {
 *     "title": "Agency Client Sites",
 *     "description": "When maintaining multiple client sites, instantly see which plugins are currently running versus archived ones you keep for reference."
 *   },
 *   {
 *     "title": "Plugin Testing Workflow",
 *     "description": "Developers testing various plugins can easily distinguish between production-active plugins and experimental ones awaiting evaluation."
 *   }
 * ]
 * Added: 2025-06-15
 * Tags: plugins, admin, sorting, organization, active
 * WhereToFind: {
 *   "location": "Plugins Page",
 *   "url": "plugins.php",
 *   "instructions": "Active plugins will appear at the top of the list, separated from inactive ones",
 *   "newTab": false
 * }
 */

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

class SwitchboardActivePluginsFirst {

    public function __construct() {
        add_filter('all_plugins', [$this, 'sort_plugins'], 999);
        add_action('pre_current_active_plugins', [$this, 'sort_plugins_list']);
    }

    /**
     * Sort plugins array with active plugins first
     *
     * @param array $plugins Array of plugin data
     * @return array Sorted plugins array
     */
    public function sort_plugins($plugins) {
        if (!is_array($plugins)) {
            return $plugins;
        }

        $active = [];
        $inactive = [];

        foreach ($plugins as $plugin_file => $plugin_data) {
            if (is_plugin_active($plugin_file)) {
                $active[$plugin_file] = $plugin_data;
            } else {
                $inactive[$plugin_file] = $plugin_data;
            }
        }

        return array_merge($active, $inactive);
    }

    /**
     * Sort the plugins list table items
     *
     * @return void
     */
    public function sort_plugins_list() {
        global $wp_list_table;

        if (!is_object($wp_list_table)) {
            return;
        }

        if (isset($wp_list_table->items)) {
            $wp_list_table->items = $this->sort_plugins($wp_list_table->items);
        }
    }

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

$switchboard_instance = new SwitchboardActivePluginsFirst();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
