Making WordPress.org

Changeset 7553


Ignore:
Timestamp:
07/31/2018 04:54:09 AM (8 years ago)
Author:
dd32
Message:

Gutenberg: Allow media uploads from the front-end, which are not actually uploaded but just stored in the browser.
This allows images to be uploaded through drag-and-drop interfaces for a customised experience.

See #3703.

File:
1 edited

Legend:

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

    r7552 r7553  
    4141
    4242                                        if ( options.method !== "GET" && ! isWhitelistedEndpoint ) {
    43                                                 return Promise.resolve( options.data ); //not sure what this actually should be, etc.
     43                                                return Promise.resolve( options.data ); // This works in enough cases to be the default return value.
     44                                        }
     45
     46                                        return next( options );
     47                                } );'
     48                        ),
     49                        'after'
     50                );
     51                wp_add_inline_script( 'wp-api-fetch',
     52                        sprintf(
     53                                'wp.apiFetch.use( function( options, next ) {
     54                                        if ( options.method == "POST" && options.path == "/wp/v2/media" ) {
     55                                                var file = options.body.get("file");
     56
     57                                                window.fakeUploadedMedia = window.fakeUploadedMedia || [];
     58                                                if ( ! window.fakeUploadedMedia.length ) {
     59                                                        window.fakeUploadedMedia[9999000] = {};
     60                                                }
     61                                                var id = window.fakeUploadedMedia.length;
     62
     63                                                window.fakeUploadedMedia[ id ] = {
     64                                                        "id": id,
     65                                                        "date": "", "date_gmt": "", "modified": "", "modified_gmt": "",
     66                                                        "guid": { "rendered": "" }, "title": { "rendered": file.name },
     67                                                        "description": { "rendered": ""}, "caption": { "rendered": "" }, "alt_text": "",
     68                                                        "slug": file.name, "status": "inherit", "type": "attachment", "link": "",
     69                                                        "author": 0, "comment_status": "open", "ping_status": "closed",
     70                                                        "media_details": {}, "media_type": "image", "mime_type": file.type,
     71                                                        "source_url": "", // This gets filled below with a data uri
     72                                                        "_links": {}
     73                                                };
     74
     75                                                 return new Promise( function( resolve ) {
     76                                                        var a = new FileReader();
     77                                                        a.onload = function(e) {
     78                                                                window.fakeUploadedMedia[ id ].source_url = e.target.result;
     79                                                                resolve( window.fakeUploadedMedia[ id ] );
     80                                                        }
     81                                                        a.readAsDataURL( file );
     82                                                } );
     83                                        }
     84
     85                                        // Drag droped media of ID 9999xxx is stored within the Browser
     86                                        var path_id_match = options.path.match( "^/wp/v2/media/(9999\\\\d+)" );
     87                                        if ( path_id_match ) {
     88                                                return Promise.resolve( window.fakeUploadedMedia[ path_id_match[1] ] );
    4489                                        }
    4590
Note: See TracChangeset for help on using the changeset viewer.