<?php
 /*
 Plugin Name: MWE Trac #42794
 Plugin URI: https://core.trac.wordpress.org/ticket/42794
 Description: Demonstration that one can't upload new media files when multiple media dialogues are present on a single page.
 Version: 1.0
 Author: carlobeltrame
 License: GPL2
 */

/* Minimal working example CPT with multiple media uploaders on the same page */
function trac42794_custom_post_type_mwe( $wpptd ) {
  $wpptd->add_components( array(
    'trac42794_mwe' => array(
      'label' => __( 'MWE', 'trac42794' ),
      'post_types' => array(
        'mwe' => array(
          'public' => true,
          'metaboxes' => array(
            'mwe_metabox' => array(
              'title' => __( 'MWE metabox', 'trac42794' ),
              'fields' => array(
                'mwe-media-picker-1' => array(
                  'title' => __( 'Media picker 1', 'trac42794' ),
                  'type' => 'media',
                ),
                'mwe-media-picker-2' => array(
                  'title' => __( 'Media picker 2', 'trac42794' ),
                  'type' => 'media',
                ),
              ),
            ),
          ),
        ),
      ),
    ),
  ), 'trac42794' );
}
add_action( 'wpptd', 'trac42794_custom_post_type_mwe' );


/* Proposed fix: modify UploaderStatus such that ready() is called at the end of every
 * initialize() invocation. This is demonstrated here by overriding UploaderStatus, but
 * in the proposed patch, we would patch the UploaderStatus class directly in core.
 * Uncomment the last line to activate the fix. */
function trac42794_fix_media_uploader() {
    ?>
<script>
    var oldUploaderStatus = wp.media.view.UploaderStatus;
    wp.media.view.UploaderStatus = oldUploaderStatus.extend({
       initialize: function() {
           oldUploaderStatus.prototype.initialize.call(this);
           this.ready();
       }
    });
</script><?php
}
function trac42794_add_media_modification_js() {
    add_action( 'admin_print_footer_scripts', 'trac42794_fix_media_uploader' );
}
//add_action( 'wp_enqueue_media', 'trac42794_add_media_modification_js' );
