Work on media uploader
This commit is contained in:
parent
d8ec3aa094
commit
fe92fb6a96
|
@ -464,53 +464,74 @@ function variable_product_type_options() {
|
|||
});
|
||||
};
|
||||
|
||||
var current_field_wrapper;
|
||||
// Uploader
|
||||
var variable_image_frame;
|
||||
var setting_variation_image_id;
|
||||
var setting_variation_image;
|
||||
var wp_media_post_id = wp.media.model.settings.post.id;
|
||||
|
||||
window.send_to_editor_default = window.send_to_editor;
|
||||
jQuery('#variable_product_options').on('click', '.upload_image_button', function( event ) {
|
||||
|
||||
jQuery('#variable_product_options').on('click', '.upload_image_button', function(){
|
||||
var $button = jQuery( this );
|
||||
var post_id = $button.attr('rel');
|
||||
var $parent = $button.closest('.upload_image');
|
||||
setting_variation_image = $parent;
|
||||
setting_variation_image_id = post_id;
|
||||
|
||||
var post_id = jQuery(this).attr('rel');
|
||||
var parent = jQuery(this).parent();
|
||||
current_field_wrapper = parent;
|
||||
event.preventDefault();
|
||||
|
||||
if (jQuery(this).is('.remove')) {
|
||||
if ( $button.is('.remove') ) {
|
||||
|
||||
jQuery('.upload_image_id', current_field_wrapper).val('');
|
||||
jQuery('img', current_field_wrapper).attr('src', '<?php echo woocommerce_placeholder_img_src(); ?>');
|
||||
jQuery(this).removeClass('remove');
|
||||
setting_variation_image.find( '.upload_image_id' ).val( '' );
|
||||
setting_variation_image.find( 'img' ).attr( 'src', '<?php echo woocommerce_placeholder_img_src(); ?>' );
|
||||
setting_variation_image.find( '.upload_image_button' ).removeClass( 'remove' );
|
||||
|
||||
} else {
|
||||
|
||||
window.send_to_editor = window.send_to_cproduct;
|
||||
formfield = jQuery('.upload_image_id', parent).attr('name');
|
||||
tb_show('', 'media-upload.php?post_id=' + post_id + '&type=image&TB_iframe=true');
|
||||
// If the media frame already exists, reopen it.
|
||||
if ( variable_image_frame ) {
|
||||
variable_image_frame.open();
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the media frame.
|
||||
variable_image_frame = wp.media.frames.variable_image = wp.media({
|
||||
// Set the title of the modal.
|
||||
title: '<?php _e( 'Choose an image', 'woocommerce' ); ?>',
|
||||
button: {
|
||||
text: '<?php _e( 'Set variation image', 'woocommerce' ); ?>'
|
||||
}
|
||||
});
|
||||
|
||||
// When an image is selected, run a callback.
|
||||
variable_image_frame.on( 'select', function() {
|
||||
|
||||
attachment = variable_image_frame.state().get('selection').first().toJSON();
|
||||
|
||||
setting_variation_image.find( '.upload_image_id' ).val( attachment.id );
|
||||
setting_variation_image.find( '.upload_image_button' ).addClass( 'remove' );
|
||||
setting_variation_image.find( 'img' ).attr( 'src', attachment.url );
|
||||
|
||||
wp.media.model.settings.post.id = wp_media_post_id;
|
||||
});
|
||||
|
||||
variable_image_frame.on( 'ready open', function() {
|
||||
|
||||
wp.media.model.settings.post.id = setting_variation_image_id;
|
||||
|
||||
});
|
||||
|
||||
console.log( variable_image_frame.uploader );
|
||||
|
||||
// Finally, open the modal.
|
||||
variable_image_frame.open();
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
window.send_to_cproduct = function(html) {
|
||||
|
||||
jQuery('body').append('<div id="temp_image">' + html + '</div>');
|
||||
|
||||
var img = jQuery('#temp_image').find('img');
|
||||
|
||||
imgurl = img.attr('src');
|
||||
imgclass = img.attr('class');
|
||||
imgid = parseInt(imgclass.replace(/\D/g, ''), 10);
|
||||
|
||||
jQuery('.upload_image_id', current_field_wrapper).val(imgid);
|
||||
jQuery('.upload_image_button', current_field_wrapper).addClass('remove');
|
||||
|
||||
jQuery('img', current_field_wrapper).attr('src', imgurl);
|
||||
tb_remove();
|
||||
jQuery('#temp_image').remove();
|
||||
|
||||
window.send_to_editor = window.send_to_editor_default;
|
||||
|
||||
}
|
||||
// Restore ID
|
||||
jQuery('a.add_media').on('click', function() {
|
||||
wp.media.model.settings.post.id = wp_media_post_id;
|
||||
} );
|
||||
|
||||
});
|
||||
<?php
|
||||
|
|
|
@ -44,7 +44,7 @@ function woocommerce_meta_boxes() {
|
|||
|
||||
// Products
|
||||
add_meta_box( 'woocommerce-product-data', __( 'Product Data', 'woocommerce' ), 'woocommerce_product_data_box', 'product', 'normal', 'high' );
|
||||
add_meta_box( 'woocommerce-product-images', __( 'Product Images', 'woocommerce' ), 'woocommerce_product_images_box', 'product', 'side' );
|
||||
//add_meta_box( 'woocommerce-product-images', __( 'Product Images', 'woocommerce' ), 'woocommerce_product_images_box', 'product', 'side' );
|
||||
|
||||
remove_meta_box( 'product_shipping_classdiv', 'product', 'side' );
|
||||
remove_meta_box( 'pageparentdiv', 'product', 'side' );
|
||||
|
|
|
@ -254,20 +254,6 @@ function woocommerce_prevent_admin_access() {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fix 'insert into post' buttons for images
|
||||
*
|
||||
* @access public
|
||||
* @param mixed $vars
|
||||
* @return array
|
||||
*/
|
||||
function woocommerce_allow_img_insertion( $vars ) {
|
||||
$vars['send'] = true; // 'send' as in "Send to Editor"
|
||||
return($vars);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Filter the directory for uploads.
|
||||
*
|
||||
|
@ -277,16 +263,15 @@ function woocommerce_allow_img_insertion( $vars ) {
|
|||
*/
|
||||
function woocommerce_downloads_upload_dir( $pathdata ) {
|
||||
|
||||
if (isset($_POST['type']) && $_POST['type'] == 'downloadable_product') :
|
||||
|
||||
// Change upload dir
|
||||
if ( isset( $_POST['type'] ) && $_POST['type'] == 'downloadable_product' ) {
|
||||
// Uploading a downloadable file
|
||||
$subdir = '/woocommerce_uploads'.$pathdata['subdir'];
|
||||
$pathdata['path'] = str_replace($pathdata['subdir'], $subdir, $pathdata['path']);
|
||||
$pathdata['url'] = str_replace($pathdata['subdir'], $subdir, $pathdata['url']);
|
||||
$pathdata['subdir'] = str_replace($pathdata['subdir'], $subdir, $pathdata['subdir']);
|
||||
return $pathdata;
|
||||
|
||||
endif;
|
||||
}
|
||||
|
||||
return $pathdata;
|
||||
}
|
||||
|
|
|
@ -31,13 +31,6 @@ add_action('admin_init', 'woocommerce_prevent_admin_access');
|
|||
add_action('woocommerce_settings_saved', 'woocomerce_check_download_folder_protection');
|
||||
add_filter('mod_rewrite_rules', 'woocommerce_ms_protect_download_rewite_rules');
|
||||
|
||||
/**
|
||||
* Filters
|
||||
*
|
||||
* @see woocommerce_allow_img_insertion()
|
||||
*/
|
||||
add_filter('get_media_item_args', 'woocommerce_allow_img_insertion');
|
||||
|
||||
/**
|
||||
* File uploads
|
||||
*
|
||||
|
|
|
@ -363,8 +363,7 @@ function woocommerce_admin_scripts() {
|
|||
// Edit product category pages
|
||||
if (in_array( $screen->id, array('edit-product_cat') )) :
|
||||
|
||||
wp_enqueue_script( 'media-upload' );
|
||||
wp_enqueue_script( 'thickbox' );
|
||||
wp_enqueue_media();
|
||||
|
||||
endif;
|
||||
|
||||
|
@ -373,8 +372,7 @@ function woocommerce_admin_scripts() {
|
|||
|
||||
wp_enqueue_script( 'woocommerce_writepanel' );
|
||||
wp_enqueue_script( 'jquery-ui-datepicker' );
|
||||
wp_enqueue_script( 'media-upload' );
|
||||
wp_enqueue_script( 'thickbox' );
|
||||
wp_enqueue_media();
|
||||
wp_enqueue_script( 'ajax-chosen' );
|
||||
wp_enqueue_script( 'chosen' );
|
||||
wp_enqueue_script( 'plupload-all' );
|
||||
|
@ -474,7 +472,6 @@ function woocommerce_admin_css() {
|
|||
endif;
|
||||
|
||||
if ( $typenow == '' || $typenow=="product" || $typenow=="shop_order" || $typenow=="shop_coupon" ) :
|
||||
wp_enqueue_style( 'thickbox' );
|
||||
wp_enqueue_style( 'woocommerce_admin_styles', $woocommerce->plugin_url() . '/assets/css/admin.css' );
|
||||
wp_enqueue_style( 'jquery-ui-style', (is_ssl()) ? 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css' : 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css' );
|
||||
endif;
|
||||
|
|
|
@ -44,35 +44,39 @@ function woocommerce_add_category_fields() {
|
|||
if ( ! jQuery('#product_cat_thumbnail_id').val() )
|
||||
jQuery('.remove_image_button').hide();
|
||||
|
||||
window.send_to_editor_default = window.send_to_editor;
|
||||
// Uploading files
|
||||
var file_frame;
|
||||
|
||||
window.send_to_termmeta = function(html) {
|
||||
jQuery('.upload_image_button').live('click', function( event ){
|
||||
|
||||
jQuery('body').append('<div id="temp_image">' + html + '</div>');
|
||||
event.preventDefault();
|
||||
|
||||
var img = jQuery('#temp_image').find('img');
|
||||
// If the media frame already exists, reopen it.
|
||||
if ( file_frame ) {
|
||||
file_frame.open();
|
||||
return;
|
||||
}
|
||||
|
||||
imgurl = img.attr('src');
|
||||
imgclass = img.attr('class');
|
||||
imgid = parseInt(imgclass.replace(/\D/g, ''), 10);
|
||||
// Create the media frame.
|
||||
file_frame = wp.media.frames.downloadable_file = wp.media({
|
||||
title: '<?php _e( 'Choose an image', 'woocommerce' ); ?>',
|
||||
button: {
|
||||
text: '<?php _e( 'Use image', 'woocommerce' ); ?>',
|
||||
},
|
||||
multiple: false
|
||||
});
|
||||
|
||||
jQuery('#product_cat_thumbnail_id').val(imgid);
|
||||
jQuery('#product_cat_thumbnail img').attr('src', imgurl);
|
||||
jQuery('.remove_image_button').show();
|
||||
jQuery('#temp_image').remove();
|
||||
// When an image is selected, run a callback.
|
||||
file_frame.on( 'select', function() {
|
||||
attachment = file_frame.state().get('selection').first().toJSON();
|
||||
|
||||
tb_remove();
|
||||
jQuery('#product_cat_thumbnail_id').val( attachment.id );
|
||||
jQuery('#product_cat_thumbnail img').attr('src', attachment.url );
|
||||
jQuery('.remove_image_button').show();
|
||||
});
|
||||
|
||||
window.send_to_editor = window.send_to_editor_default;
|
||||
}
|
||||
|
||||
jQuery('.upload_image_button').live('click', function(){
|
||||
var post_id = 0;
|
||||
|
||||
window.send_to_editor = window.send_to_termmeta;
|
||||
|
||||
tb_show('', 'media-upload.php?post_id=' + post_id + '&type=image&TB_iframe=true');
|
||||
return false;
|
||||
// Finally, open the modal.
|
||||
file_frame.open();
|
||||
});
|
||||
|
||||
jQuery('.remove_image_button').live('click', function(){
|
||||
|
@ -132,35 +136,45 @@ function woocommerce_edit_category_fields( $term, $taxonomy ) {
|
|||
</div>
|
||||
<script type="text/javascript">
|
||||
|
||||
window.send_to_termmeta = function(html) {
|
||||
// Uploading files
|
||||
var file_frame;
|
||||
|
||||
jQuery('body').append('<div id="temp_image">' + html + '</div>');
|
||||
jQuery('.upload_image_button').live('click', function( event ){
|
||||
|
||||
var img = jQuery('#temp_image').find('img');
|
||||
event.preventDefault();
|
||||
|
||||
imgurl = img.attr('src');
|
||||
imgclass = img.attr('class');
|
||||
imgid = parseInt(imgclass.replace(/\D/g, ''), 10);
|
||||
// If the media frame already exists, reopen it.
|
||||
if ( file_frame ) {
|
||||
file_frame.open();
|
||||
return;
|
||||
}
|
||||
|
||||
jQuery('#product_cat_thumbnail_id').val(imgid);
|
||||
jQuery('#product_cat_thumbnail img').attr('src', imgurl);
|
||||
jQuery('#temp_image').remove();
|
||||
// Create the media frame.
|
||||
file_frame = wp.media.frames.downloadable_file = wp.media({
|
||||
title: '<?php _e( 'Choose an image', 'woocommerce' ); ?>',
|
||||
button: {
|
||||
text: '<?php _e( 'Use image', 'woocommerce' ); ?>',
|
||||
},
|
||||
multiple: false
|
||||
});
|
||||
|
||||
tb_remove();
|
||||
}
|
||||
// When an image is selected, run a callback.
|
||||
file_frame.on( 'select', function() {
|
||||
attachment = file_frame.state().get('selection').first().toJSON();
|
||||
|
||||
jQuery('.upload_image_button').live('click', function(){
|
||||
var post_id = 0;
|
||||
jQuery('#product_cat_thumbnail_id').val( attachment.id );
|
||||
jQuery('#product_cat_thumbnail img').attr('src', attachment.url );
|
||||
jQuery('.remove_image_button').show();
|
||||
});
|
||||
|
||||
window.send_to_editor = window.send_to_termmeta;
|
||||
|
||||
tb_show('', 'media-upload.php?post_id=' + post_id + '&type=image&TB_iframe=true');
|
||||
return false;
|
||||
// Finally, open the modal.
|
||||
file_frame.open();
|
||||
});
|
||||
|
||||
jQuery('.remove_image_button').live('click', function(){
|
||||
jQuery('#product_cat_thumbnail img').attr('src', '<?php echo woocommerce_placeholder_img_src(); ?>');
|
||||
jQuery('#product_cat_thumbnail_id').val('');
|
||||
jQuery('.remove_image_button').hide();
|
||||
return false;
|
||||
});
|
||||
|
||||
|
|
|
@ -1280,33 +1280,6 @@ jQuery( function($){
|
|||
});
|
||||
|
||||
// Uploading files
|
||||
/*var file_path_field;
|
||||
|
||||
window.send_to_editor_default = window.send_to_editor;
|
||||
|
||||
jQuery('.upload_file_button').live('click', function(){
|
||||
|
||||
file_path_field = jQuery(this).parent().find('.file_paths');
|
||||
|
||||
formfield = jQuery(file_path_field).attr('name');
|
||||
|
||||
window.send_to_editor = window.send_to_download_url;
|
||||
|
||||
tb_show('', 'media-upload.php?type=downloadable_product&from=wc01&TB_iframe=true');
|
||||
return false;
|
||||
});
|
||||
|
||||
window.send_to_download_url = function(html) {
|
||||
|
||||
file_url = jQuery(html).attr('href');
|
||||
if (file_url) {
|
||||
jQuery(file_path_field).val(jQuery(file_path_field).val() ? jQuery(file_path_field).val() + "\n" + file_url : file_url);
|
||||
}
|
||||
tb_remove();
|
||||
window.send_to_editor = window.send_to_editor_default;
|
||||
|
||||
}*/
|
||||
|
||||
var downloadable_file_frame;
|
||||
|
||||
jQuery('.upload_file_button').live('click', function( event ){
|
||||
|
@ -1323,6 +1296,17 @@ jQuery( function($){
|
|||
return;
|
||||
}
|
||||
|
||||
var downloadable_file_states = [
|
||||
// Main states.
|
||||
new wp.media.controller.Library({
|
||||
library: wp.media.query(),
|
||||
multiple: true,
|
||||
title: $el.data('choose'),
|
||||
priority: 20,
|
||||
filterable: 'uploaded',
|
||||
})
|
||||
];
|
||||
|
||||
// Create the media frame.
|
||||
downloadable_file_frame = wp.media.frames.downloadable_file = wp.media({
|
||||
// Set the title of the modal.
|
||||
|
@ -1334,6 +1318,7 @@ jQuery( function($){
|
|||
text: $el.data('update'),
|
||||
},
|
||||
multiple: true,
|
||||
states: downloadable_file_states,
|
||||
});
|
||||
|
||||
// When an image is selected, run a callback.
|
||||
|
@ -1353,6 +1338,14 @@ jQuery( function($){
|
|||
$file_path_field.val( file_paths );
|
||||
});
|
||||
|
||||
// Set post to 0 and set our custom type
|
||||
downloadable_file_frame.on( 'ready', function() {
|
||||
downloadable_file_frame.uploader.options.uploader.params = {
|
||||
attach_to: 0,
|
||||
type: 'downloadable_product'
|
||||
};
|
||||
});
|
||||
|
||||
// Finally, open the modal.
|
||||
downloadable_file_frame.open();
|
||||
});
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue