| 1 |
jQuery(document).ready(function(t){"use strict";var e=!1;function r(e){return t("<div>").text(e).html()}function s(t,e,r){var s=r>0?Math.min(Math.round(e/r*100),100):5;t.find(".import-progress-bar div").css("width",s+"%"),t.find(".import-percentage").text(s+"%")}function o(r,p,a,c){if(e)return t("#top-ten-wpp-import-cancel").hide(),void c.prop("disabled",!1);if(r.currentSiteIndex>=r.sites.length&&r.sites.length>0)i(r,p,a,c);else{var d=r.sites.length?r.sites[r.currentSiteIndex]:0;p.find(".import-status").html((r.sites.length>1?topTenWPPImporter.strings.processing_site+" "+d+" - ":"")+topTenWPPImporter.strings.batch+" "+r.batch_number+(r.total_batches>0?" "+topTenWPPImporter.strings.of+" "+r.total_batches:""));var m=r.currentSiteIndex,l=r.sites.length,_=r.batch_number,u=r.total_batches;if(u>0)s(p,100*((m*u+_)/(l*u)),100);else s(p,m+1,l);t.ajax({url:topTenWPPImporter.ajaxurl,type:"POST",timeout:6e4,data:{action:"top_ten_import_wpp",nonce:topTenWPPImporter.nonce,import_mode:r.import_mode,import_data:r.import_data,min_views:r.min_views,dry_run:r.dry_run,batch:!0,blog_id:d,batch_number:r.batch_number,is_network_admin:r.is_network_admin,sites:r.sites},success:function(e){if(e.success){if(e.data.batch_status){r.results[d]||(r.results[d]={posts_processed:0,total_counts:0,total_views_imported:0,daily_counts:0,daily_views_imported:0,errors:[]}),r.total_batches=e.data.total_batches||r.total_batches;var m=r.currentSiteIndex,l=r.sites.length,_=r.batch_number,u=r.total_batches;if(u>0)s(p,100*((m*u+_)/(l*u)),100);if(e.data.batch_results){var g=e.data.batch_results;r.results[d].posts_processed+=parseInt(g.posts_processed||0),r.results[d].total_counts+=parseInt(g.total_counts||0),r.results[d].total_views_imported+=parseInt(g.total_views_imported||0),r.results[d].daily_counts+=parseInt(g.daily_counts||0),r.results[d].daily_views_imported+=parseInt(g.daily_views_imported||0),g.errors&&g.errors.length&&(r.results[d].errors=r.results[d].errors.concat(g.errors))}return e.data.has_more_batches?(r.batch_number++,void o(r,p,a,c)):(r.currentSiteIndex++,r.batch_number=1,void o(r,p,a,c))}if(e.data.results){var h=e.data.results;t.each(h,function(t,e){r.results[t]=e})}i(r,p,a,c)}else{n(e.data&&e.data.message?e.data.message:topTenWPPImporter.strings.unknown_error)}},error:function(t,e,r){"timeout"===e?n(topTenWPPImporter.strings.timeout_error):n(topTenWPPImporter.strings.server_error+" "+(t.responseJSON&&t.responseJSON.message?t.responseJSON.message:r))}})}}function n(e){var s=t("#top-ten-wpp-import-progress"),o=t("#top-ten-wpp-import-results"),n=t("#top-ten-wpp-import-submit"),i=t("#top-ten-wpp-import-cancel");s.removeClass("hidden notice-info notice-success").addClass("notice-error").html("<strong>"+topTenWPPImporter.strings.import_error+"</strong>"),o.removeClass("hidden notice-success").addClass("notice-error").html(topTenWPPImporter.strings.import_error+"<br><br>"+r(e)),n.prop("disabled",!1),i.hide()}function i(e,s,o,n){n.prop("disabled",!1),t("#top-ten-wpp-import-cancel").hide(),s.removeClass("notice-info").addClass("notice-success").removeClass("hidden").html("<strong>"+topTenWPPImporter.strings.import_complete+"</strong>"),o.removeClass("hidden notice-error").addClass("notice-success");var i="<br />",p=!!e.dry_run?topTenWPPImporter.strings.dry_run+" ":"",a=Object.keys(e.results).length;if(Object.keys(e.results).length>1)i+="<strong>"+p+topTenWPPImporter.strings.sites_processed+" "+a+"</strong><br><br>",t.each(e.results,function(e,s){i+="<strong>"+topTenWPPImporter.strings.blog_id+" "+e+":</strong><br>",i+=topTenWPPImporter.strings.posts_processed+" "+(s.posts_processed||0)+"<br>",i+=topTenWPPImporter.strings.total_records+" "+(s.total_counts||0)+"<br>",i+=topTenWPPImporter.strings.total_views_found+" "+(s.total_views_imported||0)+"<br>",i+=topTenWPPImporter.strings.daily_records+" "+(s.daily_counts||0)+"<br>",i+=topTenWPPImporter.strings.daily_views_found+" "+(s.daily_views_imported||0)+"<br>",s.errors&&s.errors.length&&(i+="<strong>"+topTenWPPImporter.strings.errors+"</strong><br>",t.each(s.errors,function(t,e){i+="- "+r(e)+"<br>"})),i+="<br>"});else{var c=Object.keys(e.results)[0]||0,d=e.results[c]||{posts_processed:0,total_counts:0,total_views_imported:0,daily_counts:0,daily_views_imported:0,errors:[]};i+="<strong>"+p+topTenWPPImporter.strings.results+"</strong><br>",i+=topTenWPPImporter.strings.posts_processed+" "+(d.posts_processed||0)+"<br>",i+=topTenWPPImporter.strings.total_records+" "+(d.total_counts||0)+"<br>",i+=topTenWPPImporter.strings.total_views_found+" "+(d.total_views_imported||0)+"<br>",i+=topTenWPPImporter.strings.daily_records+" "+(d.daily_counts||0)+"<br>",i+=topTenWPPImporter.strings.daily_views_found+" "+(d.daily_views_imported||0)+"<br>",d.errors&&d.errors.length&&(i+="<strong>"+topTenWPPImporter.strings.errors+"</strong><br>",t.each(d.errors,function(t,e){i+="- "+r(e)+"<br>"}))}o.html(i)}t("#top-ten-wpp-import-form").on("submit",function(r){if(r.preventDefault(),confirm(topTenWPPImporter.strings.confirm)){t(this);var s=t("#top-ten-wpp-import-submit"),n=t("#top-ten-wpp-import-progress"),i=t("#top-ten-wpp-import-results"),p=t("#top-ten-wpp-import-cancel");0===p.length&&(p=t("<button>",{id:"top-ten-wpp-import-cancel",class:"button",text:topTenWPPImporter.strings.cancel_button,css:{"margin-left":"10px"}}).insertAfter(s)).on("click",function(){return confirm(topTenWPPImporter.strings.cancel_confirm)&&(e=!0,n.removeClass("notice-info notice-success").addClass("notice-error").html("<strong>"+topTenWPPImporter.strings.import_cancelled+"</strong>"),s.prop("disabled",!1),t(this).hide()),!1}),e=!1,s.prop("disabled",!0),p.show(),n.removeClass("hidden notice-error notice-success").addClass("notice-info").html(topTenWPPImporter.strings.importing+' <span class="import-status">'+topTenWPPImporter.strings.starting+'</span><br><div class="import-progress-bar" style="background-color: #f5f5f5; border: 1px solid #ddd; height: 20px; margin: 10px 0;"><div style="width: 0%; height: 100%; background-color: #0073aa;"></div></div><span class="import-percentage">0%</span>'),i.addClass("hidden");var a=[],c=t('input[name="sites[]"]').length>0;if(c&&(t('input[name="sites[]"]:checked').each(function(){a.push(t(this).val())}),0===a.length))return n.removeClass("hidden notice-info notice-success").addClass("notice-error").html("<strong>"+topTenWPPImporter.strings.error+"</strong> "+topTenWPPImporter.strings.no_sites_selected),s.prop("disabled",!1),!1;var d=t('input[name="min_views"]').val();if(""!==d&&(!t.isNumeric(d)||parseInt(d)<0))return n.removeClass("hidden notice-info notice-success").addClass("notice-error").html("<strong>"+topTenWPPImporter.strings.error+"</strong> "+topTenWPPImporter.strings.invalid_min_views),s.prop("disabled",!1),!1;o({action:"top_ten_import_wpp",nonce:topTenWPPImporter.nonce,import_mode:t('input[name="import_mode"]:checked').val(),import_data:t('input[name="import_data"]:checked').val(),min_views:t('input[name="min_views"]').val(),dry_run:t('input[name="dry_run"]').is(":checked")?1:0,is_network_admin:c?1:0,batch:!0,sites:a,currentSiteIndex:0,batch_number:1,total_batches:0,results:{}},n,i,s)}})}); |