-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.php
106 lines (93 loc) · 3.73 KB
/
fetch.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
<?php
namespace Openpublishing\Fetch;
// When authorization request fails retry 1 time
$GLOBALS['openpublishing_auth_retry'] = true;
function openpublishing_print_debug($msg) {
print('<span class="OP_debug" style="display:none;">' . $msg . '<br></span>');
}
function openpublishing_get_auth_token() {
$new_token = '';
$HOST = 'https://' . get_option('openpublishing_api_host') . '/auth/auth?';
$REALM_ID = get_option('openpublishing_realm_id');
$url = $HOST . 'realm_id=' . $REALM_ID . '&type=world';
$response = wp_remote_get($url);
if ( is_wp_error( $response ) ) {
error_log("[ERROR] " . $response->get_error_message() . ' ' . $url);
} else {
$status = wp_remote_retrieve_response_code( $response );
if ( $status === 200 ) {
$json = json_decode(wp_remote_retrieve_body($response));
$new_token = $json->{'auth_token'};
update_option('openpublishing_auth_token', $new_token);
} else {
error_log("[ERROR]" . $url .' - '. $status);
}
}
return $new_token;
}
function openpublishing_get_with_auth($url, $try_again) {
$token = get_option('openpublishing_auth_token');
if (!$token) {
$token = openpublishing_get_auth_token();
}
$options = array( 'headers' => array( 'Authorization' => 'Bearer ' . $token));
$response = wp_remote_get($url, $options);
if ( is_wp_error( $response ) ) {
error_log("[ERROR] failed to get " . $url);
if ($try_again) {
$response = openpublishing_get_with_auth($url, false);
update_option('openpublishing_auth_token', '');
}
}
return $response;
}
function openpublishing_fetch_objects($object_name, $id, $lang, $is_collection = false) {
$HOST = 'https://' . get_option('openpublishing_api_host') . '/resource/v2/';
$ASPECT = '[:basic,non_academic.realm_genres.*]';
$OBJECT = 'document';
$guid = $object_name . '.' . $id;
if ($is_collection) {
$url = $HOST.$OBJECT.$ASPECT.'?sort='.$object_name.'__asc&cache=yes&display=10'.($lang?'&language='.$lang:'');
}
else {
$url = $HOST.$guid.$ASPECT.($lang?'?language='.$lang:'');
}
openpublishing_print_debug('<b>'.$object_name.':'.$id.($lang?':'.$lang:'').'</b></br>'.$url);
$response = openpublishing_get_with_auth($url, true);
if ( is_wp_error( $response ) ) {
error_log("[ERROR] " . $response->get_error_message() . ' ' . $url);
openpublishing_print_debug("[ERROR] " . $response->get_error_message() . ' ' . $url);
} else {
$status = wp_remote_retrieve_response_code($response);
if (200 == $status) {
$json = json_decode(wp_remote_retrieve_body($response));
return $json;
}
}
}
function openpublishing_fetch_templates() {
$tmpls = array();
for ($element = 1; $element <= 10; $element++) {
$tag = 'openpublishing_template_tag_' . $element;
$template = 'openpublishing_template_id_' . $element;
$id = get_option($template);
$name = get_option($tag);
$tmpl_content = '';
if (empty($id) && empty($name)) {
break;
}
// if Elementor is installed
if(defined('ELEMENTOR_PATH') && class_exists('Elementor\Widget_Base')) {
$tmpl_content = \Elementor\Plugin::$instance->frontend->get_builder_content($id);
}
//if content is still empty then retrieve it by means of wordpress
if (empty($tmpl_content)) {
$tmpl_content = get_post_field('post_content', $id);
}
if (!empty($tmpl_content)) {
$tmpls[$name] = $tmpl_content;
}
}
return $tmpls;
}
?>