@@ -138,31 +138,33 @@ function iaff_total_number_of_images() {
138
138
}
139
139
140
140
/**
141
- * Count remaining number of images to process
141
+ * Count remaining number of images to process.
142
142
*
143
- * @since 1.0
144
- * @return integer Returns the number of remaining images to process.
145
- * @param $force_return When set as true the function will always return a value even when called from ajax.
143
+ * @since 1.0
144
+ * @since 4.0 Removed the $force_return param which when set to false would echo the result. Created iaff_echo_count_remaining_images() for that.
145
+ *
146
+ * @return (integer) Returns the number of remaining images to process.
146
147
*/
147
- function iaff_count_remaining_images ( $ force_return = false ) {
148
+ function iaff_count_remaining_images () {
148
149
149
150
$ total_no_of_images = iaff_total_number_of_images ();
150
151
151
152
$ no_of_images_processed = get_option ('iaff_bulk_updater_counter ' );
152
- $ no_of_images_processed = intval ($ no_of_images_processed );
153
-
154
- $ reamining_images = max ($ total_no_of_images - $ no_of_images_processed , 0 );
153
+ $ no_of_images_processed = intval ( $ no_of_images_processed );
155
154
156
- // If called from Ajax echo the result. Else return as an integer.
157
- // :TODO: Calling iaff_count_remaining_images() from Ajax for ignores the default value of $force_return for some reason. When I set if ( wp_doing_ajax() && $force_return === false ) this does not work even though they are logically equivalent. If you know why it is so, please email me - arunbasillal@gmail.com
158
- if ( wp_doing_ajax () && $ force_return !== true ) {
159
- echo $ reamining_images ;
160
- wp_die ();
161
- } else {
162
- return $ reamining_images ;
163
- }
155
+ return max ( $ total_no_of_images - $ no_of_images_processed , 0 );
164
156
}
165
- add_action ( 'wp_ajax_iaff_count_remaining_images ' , 'iaff_count_remaining_images ' );
157
+
158
+ /**
159
+ * Wrapper for iaff_count_remaining_images() to echo the result.
160
+ *
161
+ * @since 4.0
162
+ */
163
+ function iaff_echo_count_remaining_images () {
164
+ echo iaff_count_remaining_images ();
165
+ wp_die ();
166
+ }
167
+ add_action ( 'wp_ajax_iaff_count_remaining_images ' , 'iaff_echo_count_remaining_images ' );
166
168
167
169
168
170
/**
@@ -179,7 +181,7 @@ function iaff_reset_bulk_updater_counter() {
179
181
180
182
$ response = array (
181
183
'message ' => __ ('Counter reset. The bulk updater will start from scratch in the next run. ' , 'auto-image-attributes-from-filename-with-bulk-updater ' ),
182
- 'remaining_images ' => iaff_count_remaining_images (true ),
184
+ 'remaining_images ' => iaff_count_remaining_images (),
183
185
);
184
186
wp_send_json ($ response );
185
187
}
@@ -201,6 +203,8 @@ function iaff_before_bulk_updater() {
201
203
* @link https://imageattributespro.com/codex/iaff_before_bulk_updater/
202
204
*/
203
205
do_action ('iaff_before_bulk_updater ' );
206
+
207
+ wp_die ();
204
208
}
205
209
add_action ( 'wp_ajax_iaff_before_bulk_updater ' , 'iaff_before_bulk_updater ' );
206
210
@@ -220,6 +224,8 @@ function iaff_after_bulk_updater() {
220
224
* @link https://imageattributespro.com/codex/iaff_after_bulk_updater/
221
225
*/
222
226
do_action ('iaff_after_bulk_updater ' );
227
+
228
+ wp_die ();
223
229
}
224
230
add_action ( 'wp_ajax_iaff_after_bulk_updater ' , 'iaff_after_bulk_updater ' );
225
231
@@ -247,7 +253,7 @@ function iaff_bulk_updater_skip_image() {
247
253
if ( $ image === NULL ) {
248
254
$ response = array (
249
255
'message ' => __ ( 'No more images to skip. ' , 'auto-image-attributes-from-filename-with-bulk-updater ' ),
250
- 'remaining_images ' => iaff_count_remaining_images ( true ),
256
+ 'remaining_images ' => iaff_count_remaining_images (),
251
257
);
252
258
wp_send_json ( $ response );
253
259
}
@@ -261,7 +267,7 @@ function iaff_bulk_updater_skip_image() {
261
267
262
268
$ response = array (
263
269
'message ' => __ ( 'Image skipped: ' , 'auto-image-attributes-from-filename-with-bulk-updater ' ) . '<a href=" ' . get_edit_post_link ( $ image ->ID ) .'"> ' . $ image_url .'</a> ' ,
264
- 'remaining_images ' => iaff_count_remaining_images ( true ),
270
+ 'remaining_images ' => iaff_count_remaining_images (),
265
271
);
266
272
wp_send_json ( $ response );
267
273
}
@@ -284,6 +290,14 @@ function iaff_image_bulk_updater() {
284
290
jQuery(document).ready(function($) {
285
291
286
292
var iaff_stop = false;
293
+ var iaffpro_bu_exists = <?php echo function_exists ( 'iaffpro_bu_bulk_updater_init ' ) ? 'true ' : 'false ' ; ?> ;
294
+
295
+ $("#bulk-updater-log").animate({scrollTop:$("#bulk-updater-log")[0].scrollHeight - $("#bulk-updater-log").height()},200);
296
+
297
+ // Enable "Stop Bulk Updater" button if Bulk Updater is running in the background.
298
+ <?php if ( function_exists ( 'as_has_scheduled_action ' ) && ( as_has_scheduled_action ( 'iaffpro_bu_bulk_updater ' ) === true ) ) {
299
+ echo "iaff_stop_bulk_updater_button_switch( true ); " ;
300
+ }?>
287
301
288
302
// Bulk Updater
289
303
function iaff_do_bulk_updater(iaff_test=false) {
@@ -297,12 +311,15 @@ function iaff_do_bulk_updater(iaff_test=false) {
297
311
focused = false;
298
312
};
299
313
$('.iaff-spinner').addClass("spinner"); // Turn spinner on
300
- $('.iaff_stop_bulk_updater_button').prop('disabled', false); // Enable stop button
301
- $('.iaff_stop_bulk_updater_button').removeClass("button-secondary");
302
- $('.iaff_stop_bulk_updater_button').addClass("button-primary"); // Turn stop button primary
314
+ iaff_stop_bulk_updater_button_switch( true ); // Enable stop button
303
315
304
316
// Notice to the user
305
- $('#bulk-updater-log').append('<p class="iaff-green"><span class="dashicons dashicons-controls-play"></span>Initializing bulk updater. Please be patient and do not close the browser while it\'s running. In case you do, you can always resume by returning to this page later.</p>');
317
+ if( ( iaffpro_bu_exists === true ) && ( iaff_test === false ) ) {
318
+ $('#bulk-updater-log').append('<p class="iaff-green"><span class="dashicons dashicons-controls-play"></span>Bulk Updater will now run in the background. You can close this page and check back later to see progress.</p>');
319
+ } else {
320
+ $('#bulk-updater-log').append('<p class="iaff-green"><span class="dashicons dashicons-controls-play"></span>Initializing bulk updater. Please be patient and do not close the browser while it\'s running. In case you do, you can always resume by returning to this page later.</p>');
321
+ }
322
+
306
323
$("#bulk-updater-log").animate({scrollTop:$("#bulk-updater-log")[0].scrollHeight - $("#bulk-updater-log").height()},200);
307
324
308
325
// Count Remaining Images To Process
@@ -317,6 +334,17 @@ function iaff_do_bulk_updater(iaff_test=false) {
317
334
318
335
// set remaining count as 1 when running in test mode
319
336
reamining_images_count.done(function() {
337
+
338
+ // Run the background bulk updater (since IAP 4.0) if it's available.
339
+ if( ( iaffpro_bu_exists === true ) && ( iaff_test === false ) ) {
340
+ data = {
341
+ action: 'iaffpro_bu_bulk_updater_init',
342
+ security: '<?php echo wp_create_nonce ( "iaffpro_bu_bulk_updater_init_nonce " ); ?> '
343
+ };
344
+ $.post(ajaxurl, data);
345
+
346
+ return;
347
+ }
320
348
321
349
if((iaff_test===true)&&(remaining_images>1)) {
322
350
remaining_images = 1;
@@ -369,9 +397,7 @@ function iaff_do_bulk_updater(iaff_test=false) {
369
397
}
370
398
371
399
$('.iaff-spinner').removeClass('spinner'); // Turn spinner off
372
- $('.iaff_stop_bulk_updater_button').removeClass("button-primary");
373
- $('.iaff_stop_bulk_updater_button').addClass("button-secondary"); // Turn stop button secondary
374
- $('.iaff_stop_bulk_updater_button').prop('disabled', true); // Disable stop button
400
+ iaff_stop_bulk_updater_button_switch( false ); // Disable stop button
375
401
}
376
402
});
377
403
});
@@ -422,6 +448,18 @@ function iaff_do_bulk_updater(iaff_test=false) {
422
448
// Stop Bulk Updater
423
449
$('.iaff_stop_bulk_updater_button').click(function() {
424
450
iaff_stop=true;
451
+
452
+ // Stop background processing
453
+ if( iaffpro_bu_exists === true ) {
454
+ data = {
455
+ action: 'iaffpro_bu_stop_bulk_updater',
456
+ security: '<?php echo wp_create_nonce ( "iaffpro_bu_stop_bulk_updater_nonce " ); ?> '
457
+ };
458
+
459
+ $.post(ajaxurl, data, function() {
460
+ iaff_stop_bulk_updater_button_switch( false );
461
+ });
462
+ }
425
463
});
426
464
427
465
// Reset Bulk Updater Counter
@@ -468,6 +506,28 @@ function iaff_do_bulk_updater(iaff_test=false) {
468
506
$("#bulk-updater-log").animate({scrollTop:$("#bulk-updater-log")[0].scrollHeight - $("#bulk-updater-log").height()},200);
469
507
});
470
508
});
509
+
510
+ /**
511
+ * Enable or disable "Stop Bulk Updater" button.
512
+ *
513
+ * @param state (bool) True to enable button, false to disable.
514
+ */
515
+ function iaff_stop_bulk_updater_button_switch( state ) {
516
+ switch ( state ) {
517
+ case true:
518
+ $('.iaff_stop_bulk_updater_button').prop('disabled', false); // Enable stop button
519
+ $('.iaff_stop_bulk_updater_button').removeClass("button-secondary");
520
+ $('.iaff_stop_bulk_updater_button').addClass("button-primary"); // Turn stop button primary
521
+ break;
522
+
523
+ case false:
524
+ default:
525
+ $('.iaff_stop_bulk_updater_button').removeClass("button-primary");
526
+ $('.iaff_stop_bulk_updater_button').addClass("button-secondary"); // Turn stop button secondary
527
+ $('.iaff_stop_bulk_updater_button').prop('disabled', true); // Disable stop button
528
+ break;
529
+ }
530
+ }
471
531
472
532
});
473
533
</script> <?php
0 commit comments