upload_shipping_scripts_enhanced

The WooCommerce Checkout Manager upload shipping scripts enhanced function.

Description

upload_shipping_scripts_enhanced(); 

Usage

  1. if ( !function_exists( 'upload_shipping_scripts_enhanced' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-checkout-manager/includes/templates/functions/shipping_wooccmupload.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = upload_shipping_scripts_enhanced(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/templates/functions/shipping_wooccmupload.php  
  1. function upload_shipping_scripts_enhanced() { 
  2. global $woocommerce; 
  3. $options = get_option( 'wccs_settings2' ); 
  4.  
  5. if ( WC()->cart->needs_shipping_address() === true ) { 
  6. $saved = WC()->session->get('wooccm_retain', array() ); 
  7.  
  8.  
  9. if( !empty($options['shipping_buttons']) ) { 
  10. foreach ( $options['shipping_buttons'] as $btn ) : 
  11.  
  12.  
  13. if ( $btn['type'] == 'wooccmupload' ) { 
  14. ?> 
  15. <script type="text/javascript"> 
  16. jQuery(document).ready(function($) { 
  17.  
  18. $("#shipping_<?php echo $btn['cow']; ?>_field").magnificPopup({ 
  19. delegate: "a.wooccm-zoom-special", // child items selector, by clicking on it popup will open 
  20. type: "image",  
  21. zoom: { 
  22. enabled: true,  
  23. duration: 400,  
  24. easing: "ease-out" 
  25. }); 
  26.  
  27. (function post_image_content() { 
  28. var input = document.getElementById("shipping_<?php echo $btn['cow']; ?>_file"),  
  29. formdata = false, loadfiles, formnames = [], loadfiles = []; 
  30.  
  31. $("#shipping_<?php echo $btn['cow']; ?>_files_button_wccm").click( function() { 
  32. $("#shipping_<?php echo $btn['cow']; ?>_field input[type=file]").click(); 
  33. return false; 
  34. }); 
  35.  
  36. if (window.FormData) { 
  37. formdata = new FormData(); 
  38.  
  39. function showUploadedItem ( source, getname, filetype ) { 
  40. var  
  41. list = document.getElementById("shipping_<?php echo $btn['cow']; ?>_field"),  
  42. li = document.createElement("span"),  
  43. name = document.createElement("name"),  
  44. span = document.createElement("span"),  
  45. zoom = document.createElement("a"),  
  46. edit = document.createElement("a"),  
  47. dele = document.createElement("a"),  
  48. a = document.createElement("a"),  
  49. spana = document.createElement("spana"),  
  50. img = document.createElement("img"); 
  51.  
  52. name.innerHTML = getname; 
  53. edit.innerHTML = "Edit"; 
  54. dele.innerHTML = "Delete"; 
  55. if (filetype.match("image.*")) { 
  56. img.src = source; 
  57. a.href = source; 
  58. a.title = getname; 
  59. edit.href = source; 
  60. zoom.href = source; 
  61. zoom.title = getname; 
  62. zoom.innerHTML = "Zoom <img style=display:none />"; 
  63. li.appendChild(a); 
  64. a.appendChild(img); 
  65. a.className = "wooccm-zoom-special wooccm-image-holder mfp-zoom"; 
  66. zoom.className = "wooccm-zoom-special wooccm_zoom wooccm-btn wooccm-btn-zoom"; 
  67. edit.className = "wooccm_edit wooccm-btn wooccm-btn-edit enable"; 
  68. }else{ 
  69. zoom.innerHTML = "Zoom"; 
  70. li.appendChild(spana); 
  71. spana.appendChild(img); 
  72. spana.className = "wooccm-image-holder"; 
  73. zoom.className = "wooccm_zoom wooccm-btn disable"; 
  74. edit.className = "wooccm_edit wooccm-btn disable"; 
  75. if ( ( false === filetype.match("application/ms.*") && false === filetype.match("application/x.*") && false === filetype.match("audio.*") && false === filetype.match("text.*") && false === filetype.match("video.*") ) || ( 0 === filetype.length || !filetype) ) { 
  76. img.src = "<?php echo site_url('wp-includes/images/media/interactive.png'); ?>"; 
  77. if (filetype.match("application/ms.*")) { 
  78. img.src = "<?php echo site_url('wp-includes/images/media/spreadsheet.png'); ?>"; 
  79. if (filetype.match("application/x.*")) { 
  80. img.src = "<?php echo site_url('wp-includes/images/media/archive.png'); ?>"; 
  81. if (filetype.match("audio.*")) { 
  82. img.src = "<?php echo site_url('wp-includes/images/media/audio.png'); ?>"; 
  83. if (filetype.match("text.*")) { 
  84. img.src = "<?php echo site_url('wp-includes/images/media/text.png'); ?>"; 
  85. if (filetype.match("video.*")) { 
  86. img.src = "<?php echo site_url('wp-includes/images/media/video.png'); ?>"; 
  87.  
  88. li.title = getname; 
  89. dele.title = getname; 
  90. edit.title = getname; 
  91. li.appendChild(name); 
  92. li.appendChild(span); 
  93. span.appendChild(zoom); 
  94. span.appendChild(edit); 
  95. span.appendChild(dele); 
  96. list.appendChild(li); 
  97. li.className = "wooccm_each_file"; 
  98. name.className = "wooccm_name"; 
  99. dele.id = "wooccm_dele"; 
  100. dele.className = "wooccm_dele wooccm-btn wooccm-btn-danger"; 
  101. span.className = "container"; 
  102.  
  103.  
  104.  
  105. input.addEventListener("change", function (evt) { 
  106. $("#shipping_<?php echo $btn['cow']; ?>_field").block({ message: null, overlayCSS: { background: '#fff no-repeat center', backgroundSize: '16px 16px', opacity: 0.6 } }); 
  107.  
  108. var count = $("#shipping_<?php echo $btn['cow']; ?>_files_button_wccm").data("count") || 0; 
  109. $("#shipping_<?php echo $btn['cow']; ?>_files_button_wccm").data("count", ++count); 
  110.  
  111. var img, reader, file, iname, len = this.files.length; 
  112.  
  113. for ( i = 0; i < len; i++ ) { 
  114. file = this.files[i]; 
  115.  
  116. if ( window.FileReader ) { 
  117. reader = new FileReader(); 
  118. reader.onload = (function(theFile) { 
  119. var fileName = theFile.name,  
  120. filetype = theFile.type; 
  121. return function(e) { 
  122. showUploadedItem( e.target.result, fileName, filetype ); 
  123. }; 
  124. })(file);  
  125. reader.readAsDataURL(file); 
  126.  
  127. formdata.append("shipping_<?php echo $btn['cow']; ?>[]", file); 
  128. formnames.push(file.name); 
  129. loadfiles.push(file); 
  130.  
  131. $.ajax({ 
  132. url: "<?php echo admin_url('/admin-ajax.php?action=wooccm_front_endupload&name='.$btn['cow'].''); ?>",  
  133. type: "POST",  
  134. data: formdata,  
  135. processData: false,  
  136. contentType: false,  
  137. success: function (res) { 
  138. var result = $.parseJSON(res), new_val; 
  139.  
  140. new_val = document.getElementById("shipping_<?php echo $btn['cow']; ?>").value.split("||"); 
  141. new_val[0] = result[0]; 
  142. new_val[1] += result[1] + ", "; 
  143. document.getElementById("shipping_<?php echo $btn['cow']; ?>").value = new_val[0] + "||" + new_val[1]; 
  144.  
  145. $("#shipping_<?php echo $btn['cow']; ?>_field").unblock(); 
  146. }); 
  147.  
  148. if( formdata ) {  
  149. $("#shipping_<?php echo $btn['cow']; ?>_field").unblock(); 
  150.  
  151.  
  152. }, false); 
  153.  
  154. $("#caman_content #wooccmtoolbar #save").click( function() { 
  155. $("#caman_content #wooccmtoolbar").block({ message: null, overlayCSS: { background: '#fff no-repeat center', backgroundSize: '16px 16px', opacity: 0.6 } }); 
  156.  
  157. var title = this.title, the_file, wooxtro = document.getElementById("wooccmactualimage").getAttribute("data-caman-hidpi"), listing, store = [], count = $(".wooccm_each_file").each(function() {}); 
  158. formdata = new FormData(); 
  159.  
  160. for(var t = 0; t < count.length; t++) { 
  161. if( count[t].getAttribute("wooccm-attach-id") ) { 
  162. break; 
  163. store.push(count[t]); 
  164.  
  165. if( store.length !== 0 ) { 
  166. listing = document.getElementById("shipping_<?php echo $btn['cow']; ?>").value.split("||"); 
  167. listing = listing[1].split(", "); 
  168. listing = listing.filter(Number); 
  169.  
  170. $.each(listing, function(index, value) { 
  171. $(store[index]).attr("wooccm-attach-id", value); 
  172. }); 
  173.  
  174.  
  175. $(".wooccm_each_file").each(function() { 
  176. if( this.title === title) { 
  177. var currentgutz = this; 
  178. this.firstElementChild.href = wooxtro; 
  179. this.firstElementChild.firstElementChild.src = wooxtro; 
  180. this.lastElementChild.firstElementChild.href = wooxtro; 
  181. this.lastElementChild.lastElementChild.previousElementSibling.href = wooxtro;  
  182.  
  183. var byteString; 
  184. var dataURI = wooxtro; //data:image/gif;base64, R0lGODlhyAAiALM...DfD0QAADs 
  185.  
  186. if (dataURI.split(', ')[0].indexOf('base64') >= 0) 
  187.  
  188. byteString = atob(dataURI.split(', ')[1]); 
  189.  
  190. else 
  191.  
  192. byteString = unescape(dataURI.split(', ')[1]); 
  193.  
  194. // separate out the mime component 
  195. var mimeString = dataURI.split(', ')[0].split(':')[1].split(';')[0]; 
  196.  
  197. // write the bytes of the string to a typed array 
  198. var ia = new Uint8Array(byteString.length); 
  199. for (var i = 0; i < byteString.length; i++) { 
  200. ia[i] = byteString.charCodeAt(i); 
  201.  
  202. var extension = mimeString.split('/')[1]; 
  203. var fileName = "image_" + new Date().getTime() + "." + extension; 
  204. var blob = new Blob([ia], {type:mimeString}); 
  205.  
  206. the_file = new File([blob], title, { 
  207. type: "image/png"  
  208. }); 
  209.  
  210.  
  211. formdata.append("shipping_<?php echo $btn['cow']; ?>[]", the_file); 
  212. var remove = this.getAttribute("wooccm-attach-id"); 
  213.  
  214. $.ajax({ 
  215. url: "<?php echo admin_url('/admin-ajax.php?action=wooccm_front_enduploadsave&name='.$btn['cow'].'&remove='); ?>" + remove,  
  216. type: "POST",  
  217. data: formdata,  
  218. processData: false,  
  219. contentType: false,  
  220. success: function (res) { 
  221. var result = $.parseJSON(res); 
  222. $(currentgutz).attr("wooccm-attach-id", result); 
  223. $("#shipping_<?php echo $btn['cow']; ?>").val(function(index, value) { 
  224. return value.replace(remove, result); 
  225. }); 
  226.  
  227. $("#caman_content #wooccmtoolbar").unblock(); 
  228. alert("<?php echo $options['checkness']['picture_success']; ?>"); 
  229.  
  230. }); 
  231. }); 
  232.  
  233.  
  234. }); 
  235.  
  236. $(document).on('click', '.wooccm_dele', function() {  
  237.  
  238. var c = confirm("<?php echo $options['checkness']['file_delete']; ?> " + this.title + " ?"), listing, store = [], count = $(".wooccm_each_file").each(function() {}); 
  239. if (c==true) { 
  240.  
  241. $("#shipping_<?php echo $btn['cow']; ?>_field").block({ message: null, overlayCSS: { background: '#fff no-repeat center', backgroundSize: '16px 16px', opacity: 0.6 } }); 
  242.  
  243. for(var t = 0; t < count.length; t++) { 
  244. if( count[t].getAttribute("wooccm-attach-id") ) { 
  245. break; 
  246. store.push(count[t]); 
  247.  
  248. if( store.length !== 0 ) { 
  249. listing = document.getElementById("shipping_<?php echo $btn['cow']; ?>").value.split("||"); 
  250. listing = listing[1].split(", "); 
  251. listing = listing.filter(Number); 
  252.  
  253. $.each(listing, function(index, value) { 
  254. $(store[index]).attr("wooccm-attach-id", value); 
  255. }); 
  256.  
  257. var currentname = this.title, wooccmeachfile = document.getElementsByClassName("wooccm_each_file"), remove, shell; 
  258. formdata = new FormData(); 
  259.  
  260. for( i = 0; i < wooccmeachfile.length; i++ ) { 
  261. if(wooccmeachfile[i].title === currentname) { 
  262. remove = wooccmeachfile[i].getAttribute("wooccm-attach-id"); 
  263. shell = wooccmeachfile[i]; 
  264.  
  265. $.ajax({ 
  266. url: "<?php echo admin_url('/admin-ajax.php?action=wooccm_front_enduploadsave&remove='); ?>" + remove,  
  267. type: "POST",  
  268. data: formdata,  
  269. processData: false,  
  270. contentType: false,  
  271. success: function (res) { 
  272. remove = remove + ", "; 
  273. $("#shipping_<?php echo $btn['cow']; ?>").val(function(index, value) { 
  274. return value.replace(remove, ""); 
  275. }); 
  276.  
  277. shell.outerHTML = ""; 
  278.  
  279. $("#shipping_<?php echo $btn['cow']; ?>_field").unblock(); 
  280.  
  281. }); 
  282.  
  283.  
  284. } else { 
  285.  
  286. }); 
  287.  
  288.  
  289.  
  290. $("#caman_content #wooccmtoolbar #close").click( function() { 
  291. var li = document.getElementById( "wooccmactualimage" ); 
  292. li.outerHTML = ""; 
  293. $("#caman_content .FilterValue").each(function() { 
  294. $(this).text("0"); 
  295. }); 
  296. $("#caman_content .FilterSetting input").each(function() { 
  297. $(this).val("0"); 
  298. }); 
  299. $("#PresetFilters a").each(function() { 
  300. $(this).removeClass("Active"); 
  301. }); 
  302. $("#caman_content").hide(); 
  303. $("html").css("overflow", "visible"); 
  304. }); 
  305.  
  306. $(document).on('click', '.wooccm_edit', function() { 
  307. return false; 
  308. }); 
  309.  
  310. $(document).on('click', '.wooccm_edit.enable', function() {  
  311. var imageinbase = this.href,  
  312. li = document.getElementById( "wooccmimageeditorpro" ),  
  313. data = document.createAttribute("data-caman-hidpi"),  
  314. img = document.createElement("img"); 
  315.  
  316. data.value = imageinbase; 
  317.  
  318. $("#caman_content #wooccmtoolbar #save").attr("title", this.title); 
  319.  
  320. if( $(".wooccmimageeditor img").length == 0 ) { 
  321. img.id = "wooccmactualimage"; 
  322. img.src = imageinbase; 
  323. img.setAttributeNode(data); 
  324. li.appendChild(img); 
  325.  
  326.  
  327. $.getScript( "<?php echo plugins_url('woocommerce-checkout-manager-pro/includes/pickers/caman/dist/caman_controls.js'); ?>"); 
  328.  
  329. $("#caman_content").show(); 
  330. $("html").css("overflow", "hidden"); 
  331. }); 
  332.  
  333.  
  334.  
  335.  
  336. }()); 
  337. }); 
  338. </script> 
  339.  
  340.  
  341. <?php 
  342. endforeach;