-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions-form.php
179 lines (167 loc) · 5.21 KB
/
options-form.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
168
169
170
171
172
173
174
175
176
177
178
179
<?php
wp_localize_script( 'my_ajax_script', 'myAjax', [ 'ajaxurl' => admin_url( 'admin-ajax.php' ) ] );
wp_enqueue_script( 'my_ajax_script' );
$post_types = get_post_types();
$post_types = array_filter( $post_types, function( $elm ) {
$exclude = [
'revision',
'nav_menu_item',
'custom_css',
'customize_changeset',
'oembed_cache',
'user_request',
];
return !in_array( $elm, $exclude );
} );
if ( count( $post_types ) ) {
$post_types = array_keys( $post_types );
}
?>
<h1>Cear Content</h1>
<style>
form fieldset {
border: 1px solid #aaa;
display: inline-block;
}
form fieldset legend {
margin-left: 10px;
font-weight: bold;
padding: 0 5px;
}
form fieldset > div {
margin: 5px;
}
form fieldset table td {
vertical-align: top;
}
form.jhl-cc > div {
padding-bottom:15px;
}
form.jhl-cc label {
display: inline-block;
width: 230px;
}
.columns {
display: flex;
}
.columns button {
white-space: nowrap;
margin-right: 5px;
}
.progress-wrapper {
background-color: #ccc;
border: 1px solid #999;
height: 20px;
width: 100%;
}
.progress-bar {
width: 0%;
background-color: #999;
height:20px;
}
</style>
<form action="" data-form="jhl-cc" class="jhl-cc" method="POST">
<div>
<fieldset>
<legend>Post types</legend>
<div>
<table>
<tr>
<td>
<label for="jhl_cc_post_type"><strong>Choose the post type to clear</strong></label>
</td>
<td>
<select name="post_type" id="jhl_cc_post_type">
<option value="">Select post type</option>
<?php foreach( $post_types as $post_type ) { ?>
<option value="<?php echo $post_type; ?>"><?php echo $post_type; ?></option>
<?php } ?>
</select>
<br>
<label for="remove_attachments">
<input type="checkbox" id="remove_attachments" name="remove_attachments"> Also remove attachments
</label>
</td>
</tr>
</table>
<hr>
<div class="columns">
<button disabled="disabled">Clear posts!</button>
<div class="progress-wrapper">
<div class="progress-bar" data-widget="progress-bar"></div>
</div>
</div>
</div>
</fieldset>
</div>
</form>
<script>
var post_ids = [];
var total_ids = 0;
jQuery( document ).ready( function() {
jQuery( '[data-form="jhl-cc"] select' ).on( 'change', function( e ) {
post_ids = [];
var post_type = jQuery( this ).val();
if ( post_type ) {
jQuery.ajax( {
type : "POST",
data : {
action: "get_post_ids",
post_type: post_type,
},
url : ajaxurl,
success: function( data ) {
console.log( data );
if ( data.success == true && data.data.length ) {
post_ids = data.data;
total_ids = data.data.length;
initProgressBar();
jQuery( '[data-form="jhl-cc"] button' ).attr( 'disabled', false );
} else {
jQuery( '[data-form="jhl-cc"] button' ).attr( 'disabled', true );
return;
}
}
} );
}
} );
jQuery( '[data-form="jhl-cc"] button' ).on( 'click', function( e ) {
e.preventDefault();
var post_id = post_ids.shift();
var remove_attachments = jQuery( '[data-form="jhl-cc"] input[name="remove_attachments"]' ).is( ':checked' );
console.log( remove_attachments );
processPost( post_id, remove_attachments );
})
} );
var initProgressBar = function() {
jQuery( '[data-widget="progress-bar"]' ).css( 'width', '0%' );
}
var processPost = function( post_id, remove_attachments ) {
if ( post_id == null ) {
return;
}
jQuery.ajax( {
type: "POST",
data: {
action: "clear_content",
post_id: post_id,
remove_attachments: remove_attachments
},
url: ajaxurl,
success: function( data ) {
console.log( data );
updateProgressBar();
if ( data.success == true ) {
processPost( post_ids.shift(), remove_attachments );
} else {
return;
}
}
} );
}
var updateProgressBar = function() {
var width = ( ( total_ids - post_ids.length ) / total_ids ) * 100;
console.log( width );
jQuery( '[data-widget="progress-bar"]' ).css( 'width', width + '%' );
}
</script>