Traits
4 weeks ago
AbstractDomainAbility.php
4 weeks ago
OrderAddNote.php
4 weeks ago
OrderUpdateStatus.php
4 weeks ago
OrdersQuery.php
4 weeks ago
ProductCreate.php
4 weeks ago
ProductDelete.php
4 weeks ago
ProductUpdate.php
4 weeks ago
ProductsQuery.php
4 weeks ago
OrderUpdateStatus.php
159 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Order update status ability definition file. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Abilities\Domain; |
| 9 | |
| 10 | use Automattic\WooCommerce\Abilities\AbilityDefinition; |
| 11 | use Automattic\WooCommerce\Internal\Abilities\Domain\Traits\OrderAbilityTrait; |
| 12 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Registers the WooCommerce order update status ability. |
| 18 | */ |
| 19 | class OrderUpdateStatus extends AbstractDomainAbility implements AbilityDefinition { |
| 20 | |
| 21 | use OrderAbilityTrait; |
| 22 | |
| 23 | /** |
| 24 | * Get the ability name. |
| 25 | * |
| 26 | * @return string |
| 27 | * |
| 28 | * @since 10.9.0 |
| 29 | */ |
| 30 | public static function get_name(): string { |
| 31 | return 'woocommerce/order-update-status'; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get the ability registration arguments. |
| 36 | * |
| 37 | * @return array |
| 38 | * |
| 39 | * @since 10.9.0 |
| 40 | */ |
| 41 | public static function get_registration_args(): array { |
| 42 | return array( |
| 43 | 'label' => __( 'Update order status', 'woocommerce' ), |
| 44 | 'description' => __( |
| 45 | 'Update an order status.', |
| 46 | 'woocommerce' |
| 47 | ), |
| 48 | 'category' => 'woocommerce', |
| 49 | 'input_schema' => self::get_input_schema(), |
| 50 | 'output_schema' => self::get_entity_output_schema( 'order', self::get_order_output_schema() ), |
| 51 | 'execute_callback' => array( __CLASS__, 'execute' ), |
| 52 | 'permission_callback' => array( __CLASS__, 'can_edit_order' ), |
| 53 | 'meta' => array( |
| 54 | 'show_in_rest' => true, |
| 55 | 'mcp' => array( |
| 56 | 'public' => true, |
| 57 | 'type' => 'tool', |
| 58 | ), |
| 59 | 'annotations' => array( |
| 60 | 'readonly' => false, |
| 61 | 'idempotent' => false, |
| 62 | 'destructive' => true, |
| 63 | ), |
| 64 | ), |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Update an order status. |
| 70 | * |
| 71 | * @param array $input Ability input. |
| 72 | * @return array|\WP_Error |
| 73 | * |
| 74 | * @since 10.9.0 |
| 75 | */ |
| 76 | public static function execute( array $input ) { |
| 77 | $order = self::get_order_from_input( $input ); |
| 78 | |
| 79 | if ( is_wp_error( $order ) ) { |
| 80 | return $order; |
| 81 | } |
| 82 | |
| 83 | if ( empty( $input['status'] ) ) { |
| 84 | return new \WP_Error( |
| 85 | 'woocommerce_order_status_required', |
| 86 | __( 'Order status is required.', 'woocommerce' ), |
| 87 | array( 'status' => 400 ) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | $status = OrderUtil::remove_status_prefix( sanitize_key( $input['status'] ) ); |
| 92 | |
| 93 | if ( ! in_array( $status, self::get_allowed_order_status_slugs(), true ) ) { |
| 94 | return new \WP_Error( |
| 95 | 'woocommerce_order_status_invalid', |
| 96 | __( 'Order status is invalid.', 'woocommerce' ), |
| 97 | array( 'status' => 400 ) |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | if ( $status === $order->get_status() ) { |
| 102 | return new \WP_Error( |
| 103 | 'woocommerce_order_status_unchanged', |
| 104 | __( |
| 105 | 'Order already has this status. Use the woocommerce/order-add-note ability to add a note without changing status.', |
| 106 | 'woocommerce' |
| 107 | ), |
| 108 | array( 'status' => 400 ) |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | $updated = $order->update_status( |
| 113 | $status, |
| 114 | isset( $input['note'] ) ? wp_kses_post( $input['note'] ) : '', |
| 115 | true |
| 116 | ); |
| 117 | |
| 118 | if ( ! $updated ) { |
| 119 | return new \WP_Error( |
| 120 | 'woocommerce_order_status_update_failed', |
| 121 | __( 'Failed to update order status.', 'woocommerce' ), |
| 122 | array( 'status' => 500 ) |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | return array( |
| 127 | 'order' => self::format_order_for_response( $order, false ), |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the ability input schema. |
| 133 | * |
| 134 | * @return array |
| 135 | */ |
| 136 | private static function get_input_schema(): array { |
| 137 | return array( |
| 138 | 'type' => 'object', |
| 139 | 'properties' => array( |
| 140 | 'id' => array( |
| 141 | 'type' => 'integer', |
| 142 | 'minimum' => 1, |
| 143 | ), |
| 144 | 'status' => array( |
| 145 | 'type' => 'string', |
| 146 | 'description' => __( 'Order status slug without the wc- prefix.', 'woocommerce' ), |
| 147 | 'enum' => self::get_allowed_order_status_slugs(), |
| 148 | ), |
| 149 | 'note' => array( |
| 150 | 'type' => 'string', |
| 151 | 'description' => __( 'Optional status change note. Safe HTML is allowed. Use the woocommerce/order-add-note ability for notes without a status change.', 'woocommerce' ), |
| 152 | ), |
| 153 | ), |
| 154 | 'required' => array( 'id', 'status' ), |
| 155 | 'additionalProperties' => false, |
| 156 | ); |
| 157 | } |
| 158 | } |
| 159 |