getInputs(); $title = $inputs['discount_title'] ?? '?'; $type = $inputs['discount_type'] ?? ''; $amount = $inputs['discount_amount'] ?? ''; $suffix = 'percentage' === $type ? '%' : ''; $status = $inputs['discount_status'] ?? 'inactive'; return "Create discount \"{$title}\" — {$amount}{$suffix} {$type} (status: {$status})"; } /** * 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 string $discount_title Discount code / title. * @param string $discount_type Discount type (percentage|fixed_amount|standard). * @param float $discount_amount Discount value. * @param string|null $discount_status Active status (active|inactive). * @param bool|null $is_active Customer-facing active flag. * @param string|null $product_rule_type Product scope type. * @param array|null $specific_products Product IDs for specific_products scope. * @param array|null $excluded_products Product IDs to exclude from all_products scope. * @param array|null $specific_categories Category IDs for specific_categories scope. * @param string|null $addon_discount_applicable What the discount is applied to. * @param string|null $date_rule_type Date/time rule type. * @param string|null $date_rule_date Exact date (Y-m-d) for on_date rule. * @param string|null $date_rule_start_date Start date (Y-m-d) for date_range rule. * @param string|null $date_rule_end_date End date (Y-m-d) for date_range rule. * @param string|null $date_rule_start_time Start time (HH:MM) for time_range rule. * @param string|null $date_rule_end_time End time (HH:MM) for time_range rule. * @param array|null $date_rule_days Day abbreviations for day_of_week rule. * @return array */ public function __invoke( string $discount_title, string $discount_type, float $discount_amount, ?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; } $check = $this->validate_discount_fields( $discount_title, $discount_type, $discount_amount, $discount_status, $addon_discount_applicable ); if ( null !== $check ) { return $check; } try { $data = [ 'discount_title' => sanitize_text_field( $discount_title ), 'discount_type' => $discount_type, 'discount_amount' => $discount_amount, 'discount_status' => $discount_status ?? 'inactive', 'is_active' => $is_active ?? true, 'addon_discount_applicable' => $addon_discount_applicable ?? 'total_amount', 'product_rule' => $this->build_product_rule( $product_rule_type, $specific_products, $excluded_products, $specific_categories ), '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 ), ]; $discount = \WpCafePro\Models\Discount_Model::create( $data ); if ( ! $discount ) { return [ 'error' => 'Failed to create discount. The database insert did not return a valid record.' ]; } return [ 'success' => true, 'discount_id' => $discount->id, 'discount_title' => $discount->discount_title, 'message' => "Discount \"{$discount_title}\" created successfully (ID: {$discount->id}).", ]; } catch ( \Throwable $e ) { Log::error( 'Wpcafe_Create_Discount_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to create discount: ' . $e->getMessage() ]; } } /** * Validate required and optional discount fields before writing to the model. * * @param string $title Discount title/code. * @param string $type Discount type. * @param float $amount Discount amount. * @param string|null $status Discount status. * @param string|null $applicable Addon applicable setting. * @return array|null Error array on failure, null on success. */ private function validate_discount_fields( string $title, string $type, float $amount, ?string $status, ?string $applicable ): ?array { if ( '' === trim( $title ) ) { return [ 'error' => 'discount_title is required and cannot be empty.' ]; } $check = $this->validate_discount_type( $type ); if ( null !== $check ) { return $check; } if ( $amount <= 0 ) { return [ 'error' => 'discount_amount must be greater than 0.' ]; } if ( 'percentage' === $type && $amount > 100 ) { return [ 'error' => 'Percentage discount_amount cannot exceed 100.' ]; } if ( null !== $status ) { $check = $this->validate_discount_status( $status ); if ( null !== $check ) { return $check; } } if ( null !== $applicable ) { $check = $this->validate_addon_applicable( $applicable ); if ( null !== $check ) { return $check; } } return null; } }