forked from Automattic/media-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.response.php
205 lines (165 loc) · 4.4 KB
/
class.response.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
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
<?php
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/**
* Response class. A service's request() method should return a Response object. This
* is used to populate the AJAX request's JSON response.
*/
final class MEXP_Response {
public $items = array();
public $meta = array(
'count' => null,
'max_id' => null,
'min_id' => null,
);
/**
* Add a meta value to the response. Accepts a key/value pair or an associative array.
*
* @param string|array $key The meta key, or an associative array of meta keys/values.
* @param mixed $value The meta value.
* @return null
*/
public function add_meta( $key, $value = null ) {
if ( is_array( $key ) ) {
foreach ( $key as $k => $v )
$this->meta[$k] = $v;
} else {
$this->meta[$key] = $value;
}
}
/**
* Add a response item to the response.
*
* @param Response_Item A response item.
* @return null
*/
public function add_item( MEXP_Response_Item $item ) {
$this->items[] = $item;
}
/**
* Retrieve the response output.
*
* @return array|bool The response output, or boolean false if there's nothing to output.
*/
public function output() {
if ( empty( $this->items ) )
return false;
if ( is_null( $this->meta['count'] ) )
$this->meta['count'] = count( $this->items );
if ( is_null( $this->meta['min_id'] ) )
$this->meta['min_id'] = reset( $this->items )->id;
$output = array(
'meta' => $this->meta,
'items' => array()
);
foreach ( $this->items as $item )
$output['items'][] = $item->output();
return $output;
}
}
/**
* Response Item class. Used within the Response class to populate the items in a response.
*/
final class MEXP_Response_Item {
public $id = null;
public $url = null;
public $thumbnail = null;
public $content = null;
public $date = null;
public $date_format = null;
public $meta = array();
/**
* Set the ID for the response item.
*
* @param int $id The response item ID.
* @return null
*/
public function set_id( $id ) {
$this->id = $id;
}
/**
* Set the URL for the response item.
*
* @param string $url The response item URL.
* @return null
*/
public function set_url( $url ) {
$this->url = esc_url_raw( $url );
}
/**
* Set the thumbnail URL for the response item.
*
* @param string $thumbnail The response item thumbnail URL.
* @return null
*/
public function set_thumbnail( $thumbnail ) {
$this->thumbnail = esc_url_raw( $thumbnail );
}
/**
* Set the content for the response item.
*
* @param string $content The response item content.
* @return null
*/
public function set_content( $content ) {
$this->content = $content;
}
/**
* Set the date for the response item.
*
* @param int $date The response item date in UNIX timestamp format.
* @return null
*/
public function set_date( $date ) {
$this->date = $date;
}
/**
* Set the date format for the response item date.
*
* @param string $date_format The date format in PHP date() format.
* @return null
*/
public function set_date_format( $date_format ) {
$this->date_format = $date_format;
}
/**
* Add a meta value to the response item. Accepts a key/value pair or an associative array.
*
* @param string|array $key The meta key, or an associative array of meta keys/values.
* @param mixed $value The meta value.
* @return null
*/
public function add_meta( $key, $value = null ) {
if ( is_array( $key ) ) {
foreach ( $key as $k => $v )
$this->meta[$k] = $v;
} else {
$this->meta[$key] = $value;
}
}
/**
* Retrieve the response item output.
*
* @return array The response item output.
*/
public function output() {
if ( is_null( $this->date_format ) )
$this->date_format = get_option( 'date_format' );
return array(
'id' => $this->id,
'url' => $this->url,
'thumbnail' => $this->thumbnail,
'content' => $this->content,
'date' => date( $this->date_format, $this->date ),
'meta' => $this->meta,
);
}
}