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
ProductCreate.php
137 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Product create 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\ProductAbilityTrait; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Registers the WooCommerce product create ability. |
| 17 | */ |
| 18 | class ProductCreate extends AbstractDomainAbility implements AbilityDefinition { |
| 19 | |
| 20 | use ProductAbilityTrait; |
| 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/product-create'; |
| 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' => __( 'Create product', 'woocommerce' ), |
| 43 | 'description' => __( |
| 44 | 'Create a product using supported catalog fields.', |
| 45 | 'woocommerce' |
| 46 | ), |
| 47 | 'category' => 'woocommerce', |
| 48 | 'input_schema' => self::get_input_schema(), |
| 49 | 'output_schema' => self::get_entity_output_schema( 'product', self::get_product_output_schema() ), |
| 50 | 'execute_callback' => array( __CLASS__, 'execute' ), |
| 51 | 'permission_callback' => array( __CLASS__, 'can_create_product' ), |
| 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 | * Create a product. |
| 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 | $product_config = self::get_product_config_for_alias( $input['product_type_alias'] ?? 'physical' ); |
| 77 | |
| 78 | if ( is_wp_error( $product_config ) ) { |
| 79 | return $product_config; |
| 80 | } |
| 81 | |
| 82 | $product = wc_get_product_object( $product_config['wc_type'] ); |
| 83 | |
| 84 | if ( ! $product ) { |
| 85 | return new \WP_Error( |
| 86 | 'woocommerce_invalid_product_type', |
| 87 | __( 'Invalid product type.', 'woocommerce' ), |
| 88 | array( 'status' => 400 ) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | try { |
| 93 | self::apply_product_type_config( $product, $product_config ); |
| 94 | |
| 95 | $validation_error = self::set_product_props_from_input( $product, $input, $product_config ); |
| 96 | if ( is_wp_error( $validation_error ) ) { |
| 97 | return $validation_error; |
| 98 | } |
| 99 | } catch ( \WC_Data_Exception $exception ) { |
| 100 | return self::get_product_data_exception_error( $exception ); |
| 101 | } |
| 102 | |
| 103 | $save_error = self::save_product( $product, 'woocommerce_product_create_failed' ); |
| 104 | if ( is_wp_error( $save_error ) ) { |
| 105 | return $save_error; |
| 106 | } |
| 107 | |
| 108 | return array( |
| 109 | 'product' => self::format_product_for_response( $product ), |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Check product creation access. |
| 115 | * |
| 116 | * @param mixed $input Ability input. |
| 117 | * @return bool |
| 118 | * |
| 119 | * @since 10.9.0 |
| 120 | */ |
| 121 | public static function can_create_product( $input = array() ): bool { |
| 122 | // Cap is not object-scoped for create. |
| 123 | unset( $input ); |
| 124 | |
| 125 | return wc_rest_check_post_permissions( 'product', 'create' ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get the ability input schema. |
| 130 | * |
| 131 | * @return array |
| 132 | */ |
| 133 | private static function get_input_schema(): array { |
| 134 | return self::get_product_create_input_schema(); |
| 135 | } |
| 136 | } |
| 137 |