class-wc-meta-box-order-actions.php
6.64 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
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
<?php
/**
* Order Actions
*
* Functions for displaying the order actions meta box.
*
* @package WooCommerce\Admin\Meta Boxes
* @version 2.1.0
*/
use Automattic\WooCommerce\Internal\Admin\Orders\PageController;
use Automattic\WooCommerce\Utilities\OrderUtil;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* WC_Meta_Box_Order_Actions Class.
*/
class WC_Meta_Box_Order_Actions {
/**
* Output the metabox.
*
* @param WP_Post|WC_Order $post Post or order object.
*/
public static function output( $post ) {
global $theorder;
OrderUtil::init_theorder_object( $post );
$order = $theorder;
$order_id = $order->get_id();
$order_actions = self::get_available_order_actions_for_order( $order );
?>
<ul class="order_actions submitbox">
<?php
/**
* Fires at the start of order actions meta box rendering.
*
* @since 2.1.0
*/
do_action( 'woocommerce_order_actions_start', $order_id );
?>
<li class="wide" id="actions">
<select name="wc_order_action">
<option value=""><?php esc_html_e( 'Choose an action...', 'woocommerce' ); ?></option>
<?php foreach ( $order_actions as $action => $title ) { ?>
<option value="<?php echo esc_attr( $action ); ?>"><?php echo esc_html( $title ); ?></option>
<?php } ?>
</select>
<button class="button wc-reload"><span><?php esc_html_e( 'Apply', 'woocommerce' ); ?></span></button>
</li>
<li class="wide">
<div id="delete-action">
<?php
if ( current_user_can( 'delete_post', $order_id ) ) {
if ( ! EMPTY_TRASH_DAYS ) {
$delete_text = __( 'Delete permanently', 'woocommerce' );
} else {
$delete_text = __( 'Move to Trash', 'woocommerce' );
}
?>
<a class="submitdelete deletion" href="<?php echo esc_url( self::get_trash_or_delete_order_link( $order_id ) ); ?>"><?php echo esc_html( $delete_text ); ?></a>
<?php
}
?>
</div>
<button type="submit" class="button save_order button-primary" name="save" value="<?php echo 'auto-draft' === $order->get_status() ? esc_attr__( 'Create', 'woocommerce' ) : esc_attr__( 'Update', 'woocommerce' ); ?>"><?php echo 'auto-draft' === $order->get_status() ? esc_html__( 'Create', 'woocommerce' ) : esc_html__( 'Update', 'woocommerce' ); ?></button>
</li>
<?php
/**
* Fires at the end of order actions meta box rendering.
*
* @since 2.1.0
*/
do_action( 'woocommerce_order_actions_end', $order_id );
?>
</ul>
<?php
}
/**
* Forms a trash/delete order URL.
*
* @param int $order_id The order ID for which we want a trash/delete URL.
*
* @return string
*/
private static function get_trash_or_delete_order_link( int $order_id ): string {
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$order_type = wc_get_order( $order_id )->get_type();
$order_list_url = wc_get_container()->get( PageController::class )->get_base_page_url( $order_type );
$trash_order_url = add_query_arg(
array(
'action' => 'trash',
'order' => array( $order_id ),
'_wp_http_referer' => $order_list_url,
),
$order_list_url
);
return wp_nonce_url( $trash_order_url, 'bulk-orders' );
}
return get_delete_post_link( $order_id );
}
/**
* Save meta box data.
*
* @param int $post_id Post ID.
* @param WP_Post $post Post Object.
*/
public static function save( $post_id, $post ) {
// Order data saved, now get it so we can manipulate status.
$order = wc_get_order( $post_id );
// Handle button actions.
if ( ! empty( $_POST['wc_order_action'] ) ) { // @codingStandardsIgnoreLine
$action = wc_clean( wp_unslash( $_POST['wc_order_action'] ) ); // @codingStandardsIgnoreLine
if ( 'send_order_details' === $action ) {
do_action( 'woocommerce_before_resend_order_emails', $order, 'customer_invoice' );
// Send the customer invoice email.
WC()->payment_gateways();
WC()->shipping();
WC()->mailer()->customer_invoice( $order );
// Note the event.
$order->add_order_note( __( 'Order details manually sent to customer.', 'woocommerce' ), false, true );
do_action( 'woocommerce_after_resend_order_email', $order, 'customer_invoice' );
// Change the post saved message.
add_filter( 'redirect_post_location', array( __CLASS__, 'set_email_sent_message' ) );
} elseif ( 'send_order_details_admin' === $action ) {
do_action( 'woocommerce_before_resend_order_emails', $order, 'new_order' );
WC()->payment_gateways();
WC()->shipping();
add_filter( 'woocommerce_new_order_email_allows_resend', '__return_true' );
WC()->mailer()->emails['WC_Email_New_Order']->trigger( $order->get_id(), $order, true );
remove_filter( 'woocommerce_new_order_email_allows_resend', '__return_true' );
do_action( 'woocommerce_after_resend_order_email', $order, 'new_order' );
// Change the post saved message.
add_filter( 'redirect_post_location', array( __CLASS__, 'set_email_sent_message' ) );
} elseif ( 'regenerate_download_permissions' === $action ) {
$data_store = WC_Data_Store::load( 'customer-download' );
$data_store->delete_by_order_id( $post_id );
wc_downloadable_product_permissions( $post_id, true );
} else {
if ( ! did_action( 'woocommerce_order_action_' . sanitize_title( $action ) ) ) {
do_action( 'woocommerce_order_action_' . sanitize_title( $action ), $order );
}
}
}
}
/**
* Set the correct message ID.
*
* @param string $location Location.
* @since 2.3.0
* @static
* @return string
*/
public static function set_email_sent_message( $location ) {
return add_query_arg( 'message', 11, $location );
}
/**
* Get the available order actions for a given order.
*
* @since 5.8.0
*
* @param WC_Order|null $order The order object or null if no order is available.
*
* @return array
*/
private static function get_available_order_actions_for_order( $order ) {
$actions = array(
'send_order_details' => __( 'Email invoice / order details to customer', 'woocommerce' ),
'send_order_details_admin' => __( 'Resend new order notification', 'woocommerce' ),
'regenerate_download_permissions' => __( 'Regenerate download permissions', 'woocommerce' ),
);
/**
* Filter: woocommerce_order_actions
* Allows filtering of the available order actions for an order.
*
* @since 2.1.0 Filter was added.
* @since 5.8.0 The $order param was added.
*
* @param array $actions The available order actions for the order.
* @param WC_Order|null $order The order object or null if no order is available.
*/
return apply_filters( 'woocommerce_order_actions', $actions, $order );
}
}