alpha
Этот коммит содержится в:
родитель
0599ff94da
Коммит
6cdf38a236
226
bath.php
Обычный файл
226
bath.php
Обычный файл
@ -0,0 +1,226 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Plugin Name: Batch operations
|
||||||
|
* Description: My version Drupal Batch API for WordPress.
|
||||||
|
* Version: 0.1.0a
|
||||||
|
* Author: Igor V Belousov
|
||||||
|
* Author URI: http://belousovv.ru/
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Create table on activate
|
||||||
|
register_activation_hook(ABSPATH.PLUGINDIR.'/batch-operations/bath.php','batch_operations_install');
|
||||||
|
|
||||||
|
// Add backend page without menu item
|
||||||
|
add_action( 'admin_menu', 'batch_operations_add_page' );
|
||||||
|
|
||||||
|
// Add JSON query for run operation
|
||||||
|
add_action( 'wp_ajax_batch_operations', 'batch_operations_process' );
|
||||||
|
|
||||||
|
global $batch_operations_version;
|
||||||
|
$batch_operations_version = '0.1.0a';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create table on activate
|
||||||
|
*/
|
||||||
|
function batch_operations_install () {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$table_name = $wpdb->prefix . 'batch_operations';
|
||||||
|
|
||||||
|
$charset_collate = $wpdb->get_charset_collate();
|
||||||
|
|
||||||
|
$sql = "CREATE TABLE $table_name (
|
||||||
|
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||||
|
`operations` longtext NOT NULL,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) $charset_collate AUTO_INCREMENT=1;";
|
||||||
|
|
||||||
|
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
|
||||||
|
dbDelta( $sql );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add backend page without menu item
|
||||||
|
*/
|
||||||
|
function batch_operations_add_page() {
|
||||||
|
add_management_page( 'Batch operations', '', 'edit_posts', 'batch-operations', 'batch_operations_page_view' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* View batch operations page
|
||||||
|
*/
|
||||||
|
function batch_operations_page_view() {
|
||||||
|
//WP 3.3
|
||||||
|
wp_enqueue_script( 'jquery' );
|
||||||
|
wp_enqueue_script( 'batch_operations_script', plugin_dir_url('') . 'batch-operations/js/batch.min.js' );
|
||||||
|
wp_enqueue_style( 'batch_operations_script', plugin_dir_url('') . 'batch-operations/css/batch.css' );
|
||||||
|
$id = ( intval( $_REQUEST["id"] ) < 0 )? 0 : intval( $_REQUEST["id"] );
|
||||||
|
?>
|
||||||
|
<script type="text/javascript">
|
||||||
|
var batch_id=<?php print $id; ?>,successful_page='<?php print get_admin_url(); ?>';
|
||||||
|
</script>
|
||||||
|
<div class="wrap">
|
||||||
|
<?php screen_icon(); ?>
|
||||||
|
<h2><?php echo get_admin_page_title() ?></h2>
|
||||||
|
<div class="batch-progress">
|
||||||
|
<span style="width:0%;"></span>
|
||||||
|
</div>
|
||||||
|
<div class="batch-message"></div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
|
function batch_operations_process () {
|
||||||
|
global $wpdb;
|
||||||
|
$id = ( intval( $_REQUEST["id"] ) < 0 )? 0 : intval( $_REQUEST["id"] );
|
||||||
|
|
||||||
|
if ( 1 > $id ){
|
||||||
|
wp_send_json( array( 'do' => 'finish' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$carr = $wpdb->get_var( 'SELECT `operations` FROM `' . $wpdb->prefix . "batch_operations` WHERE `id` = $id;" );
|
||||||
|
if ( empty( $carr ) ) {
|
||||||
|
wp_send_json( array( 'do' => 'finish' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$result['do'] = '';
|
||||||
|
$start = time() + 1;
|
||||||
|
$flag = true;
|
||||||
|
$carr = unserialize( $carr );
|
||||||
|
|
||||||
|
while ($flag) {
|
||||||
|
//make array of parameters for function
|
||||||
|
$arr = array();
|
||||||
|
if ( isset( $carr['operations'][0][1] ) ) {
|
||||||
|
$arr = $carr['operations'][0][1];
|
||||||
|
}
|
||||||
|
$arr[] = &$carr['context'];
|
||||||
|
//run function
|
||||||
|
call_user_func_array( $carr['operations'][0][0], $arr );
|
||||||
|
|
||||||
|
if ( true == $carr['context']['finished'] ) {
|
||||||
|
$carr['context']['sandbox'] = array();
|
||||||
|
array_splice( $carr['operations'], 0, 1 );
|
||||||
|
$carr['current']++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( time() > $start || 0 == count($carr['operations']) ) {
|
||||||
|
$flag=false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( 0 == count($carr['operations']) ) {
|
||||||
|
$result['do']='finish';
|
||||||
|
}
|
||||||
|
|
||||||
|
$result['percent'] = round( $carr['current'] / ($carr['count'] /100 ) );
|
||||||
|
$result['message'] = $carr['context']['message'];
|
||||||
|
|
||||||
|
if ( '' == $result['do'] ) {
|
||||||
|
$wpdb->update(
|
||||||
|
$wpdb->prefix . 'batch_operations',
|
||||||
|
array( 'operations' => serialize( $carr ) ),
|
||||||
|
array( 'id' => $id ),
|
||||||
|
array( '%s' ),
|
||||||
|
array( '%d' )
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$wpdb->query( 'DELETE FROM `' . $wpdb->prefix . 'batch_operations' . "` WHERE `id`=$id ;" );
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_send_json( $result );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start batch operations
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* $batch = array(
|
||||||
|
* 'title' => t('Exporting'),
|
||||||
|
* 'operations' => array(
|
||||||
|
* array('my_function_1', array(123, 'qwe')),
|
||||||
|
* array('my_function_2', array()),
|
||||||
|
* ),
|
||||||
|
* 'finished' => 'my_finished_callback',
|
||||||
|
* );
|
||||||
|
*
|
||||||
|
* BatchController::Start($batch);
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li> operations: (required) Array of operations to be performed, where each item is an array consisting of the name of an implementation of callback_batch_operation() and an array of parameter. Example:
|
||||||
|
* <li> title: A safe, translated string to use as the title for the progress page. Defaults to t('Processing').
|
||||||
|
* <li> init_message: Message displayed while the processing is initialized. Defaults to t('Initializing.').
|
||||||
|
* <li> progress_message: Message displayed while processing the batch. Available placeholders are %current% and %total%.
|
||||||
|
* <li> error_message: Message displayed if an error occurred while processing the batch. Defaults to t('An error has occurred.').
|
||||||
|
* <li> finished: Name of an implementation of callback_batch_finished(). This is executed after the batch has completed. This should be used to perform any result massaging that may be needed, and possibly save data in $_SESSION for display after final page redirection.
|
||||||
|
*
|
||||||
|
* Sample callback_batch_operation():
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* function my_function_1($id, $text, &$context) {
|
||||||
|
* $context['results'][] = $text . $id;
|
||||||
|
* $context['message'] = 'Text + id ='.$text . $id;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* The $context array gathers batch context information about the execution (read),
|
||||||
|
* as well as 'return values' for the current operation (write)
|
||||||
|
* The following keys are provided :
|
||||||
|
* 'results' (read / write): The array of results gathered so far by
|
||||||
|
* the batch processing, for the current operation to append its own.
|
||||||
|
* 'message' (write): A text message displayed in the progress page.
|
||||||
|
* The following keys allow for multi-step operations :
|
||||||
|
* 'sandbox' (read / write): An array that can be freely used to
|
||||||
|
* store persistent data between iterations. It is recommended to
|
||||||
|
* use this instead of $_SESSION, which is unsafe if the user
|
||||||
|
* continues browsing in a separate window while the batch is processing.
|
||||||
|
* 'finished' (write): A float number between 0 and 1 informing
|
||||||
|
* the processing engine of the completion level for the operation.
|
||||||
|
* 1 (or no value explicitly set) means the operation is finished
|
||||||
|
* and the batch processing can continue to the next operation.
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param array $batch_arr array operations and more
|
||||||
|
*/
|
||||||
|
function batch_operations_start($batch_arr)
|
||||||
|
{
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$batch_arr['context'] = array(
|
||||||
|
'message' => '',
|
||||||
|
'sandbox' => array(),
|
||||||
|
'finished' => true,
|
||||||
|
'results' => array()
|
||||||
|
);
|
||||||
|
$batch_arr['count'] = count( $batch_arr['operations'] );
|
||||||
|
$batch_arr['current'] = 0;
|
||||||
|
|
||||||
|
$wpdb->insert(
|
||||||
|
$wpdb->prefix . 'batch_operations',
|
||||||
|
array(
|
||||||
|
'operations' => serialize( $batch_arr )
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'%s'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$location = get_admin_url(null, 'tools.php') . "?page=batch-operations&id=" . $wpdb->insert_id;
|
||||||
|
|
||||||
|
if ( ! headers_sent() ) {
|
||||||
|
wp_redirect( $location );
|
||||||
|
} else {
|
||||||
|
// if header is set then runs this hack
|
||||||
|
echo '<script type="text/javascript">';
|
||||||
|
echo 'document.location.href="' . $location .'";';
|
||||||
|
echo '</script>';
|
||||||
|
echo '<noscript>';
|
||||||
|
echo '<meta http-equiv="refresh" content="0;url=' . $location . '" />';
|
||||||
|
echo '</noscript>';
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
1
css/ShortCSS
Подмодуль
1
css/ShortCSS
Подмодуль
@ -0,0 +1 @@
|
|||||||
|
Subproject commit c87f6175bc7af40bce6f62d0ae1855f5149fdaf6
|
1
css/batch.css
Обычный файл
1
css/batch.css
Обычный файл
@ -0,0 +1 @@
|
|||||||
|
.batch-progress{height:30px;position:relative;background:#fff;-webkit-border-radius:8px;-moz-border-radius:8px;-o-border-radius:8px;-ms-border-radius:8px;-khtml-border-radius:8px;border-radius:8px;border:1px solid #ccc;margin:10px 0;padding:2px}.batch-progress>span{display:block;height:100%;background-color:#1834ff;background-image:-moz-linear-gradient(#4d80ff,#002ffb);background-image:-webkit-linear-gradient(#4d80ff,#002ffb);background-image:-o-linear-gradient(#4d80ff,#002ffb);background-image:-ms-linear-gradient(#4d80ff,#002ffb);background-image:linear-gradient(#4d80ff,#002ffb);-webkit-border-radius:6px;-moz-border-radius:6px;-o-border-radius:6px;-ms-border-radius:6px;-khtml-border-radius:6px;border-radius:6px;position:relative;overflow:hidden}
|
19
css/batch.styl
Обычный файл
19
css/batch.styl
Обычный файл
@ -0,0 +1,19 @@
|
|||||||
|
@import "ShortCSS/ShortCSS"
|
||||||
|
|
||||||
|
.batch-progress
|
||||||
|
-h 30px
|
||||||
|
-pos relative
|
||||||
|
-bg #fff
|
||||||
|
b-radius 8px
|
||||||
|
-br 1px solid #ccc
|
||||||
|
-m 10px 0
|
||||||
|
-p 2px
|
||||||
|
& > span
|
||||||
|
-d block
|
||||||
|
-h 100%
|
||||||
|
-bgc #1834ff
|
||||||
|
l-gradient #4d80ff, #002ffb
|
||||||
|
b-radius 6px
|
||||||
|
-pos relative
|
||||||
|
overflow hidden
|
||||||
|
|
32
js/batch.js
Обычный файл
32
js/batch.js
Обычный файл
@ -0,0 +1,32 @@
|
|||||||
|
jQuery(document).ready(function($) {
|
||||||
|
var batch_progress = function (percent){
|
||||||
|
return $(".batch-progress > span").animate({ width: percent + "%" }, 600);
|
||||||
|
};
|
||||||
|
|
||||||
|
function batch_process(){
|
||||||
|
$.post(
|
||||||
|
ajaxurl+'?action=batch_operations&id='+batch_id,
|
||||||
|
function(data){
|
||||||
|
if (data['do']=='finish')
|
||||||
|
{
|
||||||
|
|
||||||
|
batch_progress(data['percent']);
|
||||||
|
$('.batch-message').html(data['message']).delay(1500).queue(function () {
|
||||||
|
$(location).attr('href',successful_page);
|
||||||
|
$(this).dequeue();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$('.batch-message').html(data['message']);
|
||||||
|
batch_progress(data['percent']);
|
||||||
|
batch_process()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
batch_process();
|
||||||
|
|
||||||
|
});
|
1
js/batch.min.js
поставляемый
Обычный файл
1
js/batch.min.js
поставляемый
Обычный файл
@ -0,0 +1 @@
|
|||||||
|
jQuery(document).ready(function(a){function b(){a.post(ajaxurl+"?action=batch_operations&id="+batch_id,function(d){"finish"==d["do"]?(c(d.percent),a(".batch-message").html(d.message).delay(1500).queue(function(){a(location).attr("href",successful_page),a(this).dequeue()})):(a(".batch-message").html(d.message),c(d.percent),b())})}var c=function(b){return a(".batch-progress > span").animate({width:b+"%"},600)};b()});
|
@ -1,5 +1,5 @@
|
|||||||
=== Batch operations ===
|
=== Batch operations ===
|
||||||
Contributors: Igor V Belousov
|
Contributors: igor-v-belousov
|
||||||
Tags: batch
|
Tags: batch
|
||||||
Requires at least: 3.3
|
Requires at least: 3.3
|
||||||
Tested up to: 3.3
|
Tested up to: 3.3
|
||||||
|
Загрузка…
Ссылка в новой задаче
Block a user