-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson-image-resolver.php
163 lines (137 loc) · 4.96 KB
/
json-image-resolver.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
<?php
/**
* Plugin Name: JSON image resolver
* Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
* Description: Get one or more images from wordpress media library.
* Version: 1.0.0
* Author: João Beno Schreiner Junior
* Author URI: http://joao.beno.net.br
* License: GPLv2
*/
class Images_API_Endpoint{
/** Hook WordPress
* @return void
*/
public function __construct(){
add_filter('query_vars', array($this, 'add_query_vars'), 0);
add_action('parse_request', array($this, 'receive_requests'), 0);
add_action('init', array($this, 'add_endpoint'), 0);
}
/** Add public query vars
* @param array $vars List of current public query vars
* @return array $vars
*/
public function add_query_vars($vars){
$vars[] = 'jir';
$vars[] = 'images';
return $vars;
}
/** Add API Endpoint
* @return void
*/
public function add_endpoint(){
add_rewrite_rule('^api/pugs/?([0-9]+)?/?','index.php?jir=1&images=$matches[1]','top');
add_rewrite_rule('^api/pugs/?([0-9]+)?/?','index.php?jir=1&image=$matches[1]','top');
}
/** Receive Requests
* If $_GET['jir'] is set, we reply
* @return die if API request
*/
public function receive_requests(){
global $wp;
if(isset($wp->query_vars['jir'])){
$this->handle_request();
exit;
}
}
/** Handle Requests
* @return void
*/
protected function handle_request(){
global $wp;
$query_vars = $wp->query_vars;
$imgs = $query_vars['images'];
//If you want everything or just many things
if ($imgs) {
$param = $imgs;
$imgs = $this->display_images_from_media_library($imgs);
if($imgs)
$this->send_response('ok', $imgs);
else
$this->send_response('Something went wrong with the all images function');
}
}
/** Response Handler
* This sends a JSON response to the browser
*/
protected function send_response($msg, $json_img = ''){
$response['status'] = $msg;
if ($json_img) {
$response['count'] = count($json_img);
$response['images'] = $json_img;
}
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Origin, Access-Control-Allow-Methods');
header('content-type: application/json; charset=utf-8');
echo json_encode($response)."\n";
exit;
}
protected function get_images_from_media_library($all_images) {
$args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => -1
);
$query_images = new WP_Query( $args );
$images = array();
foreach ( $query_images->posts as $image) {
if ($all_images === "all") {
$l_image = array();
$l_image["id"]=$image->ID;
$l_image["url"]=$image->guid;
$l_image["description"]=$image->post_content;
$l_image["title"]=$image->post_title;
$l_image["caption"]=$image->post_excerpt;
$images[]= $l_image;
} else if (is_string($all_images) && strpos($all_images,",")) {
$img_ids = explode(",",$all_images);
$img_id = intval($image->ID);
foreach ($img_ids as $iid) {
if ($img_id == intval($iid)) {
$l_image = array();
$l_image["id"]=$image->ID;
$l_image["url"]=$image->guid;
$l_image["description"]=$image->post_content;
$l_image["title"]=$image->post_title;
$l_image["caption"]=$image->post_excerpt;
$images[]= $l_image;
}
}
} else {
$req_id = intval($all_images);
$img_id = intval($image->ID);
if ($img_id == $req_id) {
$l_image = array();
$l_image["id"]=$image->ID;
$l_image["url"]=$image->guid;
$l_image["description"]=$image->post_content;
$l_image["title"]=$image->post_title;
$l_image["caption"]=$image->post_excerpt;
$images[]= $l_image;
}
}
}
return $images;
}
protected function display_images_from_media_library($all_or_some = true) {
$imgs = $this->get_images_from_media_library($all_or_some);
$html = array();
foreach($imgs as $img) {
$html[]= $img;
}
return $html;
}
}
new Images_API_Endpoint();