| 1 |
// @ref http://rachelcarden.com/2012/03/manage-wordpress-posts-using-bulk-edit-and-quick-edit/ |
| 2 |
(function ($) { |
| 3 |
|
| 4 |
// we create a copy of the WP inline edit post function |
| 5 |
var $eventpost_inline_edit = inlineEditPost.edit; |
| 6 |
// and then we overwrite the function with our own code |
| 7 |
inlineEditPost.edit = function (id) { |
| 8 |
// "call" the original WP edit function |
| 9 |
// we don't want to leave WordPress hanging |
| 10 |
$eventpost_inline_edit.apply(this, arguments); |
| 11 |
|
| 12 |
// now we take care of our business |
| 13 |
|
| 14 |
// get the post ID |
| 15 |
var $post_id = 0; |
| 16 |
if (typeof (id) === 'object') |
| 17 |
$post_id = parseInt(this.getId(id)); |
| 18 |
|
| 19 |
console.log($post_id); |
| 20 |
|
| 21 |
if ($post_id > 0) { |
| 22 |
// define the edit row |
| 23 |
var key; |
| 24 |
var $edit_row = $('#edit-' + $post_id); |
| 25 |
var $post_row = $('#post-' + $post_id); |
| 26 |
var attrs = eventpost_inline_edit.quick; |
| 27 |
|
| 28 |
// get the data and populate |
| 29 |
for(field_group in attrs){ |
| 30 |
for(key in attrs[field_group]){ |
| 31 |
$( '.eventpost-inline-'+key, $edit_row ).val( $( '.inline-edit-value.'+key, $post_row ).html() ); |
| 32 |
if(key==='event_color'){ |
| 33 |
eventpost_inline_colorpick($( '.eventpost-inline-'+key, $edit_row )); |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
}; |
| 39 |
function eventpost_inline_colorpick(target){ |
| 40 |
console.log(target); |
| 41 |
if(target.hasClass('is-bulk')){ |
| 42 |
target.val('false').trigger('change'); |
| 43 |
} |
| 44 |
if(target && target.next('.eventpost-inline-colorpicker-list').length){ |
| 45 |
return; |
| 46 |
} |
| 47 |
var eventpost_inline_colorpicker_html=$('<span class="eventpost-inline-colorpicker-list"></span>'); |
| 48 |
$('option', target).each(function(){ |
| 49 |
if($(this).attr('value')!=='false'){ |
| 50 |
color_item = $('<img src="'+$(this).data('path')+'" alt="'+$(this).attr('value')+'" class="ep-i-c-'+$(this).attr('value')+'"/>').click(function(){ |
| 51 |
$(this).parent().prev('.eventpost-inline-colorpicker').val($(this).attr('alt')).trigger('change'); |
| 52 |
}); |
| 53 |
eventpost_inline_colorpicker_html.append(color_item); |
| 54 |
} |
| 55 |
}); |
| 56 |
target.after(eventpost_inline_colorpicker_html).hide().unbind('change').on('change', function(){ |
| 57 |
value = $(this).val(); |
| 58 |
console.log(value); |
| 59 |
$('img', $(this).next('.eventpost-inline-colorpicker-list')).attr('style', '').filter('.ep-i-c-'+value).css({border: '#000 1px solid', padding: '3px'}); |
| 60 |
}); |
| 61 |
target.trigger('change'); |
| 62 |
} |
| 63 |
|
| 64 |
var $eventpost_bulk_sent = false; |
| 65 |
|
| 66 |
function eventpost_bulk_send(){ |
| 67 |
if($eventpost_bulk_sent){ |
| 68 |
return; |
| 69 |
} |
| 70 |
$eventpost_bulk_sent = true; |
| 71 |
|
| 72 |
// define the bulk edit row |
| 73 |
var key; |
| 74 |
var $bulk_row = $('#bulk-edit'); |
| 75 |
var post_type = $('#posts-filter input[name=post_type]').val(); |
| 76 |
var attrs = eventpost_inline_edit.bulk; |
| 77 |
|
| 78 |
// get the selected post ids that are being edited |
| 79 |
var $post_ids = new Array(); |
| 80 |
$bulk_row.find('#bulk-titles').children().each(function () { |
| 81 |
$post_ids.push($(this).attr('id').replace(/^(ttle)/i, '')); |
| 82 |
}); |
| 83 |
datas = { |
| 84 |
action: 'eventpost_save_bulk', // this is the name of our WP AJAX function that we'll set up next |
| 85 |
post_ids: $post_ids, // and these are the 2 parameters we're passing to our function |
| 86 |
eventpost_nonce: $('#eventpost_nonce').val() |
| 87 |
}; |
| 88 |
|
| 89 |
// get the data |
| 90 |
for(field_group in attrs){ |
| 91 |
for(key in attrs[field_group]){ |
| 92 |
if($( '#posts-filter .eventpost-inline-'+key+'' ).val() !== 'false'){ |
| 93 |
datas[key] = $( '#posts-filter .eventpost-inline-'+key+'' ).val(); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// save the data |
| 99 |
$.ajax({ |
| 100 |
url: ajaxurl, // this is a variable that WordPress has already defined for us |
| 101 |
type: 'POST', |
| 102 |
async: false, |
| 103 |
cache: false, |
| 104 |
data: datas |
| 105 |
}); |
| 106 |
} |
| 107 |
$('.eventpost-bulk-colorpicker-button').click(function(){ |
| 108 |
eventpost_inline_colorpick($(this).next('.eventpost-inline-event_color')); |
| 109 |
}).next('.eventpost-inline-event_color').hide(); |
| 110 |
$('#bulk_edit').live('click', function () { |
| 111 |
eventpost_bulk_send(); |
| 112 |
}); |
| 113 |
$('#posts-filter').live('submit', function(){ |
| 114 |
eventpost_bulk_send(); |
| 115 |
}); |
| 116 |
})(jQuery); |