-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-api-data-package-handler.php
More file actions
318 lines (259 loc) · 7 KB
/
class-api-data-package-handler.php
File metadata and controls
318 lines (259 loc) · 7 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* Abstract BB_WP_API_Handler_Data_Package class.
*
* creates a custom data_package
*
* @abstract
*/
abstract class BB_WP_API_Handler_Data_Package {
/* set in which modelclasses this datapackage is allowed */
protected $modelclasses = array();
public function __construct() {
}
/**
* is_modellclass_allowed function.
*
* test this before you apply any data package to an api handler
*
* @access public
* @param mixed $modelclass
* @return bool
*/
public function is_modellclass_allowed( $modelclass ) {
if( in_array($modelclass, $this->modelclasses))
return true;
return false;
}
/**
* read function.
*
* must be supplied by the child class
*
* @access public
* @abstract
* @param mixed $object
* @return void
*/
abstract function read($object);
/**
* create function.
*
* can be supplied by the child class
*
* @access public
* @param mixed $object_id
* @param array $data (default: array())
* @return void
*/
public function create($object_id, $data = array()) {}
/**
* update function.
*
* can be supplied by the child class
*
* @access public
* @param mixed $object_id
* @param array $data (default: array())
* @return void
*/
public function update($object_id, $data = array()) {}
/**
* delete function.
*
* can be supplied by the child class
*
* @access public
* @param mixed $object_id
* @param array $data (default: array())
* @return void
*/
public function delete($object_id, $data = array()) {}
}
/* ADDITIONAL DATAPACKAGES */
/**
* BB_WP_API_Handler_Data_Package_Postmeta class.
*
* @extends BB_WP_API_Handler_Data_Package
*/
class BB_WP_API_Handler_Data_Package_Postmeta extends BB_WP_API_Handler_Data_Package {
protected $modelclasses = array('post', 'attachment');
/**
* read function.
*
* reads the postmeta
*
* @access public
* @param mixed $object
* @return array
*/
public function read($object) {
$meta = get_post_custom( $object->ID );
if( ! empty($meta)) {
foreach ($meta as $k => $v) {
$meta[$k] = maybe_unserialize($v[0]) ;
}
}
return $meta;
}
/**
* create function.
*
* adds postmeta to the database
*
* @access public
* @param mixed $object_id
* @param array $data (default: array())
* @return void
*/
public function create($object_id, $data = array()) {
foreach($data as $key => $value) {
add_post_meta($object_id, $key, $value );
}
}
/**
* update function.
*
* updates postmeta in the database
*
* @access public
* @param mixed $object_id
* @param array $data (default: array())
* @return void
*/
public function update($object_id, $data = array()) {
foreach($data as $key => $value) {
update_post_meta($object_id, $key, $value );
}
}
}
/**
* BB_WP_API_Handler_Data_Package_Author class.
*
* @extends BB_WP_API_Handler_Data_Package
*/
class BB_WP_API_Handler_Data_Package_Author extends BB_WP_API_Handler_Data_Package {
protected $modelclasses = array('post', 'comment', 'user', 'attachment');
/**
* read function.
*
* retrieves the user of a post, comment, WP_User
*
* @access public
* @param mixed $object
* @return array
*/
public function read($object) {
$author = array();
if ( $object instanceof WP_Post )
$id = $object->post_author;
elseif ( isset($object->comment_ID )) // a WP Comment
$id = $object->user_id;
elseif ( $object instanceof WP_User )
$id = $object->ID;
elseif ( is_numeric($object) )
$id = absint($object);
else
return array(); // exit
//@TODO doesnt go through by comments
$avatar = str_replace("avatar ", "avatar media-object pull-left ", get_avatar( $id, 32 ));
$author['ID'] = $id;
$author['display_name'] = get_the_author_meta( 'display_name', $id );
$author['avatar'] = $avatar;
$author['email'] = get_the_author_meta( 'user_email', $id );
return $author;
}
}
/**
* BB_WP_API_Handler_Data_Package_Cap class.
*
* @extends BB_WP_API_Handler_Data_Package
*/
class BB_WP_API_Handler_Data_Package_Cap extends BB_WP_API_Handler_Data_Package {
protected $modelclasses = array('post', 'comment', 'attachment');
/**
* read function.
*
* retrieves the permission to update, delete a model
*
* @access public
* @param mixed $object
* @return array
*/
public function read($object) {
$permissions = array();
if ( $object instanceof WP_Post ) {
$permissions['edit'] = ( current_user_can( 'edit_post', $object->ID ) ) ? 1 : 0 ;
$permissions['delete'] = ( current_user_can( 'delete_post', $object->ID ) ) ? 1 : 0 ;
}
if ( isset($object->comment_ID ) ) { // a comment
//@TODO better
$comment_author = $object->user_id;
$permissions['edit'] = ( $comment_author == wp_get_current_user()->ID ) ? 1 : 0;
$permissions['delete'] = ( $comment_author == wp_get_current_user()->ID ) ? 1 : 0;
}
return $permissions;
}
}
class BB_WP_API_Handler_Data_Package_Nonces extends BB_WP_API_Handler_Data_Package {
protected $modelclasses = array('post', 'attachment', 'comment');
/**
* read function.
*
* retrieves different imagessizes for the attachment
*
* @access public
* @param mixed $object
* @return array
*/
public function read($object) {
$nonces = array();
if ( $object instanceof WP_Post ) {
$nonces['nonce_delete'] = wp_create_nonce( 'delete' . $object->ID );
$nonces['nonce_create'] = wp_create_nonce( 'create' . $object->ID );
$nonces['nonce_update'] = wp_create_nonce( 'update' . $object->ID );
}
return $nonces;
}
}
class BB_WP_API_Handler_Data_Package_Embedder extends BB_WP_API_Handler_Data_Package {
protected $modelclasses = array('attachment');
public function read($object) {
$array = array();
$mime = $object->post_mime_type;
$icon = wp_mime_type_icon($mime);
$metadata = wp_get_attachment_metadata( $object->ID );
$embed = '';
$html = '';
$type = (wp_attachment_is_image($object->ID )) ? 'image' : 'misc';
$frame = ($type =='image') ? "image" : "inline";
switch($type) {
case('misc'):
$embed = '<embed class="media" src="' . $object->guid . '">';
$html = '<a href="#" class="magnific"><img src="' . $icon .'"></a>';
break;
case('image'):
$embed = '';
$html = '<a href="#" class="magnific"><img src="' . wp_get_attachment_thumb_url($object->ID) .'"></a>';
break;
}
$array['type'] = $type;
$array['html'] = $html;
$array['embed'] = $embed;
$array['frame'] = $frame;
return $array;
}
}
class BB_WP_API_Handler_Data_Package_Loginuser_Meta extends BB_WP_API_Handler_Data_Package {
protected $modelclasses = array('idone');
public function read($id, $data = array() ) {
/* try to login */
$user_signon = wp_signon( $data, false );
if ( is_wp_error($user_signon) ) {
return $user_signon;
} else {
return array('user_login' => 'ok');
}
// no return on update, only errors
}
}