UNPKG

wordpress

Version:
291 lines (264 loc) 7.94 kB
var maps = {}; function extend( a, b ) { for ( var p in b ) { a[ p ] = b[ p ]; } return a; } function createFieldMaps( renames, toFns, fromFns ) { var to = extend( {}, renames ), from = {}; Object.keys( renames ).forEach(function( key ) { from[ renames[ key ] ] = key; }); return { renames: renames, to: extend( to, toFns ), from: extend( from, fromFns ) }; } function mapFields( data, map ) { var field, value, mappedField, ret = {}; for ( field in data ) { value = data[ field ]; mappedField = map[ field ]; // no map -> delete if ( !mappedField ) { continue; // string -> change field name } else if ( typeof mappedField === "string" ) { ret[ mappedField ] = value; // function -> merge result } else { extend( ret, mappedField( value ) ); } } return ret; } maps.labels = createFieldMaps({ addNewItem: "add_new_item", addOrRemoveItems: "add_or_remove_items", allItems: "all_items", chooseFromMostUsed: "choose_from_most_used", editItem: "edit_item", menuName: "menu_name", name: "name", nameAdminBar: "name_admin_bar", newItemName: "new_item_name", parentItem: "parent_item", parentItemColon: "parent_item_colon", popularItems: "popular_items", searchItems: "search_items", separateItemsWithCommas: "separate_items_with_commas", singularName: "singular_name", updateItem: "update_item", viewItem: "view_item" }); maps.post = createFieldMaps({ author: /* int */ "post_author", commentStatus: /* string */ "comment_status", content: /* string */ "post_content", customFields: /* array */ "custom_fields", date: /* datetime */ "post_date", excerpt: /* string */ "post_excerpt", format: /* string */"post_format", id: /* string */ "post_id", /* readonly */ link: /* string */ "link" /* readonly */, modified: /* datetime */ "post_modified", menuOrder: /* int */ "menu_order", name: /* string */ "post_name", pageTemplate: /* string */ "page_template", parent: /* int */ "post_parent", password: /* string */ "post_password", pingStatus: /* string */ "ping_status", status: /* string */ "post_status", sticky: /* bool */ "sticky", terms: /* struct */ "terms" /* array */, termNames: /* struct */ "terms_names", thumbnail: /* int */ "post_thumbnail", title: /* string */ "post_title", type: /* string */ "post_type" }, {}, { post_date_gmt: /* datetime */ function( date ) { return { date: new Date( date ) }; }, post_modified_gmt: /* datetime */ function( date ) { return { modified: new Date( date ) }; } }); maps.postType = createFieldMaps({ _builtin: /* bool */ "_builtin", cap: /* struct */ "cap", capabilityType: /* string */ "capability_type", description: /* string */ "description", _editLink: /* string */ "_edit_link", excludeFromSearch: /* bool */ "exclude_from_search", hasArchive: /* bool */ "has_archive", hierarchical: /* bool */ "hierarchical", label: /* string */ "label", labels: /* struct */ "labels", mapMetaCap: /* bool */ "map_meta_cap", menuIcon: /* string */ "menu_icon", menuPosition: /* int */ "menu_position", name: /* string */ "name", "public": /* bool */ "public", publiclyQuerably: /* bool */ "publicly_queryable", queryVar: /* mixed */ "query_var", rewrite: /* mixed */ "rewrite", showInAdminBar: /* bool */ "show_in_admin_bar", showInMenu: /* bool */ "show_in_menu", showInNavMenus: /* bool */ "show_in_nav_menus", showUi: /* bool */ "show_ui", supports: /* array */ "supports", taxonomies: /* array */ "taxonomies" }, {}, { cap: function( cap ) { return { cap: mapFields( cap, maps.postTypeCap.from ) }; }, labels: function( labels ) { return { labels: mapFields( labels, maps.labels.from ) }; } }); maps.postTypeCap = createFieldMaps({ deleteOthersPosts: /* string */ "delete_others_posts", deletePost: /* string */ "delete_post", deletePosts: /* string */ "delete_posts", deletePrivatePosts: /* string */ "delete_private_posts", deletePublishedPosts: /* string */ "delete_published_posts", editOthersPosts: /* string */ "edit_others_posts", editPost: /* string */ "edit_post", editPosts: /* string */ "edit_posts", editPrivatePosts: /* string */ "edit_private_posts", editPublishedPosts: /* string */ "edit_published_posts", publishPosts: /* string */ "publish_posts", read: /* string */ "read", readPost: /* sring */ "read_post", readPrivatePosts: /* string */ "read_private_posts" }); maps.taxonomy = createFieldMaps({ cap: /* struct */ "cap", hierarchical: /* bool */ "hierarchical", name: /* string */ "name", label: /* string */ "label", labels: /* struct */ "labels", objectType: /* array */ "object_type", "public": /* bool */ "public", queryVar: /* string */ "query_var", rewrite: /* struct */ "rewrite", showInNavMenus: /* bool */ "show_in_nav_menus", showTagCloud: /* bool */ "show_tagcloud", showUi: /* bool */ "show_ui" }, {}, { cap: function( cap ) { return { cap: mapFields( cap, maps.taxonomyCap.from ) }; }, labels: function( labels ) { return { labels: mapFields( labels, maps.labels.from ) }; } }); maps.taxonomyCap = createFieldMaps({ assignTerms: /* string */ "assign_terms", deleteTerms: /* string */ "delete_terms", editTerms: /* string */ "edit_terms", manageTerms: /* string */ "manage_terms" }); maps.term = createFieldMaps({ count: /* int */ "count", /* readonly */ description: /* string */ "description", name: /* string */ "name", parent: /* string */ "parent", slug: /* string */ "slug", taxonomy: /* string */ "taxonomy", termId: /* string */ "term_id", /* readonly */ termTaxonomyId: /* string */ "term_taxonomy_id" /* readonly */ }); maps.file = createFieldMaps({ name: /* string */ "name", type: /* string */ "type", bits: /* string */ "bits", overwrite: /* boolean */ "overwrite", postId: /* int */ "post_id" }); maps.media = createFieldMaps({ attachmentId: /* string */ "attachment_id", /* readonly */ caption: /* string */ "caption", description: /* string */ "description", link: /* string */ "link", parent: /* int */ "parent", thumbnail: /* string */ "thumbnail", title: /* string */ "title", type: /* string */ "type" }, {}, { date_created_gmt: /* datetime */ function( date ) { return { date: new Date( date ) }; }, metadata: /* struct */ function( data ) { return { metadata: mapFields( data, maps.mediaItemMetadata.from ) }; } }); maps.mediaItemMetadata = createFieldMaps({ file: /* string */ "file", height: /* int */ "height", sizes: /* struct */ "sizes", width: /* int */ "width" }, {}, { sizes: /* struct */ function( size ) { var keys = Object.keys( size ), results = {}; // Loop through the available sizes and map the fields keys.forEach(function( key, i ) { results[ keys[ i ] ] = mapFields( size[ keys[ i ] ], maps.mediaItemSize.from ); }); return { sizes: results }; }, image_meta: /* struct */ function( data ) { return { imageMeta: mapFields( data, maps.postThumbnailImageMeta.from ) }; } }); maps.mediaItemSize = createFieldMaps({ file: /* string */ "file", height: /* string */ "height", mimeType: /* string */ "mime-type", width: /* string */ "width" }); maps.postThumbnailImageMeta = createFieldMaps({ aperture: /* int */ "aperture", camera: /* string */ "camera", caption: /* string */ "caption", copyright: /* string */ "copyright", createdTimestamp: /* int */ "created_timestamp", credit: /* string */ "credit", focalLength: /* int */ "focal_length", iso: /* int */ "iso", keywords: /* array */ "keywords", orientation: /* string */ "orientation", shutterSpeed: /* int */ "shutter_speed", title: /* string */ "title" }); module.exports = { to: function( data, type ) { return mapFields( data, maps[ type ].to ); }, from: function( data, type ) { return mapFields( data, maps[ type ].from ); }, array: function( data, type ) { var map = maps[ type ].renames; return data.map(function( field ) { return map[ field ]; }); } };