-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathokfn-annot-injector.php
171 lines (150 loc) · 4.61 KB
/
okfn-annot-injector.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
*
* Appends the Annotator JavaScript and CSS files (with their respective dependencies)
* to the document leveraging the appropriate Wordpress hooks.
*
*
*/
class OkfnAnnotInjector extends OkfnBase {
public $conf = array(
'assets_basepath' => '/vendor/',
// In the sake of simplicity for now we only support the full minified version
// of the annotator (A more fine grained control through the settings can be implemented later).
'javascripts' => array(
/* todo deniso import jquery properly
* array(
'file' => 'javascripts/jquery.min.js'
),
*/
array(
'file' => 'javascripts/annotator/annotator-full.min.js',
'dependencies' => array('json2',/*'jquery'*/),
),
),
'stylesheets' => array(
array(
'file' => 'stylesheets/annotator.min.css'
)
)
);
private $factory;
private $content_policy;
function __construct($factory,$content_policy){
parent::__construct();
$this->factory = $factory;
$this->content_policy = $content_policy;
}
/*
* Enqueues the Annotator's dependencies (i.e. JSON2, and jQuery).
*
* returns nothing
*
*/
function load_javascript_dependencies(){
wp_enqueue_script('json2');
//deregister the javascript version used by wordpress
/*
* todo deniso import jquery properly
* wp_deregister_script('jquery');
*/
}
/*
* Public:
*
* Enqueues the Annotator javascript files
*
* returns nothing
*
*/
public function load_javascripts() {
$this->load_javascript_dependencies();
$this->load_assets('javascripts');
}
/*
* Public:
*
* Enqueues the Annotator stylesheet/s
*
* returns nothing
*
*/
function load_stylesheets() {
$this->load_assets('stylesheets');
}
/*
* Ensures that libraries are registered with the '.{js|css}' and not the '.min.{js|css}' prefix
*
* path - a relative path to an asset
*
* returns the asset id (stripped of the '.min' fragment ).
*
*/
function asset_id($path) {
return preg_replace('/(\.min)\.(js|css)$/','.$2', basename($path) );
}
/*
* Wrapper for wp_enqueue_{style|script}
*
* This has been implemented only for mocking/testing purposes.
*
* asset_id - The asset filename (without the path and the .min prefix).
* asset_src - The asset url (relative to the plugin directory).
* dependencies - Array of asset filenames specifying other assets that should be loaded first.
* version_number - A version number to be passed to the library; defaults to Wordpress version number, (optional).
* in_footer - Wether or not to load the library in the document footer rather than the header.
* javascripts_or_stylesheets - Whether to load a javascript or a stylesheet asset
*
* returns nothing
*/
function wp_enqueue_asset($asset_id, $asset_src, $dependencies, $version_number, $in_footer, $javascripts_or_stylesheets) {
$loader_function = ($javascripts_or_stylesheets === 'javascripts') ?
'wp_enqueue_script' :
'wp_enqueue_style';
call_user_func_array($loader_function, array($asset_id,
plugins_url($asset_src , dirname(__FILE__)),
$dependencies, $version_number, $in_footer
));
}
/*
* Registers the annotator assets (as specified in $conf) and enqueues them using
* the wordpress loading functions
*
* javascripts_or_stylesheets - Whether it should load javascripts or stylesheets.
*
* returns nothing
*/
private function load_assets($javascripts_or_stylesheets) {
foreach($this->conf->$javascripts_or_stylesheets as $asset) {
$in_footer = isset($asset->in_footer) && $asset->in_footer;
$this->wp_enqueue_asset(
$this->asset_id($asset->file),
"{$this->conf->assets_basepath}{$asset->file}",
isset($asset->dependencies) ? $asset->dependencies : array(),
false,
$in_footer,
$javascripts_or_stylesheets
);
}
}
/*
*
* Prints the instantiation snippet produced by the OkfnAnnotFactory object
*
* returns nothing
*/
function print_snippet() {
$snippet = $this->factory->create_snippet();
if ($snippet) {
print implode("\n",array('<script>', $snippet, '</script>'));
}
}
function inject() {
if ($this->content_policy->url_is_annotatable()) {
add_action('wp_print_styles', array($this,'load_stylesheets'));
add_action('wp_print_scripts', array($this,'load_javascripts'));
add_action('wp_footer', array($this,'print_snippet'));
}
}
}
?>