$enum ); } /** * Determine which email templates are available for the given order. * * @param WC_Order $order The order in question. * * @return WC_Email[] */ private function get_available_email_templates( WC_Order $order ): array { $all_email_templates = WC()->mailer()->emails; $order_status = $order->get_status( 'edit' ); $unavailable_statuses = array( OrderStatus::AUTO_DRAFT, OrderStatus::DRAFT, OrderStatus::NEW, OrderStatus::TRASH, ); if ( ! $order->get_billing_email() || in_array( $order_status, $unavailable_statuses, true ) ) { return array(); } $valid_template_classes = array( 'WC_Email_Customer_Invoice', ); if ( $this->order_is_partially_refunded( $order ) ) { $valid_template_classes[] = 'WC_Email_Customer_Refunded_Order'; } switch ( $order_status ) { case OrderStatus::COMPLETED: $valid_template_classes[] = 'WC_Email_Customer_Completed_Order'; break; case OrderStatus::FAILED: $valid_template_classes[] = 'WC_Email_Customer_Failed_Order'; break; case OrderStatus::ON_HOLD: $valid_template_classes[] = 'WC_Email_Customer_On_Hold_Order'; break; case OrderStatus::PROCESSING: $valid_template_classes[] = 'WC_Email_Customer_Processing_Order'; break; case OrderStatus::REFUNDED: $valid_template_classes[] = 'WC_Email_Customer_Refunded_Order'; break; } /** * Filter the list of valid email templates for a given order. * * Note that the email class must also exist in WC_Emails::$emails. * * When adding a custom email template to this list, a callback must also be added to trigger the sending * of the email. See the `woocommerce_rest_order_actions_email_send` action hook. * * @since 9.8.0 * * @param string[] $valid_template_classes Array of email template class names that are valid for a given order. * @param WC_Order $order The order. */ $valid_template_classes = apply_filters( 'woocommerce_rest_order_actions_email_valid_template_classes', $valid_template_classes, $order ); $valid_template_classes = array_filter( array_unique( $valid_template_classes ), 'is_string' ); $valid_templates = array_fill_keys( $valid_template_classes, '' ); return array_intersect_key( $all_email_templates, $valid_templates ); } /** * Retrieve an email template class using its ID, if it is available. * * @param string $template_id The ID of the desired email template class. * @param array|null $available_templates Optional. An array of available email template classes in the same * associative format as WC_Emails::$emails. If not provided, all classes * in WC_Emails::$emails will be considered available. * * @return WC_Email|null The email template class if it is available, otherwise null. */ private function get_email_template_by_id( string $template_id, ?array $available_templates = null ): ?WC_Email { if ( is_null( $available_templates ) ) { $available_templates = WC()->mailer()->emails; } $matching_templates = array_filter( $available_templates, fn( $template ) => $template->id === $template_id ); if ( empty( $matching_templates ) ) { return null; } return reset( $matching_templates ); } /** * Callback to run for GET wc/v3/orders/(?P[\d]+)/actions/email_templates. * * @param WP_REST_Request $request The incoming HTTP REST request. * * @return array */ protected function get_email_templates( WP_REST_Request $request ): array { $order = wc_get_order( $request->get_param( 'id' ) ); $available_templates = $this->get_available_email_templates( $order ); $templates = array(); foreach ( $available_templates as $template ) { $templates[] = array( 'id' => $template->id, 'title' => $template->get_title(), 'description' => $template->get_description(), ); } usort( $templates, fn( $a, $b ) => strcmp( $a['id'], $b['id'] ) ); $schema = $this->get_schema_for_email_templates(); $context = $request->get_param( 'context' ) ?? 'view'; $filtered_response = array_map( function ( $template ) use ( $schema, $context ) { return rest_filter_response_by_context( $template, $schema, $context ); }, $templates ); return $filtered_response; } /** * Callback to run for POST wc/v3/orders/(?P[\d]+)/actions/send_email. * * @param WP_REST_Request $request The incoming HTTP REST request. * * @return array|WP_Error */ protected function send_email( WP_REST_Request $request ) { $order = wc_get_order( $request->get_param( 'id' ) ); $email = $request->get_param( 'email' ); $force = wp_validate_boolean( $request->get_param( 'force_email_update' ) ); $template_id = $request->get_param( 'template_id' ); $messages = array(); if ( $email ) { $message = $this->maybe_update_billing_email( $order, $email, $force ); if ( is_wp_error( $message ) ) { return $message; } $messages[] = $message; } if ( ! is_email( $order->get_billing_email() ) ) { return new WP_Error( 'woocommerce_rest_missing_email', __( 'Order does not have an email address.', 'woocommerce' ), array( 'status' => 400 ) ); } $available_templates = $this->get_available_email_templates( $order ); $template = $this->get_email_template_by_id( $template_id, $available_templates ); if ( is_null( $template ) ) { return new WP_Error( 'woocommerce_rest_invalid_email_template', sprintf( // translators: %s is a string ID for an email template. __( '%s is not a valid template for this order.', 'woocommerce' ), esc_html( $template_id ) ), array( 'status' => 400 ) ); } switch ( $template_id ) { // phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment case 'customer_completed_order': /** This action is documented in includes/class-wc-emails.php */ do_action( 'woocommerce_order_status_completed_notification', $order->get_id(), $order ); break; case 'customer_failed_order': /** This action is documented in includes/class-wc-emails.php */ do_action( 'woocommerce_order_status_failed_notification', $order->get_id(), $order ); break; case 'customer_on_hold_order': /** This action is documented in includes/class-wc-emails.php */ do_action( 'woocommerce_order_status_pending_to_on-hold_notification', $order->get_id(), $order ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores break; case 'customer_processing_order': /** This action is documented in includes/class-wc-emails.php */ do_action( 'woocommerce_order_status_pending_to_processing_notification', $order->get_id(), $order ); break; case 'customer_refunded_order': if ( $this->order_is_partially_refunded( $order ) ) { /** This action is documented in includes/class-wc-emails.php */ do_action( 'woocommerce_order_partially_refunded_notification', $order->get_id() ); } else { /** This action is documented in includes/class-wc-emails.php */ do_action( 'woocommerce_order_fully_refunded_notification', $order->get_id() ); } break; // phpcs:enable WooCommerce.Commenting.CommentHooks.MissingSinceComment case 'customer_invoice': return $this->send_order_details( $request ); default: /** * Action to trigger sending a custom order email template from a REST API request. * * The email template must first be made available for the associated order. * See the `woocommerce_rest_order_actions_email_valid_template_classes` filter hook. * * @since 9.8.0 * * @param int $order_id The ID of the order. * @param string $template_id The ID of the template specified in the API request. */ do_action( 'woocommerce_rest_order_actions_email_send', $order->get_id(), $template_id ); break; } $user_agent = esc_html( $request->get_header( 'User-Agent' ) ); $messages[] = sprintf( // translators: 1. The name of an email template; 2. Email address; 3. User-agent that requested the action. esc_html__( 'Email template "%1$s" sent to %2$s, via %3$s.', 'woocommerce' ), esc_html( $template->get_title() ), esc_html( $order->get_billing_email() ), $user_agent ? $user_agent : 'REST API' ); $messages = array_filter( $messages ); foreach ( $messages as $message ) { $order->add_order_note( $message, false, true ); } return array( 'message' => implode( ' ', $messages ), ); } /** * Handle the POST /orders/{id}/actions/send_order_details. * * @param WP_REST_Request $request The received request. * @return array|WP_Error Request response or an error. */ protected function send_order_details( WP_REST_Request $request ) { $order = wc_get_order( $request->get_param( 'id' ) ); $email = $request->get_param( 'email' ); $force = wp_validate_boolean( $request->get_param( 'force_email_update' ) ); $messages = array(); if ( $email ) { $message = $this->maybe_update_billing_email( $order, $email, $force ); if ( is_wp_error( $message ) ) { return $message; } $messages[] = $message; } if ( ! is_email( $order->get_billing_email() ) ) { return new WP_Error( 'woocommerce_rest_missing_email', __( 'Order does not have an email address.', 'woocommerce' ), array( 'status' => 400 ) ); } // phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment /** This action is documented in includes/admin/meta-boxes/class-wc-meta-box-order-actions.php */ do_action( 'woocommerce_before_resend_order_emails', $order, 'customer_invoice' ); WC()->payment_gateways(); WC()->shipping(); WC()->mailer()->customer_invoice( $order ); $user_agent = esc_html( $request->get_header( 'User-Agent' ) ); $messages[] = sprintf( // translators: %1$s is the customer email, %2$s is the user agent that requested the action. esc_html__( 'Order details sent to %1$s, via %2$s.', 'woocommerce' ), esc_html( $order->get_billing_email() ), $user_agent ? $user_agent : 'REST API' ); $messages = array_filter( $messages ); foreach ( $messages as $message ) { $order->add_order_note( $message, false, true ); } // phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment /** This action is documented in includes/admin/meta-boxes/class-wc-meta-box-order-actions.php */ do_action( 'woocommerce_after_resend_order_email', $order, 'customer_invoice' ); return array( 'message' => implode( ' ', $messages ), ); } /** * Update the billing email of an order when certain conditions are met. * * If the order does not already have a billing email, it will be updated. If it does have one, but `$force` is set * to `true`, it will be updated. Otherwise this will return an error. This can also return an error if the given * email address is not valid. * * @param WC_Order $order The order to update. * @param string $email The email address to maybe add to the order. * @param bool $force Optional. True to update the order even if it already has a billing email. Default false. * * @return string|WP_Error A message upon success, otherwise an error. */ private function maybe_update_billing_email( WC_Order $order, string $email, ?bool $force = false ) { $existing_email = $order->get_billing_email( 'edit' ); if ( $existing_email === $email ) { return ''; } if ( $existing_email && true !== $force ) { return new WP_Error( 'woocommerce_rest_order_billing_email_exists', __( 'Order already has a billing email.', 'woocommerce' ), array( 'status' => 400 ) ); } try { $order->set_billing_email( $email ); $order->save(); } catch ( WC_Data_Exception $e ) { return new WP_Error( $e->getErrorCode(), $e->getMessage() ); } return sprintf( // translators: %s is an email address. __( 'Billing email updated to %s.', 'woocommerce' ), esc_html( $email ) ); } /** * Check if a given order has any partial refunds. * * Based on heuristics in the `wc_create_refund()` function. * * @param WC_Order $order An order object. * * @return bool */ private function order_is_partially_refunded( WC_Order $order ): bool { $remaining_amount = $order->get_remaining_refund_amount(); $remaining_items = $order->get_remaining_refund_items(); $refunds = $order->get_refunds(); $last_refund = reset( $refunds ); // phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment /** This filter is documented in includes/wc-order-functions.php */ $partially_refunded = apply_filters( 'woocommerce_order_is_partially_refunded', count( $refunds ) > 0 && ( $remaining_amount > 0 || ( $order->has_free_item() && $remaining_items > 0 ) ), $order->get_id(), $last_refund ? $last_refund->get_id() : 0 ); return (bool) $partially_refunded; } }