-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-custom-woocommerce-subscriptions-status.php
299 lines (266 loc) · 12 KB
/
class-custom-woocommerce-subscriptions-status.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
class Custom_Woocommerce_Subscription_Status
{
/**
* Initialize Hooks.
*
* @access public
*/
public function run()
{
/**
* hook: apply_filters( 'wcs_subscription_statuses', $subscription_statuses )
* in file `woocommerce-subscriptions/woocommerce-subscriptions.php`
*/
add_filter('woocommerce_subscriptions_registered_statuses', array($this, 'register_new_post_status'), 100, 1);
/**
* hook: apply_filters( 'wcs_subscription_statuses', $subscription_statuses )
* in file `woocommerce-subscriptions/wcs-functions.php`
*/
add_filter('wcs_subscription_statuses', array($this, 'add_new_subscription_statuses'), 100, 1);
/**
* hook: apply_filters('woocommerce_can_subscription_be_updated_to', $can_be_updated, $new_status, $subscription)
* in file `woocommerce-subscriptions/includes/class-wc-subscription.php`
*/
add_filter('woocommerce_can_subscription_be_updated_to', array($this, 'extends_can_be_updated'), 100, 3);
/**
* Alternative hooks available for the above hook :
* apply_filters('woocommerce_can_subscription_be_updated_to_' . $new_status, $can_be_updated, $subscription );
*/
/**
* hook: do_action('woocommerce_subscription_status_updated', $subscription, $new_status, $old_status)
* in file `woocommerce-subscriptions/includes/class-wc-subscription.php`
*/
add_action('woocommerce_subscription_status_updated', array($this, 'extends_update_status'), 100, 3);
/**
* Alternative hooks available for the above hook :
* do_action('woocommerce_subscription_status_' . $new_status, $subscription);
* do_action('woocommerce_subscription_status_' . $old_status . '_to_' . $new_status, $subscription);
* do_action('woocommerce_subscription_status_updated', $subscription, $new_status, $old_status);
* do_action('woocommerce_subscription_status_changed', $subscription_id, $old_status, $new_status);
*/
/**
* hook: apply_filters('woocommerce_can_subscription_be_updated_to_' . $new_status, $can_be_updated, $subscription)
* in file `woocommerce-subscriptions/includes/class-wc-subscription.php`
*/
add_filter('woocommerce_can_subscription_be_updated_to_active', array($this, 'enable_active_in_new_statuses'), 100, 2);
add_filter('woocommerce_can_subscription_be_updated_to_on-hold', array($this, 'enable_on_hold_in_new_statuses'), 100, 2);
/**
* hook: apply_filters('woocommerce_subscription_bulk_actions', $bulk_actions)
* in file `woocommerce-subscriptions/includes/class-wc-subscription.php`
*/
add_filter('woocommerce_subscription_bulk_actions', array($this, 'add_new_status_bulk_actions'), 100, 1);
/**
* Following is a WordPress core hook. You will find it's woocommerce-subscription implementation
* in file `includes/admin/class-wcs-admin-post-types.php`
*/
add_action('load-edit.php', array($this, 'parse_bulk_actions'));
/**
* WordPress hook for adding styles and scripts to dashboard
*/
add_action('admin_enqueue_scripts', array($this, 'custom_subscription_status_style'));
}
/**
* Registered new status by adding `Like On Hold` to $registered_statuses array.
*
* @access public
*
* @param array $registered_statuses Registered Statuses array.
* @return array $registered_statuses with the new status added to it.
*/
public function register_new_post_status($registered_statuses)
{
$registered_statuses['wc-like-on-hold'] = _nx_noop('Like On Hold <span class="count">(%s)</span>', 'Like On Hold <span class="count">(%s)</span>', 'post status label including post count', 'custom-wcs-status-texts');
return $registered_statuses;
}
/**
* Add new status `Like On Hold` to $subscription_statuses array.
*
* @access public
*
* @param array $subscription_statuses current subscription statuses array.
* @return array $subscription_statuses with the new status added to it.
*/
public function add_new_subscription_statuses($subscription_statuses)
{
$subscription_statuses['wc-like-on-hold'] = _x('Like On Hold', 'Subscription status', 'custom-wcs-status-texts');
return $subscription_statuses;
}
/**
* Extends can_be_updated_to($status) functions of Woocommerce Subscription plugin.
*
* @access public
*
* @param boolean $can_be_updated default value if the current subscription can be updated to new status or not.
* @param string $new_status New status to which current subscription it is to be updated.
* @param object $subscription current subscription object.
* @return boolean $can_be_updated If the current subscription can be updated to new status or not.
*/
public function extends_can_be_updated($can_be_updated, $new_status, $subscription)
{
if ($new_status == 'like-on-hold') {
if ($subscription->payment_method_supports('subscription_suspension') && $subscription->has_status(array('active', 'pending', 'on-hold'))) {
$can_be_updated = true;
} else {
$can_be_updated = false;
}
}
return $can_be_updated;
}
/**
* Enable `Active` status in the status change dropdown of the subcription with this new status.
* This function replaces the default code with the new one.
* This code will also activate `reactivate` link in the list page for the subscription with `Like On Hold` status
*
* @access public
*
* @param boolean $can_be_updated default value if the current subscription can be updated to new status or not.
* @param object $subscription current subscription object.
* @return boolean $can_be_updated If the current Subscription can be updated to new status or not.
*/
public function enable_active_in_new_statuses($can_be_updated, $subscription)
{
if ($subscription->payment_method_supports('subscription_reactivation') && $subscription->has_status(array('on-hold', 'like-on-hold'))) {
$can_be_updated = true;
} elseif ($subscription->has_status('pending')) {
$can_be_updated = true;
} else {
$can_be_updated = false;
}
return $can_be_updated;
}
/**
* Enable `On Hold` status in the status change dropdown of the subcription with this new status.
* This function replaces the default code with the new one.
* This code will also activate `suspend` link in the list page for the subscription with `Like On Hold` status
*
* @access public
*
* @param boolean $can_be_updated default value if the current subscription can be updated to new status or not.
* @param object $subscription current subscription object.
* @return boolean $can_be_updated If the current subscription can be updated to new status or not.
*/
public function enable_on_hold_in_new_statuses($can_be_updated, $subscription)
{
if ($subscription->payment_method_supports('subscription_suspension') && $subscription->has_status(array('active', 'pending', 'like-on-hold'))) {
$can_be_updated = true;
} else {
$can_be_updated = false;
}
return $can_be_updated;
}
/**
* Actions to be performed while the status is updated should be handled here
* For this example, I am simply copying the `On Hold` actions as it is.
*
* @access public
*
* @param object $subscription current subscription object.
* @param string $new_status New status to which current subscription it is to be updated.
* @param string $old_status Current status of current subscription.
* @return boolean $can_be_updated If the current subscription can be updated to new status or not.
*/
public function extends_update_status($subscription, $new_status, $old_status)
{
if ($new_status == 'like-on-hold') {
$subscription->update_suspension_count($subscription->suspension_count + 1);
wcs_maybe_make_user_inactive($subscription->customer_user);
}
}
/**
* Add the new status on the bulk actions drop down of the link
*
* @access public
*
* @param array $bulk_actions current bulk action array.
* @return array $bulk_actions with the new status added to it.
*/
public function add_new_status_bulk_actions($bulk_actions)
{
$bulk_actions['like-on-hold'] = _x('Mark Like On Hold', 'an action on a subscription', 'custom-wcs-status-texts');
return $bulk_actions;
}
/**
* Deals with bulk actions. The style is similar to what WooCommerce is doing. Extensions will have to define their
* own logic by copying the concept behind this method.
*
* @access public
*
*/
public function parse_bulk_actions()
{
// We only want to deal with shop_subscriptions. In case any other CPTs have an 'active' action
if (!isset($_REQUEST['post_type']) || 'shop_subscription' !== $_REQUEST['post_type'] || !isset($_REQUEST['post'])) {
return;
}
$action = '';
if (isset($_REQUEST['action']) && -1 != $_REQUEST['action']) {
$action = $_REQUEST['action'];
} elseif (isset($_REQUEST['action2']) && -1 != $_REQUEST['action2']) {
$action = $_REQUEST['action2'];
}
switch ($action) {
case 'active':
case 'on-hold':
case 'cancelled':
case 'like-on-hold':
$new_status = $action;
break;
default:
return;
}
$report_action = 'marked_' . $new_status;
$changed = 0;
$subscription_ids = array_map('absint', (array) $_REQUEST['post']);
$sendback_args = array(
'post_type' => 'shop_subscription',
$report_action => true,
'ids' => join(',', $subscription_ids),
'error_count' => 0,
);
foreach ($subscription_ids as $subscription_id) {
$subscription = wcs_get_subscription($subscription_id);
$order_note = _x('Subscription status changed by bulk edit:', 'Used in order note. Reason why status changed.', 'woocommerce-subscriptions');
try {
if ('cancelled' == $action) {
$subscription->cancel_order($order_note);
} else {
$subscription->update_status($new_status, $order_note, true);
}
// Fire the action hooks
switch ($action) {
case 'active':
case 'on-hold':
case 'cancelled':
case 'like-on-hold':
case 'trash':
do_action('woocommerce_admin_changed_subscription_to_' . $action, $subscription_id);
break;
}
$changed++;
} catch (Exception $e) {
$sendback_args['error'] = urlencode($e->getMessage());
$sendback_args['error_count']++;
}
}
$sendback_args['changed'] = $changed;
$sendback = add_query_arg($sendback_args, wp_get_referer() ? wp_get_referer() : '');
wp_safe_redirect(esc_url_raw($sendback));
exit();
}
/**
* Add css to admin dashboard
*
* @access public
*
* @param string top level hook for current page
*
*/
public function custom_subscription_status_style($hook)
{
wp_register_style('subscription-status-style', plugin_dir_url(__FILE__) . 'assets/css/subscription-status.css');
if ('edit.php' == $hook && $_GET['post_type']=='shop_subscription') {
wp_enqueue_style('subscription-status-style');
}
}
}