upload_scripts_enhanced

The WooCommerce Checkout Manager upload scripts enhanced function.

Description

upload_scripts_enhanced(); 

Usage

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

Defined (1)

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

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