-
Notifications
You must be signed in to change notification settings - Fork 3
/
wpdotorg-api.php
176 lines (126 loc) · 4.64 KB
/
wpdotorg-api.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
<?php
// 0 - none
define( 'WPDODEBUG_NONE', 0 );
// 1 - call logging only
define( 'WPDODEBUG_CALL', 1 );
// 2 - calls, and responses
define( 'WPDODEBUG_RESP', 2 );
// Selected debug level
define( 'WPDO_API_LEVEL', WPDODEBUG_NONE );
/**
* This class contains all the functions that actually retrieve information from the GitHub API
*/
class wpdotorg_api {
/**
* Limit chance of timeouts
*/
function __construct() {
add_filter( 'http_request_timeout', array( $this, 'http_request_timeout' ) );
}
/**
* Extend the timeout since API calls can exceed 5 seconds
* @param int $seconds The current timeout setting
* @return int The revised timeout setting
*/
function http_request_timeout( $seconds ) {
return $seconds < 10 ? 10 : $seconds;
}
/**
* Call the WP.org API for the request
* @param string $api_url The API endpoint URL to call
* @param string $action The action we're performing at that URL
* @param object $req The request data
* @return mixed The response from the API
*/
private function call_api( $api_url, $action, $req ) {
$args = array( 'user-agent' => 'WordPress WPDotOrg oEmbed plugin - https://github.com/leewillis77/wp-wpdotorg-embed');
$this->log( __FUNCTION__ . " : $api_url\nACTION: " . print_r( $action, 1 ) . "\nDATA: " . print_r( serialize( $req ), 1 ), WPDODEBUG_CALL );
$results = wp_remote_post( $api_url, array( 'body' => array( 'action' => $action, 'request' => serialize( $req ) ) ) );
$this->log( __FUNCTION__ . " : " . print_r( $results, 1 ), WPDODEBUG_RESP );
if ( is_wp_error( $results ) ||
! isset( $results['response']['code'] ) ||
$results['response']['code'] != '200' ) {
header( 'HTTP/1.0 404 Not Found' );
die( 'Mike Little is lost, and afraid' );
}
return $results;
}
/**
* Get plugin information from the WP.org API
* @param string $slug The plugin slug
* @return object The response from the WP.org API
*/
public function get_plugin( $slug ) {
$this->log( "get_plugin( $slug )", WPDODEBUG_CALL );
$plugin = trim( $slug, '/' );
$args = new stdClass();
$args->slug = $plugin;
$args->fields = array(
'version',
'author',
'requires',
'tested',
'downloaded',
'rating',
'num_ratings',
'sections',
'download_link',
'description',
'short_description',
'name',
'slug',
'author_profile',
'homepage',
'contributors',
'added',
'last_updated',
);
$results = $this->call_api( 'http://api.wordpress.org/plugins/info/1.0/', 'plugin_information', $args );
$this->log( "get_plugin( $slug )\n" . print_r( $results, 1 ), WPDODEBUG_RESP );
return maybe_unserialize( $results['body'] );
}
/**
* Get theme information from the WP.org API
* @param string $slug The theme slug
* @return object The response from the WP.org API
*/
public function get_theme( $slug ) {
$this->log( "get_theme( $slug )", WPDODEBUG_CALL );
$theme = trim( $slug, '/' );
$args = new stdClass();
$args->slug = $theme;
$args->fields = array(
'version',
'author',
'requires',
'tested',
'downloaded',
'rating',
'num_ratings',
'sections',
'download_link',
'description',
'short_description',
'name',
'slug',
'author_profile',
'homepage',
'contributors',
'added',
'last_updated',
);
$results = $this->call_api( 'http://api.wordpress.org/themes/info/1.0/', 'theme_information', $args );
$this->log( "get_theme( $slug )\n" . print_r( $results, 1 ), WPDODEBUG_CALL );
return maybe_unserialize( $results['body'] );
}
/**
* Internal logging function
* @param string $msg The message to log
* @param int $level The level of this message
*/
private function log( $msg, $level ) {
if ( WPDO_API_LEVEL >= $level ) {
error_log( "[WPDOE$level]: ".$msg );
}
}
}