generated from gravitywiz/batcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-gwiz-batcher.php
167 lines (132 loc) · 3.84 KB
/
class-gwiz-batcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Class Gwiz_Batcher
*/
if ( class_exists( 'Gwiz_Batcher' ) ) {
add_action( 'admin_notices', function() {
?>
<div class="notice notice-warning is-dismissible">
<p>You have two plugins using Gravity Wiz Batcher. Only one should be active at a time.</p>
</div>
<?php
} );
} else {
class Gwiz_Batcher {
private $_args;
public function __construct( $args ) {
$this->_args = wp_parse_args( $args, array(
'title' => 'GW Batcher',
'id' => 'gw-batcher',
) );
add_action( 'wp_ajax_gw_batch_' . $this->_args['id'], array( $this, 'batch' ) );
add_action( 'wp_ajax_nopriv_gw_batch_' . $this->_args['id'], array( $this, 'batch' ) );
add_filter( 'gform_addon_navigation', array( $this, 'add_menu_item' ) );
}
function add_menu_item( $menu_items ) {
$menu_items[] = array(
'name' => $this->_args['id'],
'label' => $this->_args['title'],
'callback' => array( $this, 'admin_page' ),
'permission' => 'gform_full_access',
);
return $menu_items;
}
public function admin_page() {
?>
<style>
h1 {
font-family: sans-serif;
margin-bottom: 20px;
}
#gwb-preview {
border: 1px solid #ccc;
height: 20px;
width: 100%;
margin-bottom: 20px;
padding: 2px;
border-radius: 4px;
}
#gwb-preview span {
display: block;
height: 100%;
width: 0;
background-color: #999;
border-radius: 3px;
transition: all 0.5s ease;
}
</style>
<div class="wrap">
<h2><?php echo $this->_args['title']; ?></h2>
<div class="notice updated" id="gwb-success" style="display: none;">
<p><strong>Success!</strong></p>
<p>Please deactivate this plugin.</p>
</div>
<div id="gwb-preview"><span></span></div>
<button id="gwb-start" class="button-primary">Start Batch</button>
</div>
<script>
var ajaxUrl = '<?php echo admin_url( 'admin-ajax.php' ); ?>',
action = 'gw_batch_<?php echo $this->_args['id']; ?>',
nonce = '<?php echo wp_create_nonce( "gw_batch_{$this->_args['id']}" ); ?>',
size = <?php echo $this->_args['size']; ?>;
(function ($) {
var $preview = $('#gwb-preview'),
$start = $('#gwb-start');
$start.click(function () {
$start.prop('disabled', true);
gwBatch(size, 1, 0, null);
});
function gwBatch(size, page, count, total) {
$.post(ajaxUrl, {
action: action,
nonce: nonce,
size: size,
page: page,
total: total,
count: count
}, function (response) {
if (response.error) {
console.log(response.data);
} else if (response.success) {
if (typeof response.data == 'string' && response.data == 'done') {
$preview.find('span').width('100%');
$('#gwb-success').show(500);
$preview.hide(500);
$('#gwb-start').hide(500);
} else {
$preview.find('span').width((response.data.count / response.data.total * 100) + '%');
gwBatch(response.data.size, response.data.page, response.data.count, response.data.total);
}
}
});
}
})(jQuery);
</script>
<?php
}
public function batch() {
$action = $_POST['action'];
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, $action ) ) {
wp_send_json_error( 'Invalid nonce.' );
}
$size = $_POST['size'];
$page = $_POST['page'];
$offset = ( $page * $size ) - $size;
$count = max( 0, (int) $_POST['count'] );
$items = $this->_args['get_items']( $size, $offset );
$total = $items['total'];
$items = $items['items'];
foreach ( $items as $item ) {
$this->_args['process_item']( $item );
$count ++;
}
if ( $count >= $total ) {
wp_send_json_success( 'done' );
} else {
$page ++;
wp_send_json_success( compact( 'size', 'page', 'count', 'total' ) );
}
}
}
}