diff --git a/admin/class-h5p-editor-wordpress-storage.php b/admin/class-h5p-editor-wordpress-storage.php index 177fdcdd..9ee72de5 100644 --- a/admin/class-h5p-editor-wordpress-storage.php +++ b/admin/class-h5p-editor-wordpress-storage.php @@ -178,7 +178,8 @@ public function getLibraries($libraries = NULL) { */ public function alterLibraryFiles(&$files, $libraries) { $plugin = H5P_Plugin::get_instance(); - $plugin->alter_assets($files, $libraries, 'editor'); + $custom_script_parameters = (object) array(); + $plugin->alter_assets($files, $libraries, 'editor', $custom_script_parameters); } /** diff --git a/admin/class-h5p-plugin-admin.php b/admin/class-h5p-plugin-admin.php index f6eb1d5a..d4722785 100644 --- a/admin/class-h5p-plugin-admin.php +++ b/admin/class-h5p-plugin-admin.php @@ -228,7 +228,11 @@ public function embed() { // Get content assets $preloaded_dependencies = $core->loadContentDependencies($content['id'], 'preloaded'); $files = $core->getDependenciesFiles($preloaded_dependencies); - $plugin->alter_assets($files, $preloaded_dependencies, 'external'); + $custom_script_parameters = (object) array(); + $plugin->alter_assets($files, $preloaded_dependencies, 'external', $custom_script_parameters); + if (is_object($custom_script_parameters) && !empty((array) $custom_script_parameters)) { + $integration['customScriptParameters'] = $custom_script_parameters; + } $scripts = array_merge($scripts, $core->getAssetsUrls($files['scripts'])); $styles = array_merge($styles, $core->getAssetsUrls($files['styles'])); diff --git a/public/class-h5p-plugin.php b/public/class-h5p-plugin.php index 7d06e075..dd533e74 100644 --- a/public/class-h5p-plugin.php +++ b/public/class-h5p-plugin.php @@ -1117,7 +1117,16 @@ public function add_assets($content, $no_cache = FALSE) { // Get assets for this content $preloaded_dependencies = $core->loadContentDependencies($content['id'], 'preloaded'); $files = $core->getDependenciesFiles($preloaded_dependencies); - $this->alter_assets($files, $preloaded_dependencies, $embed); + + // Optional script parameters that custom user scripts should be able to access + $custom_script_parameters = (object) array(); + + $this->alter_assets($files, $preloaded_dependencies, $embed, $custom_script_parameters); + + if ((is_object($custom_script_parameters)) && !empty((array)$custom_script_parameters)) { + // Allow to access custom user script paramters via H5PIntegration + self::$settings['customScriptParameters'] = $custom_script_parameters; + } /* * Cached assets were broken by faulty folder structure in 1.17.0 - 1.17.3 @@ -1170,8 +1179,9 @@ public function add_assets($content, $no_cache = FALSE) { * @param array $dependencies * @param array $files scripts & styles * @param string $embed type + * @param object &$custom_script_parameters Custom parameters that should be available to JavaScript */ - public function alter_assets(&$files, &$dependencies, $embed) { + public function alter_assets(&$files, &$dependencies, $embed, &$custom_script_parameters = null) { if (!has_action('h5p_alter_library_scripts') && !has_action('h5p_alter_library_styles')) { return; } @@ -1194,8 +1204,9 @@ public function alter_assets(&$files, &$dependencies, $embed) { * @param array &$scripts List of JavaScripts to be included. * @param array $libraries The list of libraries that has the scripts. * @param string $embed_type Possible values are: div, iframe, external, editor. + * @param object &$custom_script_parameters Custom parameters that should be available to JavaScript */ - do_action_ref_array('h5p_alter_library_scripts', array(&$files['scripts'], $libraries, $embed)); + do_action_ref_array('h5p_alter_library_scripts', array(&$files['scripts'], $libraries, $embed, &$custom_script_parameters)); /** * Allows you to alter which stylesheets are loaded for H5P. This is