*/ private array $libraries = []; private bool $registered = false; public function __construct() { $this->libraries = [ 'three' => [ 'id' => '@3d-print-showcase/three', 'path' => 'assets/vendor/three/three.module.js', 'dependencies' => [], 'version' => '0.185.1', ], 'orbit-controls' => [ 'id' => '@3d-print-showcase/orbit-controls', 'path' => 'assets/vendor/three/addons/controls/OrbitControls.js', 'dependencies' => ['@3d-print-showcase/three'], 'version' => '0.185.1', ], '3mf-loader' => [ 'id' => '@3d-print-showcase/3mf-loader', 'path' => 'assets/vendor/three/addons/loaders/3MFLoader.js', 'dependencies' => ['@3d-print-showcase/three'], 'version' => '0.185.1', ], 'stl-loader' => [ 'id' => '@3d-print-showcase/stl-loader', 'path' => 'assets/vendor/three/addons/loaders/STLLoader.js', 'dependencies' => ['@3d-print-showcase/three'], 'version' => '0.185.1', ], 'gltf-loader' => [ 'id' => '@3d-print-showcase/gltf-loader', 'path' => 'assets/vendor/three/addons/loaders/GLTFLoader.js', 'dependencies' => ['@3d-print-showcase/three'], 'version' => '0.185.1', ], 'buffer-geometry-utils' => [ 'id' => '@3d-print-showcase/buffer-geometry-utils', 'path' => 'assets/vendor/three/addons/utils/BufferGeometryUtils.js', 'dependencies' => ['@3d-print-showcase/three'], 'version' => '0.185.1', ], 'skeleton-utils' => [ 'id' => '@3d-print-showcase/skeleton-utils', 'path' => 'assets/vendor/three/addons/utils/SkeletonUtils.js', 'dependencies' => ['@3d-print-showcase/three'], 'version' => '0.185.1', ], 'fflate' => [ 'id' => '@3d-print-showcase/fflate', 'path' => 'assets/vendor/three/addons/libs/fflate.module.js', 'dependencies' => [], 'version' => DS82_3DPS_VERSION, ], ]; } public function registerModules(): void { if ($this->registered || !function_exists('wp_register_script_module')) { return; } /** * Filters the public library definitions before script modules are registered. * * @param array $libraries */ $libraries = apply_filters('ds82_3dps_library_registry', $this->libraries); if (is_array($libraries)) { $this->libraries = $libraries; } foreach ($this->libraries as $library) { if (!$this->isValidDefinition($library)) { continue; } wp_register_script_module( $library['id'], DS82_3DPS_URL . ltrim($library['path'], '/'), $library['dependencies'], $library['version'] ); } $this->registered = true; /** * Fires after all shared 3D Print Showcase script modules are registered. */ do_action('ds82_3dps_libraries_registered', $this); } public function has(string $library): bool { return isset($this->libraries[$library]) && $this->isValidDefinition($this->libraries[$library]); } public function url(string $library): string { if (!$this->has($library)) { return ''; } return DS82_3DPS_URL . ltrim($this->libraries[$library]['path'], '/'); } public function moduleId(string $library): string { return $this->has($library) ? $this->libraries[$library]['id'] : ''; } /** @return string[] */ public function dependencies(string $library): array { return $this->has($library) ? $this->libraries[$library]['dependencies'] : []; } public function enqueue(string $library): bool { if (!$this->has($library) || !function_exists('wp_enqueue_script_module')) { return false; } $this->registerModules(); wp_enqueue_script_module($this->moduleId($library)); return true; } /** @return array */ public function all(): array { return $this->libraries; } /** @param mixed $library */ private function isValidDefinition(mixed $library): bool { return is_array($library) && isset($library['id'], $library['path'], $library['dependencies'], $library['version']) && is_string($library['id']) && $library['id'] !== '' && is_string($library['path']) && $library['path'] !== '' && is_array($library['dependencies']) && is_string($library['version']); } }