diff --git a/h5p-default-storage.class.php b/h5p-default-storage.class.php index 38af307..ba41eb9 100644 --- a/h5p-default-storage.class.php +++ b/h5p-default-storage.class.php @@ -451,6 +451,17 @@ class H5PDefaultStorage implements \H5PFileStorage { return self::dirReady($this->path); } + public function hasPresave($name) { + $filePath = implode(DIRECTORY_SEPARATOR, [ + $this->path, + 'libraries', + $name, + 'presave.js', + ]); + + return file_exists($filePath); + } + /** * Recursive function for copying directories. * diff --git a/h5p-file-storage.interface.php b/h5p-file-storage.interface.php index 394210b..953365c 100644 --- a/h5p-file-storage.interface.php +++ b/h5p-file-storage.interface.php @@ -190,4 +190,11 @@ interface H5PFileStorage { * @return bool True if server has the proper write access */ public function hasWriteAccess(); + + /** + * Check if the library has a presave.js in the root folder + * + * @return string|null Path to script or null if missing + */ + public function hasPresave($name); } diff --git a/h5p.classes.php b/h5p.classes.php index d8ed62e..a978516 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2109,14 +2109,18 @@ class H5PCore { // Using content dependencies foreach ($dependencies as $dependency) { + $libraryName = H5PCore::libraryToString($dependency, TRUE); if (isset($dependency['path']) === FALSE) { - $dependency['path'] = 'libraries/' . H5PCore::libraryToString($dependency, TRUE); + $dependency['path'] = 'libraries/' . $libraryName; $dependency['preloadedJs'] = explode(',', $dependency['preloadedJs']); $dependency['preloadedCss'] = explode(',', $dependency['preloadedCss']); } $dependency['version'] = "?ver={$dependency['majorVersion']}.{$dependency['minorVersion']}.{$dependency['patchVersion']}"; $this->getDependencyAssets($dependency, 'preloadedJs', $files['scripts'], $prefix); $this->getDependencyAssets($dependency, 'preloadedCss', $files['styles'], $prefix); + if( $this->fs->hasPresave($libraryName) ){ + $this->addPresaveFile($files, $dependency, $prefix); + } } if ($this->aggregateAssets) { @@ -2130,6 +2134,14 @@ class H5PCore { return $files; } + public function addPresaveFile(&$assets, $library, $prefix = ''){ + $this->getDependencyAssets([ + 'path' => 'libraries' . DIRECTORY_SEPARATOR . self::libraryToString($library, true), + 'version' => array_key_exists('version', $library) ? $library['version'] : "?ver={$library['majorVersion']}.{$library['minorVersion']}.{$library['patchVersion']}", + 'presaveJs' => ['presave.js'] + ], 'presaveJs', $assets['scripts'], $prefix); + } + private static function getDependenciesHash(&$dependencies) { // Build hash of dependencies $toHash = array();