diff --git a/CHANGELOG.md b/CHANGELOG.md index 5291b9b..4d22f25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [Unreleased] +### Added +- feat: add plugin search and filter UI + ## 1.1.0-beta.1 - Feat: Refactor for WPCS and best practices diff --git a/assets/src/js/admin.js b/assets/src/js/admin.js index 525d99a..501c193 100644 --- a/assets/src/js/admin.js +++ b/assets/src/js/admin.js @@ -1,3 +1,30 @@ /** * This will have admin related code only. */ + +// Add event listener for search input +const pluginSearchInput = document.getElementById('plugin-search-input'); +const pluginTableRows = document.querySelectorAll('#plugin-browser-table tbody tr'); + +if (pluginSearchInput) { + pluginSearchInput.addEventListener('input', (event) => { + const searchTerm = event.target.value.toLowerCase(); + + pluginTableRows.forEach((row) => { + const pluginName = row.querySelector('.plugin-name').textContent.toLowerCase(); + const pluginStatus = row.querySelector('.plugin-status').textContent.toLowerCase(); + + if (pluginName.includes(searchTerm) || pluginStatus.includes(searchTerm)) { + row.style.display = ''; + } else { + row.style.display = 'none'; + } + }); + }); +} + +// Ensure the plugin browser table matches WordPress admin patterns +const pluginTable = document.getElementById('plugin-browser-table'); +if (pluginTable) { + pluginTable.classList.add('wp-list-table', 'widefat', 'fixed', 'striped'); +} diff --git a/inc/Modules/Core/Assets.php b/inc/Modules/Core/Assets.php index 4bab636..54b9134 100644 --- a/inc/Modules/Core/Assets.php +++ b/inc/Modules/Core/Assets.php @@ -210,4 +210,32 @@ private function register_style( string $handle, string $filename, array $deps = $media ); } + + /** + * AJAX handler for plugin search and filter. + */ + public function handle_plugin_search(): void { + check_ajax_referer( 'plugin_search_nonce', 'security' ); + + if ( ! current_user_can( 'manage_options' ) ) { + wp_send_json_error( [ 'message' => 'Unauthorized' ], 403 ); + } + + $search_term = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : ''; + $plugins = $this->get_plugins(); // Assume this method fetches the plugin list. + + $filtered_plugins = array_filter( $plugins, function ( $plugin ) use ( $search_term ) { + return stripos( $plugin['name'], $search_term ) !== false || + stripos( $plugin['status'], $search_term ) !== false; + }); + + wp_send_json_success( [ 'plugins' => $filtered_plugins ] ); + } + + /** + * Register AJAX actions. + */ + public function register_ajax_actions(): void { + add_action( 'wp_ajax_plugin_search', [ $this, 'handle_plugin_search' ] ); + } }