-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
b98239c
commit 627c9a0
Showing
8 changed files
with
19,713 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": [ "plugin:@wordpress/eslint-plugin/recommended" ], | ||
"rules": { | ||
"prettier/prettier": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '*' ); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php return array('dependencies' => array(), 'version' => '43f963599a1854222e61'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
); |
Oops, something went wrong.