1 line
18 KiB
JavaScript
1 line
18 KiB
JavaScript
function wpo_parse_json(s){if("object"==typeof s)return s;try{var e=JSON.parse(s);return e}catch(o){console.log("WPO: Exception when trying to parse JSON (1) - will attempt to fix/re-parse"),console.log(s)}var i=s.indexOf("{"),a=s.lastIndexOf("}");if(i>-1&&a>-1){var n=s.slice(i,a+1);try{var t=JSON.parse(n);return t}catch(o){console.log("WPO: Exception when trying to parse JSON (2) - will attempt to fix/re-parse based upon bracket counting");for(var m=i,_=0,r="",u=!1;(_>0||m==i)&&m<=a;){var c=s.charAt(m);u||"{"!=c?u||"}"!=c?'"'==c&&"\\"!=r&&(u=!u):_--:_++,r=c,m++}console.log("Started at cursor="+i+", ended at cursor="+m+" with result following:"),console.log(s.substring(i,m));try{var t=JSON.parse(s.substring(i,m));return t}catch(o){throw o}}}throw"WPO: could not parse the JSON"}jQuery(function(s){WP_Optimize_Smush=WP_Optimize_Smush()});var WP_Optimize_Smush=function(){function s(){var s=0==U('input[type="checkbox"]:checked',z).length;K.prop("disabled",s),M.prop("disabled",s)}function e(s,o){if(H){var i=U("#smush-information-modal-cancel-btn .smush-information");I("mark_all_as_uncompressed",{restore_backup:s?1:0,delete_only_backups_meta:o?1:0},function(o){if(H)return o.hasOwnProperty("error")?(x(U("#smush-information-modal"),U.unblockUI),U("#smush-information-modal .smush-information").text(o.error),void a()):void(o.completed?(x(U("#smush-information-modal"),U.unblockUI),U("#smush-information-modal .smush-information").text(o.message),a()):(i.text(o.message),e(s)))})}}function o(){U(this).toggleClass("opened"),U(this).hasClass("opened")?U(this).text(wposmush.less):U(this).text(wposmush.more)}function a(e){var e="undefined"==typeof e||e,o={use_cache:e};P.html("..."),j.hide(),w(!0),I("get_ui_update",o,function(e){O(e,u),f(),w(!1),s()})}function n(){U("#wpo_smush_images_grid input:checked").each(function(){image={attachment_id:U(this).val(),blog_id:U(this).data("blog")},F.push(image)}),data={optimization_id:"smush",selected_images:F,smush_options:{compression_server:U("input[name='compression_server']:checked").val(),image_quality:U("#image_quality").val(),lossy_compression:U("#smush-lossy-compression").is(":checked"),back_up_original:U("#smush-backup-original").is(":checked"),preserve_exif:U("#smush-preserve-exif").is(":checked")}},c(),I("process_bulk_smush",data)}function t(){if(U("#wp-optimize-wrap").length){U("#wpo_smush_images_save_options_spinner").show().delay(3e3).fadeOut(),U("#enable_custom_compression").is(":checked")?(image_quality=U("#custom_compression_slider").val(),lossy_compression=image_quality<100):(image_quality=U("#enable_lossy_compression").is(":checked")?90:100,lossy_compression=image_quality<100);var s={compression_server:U("input[name='compression_server']:checked").val(),image_quality:image_quality,lossy_compression:lossy_compression,back_up_original:U("#smush-backup-original").is(":checked"),back_up_delete_after:U("#smush-backup-delete").is(":checked"),back_up_delete_after_days:U("#smush-backup-delete-days").val(),preserve_exif:U("#smush-preserve-exif").is(":checked"),autosmush:U("#smush-automatically").is(":checked"),show_smush_metabox:U("#smush-show-metabox").is(":checked"),webp_conversion:U("#enable_webp_conversion").is(":checked")};I("update_smush_options",s,function(s){U("#wpo_smush_images_save_options_spinner").hide(),s.hasOwnProperty("saved")&&s.saved?(U("#wpo_smush_images_save_options_done").show().delay(3e3).fadeOut(),S.hide()):(U("#wpo_smush_images_save_options_fail").show().delay(3e3).fadeOut(),S.show())})}}function m(){Z=!0,X++,seconds=X%60+""<10?"0"+X%60:X%60,minutes=parseInt(X/60)+""<10?"0"+parseInt(X/60):parseInt(X/60),U("#smush_stats_timer").text(minutes+":"+seconds),_(X)}function _(s){0==s%3&&r(),0==s%60&&I("process_pending_images",{},function(s){O(s,l)})}function r(s){data={update_ui:!0,use_cache:!1},I("get_ui_update",data,function(s){O(s,l)})}function u(s){if(z.html(""),s&&s.hasOwnProperty("unsmushed_images")){s.unsmushed_images,s.pending_tasks;0==s.unsmushed_images.length&&0==s.pending_tasks&&z.text(wposmush.all_images_compressed).wrapInner("<div class='wpo-fieldgroup'> </div>"),0!=s.pending_tasks&&j.show().find(".red").text(s.pending);var e="post.php?post=",o="&action=edit";for(blog_id in s.unsmushed_images){s.unsmushed_images[blog_id].sort(function(s,e){return s.id-e.id});for(i in s.unsmushed_images[blog_id])s.unsmushed_images[blog_id].hasOwnProperty(i)&&(image=s.unsmushed_images[blog_id][i],g(image,blog_id,s.admin_urls[blog_id]+e+image.id+o))}}}function c(){Z||(x(U("#wpo_smush_images_information_container")),service=U('.compression_server input[type="radio"]:checked + label small').text(),U("#wpo_smush_images_information_server").html(service),U("#smush_stats_pending_images").html("..."),U("#smush_stats_completed_images").html("..."),U("#smush_stats_bytes_saved").html("..."),U("#smush_stats_percent_saved").html("..."),U("#smush_stats_timer").html("..."),B=window.setInterval(m,1e3),w(!0))}function l(s){U("#smush_stats_pending_images").html(s.pending_tasks),U("#smush_stats_completed_images").html(s.completed_task_count),U("#smush_stats_bytes_saved").html(s.bytes_saved),U("#smush_stats_percent_saved").html(s.percent_saved),1==s.smush_complete&&setTimeout(p,1500)}function p(){data={update_ui:!0,use_cache:!1,image_list:F},I("get_ui_update",data,function(s){summary=s.session_stats,0!=s.completed_task_count&&(summary+="<hr>"+s.summary),h(summary)})}function h(s){G||(U("#summary-message").html(s),d(),x(U("#smush-complete-summary")),G=!0)}function d(){X=0,Z=!1,G=!1,F=[],window.clearInterval(B),w(!1)}function g(s,e,o){var i=["wpo_smush_",e,"_",s.id].join("");image_html='<div class="wpo_smush_image" data-filesize="'+s.filesize+'">',image_html+='<a class="button" href="'+o+'" target="_blank"> '+wposmush.view_image+" </a>",image_html+='<input id="'+i+'" type="checkbox" data-blog="'+e+'" class="wpo_smush_image__input" value="'+s.id+'">',image_html+='<label for="'+i+'"></a>',image_html+='<div class="thumbnail">',image_html+='<img class="lazyload" src="'+s.thumb_url+'">',image_html+="</div></label></div>",z.append(image_html)}function f(){features=wposmush.features,service=U("input[name^='compression_server']:checked").val();for(feature in features[service])U("."+feature).prop("disabled",!features[service][feature]);U(".wpo_smush_image").each(function(){U(this).data("filesize")>wposmush.features[service].max_filesize?U(this).hide():U(this).show()})}function w(s){U.each([K,J,N,S,C,q,M],function(e,o){o.prop("disabled",s)}),s?(U("#wpo_smush_images_refresh").hide(),U(".wpo_smush_images_loader").show()):(U("#wpo_smush_images_refresh").show(),U(".wpo_smush_images_loader").hide())}function b(s,e){0!=s.length&&(data={selected_image:s,smush_options:e},x(wposmush.compress_single_image_dialog),I("compress_single_image",data,function(s){O(s,v)}))}function k(s,e){if(0!=e.length){x(wposmush.please_wait,U.unblockUI);var o={blog_id:s,selected_image:e};I("restore_single_image",o,function(s){O(s,v)})}}function v(s){if(s.hasOwnProperty("success")&&s.success){U(".smush-information").text(s.summary),x(U("#smush-information-modal"),U.unblockUI),U(".wpo-toggle-advanced-options.wpo_smush_single_image").removeClass("opened"),y(s.operation,s.summary,s.restore_possible,s);var e=s.blog_id||s.options.blog_id,o=s.image||s.options.attachment_id;L.hasOwnProperty(e)||(L[e]={}),L[e].hasOwnProperty(o)||(L[e][o]={}),"compress"==s.operation?L[e][o]={operation:s.operation,summary:s.summary,restore_possible:s.restore_possible}:L[e][o]={operation:s.operation}}else U(".smush-information").text(s.error_message),x(U("#smush-information-modal"),U.unblockUI)}function y(s,e,o,i){var a=U("#smush_info").closest("#smush-metabox-inside-wrapper");"compress"==s?(U(".wpo_smush_single_image").hide(),U(".wpo_restore_single_image").show(),i&&i.hasOwnProperty("sizes-info")?(U("#smush_info").text(e),U("#wpo_smush_details").html(i["sizes-info"])):(U("#smush_info").text(e),U("#wpo_smush_details").text("").hide()),U(".wpo_smush_mark_single_image").hide(),o?U(".restore_possible").show():U(".restore_possible").hide()):(U(".wpo_smush_single_image").show(),U(".wpo_restore_single_image").hide(),U(".wpo_smush_mark_single_image").show(),U(".wpo_smush_unmark_single_image",a).hide())}function x(s,e){U.blockUI({message:s,onOverlayClick:e,baseZ:160001,css:{width:"400px",padding:"20px",cursor:"pointer"}})}function O(s,e){s&&s.hasOwnProperty("status")&&s.status?e&&e(s):(alert(wposmush.error_unexpected_response),console.log(s))}function I(s,e,o,i){i="undefined"==typeof i||i,e=U.isEmptyObject(e)?{use_cache:!1}:e;var a={action:"updraft_smush_ajax",subaction:s,nonce:wposmush.smush_ajax_nonce,data:e},n={type:"POST",url:ajaxurl,data:a,success:function(s){if(i){try{var e=wpo_parse_json(s)}catch(a){console.log("smush_manager_send_command JSON parse error"),console.log(a),console.log(s),alert(wposmush.error_unexpected_response)}"undefined"!=typeof o&&o(e)}else"undefined"!=typeof o&&o(s)},error:function(s,e,i){console.log("smush_manager_send_command AJAX parse error: "+e+" ("+i+")"),"undefined"!=typeof o?o(s):(console.log(s),alert(wposmush.error_unexpected_response))},dataType:"text"};U.ajax(n)}var U=jQuery,z=(U("#wp-optimize-images-nav-tab-smush"),U("#wpo_smush_images_grid")),P=U("#smush_info_images"),j=U("#wpo_smush_images_pending_tasks_container"),q=U("#wpo_smush_images_pending_tasks_button"),S=U(".wpo-fieldgroup #wpo_smush_images_save_options_button"),C=U("#wpo_smush_images_refresh"),J=U("#wpo_smush_images_select_all"),N=U("#wpo_smush_images_select_none"),W=U("#wpo_smush_clear_stats_btn"),K=U("#wpo_smush_images_btn"),M=U("#wpo_smush_mark_as_compressed"),T=(U(".wpo_smush_single_image .button"),U(".wpo_restore_single_image .button"),U("#wpo_smush_mark_all_as_uncompressed_btn")),A=U("#wpo_smush_restore_all_compressed_images_btn"),E=U(".wpo_smush_get_logs"),Q=U("#wpo_smush_delete_backup_btn"),D=U(".compression_server"),X=0,Z=!1,B=0,F=[],G=!1,H=!1,L={},R=U("#wpo_smush_images_pending_tasks_cancel_button");z.on("click",".thumbnail",function(s){U(this).closest('input[type="checkbox"]').prop("checked",!0)});var V=!1;z.on("mousedown",".thumbnail",function(s){V=s.shiftKey||s.ctrlKey}),z.on("mouseup",".thumbnail",function(s){V=s.shiftKey||s.ctrlKey}),U("#wp-optimize-nav-tab-wrapper__wpo_images .nav-tab").on("click",function(){U(this).is("#wp-optimize-nav-tab-wpo_images-smush")&&a()}),U("#wp-optimize-wrap").on("page-change",function(s,e){"wpo_images"==e.page&&U("#wp-optimize-nav-tab-wrapper__wpo_images .nav-tab-active").is("#wp-optimize-nav-tab-wpo_images-smush")&&a()}),U("#smush-metabox").length>0&&f();var Y=null;z.on("click",".wpo_smush_image",function(e){var o=U('#wpo_smush_images_grid input[type="checkbox"]'),i=U(this).find(".wpo_smush_image__input"),a=!i.prop("checked");if(Y||(U(this).find(".wpo_smush_image__input").prop("checked",a),Y=i),!0===V){var n=o.index(i),t=o.index(Y);n===t?o.slice(Math.min(n,t),Math.max(n,t)+1).prop("checked",a):!0===Y.prop("checked")&&o.slice(Math.min(n,t),Math.max(n,t)+1).prop("checked",a)}Y=i,s()}),s(),D.on("change",function(s){f(),t()}),K.off().on("click",function(){return 0==U('#wpo_smush_images_grid input[type="checkbox"]:checked').length?(U("#smush-information-modal .smush-information").text(wposmush.please_select_images),void x(U("#smush-information-modal"),U.unblockUI)):(U("#smush-information-modal .smush-information").text(wposmush.server_check),x(U("#smush-information-modal")),data={server:U("input[name='compression_server']:checked").val()},void I("check_server_status",data,function(s){s.online?n():(s.error?(error_message=s.error+"<br>"+wposmush.server_error,U("#smush-information-modal .smush-information").html(error_message)):U("#smush-information-modal .smush-information").text(wposmush.server_error),x(U("#smush-information-modal"),U.unblockUI))}))}),M.off().on("click",function(){if(0==U('#wpo_smush_images_grid input[type="checkbox"]:checked').length)return U("#smush-information-modal .smush-information").text(wposmush.please_select_compressed_images),void x(U("#smush-information-modal"),U.unblockUI);var s,e=[];U("#wpo_smush_images_grid input:checked").each(function(){s={attachment_id:U(this).val(),blog_id:U(this).data("blog")},e.push(s)}),x(wposmush.please_updating_images_info),I("mark_as_compressed",{selected_images:e},function(s){U("#smush-information-modal .smush-information").text(s.summary),x(U("#smush-information-modal"),U.unblockUI),a()})}),T.on("click",function(){if(confirm(wposmush.mark_all_images_uncompressed)){var s=confirm(wposmush.restore_images_from_backup);x(U("#smush-information-modal-cancel-btn")),U("#smush-information-modal-cancel-btn .smush-information").text(wposmush.please_wait),H=!0,e(s)}}),A.on("click",function(){confirm(wposmush.restore_all_compressed_images)&&(x(U("#smush-information-modal-cancel-btn")),U("#smush-information-modal-cancel-btn .smush-information").text(wposmush.please_wait),H=!0,e(!0,!0))}),U('#smush-information-modal-cancel-btn input[type="button"]').on("click",function(){H=!1,a(),U.unblockUI()}),C.off().on("click",function(){a()}),J.off().on("click",function(){U('#wpo_smush_images_grid input[type="checkbox"]').prop("checked",!0),Y=null,s()}),N.off().on("click",function(){U('#wpo_smush_images_grid input[type="checkbox"]').prop("checked",!1),Y=null,s()}),E.off().on("click",function(){U("#log-panel").text("Please wait, fetching logs."),I("get_smush_logs",{},function(s){U.blockUI({message:U("#smush-log-modal"),onOverlayClick:U.unblockUI(),css:{width:"80%",height:"80%",top:"15%",left:"15%"}}),U("#log-panel").html("<pre>"+s+"</pre>"),download_link=ajaxurl+"?action=updraft_smush_ajax&subaction=get_smush_logs&nonce="+wposmush.smush_ajax_nonce,U("#smush-log-modal a").attr("href",download_link)},!1)}),Q.on("click",function(){if(confirm(wposmush.delete_image_backup_confirm)){Q.prop("disabled",!0);var s=U("#wpo_smush_delete_backup_spinner"),e=U("#wpo_smush_delete_backup_done");s.show(),I("clean_all_backup_images",{},function(){s.hide(),Q.prop("disabled",!1),e.css("display","inline-block").delay(3e3).fadeOut()})}}),S.off().on("click",function(s){t()}),W.off().on("click",function(s){U("#wpo_smush_images_clear_stats_spinner").show().delay(3e3).fadeOut(),I("clear_smush_stats",{},function(s){U("#wpo_smush_images_clear_stats_spinner").hide(),U("#wpo_smush_images_clear_stats_done").show().delay(3e3).fadeOut()})}),q.off().on("click",function(s){U("#smush-information-modal .smush-information").text(wposmush.server_check),x(U("#smush-information-modal"),U.unblockUI),data={server:U("input[name='compression_server']:checked").val()},I("check_server_status",data,function(s){s.online?(c(),I("process_pending_images",{},function(s){O(s,l)})):(s.error?(error_message=s.error+"<br>"+wposmush.server_error,U("#smush-information-modal .smush-information").html(error_message)):U("#smush-information-modal .smush-information").text(wposmush.server_error),x(U("#smush-information-modal"),U.unblockUI))})}),U("body").on("click","#wpo_smush_images_pending_tasks_cancel_button",function(s){wpoptimize.cancel===R.val()&&(R.val(wpoptimize.cancelling),R.prop("disabled",!0)),I("clear_pending_images",{},function(s){U.unblockUI(),s.status?(a(),d()):console.log("Cancelling pending images apparently failed.",s),R.val(wpoptimize.cancel),R.prop("disabled",!1)})}),U("body").on("click",".wpo_smush_single_image .button",function(){image={attachment_id:U(this).data("id"),blog_id:U(this).data("blog")},U("#enable_custom_compression").is(":checked")?(image_quality=U("#custom_compression_slider").val(),lossy_compression=image_quality<100):(image_quality=U("#enable_lossy_compression").is(":checked")?90:100,lossy_compression=image_quality<100),smush_options={compression_server:U("input[name='compression_server_"+image.attachment_id+"']:checked").val(),image_quality:image_quality,lossy_compression:lossy_compression,back_up_original:U("#smush_backup_"+image.attachment_id).is(":checked"),preserve_exif:U("#smush_exif_"+image.attachment_id).is(":checked")},data={server:U("input[name='compression_server_"+U(this).attr("id").substring(15)+"']:checked").val()},x(wposmush.server_check),I("check_server_status",data,function(s){s.online?b(image,smush_options):s.error?(error_message=s.error+"<br>"+wposmush.server_error,x(error_message,U.unblockUI)):x(wposmush.server_error,U.unblockUI)})}),U("body").on("click",".wpo_restore_single_image .button",function(){var s=U(this);blog_id=s.data("blog"),image_id=s.data("id"),image_id&&blog_id&&k(blog_id,image_id)}),U("body").on("click",".wpo_smush_mark_single_image .button",function(){var s={attachment_id:U(this).data("id"),blog_id:U(this).data("blog")},e=U(this).closest("#smush-metabox-inside-wrapper");x(wposmush.please_updating_images_info),I("mark_as_compressed",{selected_images:[s]},function(s){U("#smush-information-modal .smush-information").text(s.summary),x(U("#smush-information-modal"),U.unblockUI),s.status&&(U(".wpo_smush_single_image",e).hide(),U(".wpo-toggle-advanced-options",e).removeClass("opened"),U(".wpo_smush_mark_single_image",e).hide(),U(".wpo_smush_unmark_single_image",e).show(),U(".wpo_restore_single_image",e).show(),U("#smush_info",e).text(s.info))})}),U("body").on("click",".wpo_smush_unmark_single_image .button",function(){var s={attachment_id:U(this).data("id"),blog_id:U(this).data("blog")},e=U(this).closest("#smush-metabox-inside-wrapper");x(wposmush.please_updating_images_info),I("mark_as_compressed",{selected_images:[s],unmark:!0},function(s){U("#smush-information-modal .smush-information").text(s.summary),x(U("#smush-information-modal"),U.unblockUI),s.status&&(U(".wpo_smush_single_image",e).show(),U(".wpo_smush_mark_single_image",e).show(),U(".wpo_smush_unmark_single_image",e).hide(),U(".wpo_restore_single_image",e).hide(),U("#smush_info",e).text(""))})}),U("body").on("click","#wpo_smush_details .wpo-collapsible",o),U("body").on("click",".column-wpo_smush .wpo-collapsible",o),U("body").on("click","#smush-log-modal .close, #smush-information-modal .information-modal-close",function(){U.unblockUI()}),U("body").on("click",".wpo_smush_stats_cta_btn, .wpo_smush_get_logs, #smush-complete-summary .close",function(){U.unblockUI(),a(),setTimeout(d,500)}),U("body").on("click",".wpo-toggle-advanced-options",function(s){s.preventDefault(),U(this).toggleClass("opened")}),U(".wpo-fieldgroup .autosmush input, .wpo-fieldgroup .compression_level, .wpo-fieldgroup .image_options, #smush-show-metabox, #enable_webp_conversion").on("change",function(s){t()}),U("body").on("change",".smush-options.compression_level",function(){U("#enable_custom_compression").is(":checked")?U(".smush-options.custom_compression").show():U(".smush-options.custom_compression").hide()}),U("body").on("change",'.smush-advanced input[type="radio"]',function(){f()}),U(document).on("admin-metabox-smush-loaded",function(){var s=U('.wpo_restore_single_image input[type="button"]').first().data();if(s&&L.hasOwnProperty(s.blog)&&L[s.blog].hasOwnProperty(s.id)){var e=L[s.blog][s.id];"compress"==e.operation?y(e.operation,e.summary,e.restore_possible,e):y(e.operation)}})}; |