2013-08-06 10:41:20 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* Order Actions
2013-08-06 10:41:20 +00:00
*
* Functions for displaying the order actions meta box .
*
2020-08-05 16:36:24 +00:00
* @ package WooCommerce\Admin\Meta Boxes
2013-08-06 10:41:20 +00:00
* @ version 2.1 . 0
*/
2014-09-20 20:05:06 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2017-11-06 13:20:37 +00:00
exit ; // Exit if accessed directly.
2014-09-20 20:05:06 +00:00
}
2013-08-06 10:41:20 +00:00
2014-09-20 20:05:06 +00:00
/**
2015-11-03 12:28:01 +00:00
* WC_Meta_Box_Order_Actions Class .
2014-09-20 20:05:06 +00:00
*/
2013-08-06 10:41:20 +00:00
class WC_Meta_Box_Order_Actions {
/**
2015-11-03 12:28:01 +00:00
* Output the metabox .
2016-01-04 21:31:36 +00:00
*
2017-11-06 13:20:37 +00:00
* @ param WP_Post $post Post object .
2013-08-06 10:41:20 +00:00
*/
public static function output ( $post ) {
2015-01-20 01:12:26 +00:00
global $theorder ;
2015-01-20 08:38:13 +00:00
// This is used by some callbacks attached to hooks such as woocommerce_order_actions which rely on the global to determine if actions should be displayed for certain orders.
2021-08-11 02:53:21 +00:00
// Avoid using this global with the `woocommerce_order_actions` filter, instead use the $order filter arg.
2015-01-20 01:12:26 +00:00
if ( ! is_object ( $theorder ) ) {
$theorder = wc_get_order ( $post -> ID );
}
2017-06-15 13:19:05 +00:00
2021-08-11 02:46:43 +00:00
$theorder = $theorder instanceof WC_Order ? $theorder : null ;
$order_actions = self :: get_available_order_actions_for_order ( $theorder );
2013-08-06 10:41:20 +00:00
?>
< ul class = " order_actions submitbox " >
< ? php do_action ( 'woocommerce_order_actions_start' , $post -> ID ); ?>
< li class = " wide " id = " actions " >
< select name = " wc_order_action " >
2017-11-06 13:20:37 +00:00
< option value = " " >< ? php esc_html_e ( 'Choose an action...' , 'woocommerce' ); ?> </option>
2017-06-15 13:19:05 +00:00
< ? php foreach ( $order_actions as $action => $title ) { ?>
2017-11-06 13:20:37 +00:00
< option value = " <?php echo esc_attr( $action ); ?> " >< ? php echo esc_html ( $title ); ?> </option>
2013-08-06 10:41:20 +00:00
< ? php } ?>
</ select >
2017-11-06 13:20:37 +00:00
< button class = " button wc-reload " >< span >< ? php esc_html_e ( 'Apply' , 'woocommerce' ); ?> </span></button>
2013-08-06 10:41:20 +00:00
</ li >
< li class = " wide " >
2017-11-06 13:20:37 +00:00
< div id = " delete-action " >
< ? php
2014-11-19 23:36:47 +00:00
if ( current_user_can ( 'delete_post' , $post -> ID ) ) {
2014-08-31 07:18:21 +00:00
2014-07-04 14:20:29 +00:00
if ( ! EMPTY_TRASH_DAYS ) {
2016-10-12 10:16:30 +00:00
$delete_text = __ ( 'Delete permanently' , 'woocommerce' );
2014-07-04 14:20:29 +00:00
} else {
2019-10-18 13:44:47 +00:00
$delete_text = __ ( 'Move to Trash' , 'woocommerce' );
2014-07-04 14:20:29 +00:00
}
2018-03-05 18:59:17 +00:00
?>
< a class = " submitdelete deletion " href = " <?php echo esc_url( get_delete_post_link( $post->ID ) ); ?> " >< ? php echo esc_html ( $delete_text ); ?> </a>
< ? php
2013-08-06 10:41:20 +00:00
}
2017-11-06 13:20:37 +00:00
?>
</ div >
2013-08-06 10:41:20 +00:00
2017-11-06 13:23:00 +00:00
< button type = " submit " class = " button save_order button-primary " name = " save " value = " <?php echo 'auto-draft' === $post->post_status ? esc_attr__( 'Create', 'woocommerce' ) : esc_attr__( 'Update', 'woocommerce' ); ?> " >< ? php echo 'auto-draft' === $post -> post_status ? esc_html__ ( 'Create' , 'woocommerce' ) : esc_html__ ( 'Update' , 'woocommerce' ); ?> </button>
2013-08-06 10:41:20 +00:00
</ li >
< ? php do_action ( 'woocommerce_order_actions_end' , $post -> ID ); ?>
</ ul >
< ? php
}
/**
2015-11-03 12:28:01 +00:00
* Save meta box data .
2016-01-04 21:31:36 +00:00
*
2017-11-06 13:20:37 +00:00
* @ param int $post_id Post ID .
* @ param WP_Post $post Post Object .
2013-08-06 10:41:20 +00:00
*/
public static function save ( $post_id , $post ) {
2017-10-13 13:54:12 +00:00
// Order data saved, now get it so we can manipulate status.
2014-08-15 12:29:21 +00:00
$order = wc_get_order ( $post_id );
2013-08-06 10:41:20 +00:00
2017-11-06 13:20:37 +00:00
// Handle button actions.
if ( ! empty ( $_POST [ 'wc_order_action' ] ) ) { // @codingStandardsIgnoreLine
2013-08-06 10:41:20 +00:00
2017-11-06 13:20:37 +00:00
$action = wc_clean ( wp_unslash ( $_POST [ 'wc_order_action' ] ) ); // @codingStandardsIgnoreLine
2013-08-06 10:41:20 +00:00
2017-06-15 14:14:10 +00:00
if ( 'send_order_details' === $action ) {
do_action ( 'woocommerce_before_resend_order_emails' , $order , 'customer_invoice' );
2013-08-06 10:41:20 +00:00
2017-06-15 14:14:10 +00:00
// Send the customer invoice email.
2014-01-28 12:36:20 +00:00
WC () -> payment_gateways ();
WC () -> shipping ();
2017-06-15 14:14:10 +00:00
WC () -> mailer () -> customer_invoice ( $order );
2014-01-28 12:36:20 +00:00
2017-06-15 14:14:10 +00:00
// Note the event.
$order -> add_order_note ( __ ( 'Order details manually sent to customer.' , 'woocommerce' ), false , true );
2013-08-06 10:41:20 +00:00
2017-06-15 14:14:10 +00:00
do_action ( 'woocommerce_after_resend_order_email' , $order , 'customer_invoice' );
2017-01-23 20:40:36 +00:00
// Change the post saved message.
2014-10-14 13:18:09 +00:00
add_filter ( 'redirect_post_location' , array ( __CLASS__ , 'set_email_sent_message' ) );
2017-10-13 13:54:12 +00:00
} elseif ( 'send_order_details_admin' === $action ) {
do_action ( 'woocommerce_before_resend_order_emails' , $order , 'new_order' );
WC () -> payment_gateways ();
WC () -> shipping ();
2021-01-19 20:51:31 +00:00
add_filter ( 'woocommerce_new_order_email_allows_resend' , '__return_true' );
2021-01-13 22:13:57 +00:00
WC () -> mailer () -> emails [ 'WC_Email_New_Order' ] -> trigger ( $order -> get_id (), $order , true );
2021-01-19 20:57:48 +00:00
remove_filter ( 'woocommerce_new_order_email_allows_resend' , '__return_true' );
2017-10-13 13:54:12 +00:00
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' ) );
2016-05-03 14:22:49 +00:00
} elseif ( 'regenerate_download_permissions' === $action ) {
2013-09-20 16:01:09 +00:00
2016-11-18 17:13:02 +00:00
$data_store = WC_Data_Store :: load ( 'customer-download' );
$data_store -> delete_by_order_id ( $post_id );
wc_downloadable_product_permissions ( $post_id , true );
2013-09-20 16:01:09 +00:00
2013-08-06 10:41:20 +00:00
} else {
2014-11-30 05:14:22 +00:00
if ( ! did_action ( 'woocommerce_order_action_' . sanitize_title ( $action ) ) ) {
do_action ( 'woocommerce_order_action_' . sanitize_title ( $action ), $order );
}
2013-08-06 10:41:20 +00:00
}
}
}
2014-10-14 13:18:09 +00:00
/**
2015-11-03 12:28:01 +00:00
* Set the correct message ID .
2014-10-14 13:18:09 +00:00
*
2017-11-06 13:20:37 +00:00
* @ param string $location Location .
2014-10-14 13:18:09 +00:00
* @ since 2.3 . 0
* @ static
* @ return string
*/
2014-11-19 23:36:47 +00:00
public static function set_email_sent_message ( $location ) {
2014-10-14 13:18:09 +00:00
return add_query_arg ( 'message' , 11 , $location );
}
2021-08-11 02:46:43 +00:00
/**
* Get the available order actions for a given order .
*
2021-08-27 00:53:24 +00:00
* @ since 5.8 . 0
2021-08-11 02:46:43 +00:00
*
* @ param WC_Order | null $order The order object or null if no order is available .
*
* @ return array
*/
2021-08-26 01:23:40 +00:00
private static function get_available_order_actions_for_order ( $order ) {
2021-08-11 02:46:43 +00:00
$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 .
*
2021-08-11 03:16:02 +00:00
* @ since 2.1 . 0 Filter was added .
2021-08-27 00:53:24 +00:00
* @ since 5.8 . 0 The $order param was added .
2021-08-11 02:46:43 +00:00
*
* @ 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 );
}
2014-07-07 20:11:40 +00:00
}