This repository was archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-packagist-api.php
More file actions
223 lines (183 loc) · 5.32 KB
/
wp-packagist-api.php
File metadata and controls
223 lines (183 loc) · 5.32 KB
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
<?php
/**
* WP Packagist API (https://packagist.org/apidoc)
*
* @package WP-Packagist-API
*/
/*
Plugin Name: WP Packagist API
Plugin URI: https://github.com/wp-api-libraries/wp-packagist-api
Description: Perform API requests to packagist in WordPress.
Author: WP API Libraries
Version: 1.0.0
Text Domain: wp-packagist-api
Author URI: https://wp-api-libraries.com/
GitHub Plugin URI: https://github.com/wp-api-libraries/wp-packagist-api
GitHub Branch: master
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) { exit; }
/* Check if class exists. */
if ( ! class_exists( 'PackagistAPI' ) ) {
/**
* PackagistAPI class.
*/
class PackagistAPI {
/**
* URL to the API.
*
* @var string
*/
private $base_uri = 'https://packagist.org';
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct() {
}
/**
* Fetch the request from the API.
*
* @access private
* @param mixed $request Request URL.
* @return $body Body.
*/
private function fetch( $request ) {
$response = wp_remote_get( $request );
$code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $code ) {
return new WP_Error( 'response-error', sprintf( __( 'Server response code: %d', 'wp-packagist-api' ), $code ) );
}
$body = wp_remote_retrieve_body( $response );
return json_decode( $body );
}
/**
* Get All Packages.
*
* @access public
* @return Json Array of Packages.
*/
public function get_all_packages() {
$request = $this->base_uri . '/packages/list.json';
return $this->fetch( $request );
}
/**
* Get Packages by Organization.
*
* @access public
* @param mixed $org Organization.
* @return Json Array of Packages.
*/
public function get_packages_by_org( $org ) {
if ( empty( $org ) ) {
return new WP_Error( 'required-fields', __( 'Please provide an organization name.', 'wp-packagist-api' ) );
}
$request = $this->base_uri . '/packages/list.json?vendor=' . $org ;
return $this->fetch( $request );
}
/**
* Get Package by Type.
*
* @access public
* @param mixed $type Type.
* @return Json Array of Packages.
*/
public function get_packages_by_type( $type ) {
if ( empty( $type ) ) {
return new WP_Error( 'required-fields', __( 'Please provide a package type.', 'wp-packagist-api' ) );
}
$request = $this->base_uri . '/packages/list.json?type=' . $type ;
return $this->fetch( $request );
}
/**
* Search Packages.
*
* @access public
* @param mixed $query Query.
* @param string $tag (default: '') Tag.
* @param string $type (default: '') Type.
* @return Json Array of Packages.
*/
public function search_packages( $query, $tag = '', $type = '' ) {
if ( empty( $query ) ) {
return new WP_Error( 'required-fields', __( 'Please provide something to search.', 'wp-packagist-api' ) );
}
$request = $this->base_uri . '/search.json?q=' . $query . '&tags=' . $tag . '&type=' . $type;
return $this->fetch( $request );
}
/**
* Search Packages by Name.
*
* @access public
* @param mixed $query Query.
* @return Json Array of Packages.
*/
public function search_packages_by_name( $query ) {
if ( empty( $query ) ) {
return new WP_Error( 'required-fields', __( 'Please provide something to search.', 'wp-packagist-api' ) );
}
$request = $this->base_uri . '/search.json?q=' . $query ;
return $this->fetch( $request );
}
/**
* Search Packages by tag.
*
* @access public
* @param mixed $tag Tag.
* @return Json Array of Packages.
*/
public function search_packages_by_tag( $tag ) {
if ( empty( $tag ) ) {
return new WP_Error( 'required-fields', __( 'Please provide a tag to search.', 'wp-packagist-api' ) );
}
$request = $this->base_uri . '/search.json?tags=' . $tag ;
return $this->fetch( $request );
}
/**
* Search Packages by Type.
*
* @access public
* @param mixed $type Type.
* @return Json Array of Packages.
*/
public function search_packages_by_type( $type ) {
if ( empty( $type ) ) {
return new WP_Error( 'required-fields', __( 'Please provide a package type to search.', 'wp-packagist-api' ) );
}
$request = $this->base_uri . '/search.json?type=' . $type ;
return $this->fetch( $request );
}
/**
* Get Package Data.
*
* @access public
* @param mixed $org Organization.
* @param mixed $package Package.
* @return Package Data.
*/
public function get_package_data( $org, $package ) {
if ( empty( $org ) || empty( $package ) ) {
return new WP_Error( 'required-fields', __( 'Please provide the required fields.', 'wp-packagist-api' ) );
}
$request = $this->base_uri . '/p/' . $org . '/' . $package . '.json';
return $this->fetch( $request );
}
/**
* Get Package Data using Fallback Method.
*
* @access public
* @param mixed $org Organization.
* @param mixed $package Package.
* @return Package Data.
*/
public function get_package_data_fallback_method( $org, $package ) {
if ( empty( $org ) || empty( $package ) ) {
return new WP_Error( 'required-fields', __( 'Please provide the required fields.', 'wp-packagist-api' ) );
}
$request = $this->base_uri . '/packages/' . $org . '/' . $package . '.json';
return $this->fetch( $request );
}
}
}