Making WordPress.org

Changeset 7581


Ignore:
Timestamp:
08/02/2018 12:58:51 AM (8 years ago)
Author:
dd32
Message:

Gutenberg: Remove some redundant sprintf() calls in the inline javascript.

See #3703.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/gutenberg/functions.php

    r7580 r7581  
    3535                // Use a middleware provider to intercept and modify API calls. Short-circuit POST requests, bound queries, allow media, etc.
    3636                wp_add_inline_script( 'wp-api-fetch',
    37                         sprintf(
    38                                 'wp.apiFetch.use( function( options, next ) {
    39                                         var isWhitelistedEndpoint = (
    40                                                 lodash.startsWith( options.path, "/oembed/1.0/proxy" ) ||
    41                                                 lodash.startsWith( options.path, "/gutenberg/v1/block-renderer" )
    42                                         );
    43 
    44                                         // Prevent non-whitelisted non-GET requests (ie. POST) to prevent errors
    45                                         if ( options.method && options.method !== "GET" && ! isWhitelistedEndpoint ) {
    46                                                 // This works in enough cases to be the default return value.
    47                                                 return Promise.resolve( options.data );
    48                                         }
    49 
    50                                         // Add limits to all GET queries which attempt unbound queries
    51                                         options.path = options.path.replace( "per_page=-1", "per_page=10" );
    52 
    53                                         // Load images with the view context, seems to work
    54                                         if ( lodash.startsWith( options.path, "/wp/v2/media/" ) ) {
    55                                                 options.path = options.path.replace( "context=edit", "context=view" );
    56                                         }
    57 
    58                                         return next( options );
    59                                 } );'
    60                         ),
     37                        'wp.apiFetch.use( function( options, next ) {
     38                                var isWhitelistedEndpoint = (
     39                                        lodash.startsWith( options.path, "/oembed/1.0/proxy" ) ||
     40                                        lodash.startsWith( options.path, "/gutenberg/v1/block-renderer" )
     41                                );
     42
     43                                // Prevent non-whitelisted non-GET requests (ie. POST) to prevent errors
     44                                if ( options.method && options.method !== "GET" && ! isWhitelistedEndpoint ) {
     45                                        // This works in enough cases to be the default return value.
     46                                        return Promise.resolve( options.data );
     47                                }
     48
     49                                // Add limits to all GET queries which attempt unbound queries
     50                                options.path = options.path.replace( "per_page=-1", "per_page=10" );
     51
     52                                // Load images with the view context, seems to work
     53                                if ( lodash.startsWith( options.path, "/wp/v2/media/" ) ) {
     54                                        options.path = options.path.replace( "context=edit", "context=view" );
     55                                }
     56
     57                                return next( options );
     58                        } );',
    6159                        'after'
    6260                );
     
    112110                // Add a middleware provider which intercepts all uploads and stores them within the browser
    113111                wp_add_inline_script( 'wp-api-fetch',
    114                         sprintf(
    115                                 'wp.apiFetch.use( function( options, next ) {
    116                                         if ( options.method == "POST" && options.path == "/wp/v2/media" ) {
    117                                                 var file = options.body.get("file");
    118 
    119                                                 window.fakeUploadedMedia = window.fakeUploadedMedia || [];
    120                                                 if ( ! window.fakeUploadedMedia.length ) {
    121                                                         window.fakeUploadedMedia[9999000] = {};
    122                                                 }
    123                                                 var id = window.fakeUploadedMedia.length;
    124 
    125                                                 window.fakeUploadedMedia[ id ] = {
    126                                                         "id": id,
    127                                                         "date": "", "date_gmt": "", "modified": "", "modified_gmt": "",
    128                                                         "guid": {}, "title": { "rendered": file.name, "raw": file.name },
    129                                                         "description": {}, "caption": {}, "alt_text": "",
    130                                                         "slug": file.name, "status": "inherit", "type": "attachment", "link": "",
    131                                                         "author": 0, "comment_status": "open", "ping_status": "closed",
    132                                                         "media_details": {}, "media_type": file.type.split("/")[0], "mime_type": file.type,
    133                                                         "source_url": "", // This gets filled below with a data uri
    134                                                         "_links": {}
    135                                                 };
    136 
    137                                                  return new Promise( function( resolve ) {
    138                                                         var a = new FileReader();
    139                                                         a.onload = function(e) {
    140                                                                 window.fakeUploadedMedia[ id ].source_url = e.target.result;
    141                                                                 resolve( window.fakeUploadedMedia[ id ] );
    142                                                         }
    143                                                         a.readAsDataURL( file );
    144                                                 } );
     112                        'wp.apiFetch.use( function( options, next ) {
     113                                if ( options.method == "POST" && options.path == "/wp/v2/media" ) {
     114                                        var file = options.body.get("file");
     115
     116                                        window.fakeUploadedMedia = window.fakeUploadedMedia || [];
     117                                        if ( ! window.fakeUploadedMedia.length ) {
     118                                                window.fakeUploadedMedia[9999000] = {};
    145119                                        }
    146 
    147                                         // Drag droped media of ID 9999xxx is stored within the Browser
    148                                         var path_id_match = options.path.match( "^/wp/v2/media/(9999\\\\d+)" );
    149                                         if ( path_id_match ) {
    150                                                 return Promise.resolve( window.fakeUploadedMedia[ path_id_match[1] ] );
    151                                         }
    152 
    153                                         return next( options );
    154                                 } );'
    155                         ),
     120                                        var id = window.fakeUploadedMedia.length;
     121
     122                                        window.fakeUploadedMedia[ id ] = {
     123                                                "id": id,
     124                                                "date": "", "date_gmt": "", "modified": "", "modified_gmt": "",
     125                                                "guid": {}, "title": { "rendered": file.name, "raw": file.name },
     126                                                "description": {}, "caption": {}, "alt_text": "",
     127                                                "slug": file.name, "status": "inherit", "type": "attachment", "link": "",
     128                                                "author": 0, "comment_status": "open", "ping_status": "closed",
     129                                                "media_details": {}, "media_type": file.type.split("/")[0], "mime_type": file.type,
     130                                                "source_url": "", // This gets filled below with a data uri
     131                                                "_links": {}
     132                                        };
     133
     134                                                return new Promise( function( resolve ) {
     135                                                var a = new FileReader();
     136                                                a.onload = function(e) {
     137                                                        window.fakeUploadedMedia[ id ].source_url = e.target.result;
     138                                                        resolve( window.fakeUploadedMedia[ id ] );
     139                                                }
     140                                                a.readAsDataURL( file );
     141                                        } );
     142                                }
     143
     144                                // Drag droped media of ID 9999xxx is stored within the Browser
     145                                var path_id_match = options.path.match( "^/wp/v2/media/(9999\\\\d+)" );
     146                                if ( path_id_match ) {
     147                                        return Promise.resolve( window.fakeUploadedMedia[ path_id_match[1] ] );
     148                                }
     149
     150                                return next( options );
     151                        } );',
    156152                        'after'
    157153                );
Note: See TracChangeset for help on using the changeset viewer.