Theme Customizer: First pass for upload controls, using background image as an example. Add a wrapper for Plupload that allows for custom upload UIs. see #19910.
wp.Uploader is a wrapper that provides a simple way to upload an attachment (using the wp_ajax_upload_attachment handler). It is intentionally decoupled from the UI. When an upload succeeds, it will receive the attachment information (id, url, meta, etc) as a JSON response. If the upload fails, the wrapper handles both WordPress and plupload errors through a single handler. As todos, we should add drag classes for the uploader dropzone and account for the rough 100mb filesize limit in most browsers. The UI for the customizer upload controls could be improved as well. git-svn-id: http://svn.automattic.com/wordpress/trunk@20179 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
411853beb6
commit
c06576d410
|
@ -45,7 +45,7 @@ $core_actions_post = array(
|
||||||
'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
|
'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
|
||||||
'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
|
'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
|
||||||
'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
|
'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
|
||||||
'wp-remove-post-lock', 'dismiss-wp-pointer',
|
'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment',
|
||||||
);
|
);
|
||||||
|
|
||||||
// Register core Ajax calls.
|
// Register core Ajax calls.
|
||||||
|
|
|
@ -1544,6 +1544,50 @@ function wp_ajax_save_widget() {
|
||||||
wp_die();
|
wp_die();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function wp_ajax_upload_attachment() {
|
||||||
|
check_ajax_referer( 'media-form' );
|
||||||
|
|
||||||
|
if ( ! current_user_can( 'upload_files' ) )
|
||||||
|
wp_die( -1 );
|
||||||
|
|
||||||
|
if ( isset( $_REQUEST['post_id'] ) ) {
|
||||||
|
$post_id = $_REQUEST['post_id'];
|
||||||
|
if ( ! current_user_can( 'edit_post', $post_id ) )
|
||||||
|
wp_die( -1 );
|
||||||
|
} else {
|
||||||
|
$post_id = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_data = is_array( $_REQUEST['post_data'] ) ? $_REQUEST['post_data'] : array();
|
||||||
|
|
||||||
|
$attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data );
|
||||||
|
|
||||||
|
if ( is_wp_error( $attachment_id ) ) {
|
||||||
|
echo json_encode( array(
|
||||||
|
'type' => 'error',
|
||||||
|
'data' => array(
|
||||||
|
'message' => $attachment_id->get_error_message(),
|
||||||
|
'filename' => $_FILES['async-upload']['name'],
|
||||||
|
),
|
||||||
|
) );
|
||||||
|
wp_die();
|
||||||
|
}
|
||||||
|
|
||||||
|
$post = get_post( $attachment_id );
|
||||||
|
|
||||||
|
echo json_encode( array(
|
||||||
|
'type' => 'success',
|
||||||
|
'data' => array(
|
||||||
|
'id' => $attachment_id,
|
||||||
|
'title' => esc_attr( $post->post_title ),
|
||||||
|
'filename' => esc_html( basename( $post->guid ) ),
|
||||||
|
'url' => wp_get_attachment_url( $attachment_id ),
|
||||||
|
'meta' => wp_get_attachment_metadata( $attachment_id ),
|
||||||
|
),
|
||||||
|
) );
|
||||||
|
wp_die();
|
||||||
|
}
|
||||||
|
|
||||||
function wp_ajax_image_editor() {
|
function wp_ajax_image_editor() {
|
||||||
$attachment_id = intval($_POST['postid']);
|
$attachment_id = intval($_POST['postid']);
|
||||||
if ( empty($attachment_id) || !current_user_can('edit_post', $attachment_id) )
|
if ( empty($attachment_id) || !current_user_can('edit_post', $attachment_id) )
|
||||||
|
|
|
@ -70,6 +70,9 @@ class WP_Customize_Setting {
|
||||||
wp_enqueue_script( 'farbtastic' );
|
wp_enqueue_script( 'farbtastic' );
|
||||||
wp_enqueue_style( 'farbtastic' );
|
wp_enqueue_style( 'farbtastic' );
|
||||||
break;
|
break;
|
||||||
|
case 'upload':
|
||||||
|
wp_enqueue_script( 'wp-plupload' );
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -394,6 +397,15 @@ class WP_Customize_Setting {
|
||||||
</select>
|
</select>
|
||||||
<?php
|
<?php
|
||||||
break;
|
break;
|
||||||
|
case 'upload':
|
||||||
|
?>
|
||||||
|
<label><?php echo esc_html( $this->label ); ?><br/>
|
||||||
|
<input type="hidden" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->name(); ?> />
|
||||||
|
<a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
|
||||||
|
<a href="#" class="remove"><?php _e( 'Remove' ); ?></a>
|
||||||
|
</label>
|
||||||
|
<?php
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
do_action( 'customize_render_control-' . $this->control, $this );
|
do_action( 'customize_render_control-' . $this->control, $this );
|
||||||
|
|
||||||
|
|
|
@ -523,7 +523,13 @@ final class WP_Customize {
|
||||||
'section' => 'background',
|
'section' => 'background',
|
||||||
'control' => 'color',
|
'control' => 'color',
|
||||||
'default' => defined( 'BACKGROUND_COLOR' ) ? BACKGROUND_COLOR : '',
|
'default' => defined( 'BACKGROUND_COLOR' ) ? BACKGROUND_COLOR : '',
|
||||||
'sanitize_callback' => 'sanitize_hexcolor'
|
'sanitize_callback' => 'sanitize_hexcolor',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$this->add_setting( 'background_image', array(
|
||||||
|
'label' => 'Background Image',
|
||||||
|
'section' => 'background',
|
||||||
|
'control' => 'upload',
|
||||||
) );
|
) );
|
||||||
|
|
||||||
/* Nav Menus */
|
/* Nav Menus */
|
||||||
|
|
|
@ -70,6 +70,26 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
api.UploadControl = api.Control.extend({
|
||||||
|
initialize: function( id, value, options ) {
|
||||||
|
var control = this;
|
||||||
|
|
||||||
|
api.Control.prototype.initialize.call( this, id, value, options );
|
||||||
|
|
||||||
|
this.uploader = new wp.Uploader({
|
||||||
|
browser: this.container.find('.upload'),
|
||||||
|
success: function( attachment ) {
|
||||||
|
control.set( attachment.url );
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.container.on( 'click', '.remove', function( event ) {
|
||||||
|
control.set('');
|
||||||
|
event.preventDefault();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Change objects contained within the main customize object to Settings.
|
// Change objects contained within the main customize object to Settings.
|
||||||
api.defaultConstructor = api.Setting;
|
api.defaultConstructor = api.Setting;
|
||||||
|
|
||||||
|
@ -194,7 +214,8 @@
|
||||||
* ===================================================================== */
|
* ===================================================================== */
|
||||||
|
|
||||||
api.controls = {
|
api.controls = {
|
||||||
color: api.ColorControl
|
color: api.ColorControl,
|
||||||
|
upload: api.UploadControl
|
||||||
};
|
};
|
||||||
|
|
||||||
$( function() {
|
$( function() {
|
||||||
|
@ -229,6 +250,9 @@
|
||||||
|
|
||||||
// Background color uses postMessage by default
|
// Background color uses postMessage by default
|
||||||
api('background_color').method = 'postMessage';
|
api('background_color').method = 'postMessage';
|
||||||
|
|
||||||
|
// api('background_image').method = 'postMessage';
|
||||||
|
api('background_image').uploader.param( 'post_data', { context: 'custom-background' });
|
||||||
});
|
});
|
||||||
|
|
||||||
})( wp, jQuery );
|
})( wp, jQuery );
|
|
@ -0,0 +1,156 @@
|
||||||
|
if ( typeof wp === 'undefined' )
|
||||||
|
var wp = {};
|
||||||
|
|
||||||
|
(function( exports, $ ) {
|
||||||
|
/*
|
||||||
|
* An object that helps create a WordPress uploader using plupload.
|
||||||
|
*
|
||||||
|
* @param options - object - The options passed to the new plupload instance.
|
||||||
|
* Requires the following parameters:
|
||||||
|
* - container - The id of uploader container.
|
||||||
|
* - browser - The id of button to trigger the file select.
|
||||||
|
* - dropzone - The id of file drop target.
|
||||||
|
* - plupload - An object of parameters to pass to the plupload instance.
|
||||||
|
* - params - An object of parameters to pass to $_POST when uploading the file.
|
||||||
|
* Extends this.plupload.multipart_params under the hood.
|
||||||
|
*
|
||||||
|
* @param attributes - object - Attributes and methods for this specific instance.
|
||||||
|
*/
|
||||||
|
var Uploader = function( options ) {
|
||||||
|
var self = this,
|
||||||
|
elements = {
|
||||||
|
container: 'container',
|
||||||
|
browser: 'browse_button',
|
||||||
|
dropzone: 'drop_element'
|
||||||
|
},
|
||||||
|
key;
|
||||||
|
|
||||||
|
this.plupload = $.extend( { multipart_params: {} }, wpPluploadDefaults );
|
||||||
|
this.container = document.body; // Set default container.
|
||||||
|
|
||||||
|
// Extend the instance with options
|
||||||
|
//
|
||||||
|
// Use deep extend to allow options.plupload to override individual
|
||||||
|
// default plupload keys.
|
||||||
|
$.extend( true, this, options );
|
||||||
|
|
||||||
|
// Proxy all methods so this always refers to the current instance.
|
||||||
|
for ( key in this ) {
|
||||||
|
if ( $.isFunction( this[ key ] ) )
|
||||||
|
this[ key ] = $.proxy( this[ key ], this );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure all elements are jQuery elements and have id attributes
|
||||||
|
// Then set the proper plupload arguments to the ids.
|
||||||
|
for ( key in elements ) {
|
||||||
|
if ( ! this[ key ] )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
this[ key ] = $( this[ key ] ).first();
|
||||||
|
if ( ! this[ key ].prop('id') )
|
||||||
|
this[ key ].prop( 'id', '__wp-uploader-id-' + Uploader.uuid++ );
|
||||||
|
this.plupload[ elements[ key ] ] = this[ key ].prop('id');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.uploader = new plupload.Uploader( this.plupload );
|
||||||
|
delete this.plupload;
|
||||||
|
|
||||||
|
// Set default params and remove this.params alias.
|
||||||
|
this.param( this.params || {} );
|
||||||
|
delete this.params;
|
||||||
|
|
||||||
|
this.uploader.bind( 'Init', this.init );
|
||||||
|
|
||||||
|
this.uploader.init();
|
||||||
|
|
||||||
|
this.uploader.bind( 'UploadProgress', this.progress );
|
||||||
|
|
||||||
|
this.uploader.bind( 'FileUploaded', function( up, file, response ) {
|
||||||
|
response = JSON.parse( response.response );
|
||||||
|
|
||||||
|
if ( ! response || ! response.type || ! response.data )
|
||||||
|
return self.error( pluploadL10n.default_error );
|
||||||
|
|
||||||
|
if ( 'error' === response.type )
|
||||||
|
return self.error( response.data.message, response.data );
|
||||||
|
|
||||||
|
if ( 'success' === response.type )
|
||||||
|
return self.success( response.data );
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
this.uploader.bind( 'Error', function( up, error ) {
|
||||||
|
var message = pluploadL10n.default_error,
|
||||||
|
key;
|
||||||
|
|
||||||
|
// Check for plupload errors.
|
||||||
|
for ( key in Uploader.errorMap ) {
|
||||||
|
if ( error.code === plupload[ key ] ) {
|
||||||
|
message = Uploader.errorMap[ key ];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.error( message, error );
|
||||||
|
up.refresh();
|
||||||
|
});
|
||||||
|
|
||||||
|
this.uploader.bind( 'FilesAdded', function( up, files ) {
|
||||||
|
$.each( files, function() {
|
||||||
|
self.added( this );
|
||||||
|
});
|
||||||
|
|
||||||
|
up.refresh();
|
||||||
|
up.start();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Uploader.uuid = 0;
|
||||||
|
|
||||||
|
Uploader.errorMap = {
|
||||||
|
'FAILED': pluploadL10n.upload_failed,
|
||||||
|
'FILE_EXTENSION_ERROR': pluploadL10n.invalid_filetype,
|
||||||
|
// 'FILE_SIZE_ERROR': '',
|
||||||
|
'IMAGE_FORMAT_ERROR': pluploadL10n.not_an_image,
|
||||||
|
'IMAGE_MEMORY_ERROR': pluploadL10n.image_memory_exceeded,
|
||||||
|
'IMAGE_DIMENSIONS_ERROR': pluploadL10n.image_dimensions_exceeded,
|
||||||
|
'GENERIC_ERROR': pluploadL10n.upload_failed,
|
||||||
|
'IO_ERROR': pluploadL10n.io_error,
|
||||||
|
'HTTP_ERROR': pluploadL10n.http_error,
|
||||||
|
'SECURITY_ERROR': pluploadL10n.security_error
|
||||||
|
};
|
||||||
|
|
||||||
|
$.extend( Uploader.prototype, {
|
||||||
|
/**
|
||||||
|
* Acts as a shortcut to extending the uploader's multipart_params object.
|
||||||
|
*
|
||||||
|
* param( key )
|
||||||
|
* Returns the value of the key.
|
||||||
|
*
|
||||||
|
* param( key, value )
|
||||||
|
* Sets the value of a key.
|
||||||
|
*
|
||||||
|
* param( map )
|
||||||
|
* Sets values for a map of data.
|
||||||
|
*/
|
||||||
|
param: function( key, value ) {
|
||||||
|
if ( arguments.length === 1 && typeof key === 'string' )
|
||||||
|
return this.uploader.settings.multipart_params[ key ];
|
||||||
|
|
||||||
|
if ( arguments.length > 1 ) {
|
||||||
|
this.uploader.settings.multipart_params[ key ] = value;
|
||||||
|
} else {
|
||||||
|
$.extend( this.uploader.settings.multipart_params, key );
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
init: function() {},
|
||||||
|
error: function() {},
|
||||||
|
success: function() {},
|
||||||
|
added: function() {},
|
||||||
|
progress: function() {},
|
||||||
|
complete: function() {}
|
||||||
|
});
|
||||||
|
|
||||||
|
exports.Uploader = Uploader;
|
||||||
|
})( wp, jQuery );
|
|
@ -1441,3 +1441,46 @@ function wp_embed_handler_googlevideo( $matches, $attr, $url, $rawattr ) {
|
||||||
|
|
||||||
return apply_filters( 'embed_googlevideo', '<embed type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docid=' . esc_attr($matches[2]) . '&hl=en&fs=true" style="width:' . esc_attr($width) . 'px;height:' . esc_attr($height) . 'px" allowFullScreen="true" allowScriptAccess="always" />', $matches, $attr, $url, $rawattr );
|
return apply_filters( 'embed_googlevideo', '<embed type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docid=' . esc_attr($matches[2]) . '&hl=en&fs=true" style="width:' . esc_attr($width) . 'px;height:' . esc_attr($height) . 'px" allowFullScreen="true" allowScriptAccess="always" />', $matches, $attr, $url, $rawattr );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints default plupload arguments.
|
||||||
|
*
|
||||||
|
* @since 3.4.0
|
||||||
|
*/
|
||||||
|
function wp_plupload_default_settings() {
|
||||||
|
global $wp_scripts;
|
||||||
|
|
||||||
|
$max_upload_size = wp_max_upload_size();
|
||||||
|
|
||||||
|
$params = array(
|
||||||
|
'action' => 'upload-attachment',
|
||||||
|
);
|
||||||
|
$params = apply_filters( 'plupload_default_params', $params );
|
||||||
|
|
||||||
|
$params['_wpnonce'] = wp_create_nonce( 'media-form' );
|
||||||
|
|
||||||
|
$settings = array(
|
||||||
|
'runtimes' => 'html5,silverlight,flash,html4',
|
||||||
|
'file_data_name' => 'async-upload', // key passed to $_FILE.
|
||||||
|
'multiple_queues' => true,
|
||||||
|
'max_file_size' => $max_upload_size . 'b',
|
||||||
|
'url' => admin_url( 'admin-ajax.php' ),
|
||||||
|
'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' => $params,
|
||||||
|
);
|
||||||
|
|
||||||
|
$settings = apply_filters( 'plupload_default_settings', $settings );
|
||||||
|
|
||||||
|
$script = 'var wpPluploadDefaults = ' . json_encode( $settings ) . ';';
|
||||||
|
|
||||||
|
$data = $wp_scripts->get_data( 'wp-plupload', 'data' );
|
||||||
|
if ( $data )
|
||||||
|
$script = "$data\n$script";
|
||||||
|
|
||||||
|
$wp_scripts->add_data( 'wp-plupload', 'data', $script );
|
||||||
|
}
|
||||||
|
add_action( 'admin_init', 'wp_plupload_default_settings' );
|
||||||
|
|
|
@ -228,6 +228,9 @@ function wp_default_scripts( &$scripts ) {
|
||||||
$scripts->add( 'plupload-handlers', "/wp-includes/js/plupload/handlers$suffix.js", array('plupload-all', 'jquery') );
|
$scripts->add( 'plupload-handlers', "/wp-includes/js/plupload/handlers$suffix.js", array('plupload-all', 'jquery') );
|
||||||
$scripts->localize( 'plupload-handlers', 'pluploadL10n', $uploader_l10n );
|
$scripts->localize( 'plupload-handlers', 'pluploadL10n', $uploader_l10n );
|
||||||
|
|
||||||
|
$scripts->add( 'wp-plupload', "/wp-includes/js/plupload/wp-plupload$suffix.js", array('plupload-all', 'jquery', 'json2') );
|
||||||
|
$scripts->localize( 'wp-plupload', 'pluploadL10n', $uploader_l10n );
|
||||||
|
|
||||||
// keep 'swfupload' for back-compat.
|
// keep 'swfupload' for back-compat.
|
||||||
$scripts->add( 'swfupload', '/wp-includes/js/swfupload/swfupload.js', array(), '2201-20110113');
|
$scripts->add( 'swfupload', '/wp-includes/js/swfupload/swfupload.js', array(), '2201-20110113');
|
||||||
$scripts->add( 'swfupload-swfobject', '/wp-includes/js/swfupload/plugins/swfupload.swfobject.js', array('swfupload', 'swfobject'), '2201a');
|
$scripts->add( 'swfupload-swfobject', '/wp-includes/js/swfupload/plugins/swfupload.swfobject.js', array('swfupload', 'swfobject'), '2201a');
|
||||||
|
|
Loading…
Reference in New Issue