id, 'gregius-data' ) === false ) { return; } // Ensure WordPress REST API is initialized. wp_enqueue_script( 'wp-api' ); // Enqueue the dashboard stylesheet. The @wordpress/components styles are // provided by WordPress core via the wp-components handle (dependency below). $css_file_path = glob( plugin_dir_path( __FILE__ ) . 'build/dashboard.min.*.css' ); if ( ! empty( $css_file_path ) && file_exists( $css_file_path[0] ) ) { $css_file_uri = plugin_dir_url( __FILE__ ) . 'build/' . basename( $css_file_path[0] ); wp_enqueue_style( 'gregius-data-dashboard-style', $css_file_uri, array( 'wp-components' ), filemtime( $css_file_path[0] ), 'all' ); } // Get the asset file for dependencies. $asset_file_path = glob( plugin_dir_path( __FILE__ ) . 'build/dashboard.min.*.asset.php' ); $asset_file = ! empty( $asset_file_path ) && file_exists( $asset_file_path[0] ) ? include $asset_file_path[0] : array(); $dependencies = isset( $asset_file['dependencies'] ) ? $asset_file['dependencies'] : array( 'wp-element', 'wp-i18n', 'wp-components', 'wp-api-fetch', 'wp-data', 'wp-core-data', 'wp-dom-ready', 'wp-url', ); $version = isset( $asset_file['version'] ) ? $asset_file['version'] : null; // Enqueue the dashboard JavaScript file with proper dependencies. $js_file_path = glob( plugin_dir_path( __FILE__ ) . 'build/dashboard.min.*.js' ); if ( ! empty( $js_file_path ) && file_exists( $js_file_path[0] ) ) { $js_file_uri = plugin_dir_url( __FILE__ ) . 'build/' . basename( $js_file_path[0] ); wp_enqueue_script( 'gregius-data-dashboard-script', $js_file_uri, $dependencies, $version ?? filemtime( $js_file_path[0] ), true ); // Add inline script to set up the REST API config the dashboard consumes. wp_add_inline_script( 'gregius-data-dashboard-script', ' window.gregiusPgDashboard = { restUrl: "' . esc_js( rest_url() ) . '", nonce: "' . esc_js( wp_create_nonce( 'wp_rest' ) ) . '" }; ', 'before' ); } } add_action( 'admin_enqueue_scripts', 'gregius_data_dashboard_scripts_support' ); /** * Clear localStorage when plugin is deactivated */ function gregius_data_clear_localstorage_script() { if ( ! get_transient( 'gregius_data_clear_localstorage' ) ) { return; } delete_transient( 'gregius_data_clear_localstorage' ); $script = <<<'JS' // Clear Gregius Data localStorage on deactivation. if (typeof localStorage !== 'undefined') { const keysToRemove = []; for (let i = 0; i < localStorage.length; i++) { const key = localStorage.key(i); if (key && key.startsWith('gregius_data_')) { keysToRemove.push(key); } } keysToRemove.forEach(key => localStorage.removeItem(key)); console.log('Gregius Data: Cleared ' + keysToRemove.length + ' localStorage items after deactivation'); } JS; wp_register_script( 'gregius-data-clear-localstorage', '', array(), GREGIUS_DATA_VERSION, true ); wp_add_inline_script( 'gregius-data-clear-localstorage', $script ); wp_enqueue_script( 'gregius-data-clear-localstorage' ); } add_action( 'admin_enqueue_scripts', 'gregius_data_clear_localstorage_script' ); /** * Enqueue the deactivation modal on the plugins screen. * * Records the keep/remove data-retention preference before deactivation. * The modal never blocks deactivation; bulk/CLI deactivation bypasses it. * * @param string $hook_suffix Current admin page hook. * @return void */ function gregius_data_deactivation_modal_scripts_support( $hook_suffix ) { if ( 'plugins.php' !== $hook_suffix ) { return; } $can_deactivate = is_multisite() ? current_user_can( 'manage_network_options' ) : current_user_can( 'deactivate_plugins' ); if ( ! $can_deactivate ) { return; } $asset_file_path = plugin_dir_path( __FILE__ ) . 'build/deactivation-modal.asset.php'; if ( ! file_exists( $asset_file_path ) ) { return; } $asset_file = include $asset_file_path; // Provide wpApiSettings (root + nonce) for the apiFetch middleware. wp_enqueue_script( 'wp-api' ); wp_enqueue_script( 'gregius-data-deactivation-modal', plugin_dir_url( __FILE__ ) . 'build/deactivation-modal.js', isset( $asset_file['dependencies'] ) ? $asset_file['dependencies'] : array( 'wp-element', 'wp-i18n', 'wp-components', 'wp-api-fetch', 'wp-dom-ready' ), isset( $asset_file['version'] ) ? $asset_file['version'] : null, true ); wp_enqueue_style( 'wp-components' ); wp_localize_script( 'gregius-data-deactivation-modal', 'gregiusDataDeactivation', array( 'deactivateId' => 'deactivate-gregius-data/gregius-data.php', 'removeData' => Gregius_Data_Uninstaller::get_retention_flag(), ) ); } add_action( 'admin_enqueue_scripts', 'gregius_data_deactivation_modal_scripts_support' );