|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* The dashboard-specific functionality of the plugin. |
|
5
|
|
|
* |
|
6
|
|
|
* @link http://example.com |
|
7
|
|
|
* @since 1.0.0 |
|
8
|
|
|
* |
|
9
|
|
|
* @package PluginName |
|
10
|
|
|
* @subpackage PluginName/admin |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace logoscon\WP\RedmineEmbed\Redmine; |
|
14
|
|
|
|
|
15
|
|
|
use logoscon\WP\RedmineEmbed\Plugin; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The dashboard-specific functionality of the plugin. |
|
19
|
|
|
* |
|
20
|
|
|
* Defines the plugin name, version, and two examples hooks for how to |
|
21
|
|
|
* enqueue the dashboard-specific stylesheet and JavaScript. |
|
22
|
|
|
* |
|
23
|
|
|
* @package PluginName |
|
24
|
|
|
* @subpackage PluginName/admin |
|
25
|
|
|
* @author Your Name <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class Client { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The plugin's instance. |
|
31
|
|
|
* |
|
32
|
|
|
* @since 1.0.0 |
|
33
|
|
|
* @access private |
|
34
|
|
|
* @var Plugin $plugin This plugin's instance. |
|
35
|
|
|
*/ |
|
36
|
|
|
private $plugin; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Root URL for Redmine. |
|
40
|
|
|
* @access private |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $root_url = ''; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* REST API key for Redmine. |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $api_key = ''; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Initialize the class and set its properties. |
|
53
|
|
|
* |
|
54
|
|
|
* @since 1.0.0 |
|
55
|
|
|
* |
|
56
|
|
|
* @param Plugin $plugin This plugin's instance. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct( Plugin $plugin ) { |
|
59
|
|
|
$this->plugin = $plugin; |
|
60
|
|
|
$this->api_key = $this->get_api_key(); |
|
61
|
|
|
$this->root_url = \trailingslashit( $plugin->get_option( 'root_url' ) ); |
|
62
|
|
|
$this->url = new UrlBuilder( $plugin ); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Fetch an issue. |
|
67
|
|
|
* @param string $id Issue ID. |
|
68
|
|
|
* @param array $options Request options. |
|
69
|
|
|
* @param array $expires Cache TTL, in seconds (defaults to 3600). |
|
|
|
|
|
|
70
|
|
|
* @return mixed Response data. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function get_issue( $id, $options = array(), $expires = 3600 ) { |
|
73
|
|
|
$url = $this->url->get_json_resource_url( 'issues', $id ); |
|
74
|
|
|
|
|
75
|
|
|
return json_decode( $this->get( $url, $options, $expires ) ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Handle GET requests. |
|
80
|
|
|
* @param string $resource Resource to fetch |
|
|
|
|
|
|
81
|
|
|
* @param array $options Request options. |
|
82
|
|
|
* @param array $expires Cache TTL, in seconds (defaults to 3600). |
|
|
|
|
|
|
83
|
|
|
* @return mixed Response data. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function get( $url, $options = array(), $expires = 3600 ) { |
|
86
|
|
|
$options = $this->add_credentials( $options ); |
|
87
|
|
|
|
|
88
|
|
|
if ( $expires === false ) { |
|
89
|
|
|
// Bypass cache |
|
90
|
|
|
return $this->get_body( $url, $options ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$cache_key = sha1( $url . $this->api_key ); |
|
94
|
|
|
|
|
95
|
|
|
return \tlc_transient( $cache_key ) |
|
96
|
|
|
->updates_with( array( $this, 'get_body' ), array( $url, $options ) ) |
|
97
|
|
|
->expires_in( $expires ) |
|
98
|
|
|
->get(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Fetch the response body for a GET request. |
|
103
|
|
|
* @param string $resource Resource to fetch |
|
|
|
|
|
|
104
|
|
|
* @param array $options Request options. |
|
105
|
|
|
* @return string Request body. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function get_body( $url, $options ) { |
|
108
|
|
|
$response = \wp_remote_get( $url, $options ); |
|
109
|
|
|
$code = \wp_remote_retrieve_response_code( $response ); |
|
110
|
|
|
$message = \wp_remote_retrieve_response_message( $response ); |
|
111
|
|
|
|
|
112
|
|
|
if ( substr( $code, 0, 1 ) !== '2' ) { |
|
113
|
|
|
// A non-2xx class status code means there was an error. |
|
114
|
|
|
throw new \Exception( $message, (int) $code ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return \wp_remote_retrieve_body( $response ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Add credentials to a set of request options. |
|
122
|
|
|
* @param array $options Request options. |
|
123
|
|
|
*/ |
|
124
|
|
|
private function add_credentials( $options = array() ) { |
|
125
|
|
|
if ( ! isset( $options['headers'] ) ) { |
|
126
|
|
|
$options['headers'] = array(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$options['headers']['X-Redmine-API-Key'] = $this->api_key; |
|
130
|
|
|
|
|
131
|
|
|
return $options; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the configured Redmine API key. |
|
136
|
|
|
* |
|
137
|
|
|
* Looks for the user key first and falls back to the globally configured one. |
|
138
|
|
|
* |
|
139
|
|
|
* @return string Configured Redmine API key. |
|
140
|
|
|
*/ |
|
141
|
|
|
private function get_api_key() { |
|
142
|
|
|
$user_id = \get_current_user_id(); |
|
143
|
|
|
$api_key = \get_user_option( 'redmine_embed_api_key', $user_id ); |
|
144
|
|
|
|
|
145
|
|
|
if ( empty( $api_key ) ) { |
|
146
|
|
|
$api_key = $this->plugin->get_option( 'api_key' ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return \sanitize_key( $api_key ); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: