1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* pixabay-php-api |
4
|
|
|
* PixabayClient API |
5
|
|
|
* |
6
|
|
|
* PHP Version 5 |
7
|
|
|
* |
8
|
|
|
* @category Production |
9
|
|
|
* @package Default |
10
|
|
|
* @author Philipp Tkachev <[email protected]> |
11
|
|
|
* @date 12/14/14 9:18 AM |
12
|
|
|
* @license https://www.zoonman.com/projects/pixabay/license.txt MIT |
13
|
|
|
* @version GIT: 1.0 |
14
|
|
|
* @link https://www.zoonman.com/projects/pixabay/ |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FooGallery_PixabayClient |
19
|
|
|
*/ |
20
|
|
|
class FooGallery_PixabayClient { |
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $optionsList = [ |
25
|
|
|
'key', |
26
|
|
|
'response_group', |
27
|
|
|
'id', |
28
|
|
|
'q', |
29
|
|
|
'lang', |
30
|
|
|
'callback', |
31
|
|
|
'image_type', |
32
|
|
|
'orientation', |
33
|
|
|
'category', |
34
|
|
|
'min_width', |
35
|
|
|
'min_height', |
36
|
|
|
'editors_choice', |
37
|
|
|
'safesearch', |
38
|
|
|
'page', |
39
|
|
|
'per_page', |
40
|
|
|
'pretty', |
41
|
|
|
'response_group', |
42
|
|
|
'order', |
43
|
|
|
'video_type' |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Root of Pixabay REST API |
48
|
|
|
*/ |
49
|
|
|
const API_ROOT = 'https://pixabay.com/api/'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get Data from Pixabay API |
53
|
|
|
* |
54
|
|
|
* @param $key |
55
|
|
|
* @param $query |
56
|
|
|
* @param int $count |
57
|
|
|
* @param string $image_type |
58
|
|
|
* @param string $response_group |
59
|
|
|
* |
60
|
|
|
* @param string $safesearch |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function search( $key, $query, $count = 20, $image_type = 'photo', $response_group = 'high_resolution', $safesearch = 'true') |
65
|
|
|
{ |
66
|
|
|
$url = add_query_arg( array( |
67
|
|
|
'key' => $key, |
68
|
|
|
'q' => urlencode( $query ), |
69
|
|
|
'per_page' => $count, |
70
|
|
|
'image_type' => $image_type, |
71
|
|
|
'response_group' => $response_group, |
72
|
|
|
'safesearch' => $safesearch |
73
|
|
|
), self::API_ROOT ); |
74
|
|
|
|
75
|
|
|
$transient_key = 'foogallery-pixabay-' . urlencode($query) . '-' . $count; |
76
|
|
|
|
77
|
|
|
if ( false === ( $response_data = get_transient( $transient_key ) ) ) { |
78
|
|
|
$response = wp_remote_get( $url ); |
79
|
|
|
$response_data = wp_remote_retrieve_body( $response ); |
80
|
|
|
|
81
|
|
|
$expires = 60 * 60 * 24; //cache for 24 hours |
82
|
|
|
|
83
|
|
|
//Cache the result |
84
|
|
|
set_transient( $transient_key, $response_data, $expires ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return json_decode( $response_data, false ); |
88
|
|
|
} |
89
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.