Skip to content

Commit

Permalink
Add plugin: h5p-postmessage.
Browse files Browse the repository at this point in the history
This is needed to allow H5P to send postMessage events to the parent
frame, allowing "completion" events in openlab-modules.

See #3364.
  • Loading branch information
boonebgorges committed Jun 27, 2024
1 parent b98239c commit 627c9a0
Show file tree
Hide file tree
Showing 8 changed files with 19,713 additions and 0 deletions.
6 changes: 6 additions & 0 deletions wp-content/plugins/h5p-postmessage/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [ "plugin:@wordpress/eslint-plugin/recommended" ],
"rules": {
"prettier/prettier": "off"
}
}
63 changes: 63 additions & 0 deletions wp-content/plugins/h5p-postmessage/assets/src/frontend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* global H5P */
(() => {
if ( 'undefined' === typeof H5P ) {
return;
}

const getPostMessageData = ( event ) => {
const { data } = event;

if ( ! data ) {
return null;
}

const { statement } = data;

if ( ! statement ) {
return null;
}

const { object } = statement;

if ( ! object ) {
return null;
}

const objectId = object.id;

const verb = event.getVerb();

const baseData = {
objectId,
source: 'h5p-postmessage',
verb
}

switch ( verb ) {
case 'attempted' :
return baseData;

case 'answered' :
const isComplete = event.getScore() === event.getMaxScore() && event.getMaxScore() > 0

if ( ! isComplete ) {
return null;
}

return {
...baseData,
verb: 'completed'
};
}
}

H5P.externalDispatcher.on('xAPI', function (event) {
const postMessageData = getPostMessageData( event );

if ( ! postMessageData ) {
return;
}

window.parent.postMessage( postMessageData, '*' );
});
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array(), 'version' => '43f963599a1854222e61');
61 changes: 61 additions & 0 deletions wp-content/plugins/h5p-postmessage/build/frontend.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions wp-content/plugins/h5p-postmessage/build/frontend.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions wp-content/plugins/h5p-postmessage/h5p-postmessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* Plugin Name: H5P PostMessage
* Description: Adds postMessage support to H5P.
* Version: 1.0
* Author: City Tech OpenLab
* Author URI: https://openlab.citytech.cuny.edu
*/

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

add_action(
'wp_enqueue_scripts',
function() {
}
);

add_action(
'h5p_additional_embed_head_tags',
function( &$tags ) {
$blocks_dir = __DIR__ . '/build/';
$blocks_asset_file = include $blocks_dir . 'frontend.asset.php';

// Add the script to the tags.
$tags[] = sprintf(
'<script src="%s"></script>',
plugin_dir_url( __FILE__ ) . 'build/frontend.js'
);

return $tags;
}
);
Loading

0 comments on commit 627c9a0

Please sign in to comment.