Make WordPress Core

Changeset 40074


Ignore:
Timestamp:
02/17/2017 08:53:57 PM (8 years ago)
Author:
adamsilverstein
Message:

REST API: JavaScript client - improve route discovery for custom namespaces.

Fix parsing of custom namespace routes. Transform class names, removing dashes and capitalizing each word/route part so a route path of widgets/recent-posts becomes a collection with the name WidgetsRecentPosts. Correct parent route part when routes are longer than expected, reversing parse direction.

Props westonruter, jazbek.
Fixes #39561.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/wp-api.js

    r40040 r40074  
    121121
    122122    /**
     123     * Helper function that capitalizes the first word and camel cases any words starting
     124     * after dashes, removing the dashes.
     125     */
     126    wp.api.utils.capitalizeAndCamelCaseDashes = function( str ) {
     127        if ( _.isUndefined( str ) ) {
     128            return str;
     129        }
     130        str = wp.api.utils.capitalize( str );
     131
     132        return wp.api.utils.camelCaseDashes( str );
     133    };
     134
     135    /**
     136     * Helper function to camel case the letter after dashes, removing the dashes.
     137     */
     138    wp.api.utils.camelCaseDashes = function( str ) {
     139        return str.replace( /-([a-z])/g, function( g ) {
     140            return g[ 1 ].toUpperCase();
     141        } );
     142    };
     143
     144    /**
    123145     * Extract a route part based on negative index.
    124146     *
    125      * @param {string} route The endpoint route.
    126      * @param {int}    part  The number of parts from the end of the route to retrieve. Default 1.
    127      *                       Example route `/a/b/c`: part 1 is `c`, part 2 is `b`, part 3 is `a`.
    128      */
    129     wp.api.utils.extractRoutePart = function( route, part ) {
     147     * @param {string}   route          The endpoint route.
     148     * @param {int}      part           The number of parts from the end of the route to retrieve. Default 1.
     149     *                                  Example route `/a/b/c`: part 1 is `c`, part 2 is `b`, part 3 is `a`.
     150     * @param {string}  [versionString] Version string, defaults to `wp.api.versionString`.
     151     * @param {boolean} [reverse]       Whether to reverse the order when extracting the route part. Optional, default false.
     152     */
     153    wp.api.utils.extractRoutePart = function( route, part, versionString, reverse ) {
    130154        var routeParts;
    131155
    132         part  = part || 1;
     156        part = part || 1;
     157        versionString = versionString || wp.api.versionString;
    133158
    134159        // Remove versions string from route to avoid returning it.
    135         route = route.replace( wp.api.versionString, '' );
    136         routeParts = route.split( '/' ).reverse();
     160        if ( 0 === route.indexOf( '/' + versionString ) ) {
     161            route = route.substr( versionString.length + 1 );
     162        }
     163
     164        routeParts = route.split( '/' );
     165        if ( reverse ) {
     166            routeParts = routeParts.reverse();
     167        }
    137168        if ( _.isUndefined( routeParts[ --part ] ) ) {
    138169            return '';
     
    11271158                // Extract the name and any parent from the route.
    11281159                var modelClassName,
    1129                         routeName  = wp.api.utils.extractRoutePart( modelRoute.index, 2 ),
    1130                         parentName = wp.api.utils.extractRoutePart( modelRoute.index, 4 ),
    1131                         routeEnd   = wp.api.utils.extractRoutePart( modelRoute.index, 1 );
     1160                    routeName  = wp.api.utils.extractRoutePart( modelRoute.index, 2, routeModel.get( 'versionString' ), true ),
     1161                    parentName = wp.api.utils.extractRoutePart( modelRoute.index, 1, routeModel.get( 'versionString' ), false ),
     1162                    routeEnd   = wp.api.utils.extractRoutePart( modelRoute.index, 1, routeModel.get( 'versionString' ), true );
     1163
     1164                // Clear the parent part of the rouite if its actually the version string.
     1165                if ( parentName === routeModel.get( 'versionString' ) ) {
     1166                    parentName = '';
     1167                }
    11321168
    11331169                // Handle the special case of the 'me' route.
     
    11381174                // If the model has a parent in its route, add that to its class name.
    11391175                if ( '' !== parentName && parentName !== routeName ) {
    1140                     modelClassName = wp.api.utils.capitalize( parentName ) + wp.api.utils.capitalize( routeName );
     1176                    modelClassName = wp.api.utils.capitalizeAndCamelCaseDashes( parentName ) + wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
    11411177                    modelClassName = mapping.models[ modelClassName ] || modelClassName;
    11421178                    loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( {
     
    11441180                        // Return a constructed url based on the parent and id.
    11451181                        url: function() {
    1146                             var url = routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) +
    1147                                     parentName +  '/' +
     1182                            var url =
     1183                                routeModel.get( 'apiRoot' ) +
     1184                                routeModel.get( 'versionString' ) +
     1185                                parentName +  '/' +
    11481186                                    ( ( _.isUndefined( this.get( 'parent' ) ) || 0 === this.get( 'parent' ) ) ?
    1149                                         this.get( 'parent_post' ) :
    1150                                         this.get( 'parent' ) ) + '/' +
    1151                                     routeName;
     1187                                        ( _.isUndefined( this.get( 'parent_post' ) ) ? '' : this.get( 'parent_post' ) + '/' ) :
     1188                                        this.get( 'parent' ) + '/' ) +
     1189                                routeName;
     1190
    11521191                            if ( ! _.isUndefined( this.get( 'id' ) ) ) {
    11531192                                url +=  '/' + this.get( 'id' );
     
    11651204                        methods: modelRoute.route.methods,
    11661205
    1167                         initialize: function() {
     1206                        initialize: function( attributes, options ) {
     1207                            wp.api.WPApiBaseModel.prototype.initialize.call( this, attributes, options );
    11681208
    11691209                            /**
     
    11851225
    11861226                    // This is a model without a parent in its route
    1187                     modelClassName = wp.api.utils.capitalize( routeName );
     1227                    modelClassName = wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
    11881228                    modelClassName = mapping.models[ modelClassName ] || modelClassName;
    11891229                    loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( {
     
    12131253
    12141254                // Add defaults to the new model, pulled form the endpoint.
    1215                 wp.api.utils.decorateFromRoute( modelRoute.route.endpoints, loadingObjects.models[ modelClassName ] );
     1255                wp.api.utils.decorateFromRoute(
     1256                    modelRoute.route.endpoints,
     1257                    loadingObjects.models[ modelClassName ],
     1258                    routeModel.get( 'versionString' )
     1259                );
    12161260
    12171261            } );
     
    12271271                var collectionClassName, modelClassName,
    12281272                        routeName  = collectionRoute.index.slice( collectionRoute.index.lastIndexOf( '/' ) + 1 ),
    1229                         parentName = wp.api.utils.extractRoutePart( collectionRoute.index, 3 );
     1273                        parentName = wp.api.utils.extractRoutePart( collectionRoute.index, 1, routeModel.get( 'versionString' ), false );
    12301274
    12311275                // If the collection has a parent in its route, add that to its class name.
    1232                 if ( '' !== parentName && parentName !== routeName ) {
    1233 
    1234                     collectionClassName = wp.api.utils.capitalize( parentName ) + wp.api.utils.capitalize( routeName );
     1276                if ( '' !== parentName && parentName !== routeName && routeModel.get( 'versionString' ) !== parentName ) {
     1277
     1278                    collectionClassName = wp.api.utils.capitalizeAndCamelCaseDashes( parentName ) + wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
    12351279                    modelClassName      = mapping.models[ collectionClassName ] || collectionClassName;
    12361280                    collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName;
     
    12611305
    12621306                    // This is a collection without a parent in its route.
    1263                     collectionClassName = wp.api.utils.capitalize( routeName );
     1307                    collectionClassName = wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
    12641308                    modelClassName      = mapping.models[ collectionClassName ] || collectionClassName;
    12651309                    collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName;
Note: See TracChangeset for help on using the changeset viewer.