-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathokfn-annot-factory.php
86 lines (66 loc) · 2.06 KB
/
okfn-annot-factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* Dynamically generates the JavaScript code snippet needed for instantiating
* the Annotator.
*/
use \Firebase\JWT\JWT;
class OkfnAnnotFactory extends OkfnBase {
private $uri; //current wordpress page
private $template_vars;
private $settings;
/**
* Public:
* Class constructor
* settings - an instance of OkfnAnnotSettings.
*/
function __construct($settings) {
$this->settings = $settings;
}
/**
* Fetches the URI of the currently visited blog page
* returns a uri string
*/
private function get_current_uri(){
return OkfnUtils::current_url();
}
/**
* Wrapper method calling the Mustache template engine.
* (Needed mostly for testing and mocking...)
*/
function render_template($template_vars, $template='annotator-instance.js') {
$template = OkfnUtils::get_template($template);
$mustache = new Mustache;
return $mustache->render($template,$template_vars);
}
private function prepare_variables(){
$template_vars = $this->settings->get_options_values();
$CONSUMER_TTL = 86400;
$secret = $template_vars["annotateit_secret"];
$objDateTime = new DateTime('NOW');
$issuedAt = $objDateTime->format(DateTime::ISO8601)."Z";
$user = true ? "" : wp_get_current_user()->user_login; //client 1.2 seems to be buggy, waiting for v2
$token = array(
'consumerKey'=> $template_vars["annotateit_key"],
'userId'=> $user,
'issuedAt'=> $issuedAt,
'ttl' => $CONSUMER_TTL
);
$template_vars["token"] = JWT::encode($token, $secret);
$template_vars["user"] = $user;
//todo: add load from search
$template_vars['uri'] = $this->get_current_uri();
$template_vars['annotateit_secret'] = ""; //should never be published
return $template_vars;
}
/**
* Generates a code snippet containing a customised Javascript instantiation
* code for the Annotator.
*
* Returns a string or null if no instance is meant to be created.
*
*/
function create_snippet() {
return $this->render_template($this->prepare_variables());
}
}
?>