better-search-replace.js
3.53 KB
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
(function( $ ) {
'use strict';
/**
* Initializes our event handlers.
*/
function bsr_init() {
bsr_search_replace();
bsr_update_sliders();
}
/**
* Recursive function for performing batch operations.
*/
function bsr_process_step( action, step, page, data ) {
$.ajax({
type: 'POST',
url: bsr_object_vars.endpoint + action,
data: {
bsr_ajax_nonce : bsr_object_vars.ajax_nonce,
action: action,
bsr_step: step,
bsr_page: page,
bsr_data: data
},
dataType: 'json',
success: function( response ) {
// Maybe display more details.
if ( typeof response.message != 'undefined' ) {
$('.bsr-description').remove();
$('.bsr-progress-wrap').append( '<p class="description bsr-description">' + response.message + '</p>' );
}
if ( 'done' == response.step ) {
bsr_update_progress_bar( '100%' );
// Maybe run another action.
if ( typeof response.next_action != 'undefined' ) {
bsr_update_progress_bar( '0%', 0 );
bsr_process_step( response.next_action, 0, 0, response.bsr_data );
} else {
$('.bsr-processing-wrap').remove();
$('.bsr-disabled').removeClass('bsr-disabled button-disabled' );
window.location = response.url;
}
} else {
bsr_update_progress_bar( response.percentage );
bsr_process_step( action, response.step, response.page, response.bsr_data );
}
}
}).fail(function (response) {
$('.bsr-processing-wrap').remove();
$('.bsr-disabled').removeClass('bsr-disabled button-disabled' );
$('#bsr-error-wrap').html( '<div class="error"><p>' + bsr_object_vars.unknown + '</p></div>' );
if ( window.console && window.console.log ) {
console.log(response);
}
});
}
/**
* Initializes a search/replace.
*/
function bsr_search_replace() {
var search_replace_submit = $( '#bsr-submit' );
var bsr_error_wrap = $( '#bsr-error-wrap' );
search_replace_submit.click( function( e ) {
e.preventDefault();
if ( ! search_replace_submit.hasClass( 'button-disabled' ) ) {
if ( ! $( '#search_for' ).val() ) {
bsr_error_wrap.html( '<div class="error"><p>' + bsr_object_vars.no_search + '</p></div>' );
} else if ( ! $( '#select_tables' ).val() ) {
bsr_error_wrap.html( '<div class="error"><p>' + bsr_object_vars.no_tables + '</p></div>' );
} else {
var str = $( '.bsr-action-form' ).serialize();
var data = str.replace(/%5C/g, "#BSR_BACKSLASH#" );
bsr_error_wrap.html('');
search_replace_submit.addClass( 'bsr-disabled button-disabled' );
$( '#bsr-submit-wrap' ).append('<div class="bsr-processing-wrap"><div class="spinner is-active bsr-spinner"></div><div class="bsr-progress-wrap"><div class="bsr-progress"></div></div></div>');
$('.bsr-progress-wrap').append( '<p class="description bsr-description">' + bsr_object_vars.processing + '</p>' );
bsr_process_step( 'process_search_replace', 0, 0, data );
}
}
});
}
/**
* Updates the progress bar for AJAX bulk actions.
*/
function bsr_update_progress_bar( percentage, speed ) {
if ( typeof speed == 'undefined' ) {
speed = 150;
}
$( '.bsr-progress' ).animate({
width: percentage
}, speed );
}
/**
* Updates the "Max Page Size" slider.
*/
function bsr_update_sliders( percentage ) {
$('#bsr-page-size-slider').slider({
value: bsr_object_vars.page_size,
range: "min",
min: 1000,
max: 50000,
step: 1000,
slide: function( event, ui ) {
$('#bsr-page-size-value').text( ui.value );
$('#bsr_page_size').val( ui.value );
}
});
}
bsr_init();
})( jQuery );