-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoo-dap.php
158 lines (128 loc) · 3.75 KB
/
woo-dap.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
<?php
/*
Plugin Name: Woocommerce - Delete All Products
Description: Delete all products from Woocommerce
Author: Stefan Olaru
Author URI: http://stefanolaru.com
Version: 1.0
*/
// prevent direct access
if(!defined('ABSPATH')) {
exit;
}
class WooDAP {
public static $version = '1.0';
function __construct() {
// install/uninstall hooks
register_activation_hook( __FILE__, ['WooDAP', 'install']);
register_deactivation_hook( __FILE__, ['WooDAP', 'deactivate']);
register_uninstall_hook( __FILE__, ['WooDAP', 'uninstall']);
// add menu item
add_action('admin_menu', [$this, 'add_delete_all_menu_item']);
// start delete
add_action('admin_post_woodap', [$this, 'delete']);
}
private function _debug($s, $die = false) {
echo '<pre>'.print_r($s, true).'</pre>';
if($die) die();
}
public function add_delete_all_menu_item() {
add_submenu_page( 'edit.php?post_type=product', __('Delete All Products'), __('Delete All Products'), 'edit_products', 'delete_all_products', [$this, 'delete_products']);
}
public function delete_products() {
// count products
$count = wp_count_posts('product');
?>
<div class="wrap">
<?php if($count->publish > 0): ?>
<h1>Delete all products</h1>
<p>You currently have <strong><?php echo $count->publish; ?> published products</strong> in Woocommerce. Please type "DELETE" in the field below.</p>
<form method="post" id="woodap-form" data-url="<?php echo admin_url( 'admin-post.php' ); ?>">
<input type="text" name="what-do-you-want" placeholder="DELETE" autocomplete="off" required>
<?php submit_button('Start Delete'); ?>
</form>
<div id="woodap-output" style="display: none;">
<h2>Delete in Progress: <strong><span>0/<?php echo $count->publish; ?></span></strong></h2>
<ul></ul>
</div>
<?php else: ?>
<h1>Nothing to delete.</h1>
<?php endif; ?>
</div>
<script type="text/javascript">
var total_products = <?php echo $count->publish; ?>;
var deleted = [];
jQuery(function($) {
// submit form
$('#woodap-form').submit(function(e) {
if($('[name="what-do-you-want"]', this).val() == 'DELETE') {
if(confirm('Are you sure? There\'s no way back!')) {
// hide form
$(this).hide();
$('#woodap-output').show();
woodap();
}
} else {
alert('Please type "DELETE" to start deleting.');
}
e.preventDefault();
});
});
function woodap() {
// trigger action
jQuery.ajax({
url: jQuery('#woodap-form').data('url')+'?action=woodap',
type: 'POST',
dataType: 'json',
success: function(r) {
if(r.length) {
for(var i=0; i<r.length; i++) {
deleted.push(r[i]);
jQuery('#woodap-output ul').prepend('<li><strong>[DELETED]</strong> [ID: '+r[i].id+'] - '+r[i].name+'</li>');
}
jQuery('#woodap-output h2 span').text(deleted.length+'/'+total_products);
// continue polling
woodap();
} else {
jQuery('#woodap-output h2').text('Delete Completed!');
}
}
});
}
</script>
<?php
}
public function delete() {
// find products
$products = get_posts([
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 25
]);
// delete!
$deleted = [];
if(!empty($products)) {
foreach($products as $v) {
$deleted[] = [
'id' => $v->ID,
'name' => $v->post_title
];
wp_delete_post($v->ID, true);
}
}
header('Content-Type: application/json');
echo json_encode($deleted);
die();
}
public static function install() {
// add version option
add_option( 'woodap_version', self::$version );
}
public static function deactivate() {
// nothing here yet
}
public static function uninstall() {
delete_option( 'woodap_version' );
}
}
new WooDAP();