declare var acfw_edit_coupon: any; declare var vex: any; declare var jQuery: any; var $: any = jQuery; const { create_new_coupon_popup } = acfw_edit_coupon; const url = new URL(window.location.href); /** * Initialize new coupon popup functionality. * * @since 4.7.0 */ export default function create_new_coupon_popup_events() { if (url.pathname.includes('post-new.php') && url.searchParams.get('post_type') === 'shop_coupon') { $('#title').blur(); // Wait for the title field to be blurred before showing the popup. setTimeout(() => { showNewCouponPopup(); }, 100); } } /** * Show the new coupon popup modal. * * @since 4.7.0 */ function showNewCouponPopup() { // Show vex modal with the popup template vex.dialog.open({ unsafeMessage: newCouponPopupTemplate(), className: 'vex-theme-plain acfw-new-coupon-popup', showCloseButton: true, buttons: {}, afterOpen: function () { bindPopupEvents(); // Ensure the focus is not on the title field when the popup is opened. setTimeout(() => { $('.vex-content :focus').blur(); }, 100); }, }); } /** * Bind events for the popup modal. * * @since 4.7.0 */ function bindPopupEvents() { // Handle Create Manually button $('#acfw-create-manually').on('click', function () { setTimeout(() => { $('#title').focus(); }, 100); }); // Handle Use Coupon Template button $('#acfw-use-template').on('click', function () { window.location.href = create_new_coupon_popup.site_url + '/wp-admin/admin.php?page=acfw-coupon-templates'; }); // Handle Generate with AI button $('#acfw-generate-ai').on('click', function () { window.location.href = create_new_coupon_popup.site_url + '/wp-admin/admin.php?page=acfw-coupon-templates&show_ai_modal=true'; }); } /** * Get the AI option button HTML. * Always shown regardless of StoreAgent connection status. * The coupon templates page will handle showing the appropriate upsell modal * if StoreAgent is not yet installed, activated, or connected. * * @since 4.7.0 * * @return {string} AI option button HTML. */ function getAIOptionButton(): string { return ` `; } /** * Get the new coupon popup template. * * @since 4.7.0 * * @return {string} New coupon popup template. */ function newCouponPopupTemplate() { return `

${create_new_coupon_popup.title}

${create_new_coupon_popup.description}

${getAIOptionButton()}
`; }