Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit 59ac58e

Browse files
author
Dmitry Kochnev
committed
[FEATURE] Author API support with valid JSON passed as content in shortcode [lrn-author]<pre>JSON</pre></lrn-author]
1 parent d761346 commit 59ac58e

File tree

9 files changed

+120
-3
lines changed

9 files changed

+120
-3
lines changed

classes/Learnosity/Plugin.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function enqueue_scripts()
3232
{
3333
$lrn_items_api_url = get_option('lrn_items_api_url','https://items-va.learnosity.com/?v1');
3434
$lrn_reports_api_url = get_option('lrn_reports_api_url','https://reports-va.learnosity.com/?v1');
35+
$lrn_author_api_url = get_option('lrn_author_api_url','https://authorapi-or.learnosity.com/?v1');
3536
wp_enqueue_script(
3637
'learnosity-items',
3738
$lrn_items_api_url,
@@ -46,12 +47,20 @@ public function enqueue_scripts()
4647
null,
4748
false
4849
);
50+
wp_enqueue_script(
51+
'learnosity-author',
52+
$lrn_author_api_url,
53+
array(),
54+
null,
55+
false
56+
);
4957
}
5058

5159
public function init_settings()
5260
{
5361
register_setting('lrn_api_group', 'lrn_consumer_key');
5462
register_setting('lrn_api_group', 'lrn_consumer_secret');
63+
register_setting('lrn_api_group', 'lrn_author_api_url');
5564
register_setting('lrn_api_group', 'lrn_items_api_url');
5665
register_setting('lrn_api_group', 'lrn_reports_api_url');
5766
register_setting('lrn_api_group', 'lrn_default_type');
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Learnosity\Shortcodes;
4+
5+
require_once __DIR__ . '/../../../vendor/learnosity-utils/RequestHelper.php';
6+
7+
class AuthorEmbed
8+
{
9+
private $config;
10+
private $security;
11+
12+
public function __construct($options, $content)
13+
{
14+
$this->security = array(
15+
'consumer_key' => get_option('lrn_consumer_key'),
16+
'domain' => $_SERVER['SERVER_NAME'],
17+
'timestamp' => gmdate('Ymd-Hi')
18+
);
19+
20+
$defaults = array(
21+
'mode' => 'item_list',
22+
'user' => [
23+
'id' => 'www-site',
24+
'firstname' => 'WWW Firstname',
25+
'lastname' => 'WWW Lastname',
26+
'email' => 'www@learnosity.com'
27+
]
28+
);
29+
30+
//supporting $content to be passed inside short code
31+
//[lrn-author]<pre>{JSON}</pre>[/lrn-author]
32+
//using preformatted text <pre> to avoid replacing " for “
33+
if ($content != '') {
34+
$content = json_decode(sanitize_text_field($content), TRUE);
35+
if (is_null($content)) {
36+
$this->render_error("Invalid JSON for Learnosity Author API provided.");
37+
} else {
38+
$this->config = $content;
39+
}
40+
} else {
41+
$this->config = array_merge($defaults, $options);
42+
}
43+
}
44+
45+
public function render()
46+
{
47+
ob_start();
48+
$this->render_init_js();
49+
$this->render_author();
50+
return ob_get_clean();
51+
}
52+
53+
private function generate_signed_request()
54+
{
55+
$request = $this->config;
56+
$request_helper = new \RequestHelper(
57+
'author',
58+
$this->security,
59+
get_option('lrn_consumer_secret'),
60+
$request
61+
);
62+
$signed_request = $request_helper->generateRequest();
63+
return $signed_request;
64+
}
65+
66+
private function render_init_js()
67+
{
68+
$signed_request = $this->generate_signed_request($this->config);
69+
include(__DIR__ . '/../../../templates/init-author-js.php');
70+
}
71+
72+
private function render_author()
73+
{
74+
include(__DIR__ . '/../../../templates/author.php');
75+
}
76+
77+
private function render_error($msg)
78+
{
79+
include(__DIR__ . '/../../../templates/author_error.php');
80+
}
81+
}

classes/Learnosity/Shortcodes/Generator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
require_once 'ItemsEmbed.php';
77
require_once 'SubmitEmbed.php';
88
require_once 'ReportEmbed.php';
9+
require_once 'AuthorEmbed.php';
910

1011
use Learnosity\Shortcodes\ItemEmbed as ItemEmbed;
1112
use Learnosity\Shortcodes\ItemsEmbed as ItemsEmbed;
1213
use Learnosity\Shortcodes\SubmitEmbed as SubmitEmbed;
1314
use Learnosity\Shortcodes\ReportEmbed as ReportEmbed;
15+
use Learnosity\Shortcodes\AuthorEmbed as AuthorEmbed;
1416

1517
class Generator
1618
{
@@ -22,6 +24,7 @@ public function __construct()
2224
add_shortcode('lrn-submit', array(&$this, 'render_submit'));
2325
add_shortcode('lrn-assess', array(&$this, 'render_assess'));
2426
add_shortcode('lrn-report', array(&$this, 'render_report'));
27+
add_shortcode('lrn-author', array(&$this, 'render_author'));
2528
}
2629

2730
public function render_item($attrs)
@@ -53,4 +56,10 @@ public function render_report($attrs, $content)
5356
$report_embed = new ReportEmbed($attrs, $content);
5457
return $report_embed->render();
5558
}
59+
60+
public function render_author($attrs, $content)
61+
{
62+
$author_embed = new AuthorEmbed($attrs, $content);
63+
return $author_embed->render();
64+
}
5665
}

learnosity-api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* Plugin Name: Learnosity API
44
* Plugin URI: https://docs.learnosity.com/developers/developerguide/integration
55
* Description: Simple Learnosity API integration in WordPress.
6-
* Version: 1.7.0
6+
* Version: 1.8.0
77
* Author: Learnosity
88
* Author URI: http://www.learnosity.com
9-
* License: Copyright 2014-2017, Learnosity
9+
* License: Copyright 2014-2020, Learnosity
1010
*/
1111

1212
require_once 'classes/Learnosity/Plugin.php';

templates/author.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span class="learnosity-author" id="learnosity-author"></span>

templates/author_error.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span><?php echo htmlspecialchars($msg) ?></span>

templates/init-author-js.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
// WP can add "defer" flag to <script> inside "wp_enqueue_script" function
3+
// it might lead to the error that "LearnosityAuthor" below in undefined
4+
// to protect against it, we will use "load" global event to secure that all JS assets are loaded before calling any JS code
5+
window.addEventListener("load", function () {
6+
window.learnosityCollection = window.learnosityCollection || [];
7+
window.learnosityCollection.push(LearnosityAuthor.init(<?php echo $signed_request; ?>, {
8+
errorListener: function (e) {
9+
console.log(e);
10+
}
11+
}));
12+
});
13+
</script>

templates/settings.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
<th scope="row"><label for="lrn_consumer_secret">Consumer Secret (required)</label></th>
1414
<td><input type="text" name="lrn_consumer_secret" id="lrn_consumer_secret" value="<?php echo get_option('lrn_consumer_secret'); ?>" class="regular-text ltr"/></td>
1515
</tr>
16+
<tr valign="top">
17+
<th scope="row"><label for="lrn_author_api_url">Author API URL</label></th>
18+
<td><input type="text" name="lrn_author_api_url" id="lrn_author_api_url" value="<?php echo get_option('lrn_author_api_url'); ?>" class="regular-text ltr"/><p><i>Use this to select region and version to use. Default: https://authorapi-or.learnosity.com/?v1</i></p></td>
19+
</tr>
1620
<tr valign="top">
1721
<th scope="row"><label for="lrn_items_api_url">Items API URL</label></th>
1822
<td><input type="text" name="lrn_items_api_url" id="lrn_items_api_url" value="<?php echo get_option('lrn_items_api_url'); ?>" class="regular-text ltr"/><p><i>Use this to select region and version to use. Default: https://items-va.learnosity.com/?v1</i></p></td>

vendor/learnosity-utils/RequestHelper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ private function setServiceOptions($service)
256256
{
257257
switch ($service) {
258258
case 'assess':
259-
case 'author':
260259
case 'questions':
261260
$this->doSignRequestData = false;
262261
break;

0 commit comments

Comments
 (0)