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
ProductUpdate.php
188 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Product update 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\Enums\ProductStatus; |
| 12 | use Automattic\WooCommerce\Internal\Abilities\Domain\Traits\ProductAbilityTrait; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Registers the WooCommerce product update ability. |
| 18 | */ |
| 19 | class ProductUpdate extends AbstractDomainAbility implements AbilityDefinition { |
| 20 | |
| 21 | use ProductAbilityTrait; |
| 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/product-update'; |
| 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 product', 'woocommerce' ), |
| 44 | 'description' => __( |
| 45 | 'Update an existing product using supported catalog fields.', |
| 46 | 'woocommerce' |
| 47 | ), |
| 48 | 'category' => 'woocommerce', |
| 49 | 'input_schema' => self::get_input_schema(), |
| 50 | 'output_schema' => self::get_entity_output_schema( 'product', self::get_product_output_schema() ), |
| 51 | 'execute_callback' => array( __CLASS__, 'execute' ), |
| 52 | 'permission_callback' => array( __CLASS__, 'can_update_product' ), |
| 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 a product. |
| 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 | $product = self::get_product_from_input( $input ); |
| 78 | |
| 79 | if ( is_wp_error( $product ) ) { |
| 80 | return $product; |
| 81 | } |
| 82 | |
| 83 | if ( empty( array_diff( array_keys( $input ), array( 'id' ) ) ) ) { |
| 84 | return new \WP_Error( |
| 85 | 'woocommerce_product_update_no_fields', |
| 86 | __( 'At least one product field is required to update a product.', 'woocommerce' ), |
| 87 | array( 'status' => 400 ) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | $product_config = self::get_product_config_for_product( $product ); |
| 92 | |
| 93 | if ( is_wp_error( $product_config ) ) { |
| 94 | return $product_config; |
| 95 | } |
| 96 | |
| 97 | if ( |
| 98 | isset( $input['status'] ) |
| 99 | && in_array( |
| 100 | sanitize_key( $input['status'] ), |
| 101 | array( ProductStatus::PUBLISH, ProductStatus::FUTURE, ProductStatus::PRIVATE ), |
| 102 | true |
| 103 | ) |
| 104 | && sanitize_key( $input['status'] ) !== $product->get_status() |
| 105 | && ! self::current_user_can_publish_products() |
| 106 | ) { |
| 107 | return new \WP_Error( |
| 108 | 'woocommerce_product_publish_forbidden', |
| 109 | __( 'You are not allowed to publish products.', 'woocommerce' ), |
| 110 | array( 'status' => rest_authorization_required_code() ) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | if ( isset( $input['product_type_alias'] ) ) { |
| 115 | $product_config = self::get_product_config_for_alias( $input['product_type_alias'] ); |
| 116 | |
| 117 | if ( is_wp_error( $product_config ) ) { |
| 118 | return $product_config; |
| 119 | } |
| 120 | |
| 121 | $product = wc_get_product_object( $product_config['wc_type'], $product->get_id() ); |
| 122 | |
| 123 | if ( ! $product ) { |
| 124 | return new \WP_Error( |
| 125 | 'woocommerce_invalid_product_type', |
| 126 | __( 'Invalid product type.', 'woocommerce' ), |
| 127 | array( 'status' => 400 ) |
| 128 | ); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | try { |
| 133 | if ( isset( $input['product_type_alias'] ) ) { |
| 134 | self::apply_product_type_config( $product, $product_config ); |
| 135 | } |
| 136 | |
| 137 | $validation_error = self::set_product_props_from_input( $product, $input, $product_config ); |
| 138 | if ( is_wp_error( $validation_error ) ) { |
| 139 | return $validation_error; |
| 140 | } |
| 141 | } catch ( \WC_Data_Exception $exception ) { |
| 142 | return self::get_product_data_exception_error( $exception ); |
| 143 | } |
| 144 | |
| 145 | $save_error = self::save_product( $product, 'woocommerce_product_update_failed' ); |
| 146 | if ( is_wp_error( $save_error ) ) { |
| 147 | return $save_error; |
| 148 | } |
| 149 | |
| 150 | return array( |
| 151 | 'product' => self::format_product_for_response( $product ), |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Check product update access. |
| 157 | * |
| 158 | * @param mixed $input Ability input. |
| 159 | * @return bool |
| 160 | * |
| 161 | * @since 10.9.0 |
| 162 | */ |
| 163 | public static function can_update_product( $input = array() ): bool { |
| 164 | $product_id = self::get_id_from_input( $input ); |
| 165 | |
| 166 | return $product_id > 0 && wc_rest_check_post_permissions( 'product', 'edit', $product_id ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Check whether the current user can publish products. |
| 171 | * |
| 172 | * @return bool |
| 173 | */ |
| 174 | private static function current_user_can_publish_products(): bool { |
| 175 | // phpcs:ignore WordPress.WP.Capabilities.Unknown -- WooCommerce registers the publish_products capability. |
| 176 | return current_user_can( 'publish_products' ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Get the ability input schema. |
| 181 | * |
| 182 | * @return array |
| 183 | */ |
| 184 | private static function get_input_schema(): array { |
| 185 | return self::get_product_update_input_schema(); |
| 186 | } |
| 187 | } |
| 188 |