<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[10932] sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2: WordPress.TV: Add a progress bar to the upload process.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/10932">10932</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/10932","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-04-29 05:26:25 +0000 (Thu, 29 Apr 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>WordPress.TV: Add a progress bar to the upload process.

Fixes <a href="http://meta.trac.wordpress.org/ticket/1117">#1117</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpresstvpublic_htmlwpcontentthemeswptv2anonuploadtemplatephp">sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/anon-upload-template.php</a></li>
<li><a href="#sitestrunkwordpresstvpublic_htmlwpcontentthemeswptv2incwpcomphp">sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/inc/wpcom.php</a></li>
<li><a href="#sitestrunkwordpresstvpublic_htmlwpcontentthemeswptv2pluginswordpresstvanonuploadanonuploadphp">sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/plugins/wordpresstv-anon-upload/anon-upload.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpresstvpublic_htmlwpcontentthemeswptv2anonuploadtemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/anon-upload-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/anon-upload-template.php     2021-04-29 03:30:56 UTC (rev 10931)
+++ sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/anon-upload-template.php       2021-04-29 05:26:25 UTC (rev 10932)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -164,6 +164,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        display: inline;
</span><span class="cx" style="display: block; padding: 0 10px">                        width: auto;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               #upload-progress .status,
+               #upload-progress .percent {
+                       font-family: monospace;
+               }
+               #upload-progress a.abort:hover {
+                       color: red;
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         </style>
</span><span class="cx" style="display: block; padding: 0 10px">        <?php
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -275,7 +283,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        <p>Please review the guidelines listed on the right, then submit your video below:</p>
</span><span class="cx" style="display: block; padding: 0 10px">                                <?php } ?>
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" id="video-upload-form" enctype="multipart/form-data">
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" data-xhr-action="<?php echo admin_url('admin-post.php?xhr=1'); ?>" id="video-upload-form" enctype="multipart/form-data">
</ins><span class="cx" style="display: block; padding: 0 10px">                                         <?php wp_nonce_field('wptv-upload-video', 'wptvvideon'); ?>
</span><span class="cx" style="display: block; padding: 0 10px">                                        <input type="hidden" name="action" value="wptv_video_upload" />
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -383,6 +391,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        <p class="last">
</span><span class="cx" style="display: block; padding: 0 10px">                                                <input type="submit" id="wptv_video_upload" style="display:none;" value="<?php esc_attr_e( 'Submit' ); ?>" />
</span><span class="cx" style="display: block; padding: 0 10px">                                        </p>
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        <p id="upload-progress" style="display:none;">
+                                               <progress value="0" max="100" style="width:90%;"></progress><span class="percent"></span><br>
+                                               <span class="status"></span> <a href="#" class="abort">Cancel</a>
+                                       </p>
</ins><span class="cx" style="display: block; padding: 0 10px">                                 </form>
</span><span class="cx" style="display: block; padding: 0 10px">                        </div>
</span><span class="cx" style="display: block; padding: 0 10px">                </div>
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -434,7 +446,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                title += $( '.location input:checked' ).parent().text().trim() + " ";
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                // .. and the Year
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                title += $( '#wptv_date' ).value.substring( 0, 4 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         title += $( '#wptv_date' ).val().substring( 0, 4 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                // If a location or year has been selected, build the Event Name.
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( $.trim( title ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -486,6 +498,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                // Changes to this list must be synced with WPTV_Anon_Upload::save()
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( ! file.val() || !/\.(avi|mov|qt|mpeg|mpg|mpe|mp4|m4v|asf|asx|wax|wmv|wmx|ogv|3gp|3g2)$/.test( file.val() ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        invalid(file, e);
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        scroll = true;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                // If there's any input in the honeypot field, it was probably put there by a bot, so reject the submission
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -494,11 +507,137 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        scroll = true;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( scroll && uploaded_by.length ) {
-                                       uploaded_by.get( 0 ).scrollIntoView();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( scroll ) {
+                                       jQuery( '.invalid' ).get(0).scrollIntoView();
+                                       return;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               // Start the upload!
+                               if ( 
+                                       'undefined' != typeof XMLHttpRequest &&
+                                       'undefined' != typeof FormData
+                               ) {
+                                       e.preventDefault();
+                                       processXHRUpload();
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         } );
</span><span class="cx" style="display: block; padding: 0 10px">                } );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               function processXHRUpload() {
+                       var $form        = jQuery( '#video-upload-form' ),
+                               $file        = $form.find('input[type="file"]'),
+                               $submit      = $form.find( 'p.last' ),
+                               $progressBar = $form.find( '#upload-progress' ),
+                               $progress    = $progressBar.find( 'progress' ),
+                               $status      = $progressBar.find( '.status' ),
+                               $percent     = $progressBar.find( '.percent' ),
+                               $abort       = $progressBar.find( '.abort' ),
+                               formdata     = new FormData( $form.get(0) ),
+                               xhr          = new XMLHttpRequest(),
+                               startTime    = (new Date()).getTime(),
+                               round_to, disable_form;
+
+                       round_to = function(x, precision ) {
+                               return x.toLocaleString(
+                                       undefined,
+                                       {
+                                               minimumFractionDigits: precision,
+                                               maximumFractionDigits: precision
+                                       }
+                               );
+                       };
+
+                       disable_form = function( disable ) {
+                               $form.find( 'input,select,option,textarea' ).prop( 'disabled', !! disable );
+                       };
+
+                       disable_form( true );
+                       $submit.hide();
+                       $progress.val( 0 );
+                       $percent.text( '0%' );
+                       $progressBar.show();
+                       $abort.show();
+
+                       $status.text( 'Preparing upload..' );
+
+                       xhr.upload.addEventListener( 'progress', function(e) {
+                               var percent     = Math.round( e.loaded / e.total * 100 ),
+                                       size        = round_to( e.total / 1024 / 1024, 1 ),
+                                       uploaded    = round_to( e.loaded / 1024 / 1024, 1 ),
+                                       elapsed     = ( (new Date()).getTime() - startTime ) / 1000,
+                                       // This isn't a perfect speed measurement, but is close enough for our needs.
+                                       speed_kbps  = e.loaded / elapsed / 1024,
+                                       eta_seconds = Math.round( ( e.total - e.loaded ) / 1024 / speed_kbps ),
+                                       eta_minutes = Math.floor( eta_seconds / 60 ),
+                                       eta         = '',
+                                       speed;
+
+                               // Give some time for the upload speed to settle.
+                               if ( elapsed > 10 || percent > 5 ) {
+                                       if ( eta_minutes ) {
+                                               eta += eta_minutes + ( eta_minutes > 1 ? ' mins ' : ' min ' );
+                                       }
+                                       if ( eta_seconds % 60 ) {
+                                               eta += ( eta_seconds - eta_minutes * 60 ) + 's';
+                                       }
+                               } else {
+                                       eta = 'Calculating..';
+                               }
+
+                               if ( speed_kbps > 1024 ) {
+                                       speed = round_to( speed_kbps / 1024, 2 ) + 'mb/s';
+                               } else {
+                                       speed = round_to( Math.round( speed_kbps ), 0 ) + 'kb/s';
+                               }
+
+                               $progress.val( percent );
+
+                               $percent.text( `${percent}%` );
+
+                               $status.text(
+                                       `Uploaded ${uploaded}MB of ${size}MB. ${speed} Remaining: ${eta}`
+                               );
+                               if ( percent >= 100 ) {
+                                       $status.text( 'Processing upload.. please wait..' );
+                                       $abort.hide();
+                               }
+
+                       }, false);
+
+                       xhr.addEventListener( 'load', function( e ) {
+                               $status.text( 'Done.. please wait..' );
+                               $abort.hide();
+                               // Redirect. Upload done.
+                               document.location = e.target.responseText;
+                       }, false);
+
+                       xhr.addEventListener( 'error', function( e ) {
+                               $status.text( 'Upload failed. Network or Browser issue encountered.' );
+                               console.log( e );
+
+                               disable_form( false );
+                               $submit.show();
+                               $abort.hide();
+                       }, false );
+
+                       xhr.addEventListener( 'abort', function( e ) {
+                               $status.text( 'Aborted.' );
+
+                               disable_form( false );
+                               $submit.show();
+                               $progressBar.hide();
+                       }, false );
+
+                       // Note: form.action will return the `<input name="action">` element, which is why a data attribute is ued.
+                       xhr.withCredentials = true;
+                       xhr.open( $form.prop( 'method' ), $form.data( 'xhr-action' ) );
+                       xhr.send( formdata );
+
+                       $abort.click( function( e ) {
+                               xhr.abort();
+                               e.preventDefault();
+                       } );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         </script>
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> <?php get_footer();
</span></span></pre></div>
<a id="sitestrunkwordpresstvpublic_htmlwpcontentthemeswptv2incwpcomphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/inc/wpcom.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/inc/wpcom.php        2021-04-29 03:30:56 UTC (rev 10931)
+++ sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/inc/wpcom.php  2021-04-29 05:26:25 UTC (rev 10932)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,4 +1,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-require_once WP_CONTENT_DIR . '/themes/vip/plugins/vip-init.php';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( ! function_exists( 'wpcom_vip_load_plugin' ) ) {
+       require_once WP_CONTENT_DIR . '/themes/vip/plugins/vip-init.php';
+}
</ins><span class="cx" style="display: block; padding: 0 10px"> wpcom_vip_load_plugin( 'taxonomy-list-widget' );
</span></span></pre></div>
<a id="sitestrunkwordpresstvpublic_htmlwpcontentthemeswptv2pluginswordpresstvanonuploadanonuploadphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/plugins/wordpresstv-anon-upload/anon-upload.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/plugins/wordpresstv-anon-upload/anon-upload.php      2021-04-29 03:30:56 UTC (rev 10931)
+++ sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/plugins/wordpresstv-anon-upload/anon-upload.php        2021-04-29 05:26:25 UTC (rev 10932)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -75,6 +75,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // For an XHR request, just send the redirect location, don't redirect to it.
+               if ( isset( $_GET['xhr'] ) ) {
+                       die( $redir );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 wp_redirect( $redir );
</span><span class="cx" style="display: block; padding: 0 10px">                exit;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre>
</div>
</div>

</body>
</html>