getInputs(); $id = $inputs['discount_id'] ?? '?'; $changes = []; if ( isset( $inputs['discount_title'] ) ) { $changes[] = "code to \"{$inputs['discount_title']}\""; } if ( isset( $inputs['discount_type'], $inputs['discount_amount'] ) ) { $suffix = 'percentage' === $inputs['discount_type'] ? '%' : ''; $changes[] = "amount to {$inputs['discount_amount']}{$suffix} {$inputs['discount_type']}"; } elseif ( isset( $inputs['discount_amount'] ) ) { $changes[] = "amount to {$inputs['discount_amount']}"; } elseif ( isset( $inputs['discount_type'] ) ) { $changes[] = "type to {$inputs['discount_type']}"; } if ( isset( $inputs['discount_status'] ) ) { $changes[] = "status to {$inputs['discount_status']}"; } if ( isset( $inputs['date_rule_type'] ) ) { $changes[] = "date rule to {$inputs['date_rule_type']}"; } if ( isset( $inputs['product_rule_type'] ) ) { $changes[] = "product scope to {$inputs['product_rule_type']}"; } $summary = "Update discount #{$id}"; if ( ! empty( $changes ) ) { $summary .= ': ' . implode( ', ', $changes ); } return $summary; } /** * Human-readable version of tool inputs for the approval card. * * @return array */ public function get_approval_inputs(): array { return $this->getInputs(); } /** * Execute the tool * * @param int $discount_id Discount post ID. * @param string|null $discount_title New discount code / title. * @param string|null $discount_type New discount type. * @param float|null $discount_amount New discount value. * @param string|null $discount_status New status (active|inactive). * @param bool|null $is_active New customer-facing active flag. * @param string|null $product_rule_type New product rule type. * @param array|null $specific_products New specific product IDs. * @param array|null $excluded_products New excluded product IDs. * @param array|null $specific_categories New specific category IDs. * @param string|null $addon_discount_applicable New addon applicable setting. * @param string|null $date_rule_type New date rule type. * @param string|null $date_rule_date New on_date value (Y-m-d). * @param string|null $date_rule_start_date New range start date (Y-m-d). * @param string|null $date_rule_end_date New range end date (Y-m-d). * @param string|null $date_rule_start_time New time window start (HH:MM). * @param string|null $date_rule_end_time New time window end (HH:MM). * @param array|null $date_rule_days New day-of-week list. * @return array */ public function __invoke( int $discount_id, ?string $discount_title = null, ?string $discount_type = null, ?float $discount_amount = null, ?string $discount_status = null, ?bool $is_active = null, ?string $product_rule_type = null, ?array $specific_products = null, ?array $excluded_products = null, ?array $specific_categories = null, ?string $addon_discount_applicable = null, ?string $date_rule_type = null, ?string $date_rule_date = null, ?string $date_rule_start_date = null, ?string $date_rule_end_date = null, ?string $date_rule_start_time = null, ?string $date_rule_end_time = null, ?array $date_rule_days = null ): array { $check = $this->guard( 'manage_options' ); if ( $check ) { return $check; } $check = $this->guard_discount_pro(); if ( $check ) { return $check; } if ( $discount_id <= 0 ) { return [ 'error' => 'discount_id must be a positive integer.' ]; } try { $discount = \WpCafePro\Models\Discount_Model::find( $discount_id ); if ( ! $discount ) { return [ 'error' => "Discount #{$discount_id} not found." ]; } $data = []; $updated_fields = []; if ( null !== $discount_title ) { if ( '' === trim( $discount_title ) ) { return [ 'error' => 'discount_title cannot be empty.' ]; } $data['discount_title'] = sanitize_text_field( $discount_title ); $updated_fields[] = 'discount_title'; } if ( null !== $discount_type ) { $check = $this->validate_discount_type( $discount_type ); if ( null !== $check ) { return $check; } $data['discount_type'] = $discount_type; $updated_fields[] = 'discount_type'; } if ( null !== $discount_amount ) { if ( $discount_amount <= 0 ) { return [ 'error' => 'discount_amount must be greater than 0.' ]; } $effective_type = $discount_type ?? (string) $discount->discount_type; if ( 'percentage' === $effective_type && $discount_amount > 100 ) { return [ 'error' => 'Percentage discount_amount cannot exceed 100.' ]; } $data['discount_amount'] = $discount_amount; $updated_fields[] = 'discount_amount'; } if ( null !== $discount_status ) { $check = $this->validate_discount_status( $discount_status ); if ( null !== $check ) { return $check; } $data['discount_status'] = $discount_status; $updated_fields[] = 'discount_status'; } if ( null !== $is_active ) { $data['is_active'] = $is_active; $updated_fields[] = 'is_active'; } if ( null !== $addon_discount_applicable ) { $check = $this->validate_addon_applicable( $addon_discount_applicable ); if ( null !== $check ) { return $check; } $data['addon_discount_applicable'] = $addon_discount_applicable; $updated_fields[] = 'addon_discount_applicable'; } if ( null !== $product_rule_type ) { $data['product_rule'] = $this->build_product_rule( $product_rule_type, $specific_products, $excluded_products, $specific_categories ); $updated_fields[] = 'product_rule'; } if ( null !== $date_rule_type ) { $data['date_rule'] = $this->build_date_rule( $date_rule_type, $date_rule_date, $date_rule_start_date, $date_rule_end_date, $date_rule_start_time, $date_rule_end_time, $date_rule_days ); $updated_fields[] = 'date_rule'; } if ( empty( $data ) ) { return [ 'error' => 'No fields provided to update.' ]; } $discount->update( $data ); return [ 'success' => true, 'discount_id' => $discount_id, 'updated_fields' => $updated_fields, 'message' => "Discount #{$discount_id} updated successfully.", ]; } catch ( \Throwable $e ) { Log::error( 'Wpcafe_Update_Discount_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to update discount: ' . $e->getMessage() ]; } } }