Skip to content

Commit c0b02ad

Browse files
NathanKleekampscottgonzalez
authored andcommitted
Add support for the Media API
Closes gh-49
1 parent 4c3e564 commit c0b02ad

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

lib/fields.js

+72
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,78 @@ maps.file = createFieldMaps({
202202
postId: /* int */ "post_id"
203203
});
204204

205+
maps.media = createFieldMaps({
206+
attachmentId: /* string */ "attachment_id", /* readonly */
207+
caption: /* string */ "caption",
208+
description: /* string */ "description",
209+
link: /* string */ "link",
210+
parent: /* int */ "parent",
211+
thumbnail: /* string */ "thumbnail",
212+
title: /* string */ "title",
213+
type: /* string */ "type"
214+
}, {}, {
215+
date_created_gmt: /* datetime */ function( date ) {
216+
return {
217+
date: new Date( date )
218+
};
219+
},
220+
221+
metadata: /* struct */ function( data ) {
222+
return {
223+
metadata: mapFields( data, maps.mediaItemMetadata.from )
224+
};
225+
}
226+
});
227+
228+
maps.mediaItemMetadata = createFieldMaps({
229+
file: /* string */ "file",
230+
height: /* int */ "height",
231+
sizes: /* struct */ "sizes",
232+
width: /* int */ "width"
233+
}, {}, {
234+
sizes: /* struct */ function( size ) {
235+
var keys = Object.keys( size ),
236+
results = {};
237+
238+
// Loop through the available sizes and map the fields
239+
keys.forEach(function( key, i ) {
240+
results[ keys[ i ] ] = mapFields( size[ keys[ i ] ], maps.mediaItemSize.from );
241+
});
242+
243+
return {
244+
sizes: results
245+
};
246+
},
247+
248+
image_meta: /* struct */ function( data ) {
249+
return {
250+
imageMeta: mapFields( data, maps.postThumbnailImageMeta.from )
251+
};
252+
}
253+
});
254+
255+
maps.mediaItemSize = createFieldMaps({
256+
file: /* string */ "file",
257+
height: /* string */ "height",
258+
mimeType: /* string */ "mime-type",
259+
width: /* string */ "width"
260+
});
261+
262+
maps.postThumbnailImageMeta = createFieldMaps({
263+
aperture: /* int */ "aperture",
264+
camera: /* string */ "camera",
265+
caption: /* string */ "caption",
266+
copyright: /* string */ "copyright",
267+
createdTimestamp: /* int */ "created_timestamp",
268+
credit: /* string */ "credit",
269+
focalLength: /* int */ "focal_length",
270+
iso: /* int */ "iso",
271+
keywords: /* array */ "keywords",
272+
orientation: /* string */ "orientation",
273+
shutterSpeed: /* int */ "shutter_speed",
274+
title: /* string */ "title"
275+
});
276+
205277
module.exports = {
206278
to: function( data, type ) {
207279
return mapFields( data, maps[ type ].to );

lib/wordpress.js

+29
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,12 @@ extend( Client.prototype, {
150150
filter.post_type = filter.type;
151151
delete filter.type;
152152
}
153+
153154
if ( filter.status ) {
154155
filter.post_status = filter.status;
155156
delete filter.status;
156157
}
158+
157159
if ( filter.orderby ) {
158160
filter.orderby = fieldMap.array( [ filter.orderby ], "post" )[ 0 ];
159161
}
@@ -313,6 +315,33 @@ extend( Client.prototype, {
313315
});
314316

315317
extend( Client.prototype, {
318+
getMediaItem: function( id, fn ) {
319+
this.authenticatedCall( "wp.getMediaItem", id, function( error, media ) {
320+
if ( error ) {
321+
return fn( error );
322+
}
323+
324+
fn( null, fieldMap.from( media, "media" ) );
325+
});
326+
},
327+
328+
getMediaLibrary: function( filter, fn ) {
329+
if ( typeof filter === "function" ) {
330+
fn = filter;
331+
filter = {};
332+
}
333+
334+
this.authenticatedCall( "wp.getMediaLibrary", filter, function( error, media ) {
335+
if ( error ) {
336+
return fn( error );
337+
}
338+
339+
fn( null, media.map(function( item ) {
340+
return fieldMap.from( item, "media" );
341+
}));
342+
});
343+
},
344+
316345
uploadFile: function( data, fn ) {
317346
this.authenticatedCall( "wp.uploadFile", fieldMap.to( data, "file" ), fn );
318347
}

readme.md

+47
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ Deletes a taxonomy term.
188188

189189
### Media
190190

191+
#### client.getMediaItem( id, callback )
192+
193+
Gets a piece of media by ID.
194+
195+
* `id`: The ID of the piece of media to get.
196+
* `callback` (`function( error, media )` ): A callback to invoke when the API call is complete.
197+
198+
#### client.getMediaLibrary( [filter], callback )
199+
200+
* `filter` (optional): A hash of key/value pairs for filtering which posts to get.
201+
* `callback` (`function( error, media )` ): A callback to invoke when the API call is complete.
202+
191203
#### client.uploadFile( data, callback )
192204

193205
Uploads a file to Wordpress.
@@ -360,6 +372,41 @@ Invokes a method with the username and password provided by the client.
360372
* termId
361373
* termTaxonomyId
362374

375+
#### Media
376+
377+
* attachmentId
378+
* caption
379+
* date
380+
* description
381+
* link
382+
* metadata
383+
* file
384+
* height
385+
* imageMeta
386+
* aperture
387+
* camera
388+
* caption
389+
* copyright
390+
* createdTimestamp
391+
* credit
392+
* focalLength
393+
* iso
394+
* keywords
395+
* orientation
396+
* shutterSpeed
397+
* title
398+
* sizes
399+
* file
400+
* height
401+
* mimeType
402+
* width
403+
* width
404+
* parent
405+
* thumbnail
406+
* title
407+
* type
408+
409+
363410

364411

365412
## License

0 commit comments

Comments
 (0)