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
OrderAddNote.php
145 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Order add note 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 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Registers the WooCommerce order add note ability. |
| 17 | */ |
| 18 | class OrderAddNote extends AbstractDomainAbility implements AbilityDefinition { |
| 19 | |
| 20 | use OrderAbilityTrait; |
| 21 | |
| 22 | /** |
| 23 | * Get the ability name. |
| 24 | * |
| 25 | * @return string |
| 26 | * |
| 27 | * @since 10.9.0 |
| 28 | */ |
| 29 | public static function get_name(): string { |
| 30 | return 'woocommerce/order-add-note'; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get the ability registration arguments. |
| 35 | * |
| 36 | * @return array |
| 37 | * |
| 38 | * @since 10.9.0 |
| 39 | */ |
| 40 | public static function get_registration_args(): array { |
| 41 | return array( |
| 42 | 'label' => __( 'Add order note', 'woocommerce' ), |
| 43 | 'description' => __( |
| 44 | 'Add a note to an order.', |
| 45 | 'woocommerce' |
| 46 | ), |
| 47 | 'category' => 'woocommerce', |
| 48 | 'input_schema' => self::get_input_schema(), |
| 49 | 'output_schema' => self::get_order_note_output_schema(), |
| 50 | 'execute_callback' => array( __CLASS__, 'execute' ), |
| 51 | 'permission_callback' => array( __CLASS__, 'can_edit_order' ), |
| 52 | 'meta' => array( |
| 53 | 'show_in_rest' => true, |
| 54 | 'mcp' => array( |
| 55 | 'public' => true, |
| 56 | 'type' => 'tool', |
| 57 | ), |
| 58 | 'annotations' => array( |
| 59 | 'readonly' => false, |
| 60 | 'idempotent' => false, |
| 61 | 'destructive' => false, |
| 62 | ), |
| 63 | ), |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Add an order note. |
| 69 | * |
| 70 | * @param array $input Ability input. |
| 71 | * @return array|\WP_Error |
| 72 | * |
| 73 | * @since 10.9.0 |
| 74 | */ |
| 75 | public static function execute( array $input ) { |
| 76 | $order = self::get_order_from_input( $input ); |
| 77 | |
| 78 | if ( is_wp_error( $order ) ) { |
| 79 | return $order; |
| 80 | } |
| 81 | |
| 82 | $note = isset( $input['note'] ) ? trim( wp_kses_post( (string) $input['note'] ) ) : ''; |
| 83 | |
| 84 | if ( '' === $note ) { |
| 85 | return new \WP_Error( |
| 86 | 'woocommerce_order_note_required', |
| 87 | __( 'Order note is required.', 'woocommerce' ), |
| 88 | array( 'status' => 400 ) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | $note_id = $order->add_order_note( |
| 93 | $note, |
| 94 | ( (bool) ( $input['customer_note'] ?? false ) ) ? 1 : 0, |
| 95 | get_current_user_id() > 0 |
| 96 | ); |
| 97 | |
| 98 | if ( $note_id <= 0 ) { |
| 99 | return new \WP_Error( |
| 100 | 'woocommerce_order_note_create_failed', |
| 101 | __( 'Failed to add order note.', 'woocommerce' ), |
| 102 | array( 'status' => 500 ) |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | return array( |
| 107 | 'note_id' => (int) $note_id, |
| 108 | 'order' => self::format_order_for_response( $order, false ), |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the ability input schema. |
| 114 | * |
| 115 | * @return array |
| 116 | */ |
| 117 | private static function get_input_schema(): array { |
| 118 | return array( |
| 119 | 'type' => 'object', |
| 120 | 'properties' => array( |
| 121 | 'id' => array( |
| 122 | 'type' => 'integer', |
| 123 | 'minimum' => 1, |
| 124 | ), |
| 125 | 'note' => array( |
| 126 | 'type' => 'string', |
| 127 | 'description' => __( 'Order note content. Safe HTML is allowed.', 'woocommerce' ), |
| 128 | 'minLength' => 1, |
| 129 | 'pattern' => '\S', |
| 130 | ), |
| 131 | 'customer_note' => array( |
| 132 | 'type' => 'boolean', |
| 133 | 'description' => __( |
| 134 | 'Whether the note is visible to the customer. Defaults to false for a private/admin note.', |
| 135 | 'woocommerce' |
| 136 | ), |
| 137 | 'default' => false, |
| 138 | ), |
| 139 | ), |
| 140 | 'required' => array( 'id', 'note' ), |
| 141 | 'additionalProperties' => false, |
| 142 | ); |
| 143 | } |
| 144 | } |
| 145 |