Add support for custom script parameters in alter_scripts hook - #214
Add support for custom script parameters in alter_scripts hook#214otacke wants to merge 3 commits into
Conversation
| public function alterLibraryFiles(&$files, $libraries) { | ||
| $plugin = H5P_Plugin::get_instance(); | ||
| $plugin->alter_assets($files, $libraries, 'editor'); | ||
| $custom_script_parameters = (object) array(); |
There was a problem hiding this comment.
@otacke do we have a standard for this? I noticed in the other PR https://github.com/h5p/h5p-wordpress-plugin/pull/218/changes#diff-136c1cb88ba22448ea89193777a49f07d2f68706fabd465e66c230d05f831799R1279 you are using [] modern array syntax but here is old school array() are we adhering some sort of non defacto standard? like using array() for initializers but using [] for other scopes?
There was a problem hiding this comment.
@arzola Good catch. H5P Group does not have guidelines for PHP, no phpcs.xml or similar. I then usually fall back to using what the surrounding code dictates.
|
ok that makes sense I think I'll move this to verified by community maintainer, until we have a coding standard set up I think that's a good rule of thumb for that. |
|
@arzola Great! I'll try to push that subject. |
What kind of change does this PR introduce?
Feature: When merged in, will allow developers to pass custom parameters via the alter_scripts hook that the injected scripts can access via H5PIntegration.
What is the current behaviour?
Developers can add their own JavaScripts into the context of H5P libraries using the
alter_scriptshook. That hook, however, only allows to add the paths to the scripts to inject. Developers cannot pass any parameters that the script might need to be configured dynamically based on the user role, the user name or any other parameter that the H5P integration can provide.In particular, this becomes relevant if the script that is injected needs to establish communication with the server and requires to add a server-defined nonce/CSRF tokens/session based tokens to the request in order to avoid cross-site request forgery.
What is the new behaviour?
The hook's signature was amended to also accept a
$custom_script_parametersargument (optional in order to not break existing hook use). It's an object passed by reference that developers can amend by setting key/value pairs, e.g.If any key/value pair was set by the hook, the H5P integration will then add the custom script object to
window.H5PIntegrationaswindow.H5PIntegration.customScriptParameters. The scripts that were injected by developers can then retrieve the values, e.g.This merge request was inspired by @pjotrsavitski (#80).
PR Checklist
Before submitting, please confirm:
Read the contribution guidelines to get your pr accepted more quickly.