$c_title, 'post_type' => 'shop_coupon', 'post_status' => $status, 'post_excerpt' => $description, ) ); if ( is_wp_error( $coupon_id ) ) { return array( 'success' => false, 'coupon_id' => null, 'coupon_code' => '', 'edit_url' => '', 'message' => 'WP_ERROR: ' . $coupon_id->get_error_message() ); } // 2. Setup WC_Coupon & Basic Types $bogo_type = class_exists( 'Wbte_Smart_Coupon_Bogo_Common' ) && property_exists( 'Wbte_Smart_Coupon_Bogo_Common', 'bogo_coupon_type_name' ) ? Wbte_Smart_Coupon_Bogo_Common::$bogo_coupon_type_name : 'wbte_sc_bogo'; $coupon = new WC_Coupon( $coupon_id ); $coupon->set_code( $c_title ); $coupon->set_discount_type( $bogo_type ); if ( is_null( $coupon->get_date_created() ) ) { $coupon->set_date_created( current_time( 'mysql' ) ); } if ( ! empty( $input['expiry_date'] ) ) { $coupon->set_date_expires( $input['expiry_date'] ); } $coupon->save(); update_post_meta( $coupon_id, 'discount_type', $bogo_type ); update_post_meta( $coupon_id, 'wbte_sc_bogo_coupon_name', $coupon_name ); if ( ! empty( $input['start_date'] ) ) { update_post_meta( $coupon_id, 'wc_sc_start_date', sanitize_text_field( $input['start_date'] ) ); } // 3. Auto vs Manual Logic if ( 'auto' === $conversion ) { if ( class_exists( 'Wt_Smart_Coupon_Auto_Coupon_Admin' ) && method_exists( 'Wt_Smart_Coupon_Auto_Coupon_Admin', 'process_auto_coupon_meta' ) ) { Wt_Smart_Coupon_Auto_Coupon_Admin::process_auto_coupon_meta( $coupon_id, array( '_wt_make_auto_coupon' => 'yes' ) ); } else { update_post_meta( $coupon_id, '_wt_make_auto_coupon', 'yes' ); } update_post_meta( $coupon_id, 'wbte_sc_bogo_code_condition', 'wbte_sc_bogo_code_auto' ); } else { update_post_meta( $coupon_id, 'wbte_sc_bogo_code_condition', 'wbte_sc_bogo_code_manual' ); } // 4. Handle Campaign Type $predefined_settings = array(); if ( 'custom' === $campaign_type ) { update_post_meta( $coupon_id, 'wbte_sc_bogo_type', 'wbte_sc_bogo_bxgx' ); update_post_meta( $coupon_id, 'wbte_sc_bogo_triggers_when', 'wbte_sc_bogo_triggers_qty' ); update_post_meta( $coupon_id, 'wbte_sc_bogo_apply_offer', 'wbte_sc_bogo_apply_repeatedly' ); $buy_qty = isset( $input['buy_quantity'] ) ? absint( $input['buy_quantity'] ) : 1; update_post_meta( $coupon_id, '_wbte_sc_bogo_min_qty', $buy_qty ); if ( ! empty( $input['buy_product_ids'] ) ) { $buy_ids = array_map( 'absint', $input['buy_product_ids'] ); update_post_meta( $coupon_id, 'wbte_sc_bogo_product_ids', implode( ',', $buy_ids ) ); } $get_qty = isset( $input['get_quantity'] ) ? absint( $input['get_quantity'] ) : 1; update_post_meta( $coupon_id, 'wbte_sc_bogo_customer_gets_qty', $get_qty ); if ( ! empty( $input['get_product_ids'] ) ) { $get_ids = array_map( 'absint', $input['get_product_ids'] ); update_post_meta( $coupon_id, 'wbte_sc_bogo_free_product_ids', implode( ',', $get_ids ) ); update_post_meta( $coupon_id, 'wbte_sc_bogo_customer_gets', 'specific_product' ); } $discount_type = isset( $input['discount_type'] ) ? $input['discount_type'] : 'free'; $discount_value = isset( $input['discount_value'] ) ? floatval( $input['discount_value'] ) : 0; if ( 'percent' === $discount_type ) { update_post_meta( $coupon_id, 'wbte_sc_bogo_customer_gets_with', 'wbte_sc_bogo_customer_gets_with_discount' ); update_post_meta( $coupon_id, 'wbte_sc_bogo_customer_gets_discount_type', 'wbte_sc_bogo_customer_gets_with_perc_discount' ); update_post_meta( $coupon_id, 'wbte_sc_bogo_customer_gets_discount_perc', $discount_value ); } elseif ( 'fixed' === $discount_type ) { update_post_meta( $coupon_id, 'wbte_sc_bogo_customer_gets_with', 'wbte_sc_bogo_customer_gets_with_discount' ); update_post_meta( $coupon_id, 'wbte_sc_bogo_customer_gets_discount_type', 'wbte_sc_bogo_customer_gets_with_price_discount' ); update_post_meta( $coupon_id, 'wbte_sc_bogo_customer_gets_discount_price', $discount_value ); } else { update_post_meta( $coupon_id, 'wbte_sc_bogo_customer_gets_with', 'wbte_sc_bogo_customer_gets_with_discount' ); update_post_meta( $coupon_id, 'wbte_sc_bogo_customer_gets_discount_type', 'wbte_sc_bogo_customer_gets_free' ); } } else { // Handle static templates if ( 'default_1' === $campaign_type ) { $predefined_settings = array( 'wbte_sc_bogo_triggers_when' => 'wbte_sc_bogo_triggers_qty', '_wbte_sc_bogo_min_qty' => 1, 'wbte_sc_bogo_customer_gets_discount_type' => 'wbte_sc_bogo_customer_gets_with_perc_discount', 'wbte_sc_bogo_customer_gets_discount_perc' => 50, 'wbte_sc_bogo_apply_offer' => 'wbte_sc_bogo_apply_repeatedly', ); } elseif ( 'default_2' === $campaign_type ) { $predefined_settings = array( 'wbte_sc_bogo_triggers_when' => 'wbte_sc_bogo_triggers_qty', '_wbte_sc_bogo_min_qty' => 2, ); } elseif ( 'default_3' === $campaign_type ) { $predefined_settings = array( 'wbte_sc_bogo_triggers_when' => 'wbte_sc_bogo_triggers_subtotal', '_wbte_sc_bogo_min_amount' => 100, ); } foreach ( $predefined_settings as $key => $value ) { update_post_meta( $coupon_id, $key, $value ); } } // 5. Apply Global Defaults for anything missing if ( class_exists( 'Wbte_Smart_Coupon_Bogo_Common' ) && property_exists( 'Wbte_Smart_Coupon_Bogo_Common', 'meta_arr' ) ) { $skip_arr = array( 'wbte_sc_bogo_type', 'wbte_sc_bogo_coupon_name', 'wbte_sc_bogo_campaign_description' ); foreach ( Wbte_Smart_Coupon_Bogo_Common::$meta_arr as $meta_key => $meta_info ) { if ( in_array( $meta_key, $skip_arr, true ) || metadata_exists( 'post', $coupon_id, $meta_key ) ) { continue; } $val = $meta_info['default'] ?? ''; update_post_meta( $coupon_id, $meta_key, $val ); } } // Trigger official BOGO actions and clear caches do_action( 'wbte_sc_bogo_new_coupon_created', $coupon_id ); //phpcs:ignore $this->clear_bogo_caches(); $plugin_name_param = defined('WT_SC_PLUGIN_NAME') ? WT_SC_PLUGIN_NAME : 'wt_smart_coupon'; return array( 'success' => true, 'coupon_id' => $coupon_id, 'coupon_code' => $coupon->get_code(), 'edit_url' => admin_url( 'admin.php?page=' . $plugin_name_param . '_bogo&wbte_bogo_id=' . $coupon_id ), 'message' => __( 'BOGO Coupon successfully generated.', 'ai-content-generator-for-woocommerce' ) ); } catch ( \Throwable $e ) { return array( 'success' => false, 'coupon_id' => null, 'coupon_code' => 'CRASH', 'edit_url' => '', 'message' => 'PHP CRASH: ' . $e->getMessage() ); } } /** * Retrieves a list of active BOGO coupons. * Mirrors Wbte_Smart_Coupon_Bogo_Admin::get_bogo_coupons_list() but executes safely * via WP_Query ensuring the same conditions (publish/draft statuses and specific meta). * * @param array $input Contains limit parameter. * @return array List of coupons containing id, code, and name. */ public function execute_list_bogos( array $input ) { try { $limit = isset( $input['limit'] ) ? absint( $input['limit'] ) : 20; $bogo_type = class_exists( 'Wbte_Smart_Coupon_Bogo_Common' ) && property_exists( 'Wbte_Smart_Coupon_Bogo_Common', 'bogo_coupon_type_name' ) ? Wbte_Smart_Coupon_Bogo_Common::$bogo_coupon_type_name : 'wbte_sc_bogo'; // Using standard WP_Query against postmeta guarantees headless safety $query = new WP_Query( array( 'post_type' => 'shop_coupon', 'post_status' => array( 'publish', 'draft' ), 'posts_per_page' => $limit, 'fields' => 'ids', 'meta_query' => array( //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query array( 'key' => 'discount_type', 'value' => $bogo_type, ), ), ) ); $coupons = array(); foreach ( $query->posts as $id ) { $coupons[] = array( 'id' => $id, 'code' => get_the_title( $id ), 'name' => get_post_meta( $id, 'wbte_sc_bogo_coupon_name', true ) ); } return $coupons; } catch ( \Throwable $e ) { return array( array( 'id' => 0, 'code' => 'CRASH', 'name' => 'PHP CRASH: ' . $e->getMessage() ) ); } } /** * Executes moving a BOGO coupon to the trash. * Mirrors the logic from Wbte_Smart_Coupon_Bogo_Admin::bogo_coupon_delete_on_listing() * and Wbte_Smart_Coupon_Bogo_Admin::delete_bogo_count_cache(). * * @param array $input Contains coupon_id. * @return bool|WP_Error True on success, WP_Error on failure. */ public function execute_trash_bogo( array $input ) { $coupon_id = absint( $input['coupon_id'] ); if ( ! $coupon_id || get_post_type( $coupon_id ) !== 'shop_coupon' ) { return new WP_Error( 'invalid_id', __( 'Invalid coupon ID provided.', 'ai-content-generator-for-woocommerce' ) ); } $bogo_type = class_exists( 'Wbte_Smart_Coupon_Bogo_Common' ) && property_exists( 'Wbte_Smart_Coupon_Bogo_Common', 'bogo_coupon_type_name' ) ? Wbte_Smart_Coupon_Bogo_Common::$bogo_coupon_type_name : 'wbte_sc_bogo'; // Safety check to ensure it's specifically a BOGO coupon before trashing if ( get_post_meta( $coupon_id, 'discount_type', true ) !== $bogo_type ) { return new WP_Error( 'not_bogo', __( 'The provided ID is not a BOGO coupon.', 'ai-content-generator-for-woocommerce' ) ); } $result = wp_trash_post( $coupon_id ); if ( $result ) { // Delete BOGO count cache exactly as the admin class delete_bogo_count_cache() does delete_transient( 'wbte_sc_bogo_count_all' ); delete_transient( 'wbte_sc_bogo_count_trash' ); delete_transient( 'wbte_sc_bogo_count_active' ); // Delete general BOGO ids transient delete_transient( 'wbte_sc_bogo_coupon_ids' ); return true; } return new WP_Error( 'trash_failed', __( 'Failed to trash the coupon.', 'ai-content-generator-for-woocommerce' ) ); } /** * Helper method to clear all BOGO-related caches and transients. * Mirrors Wbte_Smart_Coupon_Bogo_Admin::delete_bogo_count_cache() and * Wbte_Smart_Coupon_Bogo_Admin::delete_bogo_ids_transient(). * * @since 1.0.6 */ private function clear_bogo_caches() { delete_transient( 'wbte_sc_bogo_count_all' ); delete_transient( 'wbte_sc_bogo_count_trash' ); delete_transient( 'wbte_sc_bogo_count_active' ); delete_transient( 'wbte_sc_bogo_coupon_ids' ); } /** * Verifies if a post ID is a valid BOGO coupon. * * @param int $coupon_id The post ID. * @return bool */ private function is_valid_bogo( $coupon_id ) { if ( ! $coupon_id || get_post_type( $coupon_id ) !== 'shop_coupon' ) { return false; } $bogo_type = class_exists( 'Wbte_Smart_Coupon_Bogo_Common' ) && property_exists( 'Wbte_Smart_Coupon_Bogo_Common', 'bogo_coupon_type_name' ) ? Wbte_Smart_Coupon_Bogo_Common::$bogo_coupon_type_name : 'wbte_sc_bogo'; return get_post_meta( $coupon_id, 'discount_type', true ) === $bogo_type; } /** * Executes toggling a BOGO coupon's status. * Mirrors the exact logic from Wbte_Smart_Coupon_Bogo_Admin::update_status_on_toggle(). * * @param array $input Contains coupon_id, is_active. * @return bool|WP_Error */ public function execute_toggle_bogo_status( array $input ) { $coupon_id = absint( $input['coupon_id'] ); $is_active = (bool) $input['is_active']; if ( ! $this->is_valid_bogo( $coupon_id ) ) { return new WP_Error( 'invalid_bogo', __( 'Invalid BOGO coupon ID provided.', 'ai-content-generator-for-woocommerce' ) ); } $coupon = new WC_Coupon( $coupon_id ); $update_result = wp_update_post( array( 'ID' => $coupon_id, 'post_status' => $is_active ? 'publish' : 'draft', ) ); if ( is_wp_error( $update_result ) || $update_result === 0 ) { return new WP_Error( 'update_failed', __( 'Failed to update BOGO status.', 'ai-content-generator-for-woocommerce' ) ); } $coupon->save(); $this->clear_bogo_caches(); return true; } /** * Executes restoring a trashed BOGO coupon. * Mirrors the exact logic from Wbte_Smart_Coupon_Bogo_Admin::bogo_coupon_single_restore(). * * @param array $input Contains coupon_id. * @return bool|WP_Error */ public function execute_restore_bogo( array $input ) { $coupon_id = absint( $input['coupon_id'] ); if ( ! $this->is_valid_bogo( $coupon_id ) ) { return new WP_Error( 'invalid_bogo', __( 'Invalid BOGO coupon ID provided.', 'ai-content-generator-for-woocommerce' ) ); } $coupon_update = array( 'ID' => $coupon_id, 'post_status' => 'publish', ); $result = wp_update_post( $coupon_update ); if ( $result ) { $this->clear_bogo_caches(); return true; } return new WP_Error( 'restore_failed', __( 'Failed to restore BOGO coupon.', 'ai-content-generator-for-woocommerce' ) ); } /** * Executes the permanent deletion of a BOGO coupon. * Mirrors the exact logic from Wbte_Smart_Coupon_Bogo_Admin::bogo_coupon_single_perm_dlt(). * * @param array $input Contains coupon_id. * @return bool|WP_Error */ public function execute_delete_bogo_permanently( array $input ) { $coupon_id = absint( $input['coupon_id'] ); if ( ! $this->is_valid_bogo( $coupon_id ) ) { return new WP_Error( 'invalid_bogo', __( 'Invalid BOGO coupon ID provided.', 'ai-content-generator-for-woocommerce' ) ); } $result = wp_delete_post( $coupon_id, true ); if ( $result ) { $this->clear_bogo_caches(); return true; } return new WP_Error( 'delete_failed', __( 'Failed to permanently delete BOGO coupon.', 'ai-content-generator-for-woocommerce' ) ); } /** * Executes duplicating a BOGO coupon. * Mirrors the exact logic from Wbte_Smart_Coupon_Bogo_Admin::bogo_coupon_single_duplicate(). * Relies on the Smart Coupon's clone engine. * * @param array $input Contains coupon_id. * @return array|WP_Error */ public function execute_duplicate_bogo( array $input ) { $old_coupon_id = absint( $input['coupon_id'] ); if ( ! $this->is_valid_bogo( $old_coupon_id ) ) { return new WP_Error( 'invalid_bogo', __( 'Invalid BOGO coupon ID provided.', 'ai-content-generator-for-woocommerce' ) ); } if ( ! class_exists( 'WT_Duplicate_Shop_Coupon' ) || ! method_exists( 'WT_Duplicate_Shop_Coupon', 'clone_coupon' ) ) { return new WP_Error( 'missing_dependency', __( 'Smart Coupon duplication module is missing or inactive.', 'ai-content-generator-for-woocommerce' ) ); } try { $old_coupon = new WC_Coupon( $old_coupon_id ); $old_coupon_code = $old_coupon->get_code(); $c_title = $old_coupon_code; $counter = 1; // FIX: post_exists() is an admin-only function. We must include it for REST API requests. if ( ! function_exists( 'post_exists' ) ) { require_once ABSPATH . 'wp-admin/includes/post.php'; } // Generate unique title while ( post_exists( $c_title ) ) { $c_title = "{$old_coupon_code}{$counter}"; ++$counter; } // Perform duplication using plugin's native tool $duplicate_coupon_id = WT_Duplicate_Shop_Coupon::clone_coupon( $old_coupon_id, $c_title ); if ( ! $duplicate_coupon_id ) { return new WP_Error( 'duplicate_failed', __( 'Duplication process returned false.', 'ai-content-generator-for-woocommerce' ) ); } $coupon = new WC_Coupon( $duplicate_coupon_id ); if ( 'draft' === $coupon->get_status() ) { if ( is_null( $coupon->get_date_created() ) ) { $coupon->set_date_created( current_time( 'mysql' ) ); } } // Delete usage limit data on duplicate as per admin logic delete_post_meta( $duplicate_coupon_id, 'usage_count' ); delete_post_meta( $duplicate_coupon_id, '_used_by' ); // Set duplicated coupon to draft wp_update_post( array( 'ID' => $duplicate_coupon_id, 'post_status' => 'draft', ) ); // Trigger action which clears cache hooks internally do_action( 'wbte_sc_bogo_new_coupon_created', $duplicate_coupon_id ); //phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound // Clear cache manually as a fallback $this->clear_bogo_caches(); return array( 'success' => true, 'new_id' => $duplicate_coupon_id, 'new_code' => $c_title, 'message' => __( 'BOGO Coupon duplicated successfully.', 'ai-content-generator-for-woocommerce' ), ); } catch ( \Throwable $e ) { return new WP_Error( 'crash', 'PHP CRASH: ' . $e->getMessage() ); } } }