[wp-trac] [WordPress Trac] #44745: REST API: incorrect slashes in url if parent empty

WordPress Trac noreply at wordpress.org
Mon Aug 6 16:15:25 UTC 2018


#44745: REST API: incorrect slashes in url if parent empty
--------------------------+-----------------------------
 Reporter:  nicomollet    |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  REST API      |    Version:  4.9.8
 Severity:  normal        |   Keywords:  needs-patch
  Focuses:                |
--------------------------+-----------------------------
 Hello everyone,

 I am using the WP.API with a custom versionString: "wc/v2/" from
 WooCommerce.

 When fetching ProductCategories (or any other collections with two capital
 letters), the url called is:
 http://websiteurl/wp-json/wc/v2/products//categories

 Example of the code I use, to be able to reproduce the error:
 {{{
   wp.api.init({'versionString': 'wc/v2/'});
   var productcategories = new wp.api.collections.ProductsCategories();
   productcategories.fetch().done( function() {
     productcategories.each( function( productcategory ) {
       console.log( productcategory.attributes );
     } );
   } );
 }}}

 My tentative fix is the following:

 In wp-api.js, line 1406
 The parent is sometimes empty, leading to two consecutive slashes.
 {{{
 // Function that returns a constructed url passed on the parent.
 url: function() {
   return routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) +
       parentName + '/' + this.parent + '/' +
       routeName;
 },
 }}}

 should be replaced by:
 {{{
 // Function that returns a constructed url passed on the parent.
 url: function() {
   return routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) +
     parentName + '/' +
     ( ( _.isUndefined( this.get( 'parent' ) ) || 0 === this.get( 'parent'
 ) ) ?
       ( _.isUndefined( this.get( 'parent_post' ) ) ? '' : this.get(
 'parent_post' ) + '/' ) :
       this.get( 'parent' ) + '/' ) +
     routeName;
 },
 }}}

 Thank you very much
 Nicolas

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/44745>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list