-
Notifications
You must be signed in to change notification settings - Fork 1
/
orcid-functions.php
374 lines (346 loc) · 12 KB
/
orcid-functions.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
// for testing
//$orcidID = "0000-0003-0265-9119"; // Alan Munn
//$orcidID = "0000-0003-1822-3109"; // Bronson Hui
//$orcidID = "0000-0002-8143-2408"; // Scott Schopieray
//$orcidID = "0000-0003-3953-7940"; // Chris Long (U of CO at Boulder)
//$orcidID = "0000-0002-5251-0307"; // Kathleen Fitzpatrick
include_once(plugin_dir_path(__FILE__) . 'config.php');
/**
* download data from orcid.org
*
* @param string $orcid_id - ORCiD ID
*
* @return string $orcid_xml
*/
function orcid_download_data($orcid_id)
{
$orcid_link = ORCID_URL . $orcid_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $orcid_link);
try {
$orcid_xml = curl_exec($ch);
} catch (Exception $e) {
throw new Exception($e);
}
curl_close($ch);
return $orcid_xml;
}
/**
* format the orcid XML into HTML with XSLT
*
* parameters:
*
* @param string $orcid_xml - XML as string
* @param array $display_parameters - which sections of orcid data to display
*
* @return string orcid_html
*/
function orcid_format_data_as_html($orcid_xml, $display_parameters)
{
$xml_doc = new DOMDocument();
$xml_doc->loadXML($orcid_xml);
$xsl_doc = new DOMDocument();
$xsl_doc->load(ORCID_XSLT);
$html_doc = new XSLTProcessor();
//
// control which sections are displayed
$html_doc->setParameter('', 'display_header', $display_parameters['display_header']);
// $html_doc->setParameter('', 'display_header', 'yes');
$html_doc->setParameter('', 'display_personal', $display_parameters['display_personal']);
$html_doc->setParameter('', 'display_education', $display_parameters['display_education']);
$html_doc->setParameter('', 'display_employment', $display_parameters['display_employment']);
//
$html_doc->setParameter('', 'display_works', $display_parameters['display_works']);
$html_doc->setParameter('', 'works_type', $display_parameters['works_type']);
$html_doc->setParameter('', 'works_start_year', $display_parameters['works_start_year']);
//
$html_doc->setParameter('', 'display_fundings', $display_parameters['display_fundings']);
$html_doc->setParameter('', 'display_peer_reviews', $display_parameters['display_peer_reviews']);
$html_doc->setParameter('', 'display_invited_positions', $display_parameters['display_invited_positions']);
$html_doc->setParameter('', 'display_memberships', $display_parameters['display_memberships']);
$html_doc->setParameter('', 'display_qualifications', $display_parameters['display_qualifications']);
$html_doc->setParameter('', 'display_research_resources', $display_parameters['display_research_resources']);
$html_doc->setParameter('', 'display_services', $display_parameters['display_services']);
$html_doc->importStylesheet($xsl_doc);
$orcid_html = $html_doc->transformToXML($xml_doc);
return $orcid_html;
}
/**
* Call back function for shortword [orcid-data section="section_name"]
*
* parameters:
*
* @param array $atts - contains the display configuration parameters
*
* @return string shortcode value
*
*/
function orcid_data_function($atts = [], $content = null, $tag = '')
{
// normalize attribute keys, lowercase
$atts = array_change_key_case((array) $atts, CASE_LOWER);
//
// override default attributes with user attributes
// extract() converts a dictionary to a set of variables
//
// section: which section of the orcid data to display.
// if no section is specified display the header by default
extract(shortcode_atts(
array(
'section' => 'header',
'works_type' => 'all',
'works_start_year' => '1900',
),
$atts
));
//
// now we want to display the *AUTHOR's* (NOT the viewer's) data
//
// get the author's WordPress user id
$author = get_the_author_meta('ID', false);
//
// get orcid data
//
// we can either download the data from orcid.org OR use the cached value
// we download the data IFF ($download_from_orcid_flag = TRUE)
// 1) there is no cached xml data
// 2) the cached value is older than ORCID_CACHE_TIMEOUT (in seconds)
//
$download_from_orcid_flag = false;
//
// 2) there is no cached xml data
if (empty(get_user_meta($author, '_orcid_xml', true))) {
$download_from_orcid_flag = true;
}
//
// 3) the cached value is older than ORCID_CACHE_TIMEOUT (in seconds)
$current_time = time();
// last download time
$orcid_xml_download_time = intval(get_user_meta($author, '_orcid_xml_download_time', true));
//
$time_diff = $current_time - $orcid_xml_download_time;
if ($time_diff >= ORCID_CACHE_TIMEOUT) {
$download_from_orcid_flag = true;
}
if ($download_from_orcid_flag) {
// return '<p>Downloading XML data from orcid.org</p>' . PHP_EOL;
$orcid_id = get_user_meta($author, '_orcid_id', true);
$orcid_xml = orcid_download_data($orcid_id);
update_user_meta($author, '_orcid_xml', $orcid_xml);
//
// keep track of when download occurred
update_user_meta($author, '_orcid_xml_download_time', strval(time()));
} else {
// return '<p>Using cached XML data</p>' . PHP_EOL;
// return '<p>author WP id = ' . intval($author) . '</p>';
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$orcid_xml = get_user_meta($author, '_orcid_xml', true);
}
// $orcid_xml = get_user_meta($author, '_orcid_xml', TRUE);
//
// determine which section to display
if ($section == 'header') {
$display_parameters['display_header'] = 'yes';
} else {
$display_parameters['display_header'] = 'no';
}
if ($section == 'personal') {
$display_parameters['display_personal'] = 'yes';
} else {
$display_parameters['display_personal'] = 'no';
}
if ($section == 'education') {
$display_parameters['display_education'] = 'yes';
} else {
$display_parameters['display_education'] = 'no';
}
if ($section == 'employment') {
$display_parameters['display_employment'] = 'yes';
} else {
$display_parameters['display_employment'] = 'no';
}
if ($section == 'works') {
$display_parameters['display_works'] = 'yes';
$display_parameters['works_type'] = $works_type;
$display_parameters['works_start_year'] = $works_start_year;
} else {
$display_parameters['display_works'] = 'no';
$display_parameters['works_type'] = 'none';
$display_parameters['works_start_year'] = '3000';
}
if ($section == 'fundings') {
$display_parameters['display_fundings'] = 'yes';
} else {
$display_parameters['display_fundings'] = 'no';
}
if ($section == 'peer_reviews') {
$display_parameters['display_peer_reviews'] = 'yes';
} else {
$display_parameters['display_peer_reviews'] = 'no';
}
if ($section == 'invited_positions') {
$display_parameters['display_invited_positions'] = 'yes';
} else {
$display_parameters['display_invited_positions'] = 'no';
}
if ($section == 'memberships') {
$display_parameters['display_memberships'] = 'yes';
} else {
$display_parameters['display_memberships'] = 'no';
}
if ($section == 'qualifications') {
$display_parameters['display_qualifications'] = 'yes';
} else {
$display_parameters['display_qualifications'] = 'no';
}
if ($section == 'research_resources') {
$display_parameters['display_research_resources'] = 'yes';
} else {
$display_parameters['display_research_resources'] = 'no';
}
if ($section == 'services') {
$display_parameters['display_services'] = 'yes';
} else {
$display_parameters['display_services'] = 'no';
}
//
// format as HTML
$orcid_html = orcid_format_data_as_html($orcid_xml, $display_parameters);
$return_string = $orcid_html;
return $return_string;
}
/**
* Return data from ORCiD for a section
* Call back function for shortword [orcid-data section="section_name"]
*
* parameters:
*
* @param string $section - ORCiD section
* @return string ORCiD data as html
*
*/
function orcid_data_block_function($section = "", $works_type = "all", $works_start_year = "3000")
{
//return "<div>the way is void</div>";
//
// convert to lower case
$section = strtolower($section);
//
// we want to display the *AUTHOR's* (NOT the viewer's) data
//
// get the author's WordPress user id
$author = get_the_author_meta('ID', false);
//
// get orcid data
//
// we can either download the data from orcid.org OR use the cached value
// we download the data IFF ($download_from_orcid_flag = TRUE)
// 1) there is no cached xml data
// 2) the cached value is older than ORCID_CACHE_TIMEOUT (in seconds)
//
$download_from_orcid_flag = false;
//
// 2) there is no cached xml data
if (empty(get_user_meta($author, '_orcid_xml', true))) {
$download_from_orcid_flag = true;
}
//
// 3) the cached value is older than ORCID_CACHE_TIMEOUT (in seconds)
$current_time = time();
// last download time
$orcid_xml_download_time = intval(get_user_meta($author, '_orcid_xml_download_time', true));
//
$time_diff = $current_time - $orcid_xml_download_time;
if ($time_diff >= ORCID_CACHE_TIMEOUT) {
$download_from_orcid_flag = true;
}
if ($download_from_orcid_flag) {
// return '<p>Downloading XML data from orcid.org</p>' . PHP_EOL;
$orcid_id = get_user_meta($author, '_orcid_id', true);
$orcid_xml = orcid_download_data($orcid_id);
update_user_meta($author, '_orcid_xml', $orcid_xml);
//
// keep track of when download occurred
update_user_meta($author, '_orcid_xml_download_time', strval(time()));
} else {
// return '<p>Using cached XML data</p>' . PHP_EOL;
// return '<p>author WP id = ' . intval($author) . '</p>';
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$orcid_xml = get_user_meta($author, '_orcid_xml', true);
}
// $orcid_xml = get_user_meta($author, '_orcid_xml', TRUE);
//
// determine which section to display
if ($section == 'header') {
$display_parameters['display_header'] = 'yes';
} else {
$display_parameters['display_header'] = 'no';
}
if ($section == 'personal') {
$display_parameters['display_personal'] = 'yes';
} else {
$display_parameters['display_personal'] = 'no';
}
if ($section == 'education') {
$display_parameters['display_education'] = 'yes';
} else {
$display_parameters['display_education'] = 'no';
}
if ($section == 'employment') {
$display_parameters['display_employment'] = 'yes';
} else {
$display_parameters['display_employment'] = 'no';
}
if ($section == 'works') {
$display_parameters['display_works'] = 'yes';
$display_parameters['works_type'] = $works_type;
$display_parameters['works_start_year'] = $works_start_year;
} else {
$display_parameters['display_works'] = 'no';
$display_parameters['works_type'] = 'none';
$display_parameters['works_start_year'] = '3000';
}
if ($section == 'fundings') {
$display_parameters['display_fundings'] = 'yes';
} else {
$display_parameters['display_fundings'] = 'no';
}
if ($section == 'peer_reviews') {
$display_parameters['display_peer_reviews'] = 'yes';
} else {
$display_parameters['display_peer_reviews'] = 'no';
}
if ($section == 'invited_positions') {
$display_parameters['display_invited_positions'] = 'yes';
} else {
$display_parameters['display_invited_positions'] = 'no';
}
if ($section == 'memberships') {
$display_parameters['display_memberships'] = 'yes';
} else {
$display_parameters['display_memberships'] = 'no';
}
if ($section == 'qualifications') {
$display_parameters['display_qualifications'] = 'yes';
} else {
$display_parameters['display_qualifications'] = 'no';
}
if ($section == 'research_resources') {
$display_parameters['display_research_resources'] = 'yes';
} else {
$display_parameters['display_research_resources'] = 'no';
}
if ($section == 'services') {
$display_parameters['display_services'] = 'yes';
} else {
$display_parameters['display_services'] = 'no';
}
//
// format as HTML
$orcid_html = orcid_format_data_as_html($orcid_xml, $display_parameters);
$return_string = $orcid_html;
return $return_string;
}