settings = Plugin::get_settings( 'plugins' ); if( !isset( $this->settings['active_plugins_first_enabled'] ) ) { $old_settings = Plugin::get_settings( 'updates-and-notifications' ); if( isset( $old_settings['active_plugins_first_enabled'] ) ) { $this->settings['active_plugins_first_enabled'] = $old_settings['active_plugins_first_enabled']; } } add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( empty( $this->settings['active_plugins_first_enabled'] ) ) { return; } if( is_admin() ) { add_action( 'admin_head-plugins.php', [ $this, 'sort_plugins_by_status' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); } } /** * Adds the settings field to the Plugins tab. * * @param array $fields The existing settings fields. * * @return array Modified settings fields. */ public function adminease_settings_fields( array $fields ): array { $fields['plugins']['fields'][] = [ 'type' => 'switch', 'id' => 'active-plugins-first-enabled', 'name' => 'adminease[plugins][active_plugins_first_enabled]', 'value' => $this->settings['active_plugins_first_enabled'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'label' => __( 'Display active plugins first', 'adminease' ), 'description' => __( 'Reorganizes the plugins list to show all active plugins at the top, separated from inactive plugins by a visual divider. This improves plugin management workflow, especially on sites with many plugins installed.', 'adminease' ), ]; return $fields; } /** * Sorts $wp_list_table->items so active plugins come first, then * captures the last active plugin key for the visual divider. * * Runs on admin_head-plugins.php, which fires after prepare_items() * has already built the final $wp_list_table->items array. * Only applies to the "All" view; status-specific views (Active, * Inactive, etc.) already show a single type and need no reordering. * * @return void */ public function sort_plugins_by_status(): void { global $wp_list_table, $status; if( in_array( $status, [ 'active', 'inactive', 'recently_activated', 'mustuse' ], true ) ) { return; } uksort( $wp_list_table->items, [ $this, 'order_callback' ] ); foreach( $wp_list_table->items as $plugin_file => $plugin_data ) { if( is_plugin_active( $plugin_file ) ) { $this->last_active_plugin = $plugin_file; } } } /** * Sort callback: active plugins before inactive, alphabetical within each group. * * @param string $a Plugin basename (e.g. 'akismet/akismet.php'). * @param string $b Plugin basename. * * @return int Negative if $a should appear first, positive if $b should. */ private function order_callback( string $a, string $b ): int { $a_active = is_plugin_active( $a ); $b_active = is_plugin_active( $b ); if( $a_active && !$b_active ) { return - 1; } if( !$a_active && $b_active ) { return 1; } global $wp_list_table; return strnatcasecmp( $wp_list_table->items[ $a ]['Name'] ?? '', $wp_list_table->items[ $b ]['Name'] ?? '' ); } /** * Enqueues inline CSS for the divider and registers the footer script. * * @param string $hook The current admin page hook suffix. * * @return void */ public function admin_enqueue_scripts( string $hook ): void { if( 'plugins.php' !== $hook ) { return; } $css = ' tr.adminease-last-active-plugin td, tr.adminease-last-active-plugin th { border-bottom: 3px solid #2271b1; box-shadow: 0 3px 6px -3px rgba(0, 0, 0, 0.1); } '; wp_add_inline_style( 'wp-admin', $css ); add_action( 'admin_footer', [ $this, 'admin_footer_script' ] ); } /** * Outputs an inline script that marks the last active plugin row * with a CSS class so the divider border is rendered in the right place. * * Runs in admin_footer, after sort_plugins_by_status() has already * determined which row is the last active one. * * @return void */ public function admin_footer_script(): void { if( empty( $this->last_active_plugin ) ) { return; } ?>