-
Notifications
You must be signed in to change notification settings - Fork 33
/
class-bulk-restore.php
164 lines (118 loc) · 3.2 KB
/
class-bulk-restore.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
<?php
/**
* GoDaddy Reseller Store bulk edit class.
*
* Bulk restore the product data from the edit-post screen.
*
* @class Reseller_Store/Bulk_Edit
* @package Reseller_Store/Plugin
* @category Class
* @author GoDaddy
* @since 1.3.0
*/
namespace Reseller_Store;
if ( ! defined( 'ABSPATH' ) ) {
// @codeCoverageIgnoreStart
exit;
// @codeCoverageIgnoreEnd
}
final class Bulk_Restore {
/**
* Class constructor.
*
* @since 1.3.0
*/
public function __construct() {
add_filter(
'bulk_actions-edit-' . Post_Type::SLUG, function ( $bulk_actions ) {
$bulk_actions['restore_product_data'] = esc_html__( 'Restore Product Data', 'reseller-store' );
return $bulk_actions;
}
);
add_filter( 'handle_bulk_actions-edit-' . Post_Type::SLUG, [ $this, 'bulk_action_handler' ], 10, 3 );
}
/**
* Action handler for bulk resetting product posts
*
* @since 1.3.0
*
* @param string $redirect_to The redirect URL.
* @param string $do_action The action being taken.
* @param array $post_ids The items to take the action on.
* @return mixed The url to redirect to after success.
*/
public function bulk_action_handler( $redirect_to, $do_action, $post_ids ) {
if ( 'restore_product_data' !== $do_action ) {
return $redirect_to;
}
$products = rstore_get_products( true );
if ( is_wp_error( $products ) ) {
rstore_error( $products );
return $redirect_to;
}
$success = 0;
foreach ( $post_ids as $post_id ) {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
$error = new \WP_Error(
'invalid_permissions',
esc_html__( 'Current user cannot modify post.', 'reseller-store' )
);
rstore_error( $error );
continue;
}
$product_id = rstore_get_product_meta( $post_id, 'id' );
if ( false === $product_id ) {
$error = new \WP_Error(
'invalid_product_id',
/* translators: product name */
esc_html__( '`%s` does not have a valid product id.', 'reseller-store' ),
get_the_title( $post_id )
);
rstore_error( $error );
continue;
}
$product = $this->find_product( $product_id, $products );
if ( false === $product ) {
$error = new \WP_Error(
'invalid_product',
/* translators: product name */
esc_html__( '`%s` is not a valid product.', 'reseller-store' ),
get_the_title( $post_id )
);
rstore_error( $error );
continue;
}
$import = new Import( $product, $post_id );
$result = $import->import_product();
if ( is_wp_error( $result ) ) {
rstore_error( $result );
continue;
}
$success += 1;
}
if ( $success > 0 ) {
$redirect_to = add_query_arg( 'bulk_restore_posts', $success, $redirect_to );
}
return $redirect_to;
}
/**
* Find the product from the array by id.
*
* @since 1.3.0
*
* @param string $id The product id (i.e. the needle).
* @param array $products Array of products (i.e the haystack).
* @return bool|Product
*/
private function find_product( $id, $products ) {
foreach ( (array) $products as $product ) {
if ( $id === $product->id ) {
$item = new Product( $product );
if ( $item->is_valid() ) {
return $item;
}
}
}
return false;
}
}