| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808 |
- /**
- * Admin JavaScript for Studiou WC Product Category Manager
- */
- (function($) {
- 'use strict';
-
- // Safe init wrapper - prevents one form's error from breaking others
- function safeInit(name, selector, initFn) {
- try {
- if ($(selector).length) {
- console.log('STUDIOU WC: ' + name + ' found, initializing');
- initFn();
- }
- } catch (e) {
- console.error('STUDIOU WC: Error initializing ' + name + ':', e);
- }
- }
- // Initialize admin scripts
- $(document).ready(function() {
- // Debug: Log that script is loaded
- console.log('STUDIOU WC: Admin script loaded v1.6.1');
- if (typeof studiouWcpcm === 'undefined') {
- console.error('STUDIOU WC: studiouWcpcm object is NOT defined - AJAX will not work');
- return;
- }
- console.log('STUDIOU WC: studiouWcpcm object:', studiouWcpcm);
- safeInit('Import form', '#studiou-wcpcm-import-form', initImportForm);
- safeInit('Export form', '#studiou-wcpcm-export-form', initExportForm);
- safeInit('Sample download', '#studiou-wcpcm-download-sample', initSampleDownload);
- safeInit('Product import form', '#studiou-wcpcm-product-import-form', initProductImportForm);
- safeInit('Product export form', '#studiou-wcpcm-product-export-form', initProductExportForm);
- // Category Manipulations page: the manipulation forms live inside
- // lazily-loaded tab partials, so they are initialised by the tab
- // controller after their partial is injected (see initManipulationTabs).
- safeInit('Manipulation tabs', '.studiou-wcpcm-tabs', initManipulationTabs);
- });
- /**
- * Initialize the tabbed Category Manipulations page.
- *
- * Each tab's markup is fetched on demand via the studiou_wcpcm_load_tab
- * AJAX handler. The first time a tab is opened its partial is rendered into
- * its own panel and its form handlers are wired; the panel is then kept in
- * the DOM (hidden) so switching tabs preserves both state and event
- * handlers - no re-fetch and no re-init.
- */
- function initManipulationTabs() {
- // Maps a tab slug to the init function that wires its partial's handlers.
- var TAB_INIT = {
- 'move': initMoveForm,
- 'batch-description': initBatchDescriptionForm,
- 'delete-products': initDeleteProductsForm,
- 'clear-media': initClearMediaForm,
- 'remove-unassigned': initRemoveUnassignedMediaForm,
- 'upvp': initUpdateVariantPricesForm,
- 'notes': null // static, nothing to wire
- };
- var i18n = (studiouWcpcm && studiouWcpcm.i18n) ? studiouWcpcm.i18n : {};
- var $tabs = $('.studiou-wcpcm-tabs .nav-tab');
- var $content = $('#studiou-wcpcm-tab-content');
- var loaded = {}; // slug -> true once its panel exists in the DOM
- var allowed = $tabs.map(function() { return $(this).data('tab'); }).get();
- function panelFor(slug) {
- return $content.children('.studiou-wcpcm-tab-panel[data-tab="' + slug + '"]');
- }
- function activate(slug) {
- if (allowed.indexOf(slug) === -1) {
- slug = allowed[0];
- }
- $tabs.removeClass('nav-tab-active')
- .filter('[data-tab="' + slug + '"]').addClass('nav-tab-active');
- if (window.history && history.replaceState) {
- history.replaceState(null, '', '#' + slug);
- } else {
- window.location.hash = slug;
- }
- // Drop any stale loading / error placeholders, hide every panel.
- $content.find('.studiou-wcpcm-tab-loading, .studiou-wcpcm-tab-error').remove();
- $content.children('.studiou-wcpcm-tab-panel').hide();
- // Already loaded - just reveal its panel (handlers + state intact).
- if (loaded[slug]) {
- panelFor(slug).show();
- return;
- }
- $content.append('<div class="studiou-wcpcm-tab-loading">' +
- (i18n.tabLoading || 'Loading...') + '</div>');
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_load_tab',
- nonce: studiouWcpcm.nonce,
- tab: slug
- },
- success: function(response) {
- $content.find('.studiou-wcpcm-tab-loading').remove();
- if (response.success && response.data && typeof response.data.html === 'string') {
- // Stale click guard: only reveal if this tab is still active.
- var stillActive = ($tabs.filter('.nav-tab-active').data('tab') === slug);
- var $panel = $('<div class="studiou-wcpcm-tab-panel"></div>')
- .attr('data-tab', slug)
- .html(response.data.html);
- $content.append($panel);
- loaded[slug] = true;
- var initFn = TAB_INIT[slug];
- if (typeof initFn === 'function') {
- try {
- initFn();
- } catch (e) {
- console.error('STUDIOU WC: tab init "' + slug + '" failed', e);
- }
- }
- if (stillActive) {
- $content.children('.studiou-wcpcm-tab-panel').hide();
- $panel.show();
- } else {
- $panel.hide();
- }
- } else {
- var msg = (response.data && response.data.message) ||
- (i18n.tabLoadError || 'Failed to load tab');
- $content.append('<div class="studiou-wcpcm-tab-error">' + msg + '</div>');
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: load_tab AJAX error:', status, error);
- $content.find('.studiou-wcpcm-tab-loading').remove();
- $content.append('<div class="studiou-wcpcm-tab-error">' +
- (i18n.tabLoadError || 'Failed to load tab') + ': ' + error + '</div>');
- }
- });
- }
- $tabs.on('click', function(e) {
- e.preventDefault();
- activate($(this).data('tab'));
- });
- // Initial tab: a valid URL hash, otherwise the first tab.
- var initial = (window.location.hash || '').replace(/^#/, '');
- activate(allowed.indexOf(initial) !== -1 ? initial : allowed[0]);
- }
-
- /**
- * Initialize import form
- */
- function initImportForm() {
- $('#studiou-wcpcm-import-form').on('submit', function(e) {
- e.preventDefault();
-
- // Show spinner
- var $submitBtn = $(this).find('#studiou-wcpcm-import-button');
- var $spinner = $(this).find('.spinner');
-
- $submitBtn.prop('disabled', true);
- $spinner.css('visibility', 'visible');
-
- // Clear previous notices
- $('.studiou-wcpcm-notice-area').empty();
-
- // Create form data
- var formData = new FormData(this);
- formData.append('action', 'studiou_wcpcm_import');
- formData.append('nonce', studiouWcpcm.nonce);
-
- // Send AJAX request
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: formData,
- processData: false,
- contentType: false,
- success: function(response) {
- if (response.success) {
- showNotice('success', studiouWcpcm.i18n.importSuccess);
-
- // Show results
- $('#studiou-wcpcm-import-results').text(response.data.message);
- $('.studiou-wcpcm-import-results').show();
- } else {
- showNotice('error', response.data.message);
- }
- },
- error: function() {
- showNotice('error', studiouWcpcm.i18n.importError);
- },
- complete: function() {
- // Hide spinner
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- });
- }
-
- /**
- * Initialize export form
- */
- function initExportForm() {
- $('#studiou-wcpcm-export-form').on('submit', function(e) {
- e.preventDefault();
-
- // Show spinner
- var $submitBtn = $(this).find('#studiou-wcpcm-export-button');
- var $spinner = $(this).find('.spinner');
-
- $submitBtn.prop('disabled', true);
- $spinner.css('visibility', 'visible');
-
- // Clear previous notices
- $('.studiou-wcpcm-notice-area').empty();
-
- // Send AJAX request
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_export',
- nonce: studiouWcpcm.nonce
- },
- success: function(response) {
- if (response.success) {
- showNotice('success', studiouWcpcm.i18n.exportSuccess);
-
- // Show download link
- $('#studiou-wcpcm-export-message').text(response.data.message);
- $('#studiou-wcpcm-download-export').attr('href', response.data.download_url);
- $('.studiou-wcpcm-export-results').show();
- } else {
- showNotice('error', response.data.message);
- }
- },
- error: function() {
- showNotice('error', studiouWcpcm.i18n.exportError);
- },
- complete: function() {
- // Hide spinner
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- });
- }
-
- /**
- * Initialize move products form
- */
- function initMoveForm() {
- console.log('STUDIOU WC: Initializing move form handlers');
-
- // Add debug button click handler first
- $('#studiou-wcpcm-move-button').on('click', function(e) {
- console.log('STUDIOU WC: Move button clicked directly');
- });
-
- $('#studiou-wcpcm-move-form').on('submit', function(e) {
- e.preventDefault();
-
- // Debug: Log form submission
- console.log('STUDIOU WC: Move form submitted');
-
- // Validate that different categories are selected
- var sourceCategory = $('#source_category').val();
- var targetCategory = $('#target_category').val();
-
- console.log('STUDIOU WC: Source category:', sourceCategory, 'Target category:', targetCategory);
-
- if (!sourceCategory || !targetCategory) {
- console.log('STUDIOU WC: Missing category selection');
- showNotice('error', 'Please select both source and target categories');
- return;
- }
-
- if (sourceCategory === targetCategory) {
- console.log('STUDIOU WC: Same category selected');
- showNotice('error', studiouWcpcm.i18n.selectDifferentCategories);
- return;
- }
-
- // Show spinner
- var $submitBtn = $(this).find('#studiou-wcpcm-move-button');
- var $spinner = $(this).find('.spinner');
-
- $submitBtn.prop('disabled', true);
- $spinner.css('visibility', 'visible');
-
- // Clear previous notices
- $('.studiou-wcpcm-notice-area').empty();
-
- // Debug: Log AJAX request details
- console.log('STUDIOU WC: Sending AJAX request to:', studiouWcpcm.ajaxUrl);
- console.log('STUDIOU WC: Nonce:', studiouWcpcm.nonce);
-
- // Send AJAX request
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_move_products',
- source_category: sourceCategory,
- target_category: targetCategory,
- nonce: studiouWcpcm.nonce
- },
- success: function(response) {
- console.log('STUDIOU WC: AJAX response:', response);
-
- if (response.success) {
- showNotice('success', studiouWcpcm.i18n.moveSuccess);
-
- // Show results
- $('#studiou-wcpcm-move-message').html('<p>' + response.data.message + '</p>');
- $('.studiou-wcpcm-move-results').show();
-
- // Refresh the page to update category counts
- setTimeout(function() {
- location.reload();
- }, 3000);
- } else {
- showNotice('error', response.data.message);
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: AJAX error:', status, error);
- console.error('STUDIOU WC: Response text:', xhr.responseText);
- showNotice('error', studiouWcpcm.i18n.moveError + ': ' + error);
- },
- complete: function() {
- // Hide spinner
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- });
- }
-
- /**
- * Initialize batch description form
- */
- function initBatchDescriptionForm() {
- console.log('STUDIOU WC: Initializing batch description form handlers');
-
- // Check all button handler
- $('#studiou-wcpcm-check-all').on('click', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Check all button clicked');
- $('#categories_list input[type="checkbox"]').prop('checked', true);
- });
-
- // Uncheck all button handler
- $('#studiou-wcpcm-uncheck-all').on('click', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Uncheck all button clicked');
- $('#categories_list input[type="checkbox"]').prop('checked', false);
- });
-
- // Form submit handler
- $('#studiou-wcpcm-batch-description-form').on('submit', function(e) {
- e.preventDefault();
-
- console.log('STUDIOU WC: Batch description form submitted');
-
- // Get selected categories
- var selectedCategories = [];
- $('#categories_list input[type="checkbox"]:checked').each(function() {
- selectedCategories.push($(this).val());
- });
-
- var description = $('#category_description').val();
-
- console.log('STUDIOU WC: Selected categories:', selectedCategories);
- console.log('STUDIOU WC: Description:', description);
-
- // Validate input
- if (selectedCategories.length === 0) {
- console.log('STUDIOU WC: No categories selected');
- showNotice('error', 'Please select at least one category');
- return;
- }
-
- // Show spinner
- var $submitBtn = $(this).find('#studiou-wcpcm-batch-description-button');
- var $spinner = $(this).find('.spinner');
-
- $submitBtn.prop('disabled', true);
- $spinner.css('visibility', 'visible');
-
- // Clear previous notices
- $('.studiou-wcpcm-notice-area').empty();
-
- // Debug: Log AJAX request details
- console.log('STUDIOU WC: Sending batch description AJAX request to:', studiouWcpcm.ajaxUrl);
- console.log('STUDIOU WC: Nonce:', studiouWcpcm.nonce);
-
- // Send AJAX request
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_batch_description',
- selected_categories: selectedCategories,
- description: description,
- nonce: studiouWcpcm.nonce
- },
- success: function(response) {
- console.log('STUDIOU WC: Batch description AJAX response:', response);
-
- if (response.success) {
- showNotice('success', studiouWcpcm.i18n.batchDescriptionSuccess || 'Batch description operation successful');
-
- // Show results
- $('#studiou-wcpcm-batch-description-message').html('<p>' + response.data.message + '</p>');
- $('.studiou-wcpcm-batch-description-results').show();
- } else {
- showNotice('error', response.data.message);
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: Batch description AJAX error:', status, error);
- console.error('STUDIOU WC: Response text:', xhr.responseText);
- showNotice('error', (studiouWcpcm.i18n.batchDescriptionError || 'Batch description operation error') + ': ' + error);
- },
- complete: function() {
- // Hide spinner
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- });
- }
-
- /**
- * Initialize sample CSV download
- */
- function initSampleDownload() {
- $('#studiou-wcpcm-download-sample').on('click', function(e) {
- e.preventDefault();
-
- // Sample CSV content
- var csvContent = 'name,url-name,parent-name,description,display-type,visibility,protected-passwords,operation\n' +
- '"Clothing","clothing","","Top quality clothing","default","public","","A"\n' +
- '"Men","men","Clothing","Men\'s clothing","products","public","","A"\n' +
- '"Women","women","Clothing","Women\'s clothing","products","protected","password123","A"\n' +
- '"Kids","kids","Clothing","Kids clothing","products","public","","U"\n' +
- '"Accessories","accessories","","All accessories","default","public","","D"';
-
- // Create download link
- var blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
- var url = URL.createObjectURL(blob);
- var link = document.createElement('a');
-
- link.setAttribute('href', url);
- link.setAttribute('download', 'sample-product-categories.csv');
- link.style.visibility = 'hidden';
-
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- });
- }
-
- /**
- * Initialize delete products form
- */
- function initDeleteProductsForm() {
- console.log('STUDIOU WC: Initializing delete products form handlers');
- console.log('STUDIOU WC: Form element:', $('#studiou-wcpcm-delete-products-form'));
- console.log('STUDIOU WC: Check all button:', $('#studiou-wcpcm-delete-check-all'));
- console.log('STUDIOU WC: Delete button:', $('#studiou-wcpcm-delete-products-button'));
- // Check all button handler
- $('#studiou-wcpcm-delete-check-all').on('click', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Delete check all button clicked');
- $('#delete_categories_list input[type="checkbox"]').prop('checked', true);
- });
- // Uncheck all button handler
- $('#studiou-wcpcm-delete-uncheck-all').on('click', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Delete uncheck all button clicked');
- $('#delete_categories_list input[type="checkbox"]').prop('checked', false);
- });
- // Form submit handler
- $('#studiou-wcpcm-delete-products-form').on('submit', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Delete products form submitted');
- // Get selected categories
- var selectedCategories = [];
- $('#delete_categories_list input[type="checkbox"]:checked').each(function() {
- selectedCategories.push($(this).val());
- });
- console.log('STUDIOU WC: Selected categories:', selectedCategories);
- // Validate input
- if (selectedCategories.length === 0) {
- console.log('STUDIOU WC: No categories selected');
- showNotice('error', studiouWcpcm.i18n.selectAtLeastOneCategory || 'Please select at least one category');
- return;
- }
- // Confirm deletion
- if (!confirm(studiouWcpcm.i18n.confirmDelete || 'Are you sure you want to permanently delete ALL products in the selected categories? This action cannot be undone!')) {
- console.log('STUDIOU WC: User cancelled deletion');
- return;
- }
- // Show spinner
- var $submitBtn = $(this).find('#studiou-wcpcm-delete-products-button');
- var $spinner = $(this).find('.spinner');
- $submitBtn.prop('disabled', true);
- $spinner.css('visibility', 'visible');
- // Clear previous notices
- $('.studiou-wcpcm-notice-area').empty();
- // Hide results, show progress
- $('.studiou-wcpcm-delete-results').hide();
- $('.studiou-wcpcm-delete-progress').show();
- $('#studiou-wcpcm-delete-progress-text').text('Initializing...');
- updateProgressBar(0);
- // Debug: Log AJAX request details
- console.log('STUDIOU WC: Sending delete products AJAX request to:', studiouWcpcm.ajaxUrl);
- console.log('STUDIOU WC: Nonce:', studiouWcpcm.nonce);
- // Send AJAX request to get product IDs
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_delete_products',
- delete_categories: selectedCategories,
- nonce: studiouWcpcm.nonce
- },
- success: function(response) {
- console.log('STUDIOU WC: Delete products AJAX response:', response);
- if (response.success) {
- var productIds = response.data.product_ids;
- var totalCount = response.data.total_count;
- console.log('STUDIOU WC: Total products to delete:', totalCount);
- if (totalCount === 0) {
- showNotice('success', studiouWcpcm.i18n.noProductsFound || 'No products found in selected categories');
- $('.studiou-wcpcm-delete-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- return;
- }
- // Process products in batches
- processDeletionBatches(productIds, totalCount, $submitBtn, $spinner);
- } else {
- showNotice('error', response.data.message);
- $('.studiou-wcpcm-delete-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: Delete products AJAX error:', status, error);
- console.error('STUDIOU WC: Response text:', xhr.responseText);
- showNotice('error', (studiouWcpcm.i18n.deleteError || 'Delete products operation error') + ': ' + error);
- $('.studiou-wcpcm-delete-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- });
- }
- /**
- * Process deletion in batches
- */
- function processDeletionBatches(productIds, totalCount, $submitBtn, $spinner) {
- var batchSize = 10; // Process 10 products at a time
- var batches = [];
- var totalDeleted = 0;
- var totalFailed = 0;
- var failedProducts = [];
- // Split products into batches
- for (var i = 0; i < productIds.length; i += batchSize) {
- batches.push(productIds.slice(i, i + batchSize));
- }
- console.log('STUDIOU WC: Processing ' + batches.length + ' batches of products');
- var currentBatch = 0;
- function processBatch() {
- if (currentBatch >= batches.length) {
- // All batches completed
- console.log('STUDIOU WC: All batches completed');
- onDeletionComplete(totalDeleted, totalFailed, failedProducts, $submitBtn, $spinner);
- return;
- }
- var batch = batches[currentBatch];
- var progress = Math.round((currentBatch / batches.length) * 100);
- $('#studiou-wcpcm-delete-progress-text').text(
- 'Deleting products... ' + (currentBatch * batchSize) + ' of ' + totalCount + ' processed'
- );
- updateProgressBar(progress);
- // Send batch delete request
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_delete_products_batch',
- product_ids: batch,
- nonce: studiouWcpcm.nonce
- },
- success: function(response) {
- if (response.success) {
- totalDeleted += response.data.deleted_count;
- totalFailed += response.data.failed_count;
- failedProducts = failedProducts.concat(response.data.failed_products);
- console.log('STUDIOU WC: Batch ' + currentBatch + ' completed - Deleted: ' +
- response.data.deleted_count + ', Failed: ' + response.data.failed_count);
- // Process next batch
- currentBatch++;
- processBatch();
- } else {
- showNotice('error', response.data.message);
- $('.studiou-wcpcm-delete-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: Batch delete AJAX error:', status, error);
- showNotice('error', 'Error deleting batch: ' + error);
- $('.studiou-wcpcm-delete-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- }
- // Start processing batches
- processBatch();
- }
- /**
- * Handle deletion completion
- */
- function onDeletionComplete(totalDeleted, totalFailed, failedProducts, $submitBtn, $spinner) {
- // Update progress bar to 100%
- updateProgressBar(100);
- $('#studiou-wcpcm-delete-progress-text').text('Deletion complete!');
- // Show results
- var message = '<p><strong>Deletion Summary:</strong></p>';
- message += '<ul>';
- message += '<li>Total products deleted: ' + totalDeleted + '</li>';
- if (totalFailed > 0) {
- message += '<li>Failed to delete: ' + totalFailed + '</li>';
- if (failedProducts.length > 0) {
- message += '<li>Failed product IDs: ' + failedProducts.join(', ') + '</li>';
- }
- }
- message += '</ul>';
- $('#studiou-wcpcm-delete-message').html(message);
- $('.studiou-wcpcm-delete-results').show();
- // Hide progress after a delay
- setTimeout(function() {
- $('.studiou-wcpcm-delete-progress').fadeOut();
- }, 2000);
- if (totalDeleted > 0) {
- showNotice('success', (studiouWcpcm.i18n.deleteSuccess || 'Successfully deleted') + ' ' + totalDeleted + ' products');
- // Reload page after 3 seconds
- setTimeout(function() {
- location.reload();
- }, 3000);
- } else {
- showNotice('error', studiouWcpcm.i18n.deleteError || 'Failed to delete any products');
- }
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- /**
- * Update progress bar
- */
- function updateProgressBar(percentage) {
- $('#studiou-wcpcm-delete-progress-bar').css('width', percentage + '%');
- $('#studiou-wcpcm-delete-progress-bar').attr('data-progress', percentage + '%');
- }
- /**
- * Initialize clear media categories form
- */
- function initClearMediaForm() {
- console.log('STUDIOU WC: Initializing clear media form handlers');
- // Check all button handler
- $('#studiou-wcpcm-clear-media-check-all').on('click', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Clear media check all button clicked');
- $('#clear_media_categories_list input[type="checkbox"]').prop('checked', true);
- });
- // Uncheck all button handler
- $('#studiou-wcpcm-clear-media-uncheck-all').on('click', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Clear media uncheck all button clicked');
- $('#clear_media_categories_list input[type="checkbox"]').prop('checked', false);
- });
- // Form submit handler
- $('#studiou-wcpcm-clear-media-form').on('submit', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Clear media form submitted');
- // Get selected categories
- var selectedCategories = [];
- $('#clear_media_categories_list input[type="checkbox"]:checked').each(function() {
- selectedCategories.push($(this).val());
- });
- console.log('STUDIOU WC: Selected media categories:', selectedCategories);
- // Validate input
- if (selectedCategories.length === 0) {
- console.log('STUDIOU WC: No media categories selected');
- showNotice('error', studiouWcpcm.i18n.selectAtLeastOneCategory || 'Please select at least one category');
- return;
- }
- // Confirm deletion
- if (!confirm(studiouWcpcm.i18n.confirmClearMedia || 'Are you sure you want to permanently delete ALL media files in the selected media categories? This action cannot be undone!')) {
- console.log('STUDIOU WC: User cancelled clear media');
- return;
- }
- // Show spinner
- var $submitBtn = $(this).find('#studiou-wcpcm-clear-media-button');
- var $spinner = $(this).find('.spinner');
- $submitBtn.prop('disabled', true);
- $spinner.css('visibility', 'visible');
- // Clear previous notices
- $('.studiou-wcpcm-notice-area').empty();
- // Hide results, show progress
- $('.studiou-wcpcm-clear-media-results').hide();
- $('.studiou-wcpcm-clear-media-progress').show();
- $('#studiou-wcpcm-clear-media-progress-text').text('Initializing...');
- updateClearMediaProgressBar(0);
- // Send AJAX request to get media IDs
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_clear_media',
- media_categories: selectedCategories,
- nonce: studiouWcpcm.nonce
- },
- success: function(response) {
- console.log('STUDIOU WC: Clear media AJAX response:', response);
- if (response.success) {
- var mediaIds = response.data.media_ids;
- var totalCount = response.data.total_count;
- console.log('STUDIOU WC: Total media files to delete:', totalCount);
- if (totalCount === 0) {
- showNotice('success', studiouWcpcm.i18n.noMediaFound || 'No media files found in selected categories');
- $('.studiou-wcpcm-clear-media-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- return;
- }
- // Process media in batches
- processMediaDeletionBatches(mediaIds, totalCount, $submitBtn, $spinner);
- } else {
- showNotice('error', response.data.message);
- $('.studiou-wcpcm-clear-media-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: Clear media AJAX error:', status, error);
- console.error('STUDIOU WC: Response text:', xhr.responseText);
- showNotice('error', (studiouWcpcm.i18n.clearMediaError || 'Clear media operation error') + ': ' + error);
- $('.studiou-wcpcm-clear-media-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- });
- }
- /**
- * Process media deletion in batches
- */
- function processMediaDeletionBatches(mediaIds, totalCount, $submitBtn, $spinner) {
- var batchSize = 10;
- var batches = [];
- var totalDeleted = 0;
- var totalFailed = 0;
- var failedMedia = [];
- // Split media into batches
- for (var i = 0; i < mediaIds.length; i += batchSize) {
- batches.push(mediaIds.slice(i, i + batchSize));
- }
- console.log('STUDIOU WC: Processing ' + batches.length + ' batches of media files');
- var currentBatch = 0;
- function processBatch() {
- if (currentBatch >= batches.length) {
- console.log('STUDIOU WC: All media batches completed');
- onMediaDeletionComplete(totalDeleted, totalFailed, failedMedia, $submitBtn, $spinner);
- return;
- }
- var batch = batches[currentBatch];
- var progress = Math.round((currentBatch / batches.length) * 100);
- $('#studiou-wcpcm-clear-media-progress-text').text(
- 'Deleting media files... ' + (currentBatch * batchSize) + ' of ' + totalCount + ' processed'
- );
- updateClearMediaProgressBar(progress);
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_clear_media_batch',
- media_ids: batch,
- nonce: studiouWcpcm.nonce
- },
- success: function(response) {
- if (response.success) {
- totalDeleted += response.data.deleted_count;
- totalFailed += response.data.failed_count;
- failedMedia = failedMedia.concat(response.data.failed_media);
- console.log('STUDIOU WC: Media batch ' + currentBatch + ' completed - Deleted: ' +
- response.data.deleted_count + ', Failed: ' + response.data.failed_count);
- currentBatch++;
- processBatch();
- } else {
- showNotice('error', response.data.message);
- $('.studiou-wcpcm-clear-media-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: Media batch delete AJAX error:', status, error);
- showNotice('error', 'Error deleting media batch: ' + error);
- $('.studiou-wcpcm-clear-media-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- }
- processBatch();
- }
- /**
- * Handle media deletion completion
- */
- function onMediaDeletionComplete(totalDeleted, totalFailed, failedMedia, $submitBtn, $spinner) {
- updateClearMediaProgressBar(100);
- $('#studiou-wcpcm-clear-media-progress-text').text('Deletion complete!');
- var message = '<p><strong>Deletion Summary:</strong></p>';
- message += '<ul>';
- message += '<li>Total media files deleted: ' + totalDeleted + '</li>';
- if (totalFailed > 0) {
- message += '<li>Failed to delete: ' + totalFailed + '</li>';
- if (failedMedia.length > 0) {
- message += '<li>Failed media IDs: ' + failedMedia.join(', ') + '</li>';
- }
- }
- message += '</ul>';
- $('#studiou-wcpcm-clear-media-message').html(message);
- $('.studiou-wcpcm-clear-media-results').show();
- setTimeout(function() {
- $('.studiou-wcpcm-clear-media-progress').fadeOut();
- }, 2000);
- if (totalDeleted > 0) {
- showNotice('success', (studiouWcpcm.i18n.clearMediaSuccess || 'Successfully deleted') + ' ' + totalDeleted + ' media files');
- setTimeout(function() {
- location.reload();
- }, 3000);
- } else {
- showNotice('error', studiouWcpcm.i18n.clearMediaError || 'Failed to delete any media files');
- }
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- /**
- * Update clear media progress bar
- */
- function updateClearMediaProgressBar(percentage) {
- $('#studiou-wcpcm-clear-media-progress-bar').css('width', percentage + '%');
- $('#studiou-wcpcm-clear-media-progress-bar').attr('data-progress', percentage + '%');
- }
- /**
- * Initialize remove unassigned media form
- */
- function initRemoveUnassignedMediaForm() {
- console.log('STUDIOU WC: Initializing remove unassigned media form handlers');
- $('#studiou-wcpcm-remove-unassigned-media-form').on('submit', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Remove unassigned media form submitted');
- if (!confirm(studiouWcpcm.i18n.confirmRemoveUnassigned || 'Are you sure you want to permanently delete ALL unused uncategorized media files? This action cannot be undone!')) {
- return;
- }
- var $submitBtn = $(this).find('#studiou-wcpcm-remove-unassigned-media-button');
- var $spinner = $(this).find('.spinner');
- $submitBtn.prop('disabled', true);
- $spinner.css('visibility', 'visible');
- $('.studiou-wcpcm-notice-area').empty();
- $('.studiou-wcpcm-remove-unassigned-media-results').hide();
- $('.studiou-wcpcm-remove-unassigned-media-progress').show();
- $('#studiou-wcpcm-remove-unassigned-media-progress-text').text('Initializing...');
- updateRemoveUnassignedProgressBar(0);
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_remove_unassigned_media',
- nonce: studiouWcpcm.nonce
- },
- success: function(response) {
- console.log('STUDIOU WC: Remove unassigned media response:', response);
- if (response.success) {
- var mediaIds = response.data.media_ids;
- var totalCount = response.data.total_count;
- if (totalCount === 0) {
- showNotice('success', studiouWcpcm.i18n.noUnassignedMedia || 'No unused uncategorized media files found');
- $('.studiou-wcpcm-remove-unassigned-media-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- return;
- }
- processUnassignedMediaBatches(mediaIds, totalCount, $submitBtn, $spinner);
- } else {
- showNotice('error', response.data.message);
- $('.studiou-wcpcm-remove-unassigned-media-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: Remove unassigned media error:', status, error);
- showNotice('error', (studiouWcpcm.i18n.removeUnassignedError || 'Error') + ': ' + error);
- $('.studiou-wcpcm-remove-unassigned-media-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- });
- }
- /**
- * Process unassigned media deletion in batches
- */
- function processUnassignedMediaBatches(mediaIds, totalCount, $submitBtn, $spinner) {
- var batchSize = 10;
- var batches = [];
- var totalDeleted = 0;
- var totalFailed = 0;
- var failedMedia = [];
- for (var i = 0; i < mediaIds.length; i += batchSize) {
- batches.push(mediaIds.slice(i, i + batchSize));
- }
- var currentBatch = 0;
- function processBatch() {
- if (currentBatch >= batches.length) {
- onUnassignedMediaComplete(totalDeleted, totalFailed, failedMedia, $submitBtn, $spinner);
- return;
- }
- var batch = batches[currentBatch];
- var progress = Math.round((currentBatch / batches.length) * 100);
- $('#studiou-wcpcm-remove-unassigned-media-progress-text').text(
- 'Deleting media files... ' + (currentBatch * batchSize) + ' of ' + totalCount + ' processed'
- );
- updateRemoveUnassignedProgressBar(progress);
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_remove_unassigned_media_batch',
- media_ids: batch,
- nonce: studiouWcpcm.nonce
- },
- success: function(response) {
- if (response.success) {
- totalDeleted += response.data.deleted_count;
- totalFailed += response.data.failed_count;
- failedMedia = failedMedia.concat(response.data.failed_media);
- currentBatch++;
- processBatch();
- } else {
- showNotice('error', response.data.message);
- $('.studiou-wcpcm-remove-unassigned-media-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: Unassigned media batch error:', status, error);
- showNotice('error', 'Error deleting batch: ' + error);
- $('.studiou-wcpcm-remove-unassigned-media-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- }
- processBatch();
- }
- /**
- * Handle unassigned media deletion completion
- */
- function onUnassignedMediaComplete(totalDeleted, totalFailed, failedMedia, $submitBtn, $spinner) {
- updateRemoveUnassignedProgressBar(100);
- $('#studiou-wcpcm-remove-unassigned-media-progress-text').text('Deletion complete!');
- var message = '<p><strong>Deletion Summary:</strong></p>';
- message += '<ul>';
- message += '<li>Total media files deleted: ' + totalDeleted + '</li>';
- if (totalFailed > 0) {
- message += '<li>Failed to delete: ' + totalFailed + '</li>';
- if (failedMedia.length > 0) {
- message += '<li>Failed media IDs: ' + failedMedia.join(', ') + '</li>';
- }
- }
- message += '</ul>';
- $('#studiou-wcpcm-remove-unassigned-media-message').html(message);
- $('.studiou-wcpcm-remove-unassigned-media-results').show();
- setTimeout(function() {
- $('.studiou-wcpcm-remove-unassigned-media-progress').fadeOut();
- }, 2000);
- if (totalDeleted > 0) {
- showNotice('success', (studiouWcpcm.i18n.removeUnassignedSuccess || 'Successfully deleted') + ' ' + totalDeleted + ' media files');
- setTimeout(function() {
- location.reload();
- }, 3000);
- } else {
- showNotice('error', studiouWcpcm.i18n.removeUnassignedError || 'Failed to delete any media files');
- }
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- /**
- * Update remove unassigned media progress bar
- */
- function updateRemoveUnassignedProgressBar(percentage) {
- $('#studiou-wcpcm-remove-unassigned-media-progress-bar').css('width', percentage + '%');
- $('#studiou-wcpcm-remove-unassigned-media-progress-bar').attr('data-progress', percentage + '%');
- }
- /**
- * Show notice message
- *
- * @param {string} type Notice type (success, error)
- * @param {string} message Notice message
- */
- function showNotice(type, message) {
- var $notice = $('<div class="studiou-wcpcm-notice studiou-wcpcm-notice-' + type + '"></div>').text(message);
- $('.studiou-wcpcm-notice-area').append($notice);
- // Scroll to notice
- $('html, body').animate({
- scrollTop: $('.studiou-wcpcm-notice-area').offset().top - 50
- }, 500);
- }
- /**
- * Initialize product import form
- */
- function initProductImportForm() {
- console.log('STUDIOU WC: Initializing product import form handlers');
- // Global variables for batch processing
- var importTransientKey = null;
- var importTotal = 0;
- var importProcessed = 0;
- var importSuccess = 0;
- var importFailed = 0;
- var importSkipped = 0;
- var importMessages = [];
- var importFailedCsv = '';
- $('#studiou-wcpcm-product-import-form').on('submit', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Product import form submitted');
- // Validate file is selected
- var fileInput = $('#import_file')[0];
- if (!fileInput.files || !fileInput.files[0]) {
- showNotice('error', 'Please select a CSV file to import');
- return;
- }
- // Show spinner and progress
- var $submitBtn = $(this).find('#studiou-wcpcm-product-import-button');
- var $spinner = $(this).find('.spinner');
- $submitBtn.prop('disabled', true);
- $spinner.css('visibility', 'visible');
- // Clear previous results
- $('.studiou-wcpcm-notice-area').empty();
- $('.studiou-wcpcm-product-import-results').hide();
- $('.studiou-wcpcm-product-import-failed').hide();
- // Reset counters
- importTransientKey = null;
- importTotal = 0;
- importProcessed = 0;
- importSuccess = 0;
- importFailed = 0;
- importSkipped = 0;
- importMessages = [];
- importFailedCsv = '';
- // Show progress bar
- $('.studiou-wcpcm-product-import-progress').show();
- $('#studiou-wcpcm-product-import-progress-text').text('Parsing CSV file...');
- updateProductImportProgressBar(0);
- // Step 1: Parse CSV file
- var formData = new FormData(this);
- formData.append('action', 'studiou_wcpcm_product_import_parse');
- formData.append('nonce', studiouWcpcm.nonce);
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: formData,
- processData: false,
- contentType: false,
- success: function(response) {
- console.log('STUDIOU WC: Product import parse response:', response);
- if (response.success) {
- importTransientKey = response.data.transient_key;
- importTotal = response.data.total;
- // Start batch processing
- processBatch(0, $submitBtn, $spinner);
- } else {
- showNotice('error', response.data.message || 'Parse failed');
- $('.studiou-wcpcm-product-import-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: Product import parse error:', status, error);
- console.error('STUDIOU WC: Response text:', xhr.responseText);
- showNotice('error', 'Product import error: ' + error);
- $('.studiou-wcpcm-product-import-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- });
- // Process batch of products
- function processBatch(offset, $submitBtn, $spinner) {
- console.log('STUDIOU WC: Processing batch at offset', offset);
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_product_import_batch',
- nonce: studiouWcpcm.nonce,
- transient_key: importTransientKey,
- offset: offset
- },
- success: function(response) {
- console.log('STUDIOU WC: Batch response:', response);
- if (response.success) {
- // Accumulate results
- importSuccess += response.data.success;
- importFailed += response.data.failed;
- importSkipped += response.data.skipped;
- importProcessed = response.data.processed;
- importTotal = response.data.total;
- importMessages = importMessages.concat(response.data.messages);
- if (response.data.failed_csv) {
- importFailedCsv = response.data.failed_csv;
- }
- // Update progress
- var percent = (importProcessed / importTotal) * 100;
- updateProductImportProgressBar(percent);
- $('#studiou-wcpcm-product-import-progress-text').text(
- 'Processing: ' + importProcessed + ' / ' + importTotal + ' products'
- );
- // Check if complete
- if (importProcessed >= importTotal) {
- // Import complete
- $('#studiou-wcpcm-product-import-progress-text').text('Complete!');
- showNotice('success', 'Product import completed!');
- // Show results
- var message = '<p><strong>Import Summary:</strong></p>';
- message += '<ul>';
- message += '<li>Successful: ' + importSuccess + '</li>';
- message += '<li>Failed: ' + importFailed + '</li>';
- message += '<li>Skipped: ' + importSkipped + '</li>';
- message += '</ul>';
- $('#studiou-wcpcm-product-import-message').html(message);
- // Show detailed messages
- if (importMessages.length > 0) {
- var details = '<pre>' + importMessages.join('\n') + '</pre>';
- $('#studiou-wcpcm-product-import-details-content').html(details);
- }
- $('.studiou-wcpcm-product-import-results').show();
- // Show save failed button if there are failed items
- if (importFailed > 0 && importFailedCsv) {
- window.studiouWcpcmFailedCsv = importFailedCsv;
- $('.studiou-wcpcm-product-import-failed').show();
- }
- // Hide progress after delay
- setTimeout(function() {
- $('.studiou-wcpcm-product-import-progress').fadeOut();
- }, 2000);
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- } else {
- // Process next batch
- processBatch(importProcessed, $submitBtn, $spinner);
- }
- } else {
- showNotice('error', response.data.message || 'Batch processing failed');
- $('.studiou-wcpcm-product-import-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: Batch processing error:', status, error);
- showNotice('error', 'Batch processing error: ' + error);
- $('.studiou-wcpcm-product-import-progress').hide();
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- }
- // Save failed products button handler
- $('#studiou-wcpcm-save-failed-products').on('click', function(e) {
- e.preventDefault();
- if (window.studiouWcpcmFailedCsv) {
- var blob = new Blob([window.studiouWcpcmFailedCsv], { type: 'text/csv;charset=utf-8;' });
- var url = URL.createObjectURL(blob);
- var link = document.createElement('a');
- link.setAttribute('href', url);
- link.setAttribute('download', 'failed-products-' + Date.now() + '.csv');
- link.style.visibility = 'hidden';
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- }
- });
- }
- /**
- * Initialize product export form
- */
- function initProductExportForm() {
- console.log('STUDIOU WC: Initializing product export form handlers');
- // Select all button handler
- $('#studiou-wcpcm-export-select-all').on('click', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Export select all button clicked');
- $('#export_categories_list input[type="checkbox"]').prop('checked', true);
- });
- // Deselect all button handler
- $('#studiou-wcpcm-export-deselect-all').on('click', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Export deselect all button clicked');
- $('#export_categories_list input[type="checkbox"]').prop('checked', false);
- });
- // Form submit handler
- $('#studiou-wcpcm-product-export-form').on('submit', function(e) {
- e.preventDefault();
- console.log('STUDIOU WC: Product export form submitted');
- // Get selected categories
- var selectedCategories = [];
- $('#export_categories_list input[type="checkbox"]:checked').each(function() {
- selectedCategories.push($(this).val());
- });
- console.log('STUDIOU WC: Selected categories:', selectedCategories);
- // Validate input
- if (selectedCategories.length === 0) {
- console.log('STUDIOU WC: No categories selected');
- showNotice('error', 'Please select at least one category');
- return;
- }
- // Show spinner
- var $submitBtn = $(this).find('#studiou-wcpcm-product-export-button');
- var $spinner = $(this).find('.spinner');
- $submitBtn.prop('disabled', true);
- $spinner.css('visibility', 'visible');
- // Clear previous notices
- $('.studiou-wcpcm-notice-area').empty();
- $('.studiou-wcpcm-product-export-results').hide();
- // Debug: Log AJAX request details
- console.log('STUDIOU WC: Sending product export AJAX request to:', studiouWcpcm.ajaxUrl);
- console.log('STUDIOU WC: Nonce:', studiouWcpcm.nonce);
- // Send AJAX request
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_product_export',
- export_categories: selectedCategories,
- nonce: studiouWcpcm.nonce
- },
- success: function(response) {
- console.log('STUDIOU WC: Product export AJAX response:', response);
- if (response.success) {
- showNotice('success', 'Export successful!');
- // Show results
- $('#studiou-wcpcm-product-export-message').html('<p>' + response.data.message + '</p>');
- $('#studiou-wcpcm-download-product-export').attr('href', response.data.download_url);
- $('.studiou-wcpcm-product-export-results').show();
- } else {
- showNotice('error', response.data.message || 'Export failed');
- }
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC: Product export AJAX error:', status, error);
- console.error('STUDIOU WC: Response text:', xhr.responseText);
- showNotice('error', 'Product export error: ' + error);
- },
- complete: function() {
- // Hide spinner
- $submitBtn.prop('disabled', false);
- $spinner.css('visibility', 'hidden');
- }
- });
- });
- }
- /**
- * Update product import progress bar
- */
- function updateProductImportProgressBar(percentage) {
- $('#studiou-wcpcm-product-import-progress-bar').css('width', percentage + '%');
- $('#studiou-wcpcm-product-import-progress-bar').attr('data-progress', Math.round(percentage) + '%');
- }
- // Additional debug: Check page state after DOM load
- $(window).on('load', function() {
- console.log('STUDIOU WC: Window loaded');
- console.log('STUDIOU WC: Manipulation tabs exist:', $('.studiou-wcpcm-tabs').length > 0);
- console.log('STUDIOU WC: Product import form exists:', $('#studiou-wcpcm-product-import-form').length > 0);
- console.log('STUDIOU WC: Product export form exists:', $('#studiou-wcpcm-product-export-form').length > 0);
- });
- /**
- * Initialize Update Product Variant Prices form (UPVP) — v1.5.3
- */
- function initUpdateVariantPricesForm() {
- var $form = $('#studiou-wcpcm-upvp-form');
- var $catList = $('#upvp_categories_list');
- var $attrSelect = $('#upvp_attribute');
- var $valueSelect = $('#upvp_attribute_value');
- var $newPrice = $('#upvp_new_price');
- var $countLabel = $('#studiou-wcpcm-upvp-affected-count');
- var $dryRunBtn = $('#studiou-wcpcm-upvp-dry-run-button');
- var $applyBtn = $('#studiou-wcpcm-upvp-apply-button');
- var $spinner = $form.find('.spinner');
- var $resultsBox = $('.studiou-wcpcm-upvp-results');
- var $resultsMsg = $('#studiou-wcpcm-upvp-message');
- var i18n = (studiouWcpcm && studiouWcpcm.i18n) ? studiouWcpcm.i18n : {};
- var countDebounceHandle = null;
- var countInFlight = false;
- function getSelectedCategoryIds() {
- var ids = [];
- $catList.find('input[type="checkbox"]:checked').each(function() {
- ids.push($(this).val());
- });
- return ids;
- }
- function setApplyEnabled(count) {
- var enabled = (count > 0) && !countInFlight;
- $dryRunBtn.prop('disabled', !enabled);
- $applyBtn.prop('disabled', !enabled);
- }
- function loadAttributes() {
- $attrSelect.prop('disabled', true).html(
- '<option value="">' + (i18n.upvpLoadingAttributes || 'Loading attributes...') + '</option>'
- );
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_upvp_get_attributes',
- nonce: studiouWcpcm.nonce
- },
- success: function(response) {
- $attrSelect.empty();
- $attrSelect.append('<option value="">' + (i18n.upvpSelectAttribute || '-- Select attribute --') + '</option>');
- if (response.success && response.data && response.data.attributes) {
- $.each(response.data.attributes, function(_, a) {
- $attrSelect.append(
- $('<option/>')
- .val(a.key)
- .attr('data-istax', a.is_taxonomy ? '1' : '0')
- .text(a.label)
- );
- });
- }
- $attrSelect.prop('disabled', false);
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC UPVP: get_attributes failed', status, error);
- $attrSelect.prop('disabled', false);
- }
- });
- }
- function loadValues() {
- var key = $attrSelect.val();
- $valueSelect.prop('disabled', true).empty();
- $valueSelect.append('<option value="">' + (i18n.upvpLoadingValues || 'Loading values...') + '</option>');
- if (!key) {
- $valueSelect.empty();
- $valueSelect.append('<option value="">' + (i18n.upvpSelectValue || '-- Select value --') + '</option>');
- $valueSelect.prop('disabled', true);
- refreshCount();
- return;
- }
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_upvp_get_attribute_values',
- nonce: studiouWcpcm.nonce,
- attribute_key: key,
- category_ids: getSelectedCategoryIds()
- },
- success: function(response) {
- $valueSelect.empty();
- $valueSelect.append('<option value="">' + (i18n.upvpSelectValue || '-- Select value --') + '</option>');
- if (response.success && response.data && response.data.values) {
- $.each(response.data.values, function(_, v) {
- $valueSelect.append($('<option/>').val(v.value).text(v.label));
- });
- }
- $valueSelect.prop('disabled', false);
- refreshCount();
- },
- error: function(xhr, status, error) {
- console.error('STUDIOU WC UPVP: get_attribute_values failed', status, error);
- $valueSelect.empty();
- $valueSelect.append('<option value="">' + (i18n.upvpSelectValue || '-- Select value --') + '</option>');
- $valueSelect.prop('disabled', false);
- }
- });
- }
- function refreshCount() {
- if (countDebounceHandle) {
- clearTimeout(countDebounceHandle);
- }
- countDebounceHandle = setTimeout(doRefreshCount, 300);
- }
- function doRefreshCount() {
- var cats = getSelectedCategoryIds();
- var key = $attrSelect.val();
- var val = $valueSelect.val();
- if (!cats.length || !key || !val) {
- $countLabel.text('0');
- setApplyEnabled(0);
- return;
- }
- countInFlight = true;
- setApplyEnabled(0);
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_upvp_count',
- nonce: studiouWcpcm.nonce,
- category_ids: cats,
- attribute_key: key,
- attribute_value: val
- },
- success: function(response) {
- countInFlight = false;
- if (response.success && response.data) {
- var c = parseInt(response.data.count, 10) || 0;
- $countLabel.text(c);
- setApplyEnabled(c);
- } else {
- $countLabel.text('0');
- setApplyEnabled(0);
- }
- },
- error: function() {
- countInFlight = false;
- $countLabel.text('0');
- setApplyEnabled(0);
- }
- });
- }
- function renderSampleTable(sample, newPriceLabel) {
- if (!sample || !sample.length) return '';
- var html = '<table class="studiou-wcpcm-upvp-preview-table">';
- html += '<thead><tr>';
- html += '<th>ID</th><th>SKU</th><th>Name</th><th>Parent</th><th>Current</th>';
- if (newPriceLabel !== undefined) html += '<th>New</th>';
- html += '</tr></thead><tbody>';
- $.each(sample, function(_, r) {
- html += '<tr>';
- html += '<td>' + r.id + '</td>';
- html += '<td>' + (r.sku || '') + '</td>';
- html += '<td>' + (r.name || '') + '</td>';
- html += '<td>' + (r.parent_name || '') + '</td>';
- html += '<td>' + (r.current_price || '') + '</td>';
- if (newPriceLabel !== undefined) html += '<td>' + newPriceLabel + '</td>';
- html += '</tr>';
- });
- html += '</tbody></table>';
- return html;
- }
- // Wire up Check All / Uncheck All
- $('#studiou-wcpcm-upvp-check-all').on('click', function(e) {
- e.preventDefault();
- $catList.find('input[type="checkbox"]').prop('checked', true);
- refreshCount();
- });
- $('#studiou-wcpcm-upvp-uncheck-all').on('click', function(e) {
- e.preventDefault();
- $catList.find('input[type="checkbox"]').prop('checked', false);
- refreshCount();
- });
- $catList.on('change', 'input[type="checkbox"]', refreshCount);
- $attrSelect.on('change', loadValues);
- $valueSelect.on('change', refreshCount);
- // Dry Run
- $dryRunBtn.on('click', function(e) {
- e.preventDefault();
- var cats = getSelectedCategoryIds();
- var key = $attrSelect.val();
- var val = $valueSelect.val();
- if (!cats.length) {
- showNotice('error', i18n.selectAtLeastOneCategory || 'Please select at least one category');
- return;
- }
- if (!key) {
- showNotice('error', i18n.upvpSelectAttribute || 'Please select an attribute');
- return;
- }
- if (!val) {
- showNotice('error', i18n.upvpSelectValue || 'Please select an attribute value');
- return;
- }
- $('.studiou-wcpcm-notice-area').empty();
- $resultsBox.hide();
- $spinner.css('visibility', 'visible');
- $dryRunBtn.prop('disabled', true);
- $applyBtn.prop('disabled', true);
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_upvp_count',
- nonce: studiouWcpcm.nonce,
- category_ids: cats,
- attribute_key: key,
- attribute_value: val
- },
- success: function(response) {
- if (response.success && response.data) {
- var c = parseInt(response.data.count, 10) || 0;
- var sample = response.data.sample || [];
- var price = $newPrice.val();
- var msg = '';
- if (c === 0) {
- msg = '<p>' + (i18n.upvpNoMatches || 'No variations match the current filter.') + '</p>';
- } else {
- var tpl = i18n.upvpDryRunComplete || 'Dry run complete: %d variations would be updated to %s.';
- msg = '<p>' + tpl.replace('%d', c).replace('%s', price !== '' ? price : '—') + '</p>';
- msg += renderSampleTable(sample, price !== '' ? price : undefined);
- }
- $resultsMsg.html(msg);
- $resultsBox.show();
- $countLabel.text(c);
- setApplyEnabled(c);
- } else {
- showNotice('error', (response.data && response.data.message) || 'Dry run failed');
- }
- },
- error: function(xhr, status, error) {
- showNotice('error', 'Dry run error: ' + error);
- },
- complete: function() {
- $spinner.css('visibility', 'hidden');
- }
- });
- });
- // Apply
- $applyBtn.on('click', function(e) {
- e.preventDefault();
- var cats = getSelectedCategoryIds();
- var key = $attrSelect.val();
- var val = $valueSelect.val();
- var price = $newPrice.val();
- var count = parseInt($countLabel.text(), 10) || 0;
- if (!cats.length) {
- showNotice('error', i18n.selectAtLeastOneCategory || 'Please select at least one category');
- return;
- }
- if (!key) {
- showNotice('error', i18n.upvpSelectAttribute || 'Please select an attribute');
- return;
- }
- if (!val) {
- showNotice('error', i18n.upvpSelectValue || 'Please select an attribute value');
- return;
- }
- if (price === '' || isNaN(parseFloat(price)) || parseFloat(price) < 0) {
- showNotice('error', i18n.upvpInvalidPrice || 'Please enter a valid new price');
- return;
- }
- var confirmTpl = i18n.upvpConfirmApply || 'Are you sure you want to update %d variations?';
- if (!confirm(confirmTpl.replace('%d', count))) {
- return;
- }
- $('.studiou-wcpcm-notice-area').empty();
- $resultsBox.hide();
- $spinner.css('visibility', 'visible');
- $dryRunBtn.prop('disabled', true);
- $applyBtn.prop('disabled', true);
- $.ajax({
- url: studiouWcpcm.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcpcm_upvp_apply',
- nonce: studiouWcpcm.nonce,
- category_ids: cats,
- attribute_key: key,
- attribute_value: val,
- new_price: price
- },
- success: function(response) {
- if (response.success && response.data) {
- showNotice('success', i18n.upvpApplySuccess || 'Variant prices updated successfully');
- var html = '<p><strong>' + (response.data.message || '') + '</strong></p>';
- if (response.data.failed_ids && response.data.failed_ids.length) {
- html += '<p>Failed IDs: ' + response.data.failed_ids.join(', ') + '</p>';
- }
- $resultsMsg.html(html);
- $resultsBox.show();
- refreshCount();
- } else {
- showNotice('error', (response.data && response.data.message) || (i18n.upvpApplyError || 'Variant prices update error'));
- }
- },
- error: function(xhr, status, error) {
- showNotice('error', (i18n.upvpApplyError || 'Variant prices update error') + ': ' + error);
- },
- complete: function() {
- $spinner.css('visibility', 'hidden');
- $dryRunBtn.prop('disabled', false);
- $applyBtn.prop('disabled', false);
- }
- });
- });
- // Initial load
- loadAttributes();
- setApplyEnabled(0);
- }
- })(jQuery);
|