* @copyright 2026 Dobrzepostawieni.pl * @license GPL-3.0-or-later */ namespace Primeplaced_Logless_Analytics\Backend; // Prevent direct file access. if ( ! defined( 'ABSPATH' ) ) { exit; } use Primeplaced_Logless_Analytics\Engine\Base; use Primeplaced_Logless_Analytics\Engine\Traits\AssetLoader; use Primeplaced_Logless_Analytics\Engine\Traits\ScriptDataHelper; use Inpsyde\Assets\Asset; use Inpsyde\Assets\AssetManager; use Inpsyde\Assets\Script; use Inpsyde\Assets\Style; /** * Enqueue assets for the admin settings pages. */ class Enqueue extends Base { use AssetLoader; use ScriptDataHelper; /** * Unique handle for the admin settings script. */ private const HANDLE = \PPLA_TEXTDOMAIN . '-settings-admin'; /** * Initialize the class and configure translations. * * @return bool True on success. */ public function initialize(): bool { if ( ! parent::initialize() ) { return false; } \add_action( AssetManager::ACTION_SETUP, array( $this, 'enqueue_assets' ) ); $this->setup_translations( self::HANDLE ); return true; } /** * Enqueue assets for the plugin settings screen. * * @param AssetManager $asset_manager Asset Manager instance. */ public function enqueue_assets( AssetManager $asset_manager ): void { $screen = \get_current_screen(); if ( ! $screen || false === \strpos( $screen->id, \PPLA_TEXTDOMAIN ) ) { return; } \wp_enqueue_media(); $this->register_styles( $asset_manager ); $this->register_scripts( $asset_manager ); } /** * Registers admin styles. * * @param AssetManager $asset_manager Manager instance. */ private function register_styles( AssetManager $asset_manager ): void { $config = $this->get_asset_config(); $style = new Style( self::HANDLE, \plugins_url( 'assets/build/primeplaced-logless-analytics-admin.css', \PPLA_PLUGIN_ABSOLUTE ) ); $style->forLocation( Asset::BACKEND ) ->withVersion( $config['version'] ) ->dependencies(); $asset_manager->register( $style ); } /** * Registers admin scripts and localizes data. * * @param AssetManager $asset_manager Manager instance. */ private function register_scripts( AssetManager $asset_manager ): void { $config = $this->get_asset_config(); $safe_deps = \array_diff( $config['dependencies'], array( 'wp-edit-post', 'wp-editor', 'wp-block-editor', 'wp-block-library', 'wp-edit-site' ) ); $script = new Script( self::HANDLE, \plugins_url( 'assets/build/primeplaced-logless-analytics-admin.js', \PPLA_PLUGIN_ABSOLUTE ) ); $script->forLocation( Asset::BACKEND ) ->withVersion( $config['version'] ) ->dependencies( ...\array_values( $safe_deps ) ); $script->withLocalize( 'pplaAdminData', $this->get_script_data() ); $asset_manager->register( $script ); } /** * Prepare dynamic data for the JS application. * * @return array */ private function get_script_data(): array { $data = $this->get_admin_base_data(); return (array) \apply_filters( 'ppla_admin_script_data', $data ); } /** * Helper: Reads the .asset.php file generated by Webpack. * * @return array{dependencies: string[], version: string} */ private function get_asset_config(): array { $path = \plugin_dir_path( \PPLA_PLUGIN_ABSOLUTE ) . 'assets/build/primeplaced-logless-analytics-admin.asset.php'; return $this->get_webpack_asset_config( $path, array( 'wp-element', 'wp-components', 'wp-i18n' ) ); } }