Permalink
Browse files

Uploading:

* First pass at adding Plupload support
* Adds in filesize function to replace core admin functions
* Adds pluload.js to handle callbacks and some basic UI effects
* Adds basic CSS for UI changes
  • Loading branch information...
1 parent e90817f commit 8413aff21ea8d27bc441f768b4091a9f7bd70e95 @JJJ JJJ committed Jul 31, 2012
Showing with 157 additions and 6 deletions.
  1. +17 −0 css/liveblog.css
  2. +31 −0 js/plupload.js
  3. +109 −6 liveblog.php
View
@@ -28,6 +28,18 @@ fieldset#liveblog-actions {
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ffffff',GradientType=0 ); /* IE6-9 */
}
+fieldset#liveblog-actions.uploading {
+ border: 1px solid #ccc;
+ background: #f6f8f9; /* Old browsers */
+ background: -moz-linear-gradient(top, #f6f8f9 0%, #e5ebee 33%, #d7dee3 34%, #f5f7f9 100%); /* FF3.6+ */
+ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f6f8f9), color-stop(33%,#e5ebee), color-stop(34%,#d7dee3), color-stop(100%,#f5f7f9)); /* Chrome,Safari4+ */
+ background: -webkit-linear-gradient(top, #f6f8f9 0%,#e5ebee 34%,#d7dee3 34%,#f5f7f9 100%); /* Chrome10+,Safari5.1+ */
+ background: -o-linear-gradient(top, #f6f8f9 0%,#e5ebee 33%,#d7dee3 34%,#f5f7f9 100%); /* Opera 11.10+ */
+ background: -ms-linear-gradient(top, #f6f8f9 0%,#e5ebee 33%,#d7dee3 34%,#f5f7f9 100%); /* IE10+ */
+ background: linear-gradient(to bottom, #f6f8f9 0%,#e5ebee 33%,#d7dee3 34%,#f5f7f9 100%); /* W3C */
+ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f6f8f9', endColorstr='#f5f7f9',GradientType=0 ); /* IE6-9 */
+}
+
fieldset#liveblog-actions legend {
padding: 5px;
background: transparent;
@@ -50,6 +62,11 @@ textarea#liveblog-form-entry {
outline-color: #acc;
}
+textarea#liveblog-form-entry.drag-over {
+ background: #efe;
+ color: #333;
+}
+
#liveblog-actions div.liveblog-submit-wrapper {
text-align: right;
position: relative;
View
@@ -0,0 +1,31 @@
+jQuery(document).ready(function($) {
+ new wp.Uploader({
+
+    /* Selectors */
+ browser: '#liveblog-actions legend',
+    dropzone:  '#liveblog-form-entry',
+
+    /* Callbacks */
+    success : function( attachment ) {
+ $( '#liveblog-form-entry' ).val( $('#liveblog-form-entry' ).val() + '<img src="' + attachment.url + '" />' );
+ $( '#liveblog-actions legend' ).html( attachment.filename + ' Finished' );
+ $( '#liveblog-actions' ).removeClass( 'uploading' );
+    },
+
+ error : function ( reason ) {
+ $( '#liveblog-actions legend' ).html( reason );
+ },
+
+ added : function() {
+ $( '#liveblog-actions' ).addClass( 'uploading' );
+ },
+
+ progress : function( up, file ) {
+ $( '#liveblog-actions legend' ).html( "Uploading: " + file.name + ' ' + file.percent + '%' );
+ },
+
+ complete : function() {
+ $( '#liveblog-actions legend' ).html( 'All done!' );
+ }
+ });
+});
View
@@ -19,7 +19,7 @@
*
* This class is a big container for a bunch of static methods, similar to a
* factory but without object inheritance or instantiation.
- *
+ *
* Things yet to be implimented:
*
* -- PHP and JS Actions/Filters/Triggers
@@ -80,6 +80,7 @@ private static function includes() {
*/
private static function add_actions() {
add_action( 'init', array( __CLASS__, 'init' ) );
+ //add_action( 'wp_head', array( __CLASS__, 'wp_head' ) );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
add_action( 'wp_ajax_liveblog_insert_entry', array( __CLASS__, 'ajax_insert_entry' ) );
}
@@ -174,7 +175,7 @@ public static function handle_request() {
* If this is an ajax request to update the entries, call the method
* responsible for updating entries between now and the last time they
* were successfully returned.
- *
+ *
* Once we've added them, bail.
*/
if ( self::is_entries_ajax_request() ) {
@@ -424,7 +425,7 @@ public static function comments_clauses( $clauses = array() ) {
return $clauses;
$clauses['where'] = "comment_approved = '" . self::key . "' AND comment_post_ID = " . self::$post_id . " AND comment_type = '" . self::key . "'";
-
+
return $clauses;
}
@@ -446,8 +447,17 @@ public static function enqueue_scripts() {
wp_enqueue_script( self::key, plugins_url( 'js/liveblog.js', __FILE__ ), array( 'jquery' ), self::version, true );
// Only for users that can publish liveblog content
- if ( self::current_user_can_edit_liveblog() ) {
+ if ( self::current_user_can_edit_liveblog() ) {
+
+ // Publisher ajax
wp_enqueue_script( 'liveblog-publisher', plugins_url( 'js/liveblog-publisher.js', __FILE__ ), array( self::key ), self::version, true );
+
+ // Plupload
+ wp_enqueue_script( 'wp-plupload' );
+ wp_enqueue_script( 'liveblog-plupload', plugins_url( 'js/plupload.js', __FILE__ ), array( 'wp-plupload', 'jquery' ) );
+
+ // Set the default Plupload settings
+ self::default_plupload_settings();
}
// Use the WordPress core jQuery spinner plugin
@@ -486,6 +496,48 @@ public static function enqueue_scripts() {
}
/**
+ * Sets up some default Plupload settings so we can upload meda theme-side
+ *
+ * @global type $wp_scripts
+ */
+ private static function default_plupload_settings() {
+ global $wp_scripts;
+
+ $defaults = array(
+ 'runtimes' => 'html5,silverlight,flash,html4',
+ 'file_data_name' => 'async-upload',
+ 'multiple_queues' => true,
+ 'max_file_size' => self::max_upload_size() . 'b',
+ 'url' => admin_url( 'admin-ajax.php', 'relative' ),
+ 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),
+ 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
+ 'filters' => array( array( 'title' => __( 'Allowed Files' ), 'extensions' => '*') ),
+ 'multipart' => true,
+ 'urlstream_upload' => true,
+ 'multipart_params' => array(
+ 'action' => 'upload-attachment',
+ '_wpnonce' => wp_create_nonce( 'media-form' )
+ )
+ );
+
+ $settings = array(
+ 'defaults' => $defaults,
+ 'browser' => array(
+ 'mobile' => wp_is_mobile(),
+ 'supported' => _device_can_upload(),
+ )
+ );
+
+ $script = 'var _wpPluploadSettings = ' . json_encode( $settings ) . ';';
+ $data = $wp_scripts->get_data( 'wp-plupload', 'data' );
+
+ if ( ! empty( $data ) )
+ $script = "$data\n$script";
+
+ $wp_scripts->add_data( 'wp-plupload', 'data', $script );
+ }
+
+ /**
* Get the URL of a specific liveblog entry.
*
* @return string
@@ -531,7 +583,7 @@ private static function get_editor_output() {
* Get all the liveblog entries for this post
*/
private static function get_all_entry_output() {
-
+
// Get liveblog entries
$entries = (array) self::$entry_query->get_all( array( 'order' => 'ASC' ) );
@@ -553,7 +605,7 @@ public static function get_template_part( $template_name, $template_variables =
include( dirname( __FILE__ ) . '/templates/' . $template_name );
return ob_get_clean();
}
-
+
/** Admin Methods *********************************************************/
/**
@@ -686,6 +738,57 @@ private static function status_header_with_message( $status, $message ) {
$wp_header_to_desc[$status] = $official_message;
}
+
+ /** Plupload Helpers ******************************************************/
+
+ /**
+ * Convert hours to bytes
+ *
+ * @param unknown_type $size
+ * @return unknown
+ */
+ private static function convert_hr_to_bytes( $size ) {
+ $size = strtolower( $size );
+ $bytes = (int) $size;
+
+ if ( strpos( $size, 'k' ) !== false )
+ $bytes = intval( $size ) * 1024;
+ elseif ( strpos( $size, 'm' ) !== false )
+ $bytes = intval( $size ) * 1024 * 1024;
+ elseif ( strpos( $size, 'g' ) !== false )
+ $bytes = intval( $size ) * 1024 * 1024 * 1024;
+
+ return $bytes;
+ }
+
+ /**
+ * Convert bytes to hour
+ *
+ * @param string $bytes
+ * @return string
+ */
+ private static function convert_bytes_to_hr( $bytes ) {
+ $units = array( 0 => 'B', 1 => 'kB', 2 => 'MB', 3 => 'GB' );
+ $log = log( $bytes, 1024 );
+ $power = (int) $log;
+ $size = pow( 1024, $log - $power );
+
+ return $size . $units[$power];
+ }
+
+ /**
+ * Get the maximum upload file size
+ *
+ * @see wp_max_upload_size()
+ * @return string
+ */
+ private static function max_upload_size() {
+ $u_bytes = self::convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
+ $p_bytes = self::convert_hr_to_bytes( ini_get( 'post_max_size' ) );
+ $bytes = apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
+
+ return $bytes;
+ }
}
/**

0 comments on commit 8413aff

Please sign in to comment.