OrderAbilityTrait.php
| 1 | <?php |
| 2 | /** |
| 3 | * Order ability trait file. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Abilities\Domain\Traits; |
| 9 | |
| 10 | use Automattic\WooCommerce\Enums\OrderStatus; |
| 11 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Shared order helpers for WooCommerce domain ability definitions. |
| 17 | */ |
| 18 | trait OrderAbilityTrait { |
| 19 | |
| 20 | /** |
| 21 | * Allowed order status slugs (without the `wc-` prefix). |
| 22 | * |
| 23 | * @return array<int, string> |
| 24 | */ |
| 25 | protected static function get_allowed_order_status_slugs(): array { |
| 26 | return array_values( |
| 27 | array_diff( |
| 28 | array_map( |
| 29 | array( OrderUtil::class, 'remove_status_prefix' ), |
| 30 | array_keys( wc_get_order_statuses() ) |
| 31 | ), |
| 32 | array( OrderStatus::CHECKOUT_DRAFT ) |
| 33 | ) |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Possible order status slugs (without the `wc-` prefix) for ability output. |
| 39 | * |
| 40 | * @return array<int, string> |
| 41 | */ |
| 42 | protected static function get_order_output_status_slugs(): array { |
| 43 | return array_values( |
| 44 | array_unique( |
| 45 | array_map( |
| 46 | array( OrderUtil::class, 'remove_status_prefix' ), |
| 47 | array_merge( OrderStatus::get_all(), array_keys( wc_get_order_statuses() ) ) |
| 48 | ) |
| 49 | ) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get an order note output schema. |
| 55 | * |
| 56 | * @return array |
| 57 | */ |
| 58 | protected static function get_order_note_output_schema(): array { |
| 59 | return array( |
| 60 | 'type' => 'object', |
| 61 | 'properties' => array( |
| 62 | 'note_id' => array( 'type' => 'integer' ), |
| 63 | 'order' => self::get_order_output_schema(), |
| 64 | ), |
| 65 | 'additionalProperties' => false, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the schema for a single order in a response. |
| 71 | * |
| 72 | * @return array |
| 73 | */ |
| 74 | protected static function get_order_output_schema(): array { |
| 75 | return array( |
| 76 | 'type' => 'object', |
| 77 | 'properties' => array( |
| 78 | 'id' => array( 'type' => 'integer' ), |
| 79 | 'status' => array( |
| 80 | 'type' => 'string', |
| 81 | 'enum' => self::get_order_output_status_slugs(), |
| 82 | ), |
| 83 | 'currency' => array( |
| 84 | 'type' => 'string', |
| 85 | 'enum' => array_keys( get_woocommerce_currencies() ), |
| 86 | ), |
| 87 | 'currency_symbol' => array( 'type' => 'string' ), |
| 88 | 'total' => array( 'type' => 'string' ), |
| 89 | 'customer_id' => array( 'type' => 'integer' ), |
| 90 | 'billing_email' => array( |
| 91 | 'type' => array( 'string', 'null' ), |
| 92 | 'format' => 'email', |
| 93 | ), |
| 94 | 'payment_method' => array( 'type' => 'string' ), |
| 95 | 'payment_method_title' => array( 'type' => 'string' ), |
| 96 | 'date_created' => array( |
| 97 | 'type' => array( 'string', 'null' ), |
| 98 | 'format' => 'date-time', |
| 99 | ), |
| 100 | 'date_created_gmt' => array( |
| 101 | 'type' => array( 'string', 'null' ), |
| 102 | 'format' => 'date-time', |
| 103 | ), |
| 104 | 'date_modified' => array( |
| 105 | 'type' => array( 'string', 'null' ), |
| 106 | 'format' => 'date-time', |
| 107 | ), |
| 108 | 'date_modified_gmt' => array( |
| 109 | 'type' => array( 'string', 'null' ), |
| 110 | 'format' => 'date-time', |
| 111 | ), |
| 112 | 'line_items' => array( |
| 113 | 'type' => 'array', |
| 114 | 'description' => __( 'Order line items. Only present when include_line_items is true.', 'woocommerce' ), |
| 115 | 'items' => array( |
| 116 | 'type' => 'object', |
| 117 | 'properties' => array( |
| 118 | 'id' => array( 'type' => 'integer' ), |
| 119 | 'name' => array( 'type' => 'string' ), |
| 120 | 'product_id' => array( 'type' => 'integer' ), |
| 121 | 'variation_id' => array( 'type' => 'integer' ), |
| 122 | 'quantity' => array( 'type' => 'integer' ), |
| 123 | 'subtotal' => array( 'type' => 'string' ), |
| 124 | 'total' => array( 'type' => 'string' ), |
| 125 | ), |
| 126 | 'additionalProperties' => false, |
| 127 | ), |
| 128 | ), |
| 129 | ), |
| 130 | 'additionalProperties' => false, |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get an order from ability input. |
| 136 | * |
| 137 | * @param array $input Ability input. |
| 138 | * @return \WC_Order|\WP_Error |
| 139 | */ |
| 140 | protected static function get_order_from_input( array $input ) { |
| 141 | if ( empty( $input['id'] ) ) { |
| 142 | return new \WP_Error( |
| 143 | 'woocommerce_order_id_required', |
| 144 | __( 'Order ID is required.', 'woocommerce' ), |
| 145 | array( 'status' => 400 ) |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | $order_id = (int) $input['id']; |
| 150 | |
| 151 | if ( $order_id < 1 ) { |
| 152 | return new \WP_Error( |
| 153 | 'woocommerce_order_id_required', |
| 154 | __( 'Order ID is required.', 'woocommerce' ), |
| 155 | array( 'status' => 400 ) |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | $order = wc_get_order( $order_id ); |
| 160 | |
| 161 | if ( ! $order instanceof \WC_Order ) { |
| 162 | return new \WP_Error( |
| 163 | 'woocommerce_order_not_found', |
| 164 | __( 'Order not found.', 'woocommerce' ), |
| 165 | array( 'status' => 404 ) |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | return $order; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Check order edit access for an ability input payload. |
| 174 | * |
| 175 | * @param mixed $input Ability input. |
| 176 | * @return bool |
| 177 | * |
| 178 | * @since 10.9.0 |
| 179 | */ |
| 180 | public static function can_edit_order( $input = array() ): bool { |
| 181 | $order_id = self::get_id_from_input( $input ); |
| 182 | |
| 183 | return $order_id > 0 && wc_rest_check_post_permissions( 'shop_order', 'edit', $order_id ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Format an order for ability output. |
| 188 | * |
| 189 | * @param \WC_Order $order Order object. |
| 190 | * @param bool $include_line_items Whether to include line items. |
| 191 | * @return array |
| 192 | */ |
| 193 | protected static function format_order_for_response( \WC_Order $order, bool $include_line_items ): array { |
| 194 | $billing_email = $order->get_billing_email(); |
| 195 | |
| 196 | $data = array( |
| 197 | 'id' => $order->get_id(), |
| 198 | 'status' => $order->get_status(), |
| 199 | 'currency' => $order->get_currency(), |
| 200 | 'currency_symbol' => html_entity_decode( |
| 201 | get_woocommerce_currency_symbol( $order->get_currency() ), |
| 202 | ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 |
| 203 | ), |
| 204 | 'total' => $order->get_total(), |
| 205 | 'customer_id' => $order->get_customer_id(), |
| 206 | 'billing_email' => '' === $billing_email ? null : $billing_email, |
| 207 | 'payment_method' => $order->get_payment_method(), |
| 208 | 'payment_method_title' => $order->get_payment_method_title(), |
| 209 | 'date_created' => wc_rest_prepare_date_response( $order->get_date_created(), false ), |
| 210 | 'date_created_gmt' => wc_rest_prepare_date_response( $order->get_date_created() ), |
| 211 | 'date_modified' => wc_rest_prepare_date_response( $order->get_date_modified(), false ), |
| 212 | 'date_modified_gmt' => wc_rest_prepare_date_response( $order->get_date_modified() ), |
| 213 | ); |
| 214 | |
| 215 | if ( $include_line_items ) { |
| 216 | $data['line_items'] = array(); |
| 217 | |
| 218 | foreach ( $order->get_items() as $item ) { |
| 219 | if ( ! $item instanceof \WC_Order_Item_Product ) { |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | $data['line_items'][] = array( |
| 224 | 'id' => $item->get_id(), |
| 225 | 'name' => $item->get_name(), |
| 226 | 'product_id' => $item->get_product_id(), |
| 227 | 'variation_id' => $item->get_variation_id(), |
| 228 | 'quantity' => $item->get_quantity(), |
| 229 | 'subtotal' => $item->get_subtotal(), |
| 230 | 'total' => $item->get_total(), |
| 231 | ); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return $data; |
| 236 | } |
| 237 | } |
| 238 |