init_hooks(); $this->load_dependencies(); } private function init_hooks() { add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_scripts')); add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts')); } private function load_dependencies() { require_once AITOA_PLUGIN_PATH . 'includes/class-aitoa-admin.php'; require_once AITOA_PLUGIN_PATH . 'includes/class-aitoa-frontend.php'; require_once AITOA_PLUGIN_PATH . 'includes/class-aitoa-ajax.php'; require_once AITOA_PLUGIN_PATH . 'includes/class-aitoa-gemini.php'; // Initialize classes new AITOA_Admin(); new AITOA_Frontend(); new AITOA_Ajax(); } public function enqueue_frontend_scripts() { if (is_product() || is_account_page()) { wp_enqueue_style( 'aitoa-frontend-css', AITOA_PLUGIN_URL . 'public/css/frontend-style.css', array(), AITOA_VERSION ); wp_enqueue_script( 'aitoa-frontend-js', AITOA_PLUGIN_URL . 'public/js/frontend-script.js', array('jquery'), AITOA_VERSION, true ); wp_localize_script('aitoa-frontend-js', 'aitoa_ajax', array( 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('aitoa_nonce'), 'user_logged_in' => is_user_logged_in(), 'photo_upload_url' => wc_get_account_endpoint_url('edit-account'), 'strings' => array( 'loading' => esc_html__('Loading...', 'ai-try-on-assistant'), 'error' => esc_html__('An error occurred, please try again later', 'ai-try-on-assistant'), 'login_required' => esc_html__('Please login as a member first', 'ai-try-on-assistant'), 'photo_required' => esc_html__('Photo Required', 'ai-try-on-assistant'), 'photo_upload_message' => esc_html__('Please add your photo to your profile first to use the virtual try-on feature.', 'ai-try-on-assistant'), 'upload_photo' => esc_html__('Go to Profile', 'ai-try-on-assistant'), 'processing' => esc_html__('Generating try-on effect...', 'ai-try-on-assistant'), 'please_wait' => esc_html__('This may take a few seconds, please wait...', 'ai-try-on-assistant'), ) )); // Add inline scripts for specific functionality $this->add_frontend_inline_scripts(); } } public function enqueue_admin_scripts($hook) { // Only load on plugin settings page and product edit page if ($hook == 'toplevel_page_aitoa-settings' || $hook == 'post.php' || $hook == 'post-new.php') { wp_enqueue_style( 'aitoa-admin-css', AITOA_PLUGIN_URL . 'admin/css/admin-style.css', array(), AITOA_VERSION ); wp_enqueue_script( 'aitoa-admin-js', AITOA_PLUGIN_URL . 'admin/js/admin-script.js', array('jquery'), AITOA_VERSION, true ); wp_localize_script('aitoa-admin-js', 'aitoa_admin_ajax', array( 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('aitoa_admin_nonce'), )); // Add inline scripts and styles for admin functionality $this->add_admin_inline_scripts($hook); } } /** * Add inline scripts for frontend functionality */ private function add_frontend_inline_scripts() { // Account form enctype modification if (is_account_page()) { $account_form_script = " document.addEventListener('DOMContentLoaded', function() { var form = document.querySelector('form.woocommerce-EditAccountForm'); if (form) { form.setAttribute('enctype', 'multipart/form-data'); } }); "; wp_add_inline_script('aitoa-frontend-js', $account_form_script); } // Product page try-on functionality if (is_product()) { global $product; if ($product) { $product_id = $product->get_id(); $enable_try_on = get_post_meta($product_id, '_aitoa_enable_try_on', true); if ($enable_try_on === 'yes') { $try_on_type = get_post_meta($product_id, '_aitoa_try_on_type', true); $tryable_images = get_post_meta($product_id, '_aitoa_tryable_images', true); if (!empty($tryable_images) && is_array($tryable_images)) { $valid_images = array(); foreach ($tryable_images as $image_id) { $image_id = intval($image_id); if ($image_id > 0 && wp_get_attachment_url($image_id)) { $valid_images[] = $image_id; } } if (!empty($valid_images)) { if (empty($try_on_type)) { $try_on_type = 'clothes'; } $this->add_try_on_button_script($product_id, $try_on_type, $valid_images); } } } } } } /** * Add inline scripts for admin functionality */ private function add_admin_inline_scripts($hook) { if ($hook == 'post.php' || $hook == 'post-new.php') { // Product meta box functionality $meta_box_script = " jQuery(document).ready(function($) { // Enable/disable try-on settings $('#aitoa_enable_try_on, #vtoa_enable_try_on').change(function() { var settingsDiv = $('#aitoa_try_on_settings, #vtoa_try_on_settings'); if ($(this).is(':checked')) { settingsDiv.show(); } else { settingsDiv.hide(); } }); // Real-time image selection feedback for vtoa $(document).on('change', 'input[name=\"vtoa_tryable_images[]\"]', function() { updateImageSelectionPreview(); }); // Try-on type change feedback $('#vtoa_try_on_type').change(function() { const type = $(this).val(); updateTypeDescription(type); updateImageSelectionHint(type); }); function updateImageSelectionPreview() { const selectedCount = $('input[name=\"vtoa_tryable_images[]\"]:checked').length; let counter = $('#vtoa-selected-count'); if (counter.length === 0) { $('.vtoa-image-selection').before('
'); counter = $('#vtoa-selected-count'); } if (selectedCount === 0) { counter.text('No images selected for try-on').css('color', '#dc3232'); } else if (selectedCount === 1) { counter.text('1 image selected for try-on').css('color', '#46b450'); } else { counter.text(selectedCount + ' images selected for try-on').css('color', '#46b450'); } } function updateTypeDescription(type) { const descriptions = { 'clothes': 'Clothes Try-On: Select clear clothing images for better results', 'hair': 'Hair Try-On: Select images where hairstyles are clearly visible', 'makeup': 'Makeup Try-On: Select images with clear facial features' }; $('.vtoa-type-description').remove(); if (type && descriptions[type]) { $('#vtoa_try_on_type').after('' + descriptions[type] + '
'); } } function updateImageSelectionHint(type) { const hints = { 'clothes': 'Tip: Select product images showing the full garment clearly', 'hair': 'Tip: Select images where hair color and style are prominently featured', 'makeup': 'Tip: Select close-up images showing makeup details' }; $('.vtoa-selection-hint').remove(); if (type && hints[type] && $('.vtoa-image-selection').length > 0) { $('.vtoa-image-selection').after('' + hints[type] + '
'); } } // Initialize on page load if (typeof updateImageSelectionPreview === 'function') { updateImageSelectionPreview(); } const currentType = $('#vtoa_try_on_type').val(); if (currentType && typeof updateTypeDescription === 'function') { updateTypeDescription(currentType); } }); "; wp_add_inline_script('aitoa-admin-js', $meta_box_script); } } /** * Add try-on button script for product pages */ private function add_try_on_button_script($product_id, $try_on_type, $valid_images) { $type_labels = array( 'clothes' => __('Try On', 'ai-try-on-assistant'), 'hair' => __('Try Hair Style', 'ai-try-on-assistant'), 'makeup' => __('Try Makeup', 'ai-try-on-assistant'), ); $button_text = isset($type_labels[$try_on_type]) ? $type_labels[$try_on_type] : __('Try It', 'ai-try-on-assistant'); $script = " jQuery(document).ready(function($) { var tryableImages = " . wp_json_encode($valid_images) . "; var productId = " . intval($product_id) . "; var tryOnType = '" . esc_js($try_on_type) . "'; var buttonText = '" . esc_js($button_text) . "'; // Add try-on button to each tryable image tryableImages.forEach(function(imageId) { // Get the actual image URL for this attachment ID via AJAX $.post(aitoa_ajax.ajax_url, { action: 'aitoa_get_image_data', image_id: imageId, nonce: aitoa_ajax.nonce }, function(response) { if (response.success) { var targetImageUrl = response.data.url; // Find image by matching URL $('.woocommerce-product-gallery__image img, .product-image img, figure img').each(function() { var \$img = $(this); var imgSrc = \$img.attr('src') || ''; var imgSrcset = \$img.attr('srcset') || ''; var imgDataSrc = \$img.attr('data-src') || ''; // Check if this image URL matches (compare just the filename) var targetFilename = targetImageUrl.split('/').pop().split('?')[0]; if (imgSrc.indexOf(targetFilename) !== -1 || imgSrcset.indexOf(targetFilename) !== -1 || imgDataSrc.indexOf(targetFilename) !== -1) { var \$container = \$img.closest('figure, .woocommerce-product-gallery__image, .product-image, .wc-block-product-gallery-image'); if (\$container.length && !\$container.find('.aitoa-try-on-badge').length) { \$container.css('position', 'relative'); var buttonHtml = ''; \$container.append(buttonHtml); } } }); } }); }); }); "; wp_add_inline_script('aitoa-frontend-js', $script); } }