Traits
1 month ago
AbstractDomainAbility.php
1 month ago
OrderAddNote.php
1 month ago
OrderUpdateStatus.php
1 month ago
OrdersQuery.php
1 month ago
ProductCreate.php
1 month ago
ProductDelete.php
1 month ago
ProductUpdate.php
1 month ago
ProductsQuery.php
1 month ago
ProductDelete.php
173 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Product delete 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\ProductType; |
| 12 | use Automattic\WooCommerce\Internal\Abilities\Domain\Traits\ProductAbilityTrait; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Registers the WooCommerce product delete ability. |
| 18 | */ |
| 19 | class ProductDelete 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-delete'; |
| 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' => __( 'Delete product', 'woocommerce' ), |
| 44 | 'description' => __( |
| 45 | 'Delete, trash, or restore a product.', |
| 46 | 'woocommerce' |
| 47 | ), |
| 48 | 'category' => 'woocommerce', |
| 49 | 'input_schema' => self::get_input_schema(), |
| 50 | 'output_schema' => self::get_delete_output_schema(), |
| 51 | 'execute_callback' => array( __CLASS__, 'execute' ), |
| 52 | 'permission_callback' => array( __CLASS__, 'can_delete_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' => true, |
| 62 | 'destructive' => true, |
| 63 | ), |
| 64 | ), |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Delete 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 ( $product->is_type( ProductType::VARIATION ) ) { |
| 84 | return new \WP_Error( |
| 85 | 'woocommerce_product_type_unsupported', |
| 86 | __( 'Product type is not supported by this ability.', 'woocommerce' ), |
| 87 | array( 'status' => 400 ) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | $product_id = $product->get_id(); |
| 92 | $force = (bool) ( $input['force'] ?? false ); |
| 93 | |
| 94 | /** |
| 95 | * Filter whether a product supports trashing in WooCommerce domain abilities. |
| 96 | * |
| 97 | * @param bool $supports_trash Whether the product supports trashing. |
| 98 | * @param \WC_Product $product The product being considered for trashing. |
| 99 | * |
| 100 | * @since 10.9.0 |
| 101 | */ |
| 102 | $supports_trash = apply_filters( 'woocommerce_product_object_trashable', EMPTY_TRASH_DAYS > 0, $product ); |
| 103 | |
| 104 | if ( ! $force && ! $supports_trash ) { |
| 105 | return new \WP_Error( |
| 106 | 'woocommerce_trash_not_supported', |
| 107 | __( 'Trash is disabled on this site. Pass force: true to permanently delete.', 'woocommerce' ), |
| 108 | array( 'status' => 501 ) |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | $deleted = $product->delete( $force ); |
| 113 | |
| 114 | if ( |
| 115 | ! $deleted |
| 116 | || ( $force && null !== get_post( $product_id ) ) |
| 117 | || ( ! $force && 'trash' !== get_post_status( $product_id ) ) |
| 118 | ) { |
| 119 | return new \WP_Error( |
| 120 | 'woocommerce_product_delete_failed', |
| 121 | __( 'Failed to delete product.', 'woocommerce' ), |
| 122 | array( 'status' => 500 ) |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | return array( |
| 127 | 'deleted' => (bool) $deleted, |
| 128 | 'id' => $product_id, |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Check product deletion access. |
| 134 | * |
| 135 | * @param mixed $input Ability input. |
| 136 | * @return bool |
| 137 | * |
| 138 | * @since 10.9.0 |
| 139 | */ |
| 140 | public static function can_delete_product( $input = array() ): bool { |
| 141 | $product_id = self::get_id_from_input( $input ); |
| 142 | |
| 143 | return $product_id > 0 && wc_rest_check_post_permissions( 'product', 'delete', $product_id ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get the ability input schema. |
| 148 | * |
| 149 | * @return array |
| 150 | */ |
| 151 | private static function get_input_schema(): array { |
| 152 | return array( |
| 153 | 'type' => 'object', |
| 154 | 'properties' => array( |
| 155 | 'id' => array( |
| 156 | 'type' => 'integer', |
| 157 | 'minimum' => 1, |
| 158 | ), |
| 159 | 'force' => array( |
| 160 | 'type' => 'boolean', |
| 161 | 'description' => __( |
| 162 | 'Permanently delete the product. Defaults to false, which moves the product to trash.', |
| 163 | 'woocommerce' |
| 164 | ), |
| 165 | 'default' => false, |
| 166 | ), |
| 167 | ), |
| 168 | 'required' => array( 'id' ), |
| 169 | 'additionalProperties' => false, |
| 170 | ); |
| 171 | } |
| 172 | } |
| 173 |