class-wc-meta-box-order-actions.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Order Actions
  4. *
  5. * Functions for displaying the order actions meta box.
  6. *
  7. * @author WooThemes
  8. * @category Admin
  9. * @package WooCommerce/Admin/Meta Boxes
  10. * @version 2.1.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit; // Exit if accessed directly.
  14. }
  15. /**
  16. * WC_Meta_Box_Order_Actions Class.
  17. */
  18. class WC_Meta_Box_Order_Actions {
  19. /**
  20. * Output the metabox.
  21. *
  22. * @param WP_Post $post Post object.
  23. */
  24. public static function output( $post ) {
  25. global $theorder;
  26. // 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.
  27. if ( ! is_object( $theorder ) ) {
  28. $theorder = wc_get_order( $post->ID );
  29. }
  30. $order_actions = apply_filters(
  31. 'woocommerce_order_actions', array(
  32. 'send_order_details' => __( 'Email invoice / order details to customer', 'woocommerce' ),
  33. 'send_order_details_admin' => __( 'Resend new order notification', 'woocommerce' ),
  34. 'regenerate_download_permissions' => __( 'Regenerate download permissions', 'woocommerce' ),
  35. )
  36. );
  37. ?>
  38. <ul class="order_actions submitbox">
  39. <?php do_action( 'woocommerce_order_actions_start', $post->ID ); ?>
  40. <li class="wide" id="actions">
  41. <select name="wc_order_action">
  42. <option value=""><?php esc_html_e( 'Choose an action...', 'woocommerce' ); ?></option>
  43. <?php foreach ( $order_actions as $action => $title ) { ?>
  44. <option value="<?php echo esc_attr( $action ); ?>"><?php echo esc_html( $title ); ?></option>
  45. <?php } ?>
  46. </select>
  47. <button class="button wc-reload"><span><?php esc_html_e( 'Apply', 'woocommerce' ); ?></span></button>
  48. </li>
  49. <li class="wide">
  50. <div id="delete-action">
  51. <?php
  52. if ( current_user_can( 'delete_post', $post->ID ) ) {
  53. if ( ! EMPTY_TRASH_DAYS ) {
  54. $delete_text = __( 'Delete permanently', 'woocommerce' );
  55. } else {
  56. $delete_text = __( 'Move to trash', 'woocommerce' );
  57. }
  58. ?>
  59. <a class="submitdelete deletion" href="<?php echo esc_url( get_delete_post_link( $post->ID ) ); ?>"><?php echo esc_html( $delete_text ); ?></a>
  60. <?php
  61. }
  62. ?>
  63. </div>
  64. <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>
  65. </li>
  66. <?php do_action( 'woocommerce_order_actions_end', $post->ID ); ?>
  67. </ul>
  68. <?php
  69. }
  70. /**
  71. * Save meta box data.
  72. *
  73. * @param int $post_id Post ID.
  74. * @param WP_Post $post Post Object.
  75. */
  76. public static function save( $post_id, $post ) {
  77. // Order data saved, now get it so we can manipulate status.
  78. $order = wc_get_order( $post_id );
  79. // Handle button actions.
  80. if ( ! empty( $_POST['wc_order_action'] ) ) { // @codingStandardsIgnoreLine
  81. $action = wc_clean( wp_unslash( $_POST['wc_order_action'] ) ); // @codingStandardsIgnoreLine
  82. if ( 'send_order_details' === $action ) {
  83. do_action( 'woocommerce_before_resend_order_emails', $order, 'customer_invoice' );
  84. // Send the customer invoice email.
  85. WC()->payment_gateways();
  86. WC()->shipping();
  87. WC()->mailer()->customer_invoice( $order );
  88. // Note the event.
  89. $order->add_order_note( __( 'Order details manually sent to customer.', 'woocommerce' ), false, true );
  90. do_action( 'woocommerce_after_resend_order_email', $order, 'customer_invoice' );
  91. // Change the post saved message.
  92. add_filter( 'redirect_post_location', array( __CLASS__, 'set_email_sent_message' ) );
  93. } elseif ( 'send_order_details_admin' === $action ) {
  94. do_action( 'woocommerce_before_resend_order_emails', $order, 'new_order' );
  95. WC()->payment_gateways();
  96. WC()->shipping();
  97. WC()->mailer()->emails['WC_Email_New_Order']->trigger( $order->get_id(), $order );
  98. do_action( 'woocommerce_after_resend_order_email', $order, 'new_order' );
  99. // Change the post saved message.
  100. add_filter( 'redirect_post_location', array( __CLASS__, 'set_email_sent_message' ) );
  101. } elseif ( 'regenerate_download_permissions' === $action ) {
  102. $data_store = WC_Data_Store::load( 'customer-download' );
  103. $data_store->delete_by_order_id( $post_id );
  104. wc_downloadable_product_permissions( $post_id, true );
  105. } else {
  106. if ( ! did_action( 'woocommerce_order_action_' . sanitize_title( $action ) ) ) {
  107. do_action( 'woocommerce_order_action_' . sanitize_title( $action ), $order );
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * Set the correct message ID.
  114. *
  115. * @param string $location Location.
  116. * @since 2.3.0
  117. * @static
  118. * @return string
  119. */
  120. public static function set_email_sent_message( $location ) {
  121. return add_query_arg( 'message', 11, $location );
  122. }
  123. }