Edit File by line
/home/barbar84/www/wp-inclu.../js
File: wp-api.js
/**
[0] Fix | Delete
* @output wp-includes/js/wp-api.js
[1] Fix | Delete
*/
[2] Fix | Delete
[3] Fix | Delete
(function( window, undefined ) {
[4] Fix | Delete
[5] Fix | Delete
'use strict';
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Initialise the WP_API.
[9] Fix | Delete
*/
[10] Fix | Delete
function WP_API() {
[11] Fix | Delete
/** @namespace wp.api.models */
[12] Fix | Delete
this.models = {};
[13] Fix | Delete
/** @namespace wp.api.collections */
[14] Fix | Delete
this.collections = {};
[15] Fix | Delete
/** @namespace wp.api.views */
[16] Fix | Delete
this.views = {};
[17] Fix | Delete
}
[18] Fix | Delete
[19] Fix | Delete
/** @namespace wp */
[20] Fix | Delete
window.wp = window.wp || {};
[21] Fix | Delete
/** @namespace wp.api */
[22] Fix | Delete
wp.api = wp.api || new WP_API();
[23] Fix | Delete
wp.api.versionString = wp.api.versionString || 'wp/v2/';
[24] Fix | Delete
[25] Fix | Delete
// Alias _includes to _.contains, ensuring it is available if lodash is used.
[26] Fix | Delete
if ( ! _.isFunction( _.includes ) && _.isFunction( _.contains ) ) {
[27] Fix | Delete
_.includes = _.contains;
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
})( window );
[31] Fix | Delete
[32] Fix | Delete
(function( window, undefined ) {
[33] Fix | Delete
[34] Fix | Delete
'use strict';
[35] Fix | Delete
[36] Fix | Delete
var pad, r;
[37] Fix | Delete
[38] Fix | Delete
/** @namespace wp */
[39] Fix | Delete
window.wp = window.wp || {};
[40] Fix | Delete
/** @namespace wp.api */
[41] Fix | Delete
wp.api = wp.api || {};
[42] Fix | Delete
/** @namespace wp.api.utils */
[43] Fix | Delete
wp.api.utils = wp.api.utils || {};
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Determine model based on API route.
[47] Fix | Delete
*
[48] Fix | Delete
* @param {string} route The API route.
[49] Fix | Delete
*
[50] Fix | Delete
* @return {Backbone Model} The model found at given route. Undefined if not found.
[51] Fix | Delete
*/
[52] Fix | Delete
wp.api.getModelByRoute = function( route ) {
[53] Fix | Delete
return _.find( wp.api.models, function( model ) {
[54] Fix | Delete
return model.prototype.route && route === model.prototype.route.index;
[55] Fix | Delete
} );
[56] Fix | Delete
};
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Determine collection based on API route.
[60] Fix | Delete
*
[61] Fix | Delete
* @param {string} route The API route.
[62] Fix | Delete
*
[63] Fix | Delete
* @return {Backbone Model} The collection found at given route. Undefined if not found.
[64] Fix | Delete
*/
[65] Fix | Delete
wp.api.getCollectionByRoute = function( route ) {
[66] Fix | Delete
return _.find( wp.api.collections, function( collection ) {
[67] Fix | Delete
return collection.prototype.route && route === collection.prototype.route.index;
[68] Fix | Delete
} );
[69] Fix | Delete
};
[70] Fix | Delete
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* ECMAScript 5 shim, adapted from MDN.
[74] Fix | Delete
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
[75] Fix | Delete
*/
[76] Fix | Delete
if ( ! Date.prototype.toISOString ) {
[77] Fix | Delete
pad = function( number ) {
[78] Fix | Delete
r = String( number );
[79] Fix | Delete
if ( 1 === r.length ) {
[80] Fix | Delete
r = '0' + r;
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
return r;
[84] Fix | Delete
};
[85] Fix | Delete
[86] Fix | Delete
Date.prototype.toISOString = function() {
[87] Fix | Delete
return this.getUTCFullYear() +
[88] Fix | Delete
'-' + pad( this.getUTCMonth() + 1 ) +
[89] Fix | Delete
'-' + pad( this.getUTCDate() ) +
[90] Fix | Delete
'T' + pad( this.getUTCHours() ) +
[91] Fix | Delete
':' + pad( this.getUTCMinutes() ) +
[92] Fix | Delete
':' + pad( this.getUTCSeconds() ) +
[93] Fix | Delete
'.' + String( ( this.getUTCMilliseconds() / 1000 ).toFixed( 3 ) ).slice( 2, 5 ) +
[94] Fix | Delete
'Z';
[95] Fix | Delete
};
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/**
[99] Fix | Delete
* Parse date into ISO8601 format.
[100] Fix | Delete
*
[101] Fix | Delete
* @param {Date} date.
[102] Fix | Delete
*/
[103] Fix | Delete
wp.api.utils.parseISO8601 = function( date ) {
[104] Fix | Delete
var timestamp, struct, i, k,
[105] Fix | Delete
minutesOffset = 0,
[106] Fix | Delete
numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ];
[107] Fix | Delete
[108] Fix | Delete
/*
[109] Fix | Delete
* ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
[110] Fix | Delete
* before falling back to any implementation-specific date parsing, so that’s what we do, even if native
[111] Fix | Delete
* implementations could be faster.
[112] Fix | Delete
*/
[113] Fix | Delete
// 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
[114] Fix | Delete
if ( ( struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec( date ) ) ) {
[115] Fix | Delete
[116] Fix | Delete
// Avoid NaN timestamps caused by “undefined” values being passed to Date.UTC.
[117] Fix | Delete
for ( i = 0; ( k = numericKeys[i] ); ++i ) {
[118] Fix | Delete
struct[k] = +struct[k] || 0;
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
// Allow undefined days and months.
[122] Fix | Delete
struct[2] = ( +struct[2] || 1 ) - 1;
[123] Fix | Delete
struct[3] = +struct[3] || 1;
[124] Fix | Delete
[125] Fix | Delete
if ( 'Z' !== struct[8] && undefined !== struct[9] ) {
[126] Fix | Delete
minutesOffset = struct[10] * 60 + struct[11];
[127] Fix | Delete
[128] Fix | Delete
if ( '+' === struct[9] ) {
[129] Fix | Delete
minutesOffset = 0 - minutesOffset;
[130] Fix | Delete
}
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
timestamp = Date.UTC( struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7] );
[134] Fix | Delete
} else {
[135] Fix | Delete
timestamp = Date.parse ? Date.parse( date ) : NaN;
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
return timestamp;
[139] Fix | Delete
};
[140] Fix | Delete
[141] Fix | Delete
/**
[142] Fix | Delete
* Helper function for getting the root URL.
[143] Fix | Delete
* @return {[type]} [description]
[144] Fix | Delete
*/
[145] Fix | Delete
wp.api.utils.getRootUrl = function() {
[146] Fix | Delete
return window.location.origin ?
[147] Fix | Delete
window.location.origin + '/' :
[148] Fix | Delete
window.location.protocol + '//' + window.location.host + '/';
[149] Fix | Delete
};
[150] Fix | Delete
[151] Fix | Delete
/**
[152] Fix | Delete
* Helper for capitalizing strings.
[153] Fix | Delete
*/
[154] Fix | Delete
wp.api.utils.capitalize = function( str ) {
[155] Fix | Delete
if ( _.isUndefined( str ) ) {
[156] Fix | Delete
return str;
[157] Fix | Delete
}
[158] Fix | Delete
return str.charAt( 0 ).toUpperCase() + str.slice( 1 );
[159] Fix | Delete
};
[160] Fix | Delete
[161] Fix | Delete
/**
[162] Fix | Delete
* Helper function that capitalizes the first word and camel cases any words starting
[163] Fix | Delete
* after dashes, removing the dashes.
[164] Fix | Delete
*/
[165] Fix | Delete
wp.api.utils.capitalizeAndCamelCaseDashes = function( str ) {
[166] Fix | Delete
if ( _.isUndefined( str ) ) {
[167] Fix | Delete
return str;
[168] Fix | Delete
}
[169] Fix | Delete
str = wp.api.utils.capitalize( str );
[170] Fix | Delete
[171] Fix | Delete
return wp.api.utils.camelCaseDashes( str );
[172] Fix | Delete
};
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Helper function to camel case the letter after dashes, removing the dashes.
[176] Fix | Delete
*/
[177] Fix | Delete
wp.api.utils.camelCaseDashes = function( str ) {
[178] Fix | Delete
return str.replace( /-([a-z])/g, function( g ) {
[179] Fix | Delete
return g[ 1 ].toUpperCase();
[180] Fix | Delete
} );
[181] Fix | Delete
};
[182] Fix | Delete
[183] Fix | Delete
/**
[184] Fix | Delete
* Extract a route part based on negative index.
[185] Fix | Delete
*
[186] Fix | Delete
* @param {string} route The endpoint route.
[187] Fix | Delete
* @param {number} part The number of parts from the end of the route to retrieve. Default 1.
[188] Fix | Delete
* Example route `/a/b/c`: part 1 is `c`, part 2 is `b`, part 3 is `a`.
[189] Fix | Delete
* @param {string} [versionString] Version string, defaults to `wp.api.versionString`.
[190] Fix | Delete
* @param {boolean} [reverse] Whether to reverse the order when extracting the route part. Optional, default false.
[191] Fix | Delete
*/
[192] Fix | Delete
wp.api.utils.extractRoutePart = function( route, part, versionString, reverse ) {
[193] Fix | Delete
var routeParts;
[194] Fix | Delete
[195] Fix | Delete
part = part || 1;
[196] Fix | Delete
versionString = versionString || wp.api.versionString;
[197] Fix | Delete
[198] Fix | Delete
// Remove versions string from route to avoid returning it.
[199] Fix | Delete
if ( 0 === route.indexOf( '/' + versionString ) ) {
[200] Fix | Delete
route = route.substr( versionString.length + 1 );
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
routeParts = route.split( '/' );
[204] Fix | Delete
if ( reverse ) {
[205] Fix | Delete
routeParts = routeParts.reverse();
[206] Fix | Delete
}
[207] Fix | Delete
if ( _.isUndefined( routeParts[ --part ] ) ) {
[208] Fix | Delete
return '';
[209] Fix | Delete
}
[210] Fix | Delete
return routeParts[ part ];
[211] Fix | Delete
};
[212] Fix | Delete
[213] Fix | Delete
/**
[214] Fix | Delete
* Extract a parent name from a passed route.
[215] Fix | Delete
*
[216] Fix | Delete
* @param {string} route The route to extract a name from.
[217] Fix | Delete
*/
[218] Fix | Delete
wp.api.utils.extractParentName = function( route ) {
[219] Fix | Delete
var name,
[220] Fix | Delete
lastSlash = route.lastIndexOf( '_id>[\\d]+)/' );
[221] Fix | Delete
[222] Fix | Delete
if ( lastSlash < 0 ) {
[223] Fix | Delete
return '';
[224] Fix | Delete
}
[225] Fix | Delete
name = route.substr( 0, lastSlash - 1 );
[226] Fix | Delete
name = name.split( '/' );
[227] Fix | Delete
name.pop();
[228] Fix | Delete
name = name.pop();
[229] Fix | Delete
return name;
[230] Fix | Delete
};
[231] Fix | Delete
[232] Fix | Delete
/**
[233] Fix | Delete
* Add args and options to a model prototype from a route's endpoints.
[234] Fix | Delete
*
[235] Fix | Delete
* @param {Array} routeEndpoints Array of route endpoints.
[236] Fix | Delete
* @param {Object} modelInstance An instance of the model (or collection)
[237] Fix | Delete
* to add the args to.
[238] Fix | Delete
*/
[239] Fix | Delete
wp.api.utils.decorateFromRoute = function( routeEndpoints, modelInstance ) {
[240] Fix | Delete
[241] Fix | Delete
/**
[242] Fix | Delete
* Build the args based on route endpoint data.
[243] Fix | Delete
*/
[244] Fix | Delete
_.each( routeEndpoints, function( routeEndpoint ) {
[245] Fix | Delete
[246] Fix | Delete
// Add post and edit endpoints as model args.
[247] Fix | Delete
if ( _.includes( routeEndpoint.methods, 'POST' ) || _.includes( routeEndpoint.methods, 'PUT' ) ) {
[248] Fix | Delete
[249] Fix | Delete
// Add any non-empty args, merging them into the args object.
[250] Fix | Delete
if ( ! _.isEmpty( routeEndpoint.args ) ) {
[251] Fix | Delete
[252] Fix | Delete
// Set as default if no args yet.
[253] Fix | Delete
if ( _.isEmpty( modelInstance.prototype.args ) ) {
[254] Fix | Delete
modelInstance.prototype.args = routeEndpoint.args;
[255] Fix | Delete
} else {
[256] Fix | Delete
[257] Fix | Delete
// We already have args, merge these new args in.
[258] Fix | Delete
modelInstance.prototype.args = _.extend( modelInstance.prototype.args, routeEndpoint.args );
[259] Fix | Delete
}
[260] Fix | Delete
}
[261] Fix | Delete
} else {
[262] Fix | Delete
[263] Fix | Delete
// Add GET method as model options.
[264] Fix | Delete
if ( _.includes( routeEndpoint.methods, 'GET' ) ) {
[265] Fix | Delete
[266] Fix | Delete
// Add any non-empty args, merging them into the defaults object.
[267] Fix | Delete
if ( ! _.isEmpty( routeEndpoint.args ) ) {
[268] Fix | Delete
[269] Fix | Delete
// Set as default if no defaults yet.
[270] Fix | Delete
if ( _.isEmpty( modelInstance.prototype.options ) ) {
[271] Fix | Delete
modelInstance.prototype.options = routeEndpoint.args;
[272] Fix | Delete
} else {
[273] Fix | Delete
[274] Fix | Delete
// We already have options, merge these new args in.
[275] Fix | Delete
modelInstance.prototype.options = _.extend( modelInstance.prototype.options, routeEndpoint.args );
[276] Fix | Delete
}
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
}
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
} );
[283] Fix | Delete
[284] Fix | Delete
};
[285] Fix | Delete
[286] Fix | Delete
/**
[287] Fix | Delete
* Add mixins and helpers to models depending on their defaults.
[288] Fix | Delete
*
[289] Fix | Delete
* @param {Backbone Model} model The model to attach helpers and mixins to.
[290] Fix | Delete
* @param {string} modelClassName The classname of the constructed model.
[291] Fix | Delete
* @param {Object} loadingObjects An object containing the models and collections we are building.
[292] Fix | Delete
*/
[293] Fix | Delete
wp.api.utils.addMixinsAndHelpers = function( model, modelClassName, loadingObjects ) {
[294] Fix | Delete
[295] Fix | Delete
var hasDate = false,
[296] Fix | Delete
[297] Fix | Delete
/**
[298] Fix | Delete
* Array of parseable dates.
[299] Fix | Delete
*
[300] Fix | Delete
* @type {string[]}.
[301] Fix | Delete
*/
[302] Fix | Delete
parseableDates = [ 'date', 'modified', 'date_gmt', 'modified_gmt' ],
[303] Fix | Delete
[304] Fix | Delete
/**
[305] Fix | Delete
* Mixin for all content that is time stamped.
[306] Fix | Delete
*
[307] Fix | Delete
* This mixin converts between mysql timestamps and JavaScript Dates when syncing a model
[308] Fix | Delete
* to or from the server. For example, a date stored as `2015-12-27T21:22:24` on the server
[309] Fix | Delete
* gets expanded to `Sun Dec 27 2015 14:22:24 GMT-0700 (MST)` when the model is fetched.
[310] Fix | Delete
*
[311] Fix | Delete
* @type {{toJSON: toJSON, parse: parse}}.
[312] Fix | Delete
*/
[313] Fix | Delete
TimeStampedMixin = {
[314] Fix | Delete
[315] Fix | Delete
/**
[316] Fix | Delete
* Prepare a JavaScript Date for transmitting to the server.
[317] Fix | Delete
*
[318] Fix | Delete
* This helper function accepts a field and Date object. It converts the passed Date
[319] Fix | Delete
* to an ISO string and sets that on the model field.
[320] Fix | Delete
*
[321] Fix | Delete
* @param {Date} date A JavaScript date object. WordPress expects dates in UTC.
[322] Fix | Delete
* @param {string} field The date field to set. One of 'date', 'date_gmt', 'date_modified'
[323] Fix | Delete
* or 'date_modified_gmt'. Optional, defaults to 'date'.
[324] Fix | Delete
*/
[325] Fix | Delete
setDate: function( date, field ) {
[326] Fix | Delete
var theField = field || 'date';
[327] Fix | Delete
[328] Fix | Delete
// Don't alter non-parsable date fields.
[329] Fix | Delete
if ( _.indexOf( parseableDates, theField ) < 0 ) {
[330] Fix | Delete
return false;
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
this.set( theField, date.toISOString() );
[334] Fix | Delete
},
[335] Fix | Delete
[336] Fix | Delete
/**
[337] Fix | Delete
* Get a JavaScript Date from the passed field.
[338] Fix | Delete
*
[339] Fix | Delete
* WordPress returns 'date' and 'date_modified' in the timezone of the server as well as
[340] Fix | Delete
* UTC dates as 'date_gmt' and 'date_modified_gmt'. Draft posts do not include UTC dates.
[341] Fix | Delete
*
[342] Fix | Delete
* @param {string} field The date field to set. One of 'date', 'date_gmt', 'date_modified'
[343] Fix | Delete
* or 'date_modified_gmt'. Optional, defaults to 'date'.
[344] Fix | Delete
*/
[345] Fix | Delete
getDate: function( field ) {
[346] Fix | Delete
var theField = field || 'date',
[347] Fix | Delete
theISODate = this.get( theField );
[348] Fix | Delete
[349] Fix | Delete
// Only get date fields and non-null values.
[350] Fix | Delete
if ( _.indexOf( parseableDates, theField ) < 0 || _.isNull( theISODate ) ) {
[351] Fix | Delete
return false;
[352] Fix | Delete
}
[353] Fix | Delete
[354] Fix | Delete
return new Date( wp.api.utils.parseISO8601( theISODate ) );
[355] Fix | Delete
}
[356] Fix | Delete
},
[357] Fix | Delete
[358] Fix | Delete
/**
[359] Fix | Delete
* Build a helper function to retrieve related model.
[360] Fix | Delete
*
[361] Fix | Delete
* @param {string} parentModel The parent model.
[362] Fix | Delete
* @param {number} modelId The model ID if the object to request
[363] Fix | Delete
* @param {string} modelName The model name to use when constructing the model.
[364] Fix | Delete
* @param {string} embedSourcePoint Where to check the embedded object for _embed data.
[365] Fix | Delete
* @param {string} embedCheckField Which model field to check to see if the model has data.
[366] Fix | Delete
*
[367] Fix | Delete
* @return {Deferred.promise} A promise which resolves to the constructed model.
[368] Fix | Delete
*/
[369] Fix | Delete
buildModelGetter = function( parentModel, modelId, modelName, embedSourcePoint, embedCheckField ) {
[370] Fix | Delete
var getModel, embeddedObjects, attributes, deferred;
[371] Fix | Delete
[372] Fix | Delete
deferred = jQuery.Deferred();
[373] Fix | Delete
embeddedObjects = parentModel.get( '_embedded' ) || {};
[374] Fix | Delete
[375] Fix | Delete
// Verify that we have a valid object id.
[376] Fix | Delete
if ( ! _.isNumber( modelId ) || 0 === modelId ) {
[377] Fix | Delete
deferred.reject();
[378] Fix | Delete
return deferred;
[379] Fix | Delete
}
[380] Fix | Delete
[381] Fix | Delete
// If we have embedded object data, use that when constructing the getModel.
[382] Fix | Delete
if ( embeddedObjects[ embedSourcePoint ] ) {
[383] Fix | Delete
attributes = _.findWhere( embeddedObjects[ embedSourcePoint ], { id: modelId } );
[384] Fix | Delete
}
[385] Fix | Delete
[386] Fix | Delete
// Otherwise use the modelId.
[387] Fix | Delete
if ( ! attributes ) {
[388] Fix | Delete
attributes = { id: modelId };
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
// Create the new getModel model.
[392] Fix | Delete
getModel = new wp.api.models[ modelName ]( attributes );
[393] Fix | Delete
[394] Fix | Delete
if ( ! getModel.get( embedCheckField ) ) {
[395] Fix | Delete
getModel.fetch( {
[396] Fix | Delete
success: function( getModel ) {
[397] Fix | Delete
deferred.resolve( getModel );
[398] Fix | Delete
},
[399] Fix | Delete
error: function( getModel, response ) {
[400] Fix | Delete
deferred.reject( response );
[401] Fix | Delete
}
[402] Fix | Delete
} );
[403] Fix | Delete
} else {
[404] Fix | Delete
// Resolve with the embedded model.
[405] Fix | Delete
deferred.resolve( getModel );
[406] Fix | Delete
}
[407] Fix | Delete
[408] Fix | Delete
// Return a promise.
[409] Fix | Delete
return deferred.promise();
[410] Fix | Delete
},
[411] Fix | Delete
[412] Fix | Delete
/**
[413] Fix | Delete
* Build a helper to retrieve a collection.
[414] Fix | Delete
*
[415] Fix | Delete
* @param {string} parentModel The parent model.
[416] Fix | Delete
* @param {string} collectionName The name to use when constructing the collection.
[417] Fix | Delete
* @param {string} embedSourcePoint Where to check the embedded object for _embed data.
[418] Fix | Delete
* @param {string} embedIndex An additional optional index for the _embed data.
[419] Fix | Delete
*
[420] Fix | Delete
* @return {Deferred.promise} A promise which resolves to the constructed collection.
[421] Fix | Delete
*/
[422] Fix | Delete
buildCollectionGetter = function( parentModel, collectionName, embedSourcePoint, embedIndex ) {
[423] Fix | Delete
/**
[424] Fix | Delete
* Returns a promise that resolves to the requested collection
[425] Fix | Delete
*
[426] Fix | Delete
* Uses the embedded data if available, otherwise fetches the
[427] Fix | Delete
* data from the server.
[428] Fix | Delete
*
[429] Fix | Delete
* @return {Deferred.promise} promise Resolves to a wp.api.collections[ collectionName ]
[430] Fix | Delete
* collection.
[431] Fix | Delete
*/
[432] Fix | Delete
var postId, embeddedObjects, getObjects,
[433] Fix | Delete
classProperties = '',
[434] Fix | Delete
properties = '',
[435] Fix | Delete
deferred = jQuery.Deferred();
[436] Fix | Delete
[437] Fix | Delete
postId = parentModel.get( 'id' );
[438] Fix | Delete
embeddedObjects = parentModel.get( '_embedded' ) || {};
[439] Fix | Delete
[440] Fix | Delete
// Verify that we have a valid post ID.
[441] Fix | Delete
if ( ! _.isNumber( postId ) || 0 === postId ) {
[442] Fix | Delete
deferred.reject();
[443] Fix | Delete
return deferred;
[444] Fix | Delete
}
[445] Fix | Delete
[446] Fix | Delete
// If we have embedded getObjects data, use that when constructing the getObjects.
[447] Fix | Delete
if ( ! _.isUndefined( embedSourcePoint ) && ! _.isUndefined( embeddedObjects[ embedSourcePoint ] ) ) {
[448] Fix | Delete
[449] Fix | Delete
// Some embeds also include an index offset, check for that.
[450] Fix | Delete
if ( _.isUndefined( embedIndex ) ) {
[451] Fix | Delete
[452] Fix | Delete
// Use the embed source point directly.
[453] Fix | Delete
properties = embeddedObjects[ embedSourcePoint ];
[454] Fix | Delete
} else {
[455] Fix | Delete
[456] Fix | Delete
// Add the index to the embed source point.
[457] Fix | Delete
properties = embeddedObjects[ embedSourcePoint ][ embedIndex ];
[458] Fix | Delete
}
[459] Fix | Delete
} else {
[460] Fix | Delete
[461] Fix | Delete
// Otherwise use the postId.
[462] Fix | Delete
classProperties = { parent: postId };
[463] Fix | Delete
}
[464] Fix | Delete
[465] Fix | Delete
// Create the new getObjects collection.
[466] Fix | Delete
getObjects = new wp.api.collections[ collectionName ]( properties, classProperties );
[467] Fix | Delete
[468] Fix | Delete
// If we didn’t have embedded getObjects, fetch the getObjects data.
[469] Fix | Delete
if ( _.isUndefined( getObjects.models[0] ) ) {
[470] Fix | Delete
getObjects.fetch( {
[471] Fix | Delete
success: function( getObjects ) {
[472] Fix | Delete
[473] Fix | Delete
// Add a helper 'parent_post' attribute onto the model.
[474] Fix | Delete
setHelperParentPost( getObjects, postId );
[475] Fix | Delete
deferred.resolve( getObjects );
[476] Fix | Delete
},
[477] Fix | Delete
error: function( getModel, response ) {
[478] Fix | Delete
deferred.reject( response );
[479] Fix | Delete
}
[480] Fix | Delete
} );
[481] Fix | Delete
} else {
[482] Fix | Delete
[483] Fix | Delete
// Add a helper 'parent_post' attribute onto the model.
[484] Fix | Delete
setHelperParentPost( getObjects, postId );
[485] Fix | Delete
deferred.resolve( getObjects );
[486] Fix | Delete
}
[487] Fix | Delete
[488] Fix | Delete
// Return a promise.
[489] Fix | Delete
return deferred.promise();
[490] Fix | Delete
[491] Fix | Delete
},
[492] Fix | Delete
[493] Fix | Delete
/**
[494] Fix | Delete
* Set the model post parent.
[495] Fix | Delete
*/
[496] Fix | Delete
setHelperParentPost = function( collection, postId ) {
[497] Fix | Delete
[498] Fix | Delete
// Attach post_parent id to the collection.
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function