Skip to content

Commit 3ec42fc

Browse files
authored
Merge pull request #10 from arunbasillal/branch-4.0
Branch 4.0
2 parents 21e48dc + 968e3c0 commit 3ec42fc

7 files changed

+155
-79
lines changed

admin/iaff_image-attributes-from-filename-admin-ui-render.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,9 +1072,21 @@ function iaff_admin_interface_render () {
10721072
<legend><span class="dashicons dashicons-welcome-write-blog"></span>&nbsp;<strong><?php _e('Event Log', 'auto-image-attributes-from-filename-with-bulk-updater'); ?></strong>&nbsp;<div class="iaff-spinner is-active" style="margin-top:0px;"></div></legend>
10731073

10741074
<div id="bulk-updater-log">
1075+
1076+
<?php
1077+
if ( function_exists( 'iaffpro_event_log_read' ) ) {
1078+
$event_log = iaffpro_event_log_read();
1079+
1080+
foreach ( $event_log as $log ) {
1081+
echo '<p>' . $log . '</p>';
1082+
}
1083+
}
1084+
?>
1085+
10751086
<p id="iaff_remaining_images_text"><?php _e('Number of Images Remaining: ', 'auto-image-attributes-from-filename-with-bulk-updater') ?><?php echo iaff_count_remaining_images(); ?></p>
10761087

10771088
<p><?php _e('Number of Images Updated: ', 'auto-image-attributes-from-filename-with-bulk-updater') ?><?php echo iaff_number_of_images_updated(); ?></p>
1089+
10781090
</div>
10791091
</fieldset>
10801092
</div>

admin/iaff_image-attributes-from-filename-do.php

Lines changed: 87 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -138,31 +138,33 @@ function iaff_total_number_of_images() {
138138
}
139139

140140
/**
141-
* Count remaining number of images to process
141+
* Count remaining number of images to process.
142142
*
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.
146147
*/
147-
function iaff_count_remaining_images( $force_return = false ) {
148+
function iaff_count_remaining_images() {
148149

149150
$total_no_of_images = iaff_total_number_of_images();
150151

151152
$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 );
155154

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 );
164156
}
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' );
166168

167169

168170
/**
@@ -179,7 +181,7 @@ function iaff_reset_bulk_updater_counter() {
179181

180182
$response = array(
181183
'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(),
183185
);
184186
wp_send_json($response);
185187
}
@@ -201,6 +203,8 @@ function iaff_before_bulk_updater() {
201203
* @link https://imageattributespro.com/codex/iaff_before_bulk_updater/
202204
*/
203205
do_action('iaff_before_bulk_updater');
206+
207+
wp_die();
204208
}
205209
add_action( 'wp_ajax_iaff_before_bulk_updater', 'iaff_before_bulk_updater' );
206210

@@ -220,6 +224,8 @@ function iaff_after_bulk_updater() {
220224
* @link https://imageattributespro.com/codex/iaff_after_bulk_updater/
221225
*/
222226
do_action('iaff_after_bulk_updater');
227+
228+
wp_die();
223229
}
224230
add_action( 'wp_ajax_iaff_after_bulk_updater', 'iaff_after_bulk_updater');
225231

@@ -247,7 +253,7 @@ function iaff_bulk_updater_skip_image() {
247253
if ( $image === NULL ) {
248254
$response = array(
249255
'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(),
251257
);
252258
wp_send_json( $response );
253259
}
@@ -261,7 +267,7 @@ function iaff_bulk_updater_skip_image() {
261267

262268
$response = array(
263269
'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(),
265271
);
266272
wp_send_json( $response );
267273
}
@@ -284,6 +290,14 @@ function iaff_image_bulk_updater() {
284290
jQuery(document).ready(function($) {
285291

286292
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+
}?>
287301

288302
// Bulk Updater
289303
function iaff_do_bulk_updater(iaff_test=false) {
@@ -297,12 +311,15 @@ function iaff_do_bulk_updater(iaff_test=false) {
297311
focused = false;
298312
};
299313
$('.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
303315

304316
// 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+
306323
$("#bulk-updater-log").animate({scrollTop:$("#bulk-updater-log")[0].scrollHeight - $("#bulk-updater-log").height()},200);
307324

308325
// Count Remaining Images To Process
@@ -317,6 +334,17 @@ function iaff_do_bulk_updater(iaff_test=false) {
317334

318335
// set remaining count as 1 when running in test mode
319336
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+
}
320348

321349
if((iaff_test===true)&&(remaining_images>1)) {
322350
remaining_images = 1;
@@ -369,9 +397,7 @@ function iaff_do_bulk_updater(iaff_test=false) {
369397
}
370398

371399
$('.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
375401
}
376402
});
377403
});
@@ -422,6 +448,18 @@ function iaff_do_bulk_updater(iaff_test=false) {
422448
// Stop Bulk Updater
423449
$('.iaff_stop_bulk_updater_button').click(function() {
424450
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+
}
425463
});
426464

427465
// Reset Bulk Updater Counter
@@ -468,6 +506,28 @@ function iaff_do_bulk_updater(iaff_test=false) {
468506
$("#bulk-updater-log").animate({scrollTop:$("#bulk-updater-log")[0].scrollHeight - $("#bulk-updater-log").height()},200);
469507
});
470508
});
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+
}
471531

472532
});
473533
</script> <?php

iaff_image-attributes-from-filename.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Description: Automatically Add Image Title, Image Caption, Description And Alt Text From Image Filename. Since this plugin includes a bulk updater this can update both existing images in the Media Library and new images.
66
* Author: Arun Basil Lal
77
* Author URI: https://imageattributespro.com/?utm_source=plugin-header&utm_medium=author-uri
8-
* Version: 3.3
8+
* Version: 4.0
99
* Text Domain: auto-image-attributes-from-filename-with-bulk-updater
1010
* Domain Path: /languages
1111
* License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
@@ -21,14 +21,12 @@
2121
* ~ Directory Structure ~
2222
*
2323
* /admin/ - Plugin backend stuff.
24-
* /includes/ - External third party classes and libraries.
25-
* /languages/ - Translation files go here.
26-
* /public/ - Front end files go here.
24+
* /languages/ - Translation files go here.
2725
* index.php - Dummy file.
2826
* license.txt - GPL v2
2927
* iaff_starter-plugin.php - File containing plugin name and other version info for WordPress.
3028
* readme.txt - Readme for WordPress plugin repository. https://wordpress.org/plugins/files/2017/03/readme.txt
31-
* uninstall.php - Fired when the plugin is uninstalled.
29+
* uninstall.php - Fired when the plugin is uninstalled.
3230
*/
3331

3432
// Exit if accessed directly
@@ -67,7 +65,7 @@
6765
* @since 1.3
6866
*/
6967
if ( ! defined( 'IAFF_VERSION_NUM' ) ) {
70-
define( 'IAFF_VERSION_NUM', '3.3' );
68+
define( 'IAFF_VERSION_NUM', '4.0' );
7169
}
7270

7371
/**

includes/index.php

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)