WC_Pay_To_Upload

WC_Pay_To_Upload class.

Defined (1)

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

/classes/class-wc-pay-to-upload.php  
  1. class WC_Pay_To_Upload extends Airy_Framework { 
  2. /** 
  3. * __construct function. 
  4. *  
  5. * @access public 
  6. * @return void 
  7. */ 
  8.  
  9. function __construct() { 
  10.  
  11. $this->shortname = 'wc_pay_to_upload'; 
  12. $this->plugin_name = __('Pay to Upload', 'wc_pay_to_upload' ); 
  13. $this->desc = __('Let WC customers pay to upload files when purchasing specific products.', 'wc_pay_to_upload' ); 
  14. $this->version = '2.0.4'; 
  15. $this->menu_location = 'woocommerce'; 
  16. $uploads = wp_upload_dir(); 
  17. $this->fileuploaddir = trailingslashit( $uploads['basedir'] ) . 'wc-pay-to-upload'; 
  18. $this->fileurl = trailingslashit( $uploads['baseurl'] ) . 'wc-pay-to-upload'; 
  19. $this->defaultLimit = (get_option('wc_ptu_default_limit') ? get_option('wc_ptu_default_limit') : 1); 
  20. $this->defaultPath = (get_option('wc_ptu_uploads_path') ? get_option('wc_ptu_uploads_path') : $this->fileuploaddir); 
  21. $this->defaultFileTypes = (get_option('wc_ptu_file_types') ? get_option('wc_ptu_file_types') : 'jpg, png, gif'); 
  22. $this->defaultListType = (get_option('wc_ptu_list_type') ? get_option('wc_ptu_list_type') : 'all'); 
  23. $this->defaultStatus = (get_option('wc_ptu_order_statuses') ? get_option('wc_ptu_order_statuses') : 'wc-completed'); 
  24. $this->fields = array( 
  25. array( 
  26. 'name' => 'wc_ptu_default_limit',  
  27. 'title' => __( 'Default Upload Limit', 'wc_pay_to_upload' ),  
  28. 'type' => 'text',  
  29. 'desc' => __( 'Default upload limit when activating feature for products.', 'wc_pay_to_upload' ),  
  30. 'default' => $this->defaultLimit,  
  31. ),  
  32. array( 
  33. 'name' => 'wc_ptu_uploads_path',  
  34. 'title' => __( 'Uploads Path', 'wc_pay_to_upload' ),  
  35. 'type' => 'text',  
  36. 'desc' => __( 'Uploads path for all new uploads, subfolders with the order ID name will be created within this.', 'wc_pay_to_upload' ),  
  37. 'default' => $this->defaultPath,  
  38. ),  
  39. array( 
  40. 'name' => 'wc_ptu_list_type',  
  41. 'title' => __( 'Allow or Disallow File Types', 'wc_pay_to_upload' ),  
  42. 'type' => 'select',  
  43. 'desc' => __( 'The file types that are allowed/disallowed by your selection above.', 'wc_pay_to_upload' ),  
  44. 'values' => array( 
  45. 'all' => __( 'Allow All', 'wc_pay_to_upload' ),  
  46. 'white' => __( 'Whitelist', 'wc_pay_to_upload' ),  
  47. 'black' => __( 'Blacklist', 'wc_pay_to_upload' ),  
  48. ),  
  49. 'default' => $this->defaultListType 
  50. ),  
  51. array( 
  52. 'name' => 'wc_ptu_file_types',  
  53. 'title' => __('File Types', 'wc_pay_to_upload' ),  
  54. 'type' => 'text',  
  55. 'desc' => __('The file types that are allowed/disallowed by your selection above, separate file types by commas.', 'wc_pay_to_upload' ),  
  56. 'default' => $this->defaultFileTypes 
  57. ),  
  58. ); 
  59.  
  60. add_action( 'add_meta_boxes', array( &$this, 'add_meta_boxes' ) ); 
  61. add_action( 'woocommerce_init', array( &$this, 'woocommerce_init' ) ); 
  62. add_action( 'save_post', array( &$this, 'save_meta_box' ) ); 
  63. add_action( 'woocommerce_view_order', array( &$this, 'uploader' ) ); 
  64.  
  65. /** 
  66. * add_meta_boxes function. 
  67. *  
  68. * @access public 
  69. * @return void 
  70. */ 
  71. function add_meta_boxes() { 
  72. add_meta_box( 'wc_ptu_enable', __( 'Pay to Upload', 'wc_pay_to_upload' ), array( &$this, 'product_upload_options'), 'product', 'side' ); 
  73. add_meta_box( 'wc_ptu_files', __( 'Uploaded Files', 'wc_pay_to_upload' ), array( &$this, 'order_uploaded_files'), 'shop_order', 'side' ); 
  74.  
  75. /** 
  76. * woocommerce_init function. 
  77. *  
  78. * @access public 
  79. * @return void 
  80. */ 
  81. function woocommerce_init() { 
  82.  
  83. $order_status = wc_get_order_statuses(); 
  84.  
  85. $statuses = $order_status; 
  86.  
  87. $values = array(); 
  88.  
  89. foreach( $statuses as $status => $key ) { 
  90.  
  91. $values[ $status ] = $key; 
  92.  
  93.  
  94.  
  95.  
  96. $this->fields[] = array( 
  97. 'name' => 'wc_ptu_order_statuses',  
  98. 'title' => __('Required Status(es)', 'wc_pay_to_upload' ),  
  99. 'type' => 'select',  
  100. 'desc' => __('The required order status to allow customers to upload files.', 'wc_pay_to_upload' ),  
  101. 'values' => $values,  
  102. 'default' => $this->defaultStatus,  
  103. ); 
  104. parent::__construct(); 
  105.  
  106. /** 
  107. * order_uploaded_files function. 
  108. *  
  109. * @access public 
  110. * @param mixed $post 
  111. * @return void 
  112. */ 
  113. function order_uploaded_files( $post ) { 
  114. $order = new WC_Order( $post->ID ); 
  115.  
  116. $items = $order->get_items(); 
  117. $limit = $this->check_for_uploadables( $post->ID ); 
  118. echo '<table style="border-collapse:collapse;" border="1" cellpadding="5" cellspacing="0">'; 
  119. foreach( $items as $item ) { 
  120. echo '<tr><th colspan="2">Uploaded Files for '.$item['name'].'</th></tr>'; 
  121. echo '<tr><th>S.No.</th><th>File Name</th></tr>'; 
  122.  
  123. $limit = (int) get_post_meta( $item['product_id'], '_wc_ptu_limit', true ); //Changed $item['item_id'] to $item['product_id'] for wordpress version 3.5.1 Ashok G 
  124. if( get_post_meta( $item['product_id'], '_wc_ptu_enable', true ) == 1 && $limit > 0 ) { 
  125. $limits = $limit; 
  126. for ($i = 1; $i <= $limit; $i++) { 
  127. $item_id = $item['product_id'].$i; 
  128. $url = get_post_meta( $post->ID, '_wc_uploaded_file_path_' . $item_id, true ); 
  129. $name = get_post_meta( $post->ID, '_wc_uploaded_file_name_' . $item_id, true ); 
  130. if( !empty( $url ) && !empty( $name ) ) { 
  131. printf( __('<tr><td>%s</td><td> <a href="%s" target="_blank">%s</a></td></tr>', 'wc_pay_to_upload'), $i, $url, $name); 
  132. } else { 
  133. printf( __('<tr><td> %s</td><td> has not been uploaded.</td></tr>', 'wc_pay_to_upload'), $i ); 
  134.  
  135. echo '</table>'; 
  136.  
  137. /** 
  138. * product_upload_options function. 
  139. *  
  140. * @access public 
  141. * @param mixed $post 
  142. * @return void 
  143. */ 
  144. function product_upload_options( $post ) { 
  145. wp_nonce_field( 'wc_ptu_nonce', 'wc_ptu_nonce' ); 
  146. echo '<p>'; 
  147. echo '<label for="_wc_ptu_enable" style="float:left; width:50px;">' . __('Enable', 'wc_pay_to_upload' ) . '</label>'; 
  148. echo '<input type="hidden" name="_wc_ptu_enable" value="0" />'; 
  149. /** Individual product checks Auto enable primarily. Ashok G. 31/07/2016 */  
  150. $upStats = get_post_meta( $post->ID, '_wc_ptu_enable', true ); 
  151. if($upStats !=0 || $upStats =='' || $upStats == Null) { $chked = 'checked="true"'; }  
  152. echo '<input type="checkbox" id="_wc_ptu_enable" '.$chked.' class="checkbox" name="_wc_ptu_enable" value="1" ' . checked( get_post_meta( $post->ID, '_wc_ptu_enable', true ), 1, false ) . ' />'; 
  153. echo '</p>'; 
  154. echo '<p>'; 
  155. $value = get_post_meta( $post->ID, '_wc_ptu_limit', true ); 
  156. $value = ( !empty( $value ) ) ? $value : $this->defaultLimit; 
  157. echo '<label for="_wc_ptu_limit" style="float:left; width:50px;">' . __('Limit', 'wc_pay_to_upload' ) . '</label>'; 
  158. echo '<input type="text" id="_wc_ptu_limit" class="short" name="_wc_ptu_limit" value="' . $value . '" />'; 
  159. echo '</p>'; 
  160.  
  161. /** 
  162. * save_meta_box function. 
  163. *  
  164. * @access public 
  165. * @param mixed $post_id 
  166. * @return void 
  167. */ 
  168. function save_meta_box( $post_id ) { 
  169. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; 
  170. if ( !isset( $_POST['wc_ptu_nonce'] ) || !wp_verify_nonce( $_POST['wc_ptu_nonce'], 'wc_ptu_nonce' ) ) return; 
  171. update_post_meta( $post_id, '_wc_ptu_enable', (int) $_POST['_wc_ptu_enable'] ); 
  172. update_post_meta( $post_id, '_wc_ptu_limit', (int) $_POST['_wc_ptu_limit'] );  
  173.  
  174. /** 
  175. * check_for_uploadables function. 
  176. *  
  177. * @access public 
  178. * @param mixed $order_id 
  179. * @return void 
  180. */ 
  181. function check_for_uploadables( $order_id ) { 
  182. //Changed the Previous parameters to existing parameters for wordpress Version 3.5.1 Ashok G 
  183. global $woocommerce; 
  184. $order = new WC_Order( $order_id ); 
  185. $items = get_post_meta( $order_id , '_order_items', true); 
  186. $new_items = $order->get_items(); 
  187.  
  188.  
  189.  
  190. $limits = 0;  
  191. if( is_array( $new_items ) ) { 
  192. foreach( $new_items as $item ) { 
  193. $limit = (int) get_post_meta( $item['product_id'], '_wc_ptu_limit', true ); //Changed $item['item_id'] to $item['product_id'] for wordpress version 3.5.1 Ashok G 
  194. if( get_post_meta( $item['product_id'], '_wc_ptu_enable', true ) == 1 && $limit > 0 ) { 
  195. $limits += $limit; 
  196. } else { 
  197. echo wpautop( __( 'Sorry, no files have been uploaded yet.', 'wc_pay_to_upload' ) ); 
  198.  
  199. return $limits; 
  200.  
  201.  
  202. /** 
  203. * uploader function. 
  204. *  
  205. * @access public 
  206. * @param mixed $order_id 
  207. * @return void 
  208. */ 
  209. function uploader( $order_id ) { 
  210.  
  211. $x = new WC_Order( $order_id ); 
  212. if(isset($_GET['delete']) && isset($_GET['item'])) 
  213. echo $this->delete_file($order_id, $_GET['item']); 
  214.  
  215.  
  216. if(!defined("PHP_EOL")) {define("PHP_EOL", strtoupper(substr(PHP_OS, 0, 3) == "WIN") ? "\r\n" : "\n");} 
  217. $order = new WC_Order( $order_id ); 
  218. $items = $order->get_items(); 
  219. $limits = $this->check_for_uploadables( $order_id ); 
  220.  
  221.  
  222. $admin_email = get_option('admin_email');  
  223. echo '<h2>' . __( 'Upload Files', 'wc_pay_to_upload' ) . '</h2>'; 
  224. global $current_user; 
  225. wp_get_current_user(); 
  226. $from = $current_user->user_email; 
  227. $to = $admin_email; 
  228. if( isset( $_FILES ) ) { 
  229. $path = trailingslashit( trailingslashit( $this->defaultPath ) . $order_id ); 
  230. foreach( $_FILES as $key => $file ) { 
  231. if( empty( $file['name'] ) ) continue; 
  232. wp_mkdir_p( $path ); 
  233. $filepath = $path . $file['name']; 
  234. $ext = strtolower( pathinfo( $filepath, PATHINFO_EXTENSION ) ); 
  235. $types = explode( ', ', $this->defaultFileTypes ); 
  236. foreach( $types as $k => $v ) { $types[$k] = strtolower( trim( $v ) ); } 
  237. switch( $this->defaultListType ) { 
  238. case 'all': 
  239. $allow = true; 
  240. break; 
  241. case 'white': 
  242. if( in_array( $ext, $types ) ) $allow = true; 
  243. else $allow = false; 
  244. break; 
  245. case 'black': 
  246. if( in_array( $ext, $types ) ) $allow = false; 
  247. else $allow = true; 
  248. break; 
  249. if( $allow == true ) { 
  250. $headers = ''; 
  251. $dburl = $this->fileurl.'/'.$order_id.'/'.$file['name']; 
  252. if( copy( $file['tmp_name'], $filepath ) ) { 
  253.  
  254. $subject = 'File Upload Notification OrderId - '.$order_id; 
  255. $message = "Dear Admin, <br><br>User Has uploaded files for order - ".$order_id."<br><br> Please logon to your Admin Panel to download the files."; 
  256. $headers .= 'From:'. $from.PHP_EOL; 
  257. $headers .= 'Content-type: text/html; charset=iso-8859-1'.PHP_EOL; 
  258. if (wp_mail( $to, $subject, $message, $headers )) 
  259. echo '<p class="success">' . __( 'Your file(s) were uploaded successfully.', 'wc_pay_to_upload') . '</p>'; 
  260. //echo 'Mail Success'; 
  261. else 
  262. //echo 'Mail Error'; 
  263.  
  264. update_post_meta( $order_id, '_wc_uploaded_file_name_' . $key, $file['name'] ); 
  265. update_post_meta( $order_id, '_wc_uploaded_file_path_' . $key, $dburl ); 
  266. } else { 
  267. echo '<p class="error">' . __( 'There was an error in uploading your file(s).', 'wc_pay_to_upload') . '</p>'; 
  268. } else { 
  269. echo '<p class="error">' . sprintf( __( 'There %s filetype is not allowed.', 'wc_pay_to_upload'), $ext ) . '</p>'; 
  270.  
  271. $max_upload = (int)(ini_get('upload_max_filesize')); 
  272. $max_post = (int)(ini_get('post_max_size')); 
  273. $memory_limit = (int)(ini_get('memory_limit')); 
  274. $upload_mb = min($max_upload, $max_post, $memory_limit); 
  275.  
  276. echo '<form enctype="multipart/form-data" action="" method="POST">'; 
  277. $upload = false; 
  278. if( is_array( $items ) ) { 
  279. foreach( $items as $item ) { 
  280. /** Individual product checks, whether item ie enabled for upload. Ashok G. 31/07/2016 */ 
  281. if(get_post_meta( $item['product_id'], '_wc_ptu_enable', true ) == 1) 
  282. echo '<h3> Upload Files for '.$item['name'].'</h3>'; 
  283. $limit = (int) get_post_meta( $item['product_id'], '_wc_ptu_limit', true ); //Changed $item['item_id'] to $item['product_id'] for wordpress version 3.5.1 Ashok G 
  284. if( get_post_meta( $item['product_id'], '_wc_ptu_enable', true ) == 1 && $limit > 0 ) { 
  285. $limits = $limit; 
  286.  
  287. for ($i = 1; $i <= $limits; $i++) { 
  288. $item_id_upload = $item['product_id'].$i; 
  289. $url_upload = get_post_meta( $order_id, '_wc_uploaded_file_path_' . $item_id_upload, true ); 
  290. $name = get_post_meta( $order_id, '_wc_uploaded_file_name_' . $item_id_upload, true ); 
  291. echo '<label for="' . $i . '">File ' . $i . ': </label>'; 
  292. $file_name_append = $item['product_id'].$i; 
  293. $name = get_post_meta( $order_id, '_wc_uploaded_file_name_' . $file_name_append, true ); 
  294. if( empty( $name ) ) { 
  295. echo '<input type="file" name="' . $file_name_append . '" />'; 
  296. $upload = true; 
  297. } else { 
  298. echo $this->file_type_icons($url_upload, $order_id, $name, $item_id_upload, $x->post_status); 
  299. echo '<br/>'; 
  300.  
  301. if( $upload && $x->post_status == $this->defaultStatus) { 
  302. echo '<input type="submit" class="button" value="' . __( 'Upload', 'wc_pay_to_upload' ) . '" /><br/>'; 
  303. echo '<p>' . sprintf( __( 'Max upload size: %s', 'wc_pay_to_upload' ), $upload_mb ) . 'MB</p>'; 
  304. else 
  305. echo $upload; 
  306. echo '<p>' . sprintf( __( 'The order status has been changed so you cannot upload files contact Site Admin.', 'wc_pay_to_upload' )). ' </p>'; 
  307. echo '</form>'; 
  308.  
  309.  
  310. /** 
  311. File type icons display 
  312. */ 
  313. function file_type_icons($file, $order, $fname, $srl, $stats) 
  314.  
  315. $image = array('gif', 'png' , 'jpg', 'tiff', 'bmp', 'jpeg', 'svg'); 
  316. $ext = pathinfo($file, PATHINFO_EXTENSION); 
  317. if(in_array($ext, $image) ) { 
  318. $iconURL = $file; 
  319. $fileSize = 'width="250"'; 
  320.  
  321. else 
  322. switch($ext) { 
  323. case "csv": 
  324. $icon = 'csv.png'; 
  325. break; 
  326.  
  327. case "doc": 
  328. $icon = 'word.png'; 
  329. break; 
  330.  
  331. case "docx": 
  332. $icon = 'word.png'; 
  333. break; 
  334.  
  335. case "xls": 
  336. $icon = 'excel.png'; 
  337. break; 
  338.  
  339. case "xlsx": 
  340. $icon = 'excel.png'; 
  341. break; 
  342.  
  343. case "pdf": 
  344. $icon = 'pdf.png'; 
  345. break; 
  346.  
  347. case "txt": 
  348. $icon = 'text.png'; 
  349. break; 
  350.  
  351. default:  
  352. $icon = 'text.png'; 
  353. break; 
  354.  
  355. $iconURL = plugin_dir_url( __FILE__ ).'../icons/'.$icon; 
  356. $fileSize = ''; 
  357.  
  358.  
  359. if(( $stats == $this->defaultStatus )) { 
  360. $fileout = '<a href="'.$file.'"><img src="'.$iconURL.'" alt="'.$fname.'" '.$fileSize.' title="'.$fname.'"></a>   <a onclick="return confirm(\'Are you sure that you want delete '.$fname.' ?\');" href="?delete='.$order.'&item='.$srl.'">X</a>'; 
  361. else{ 
  362. $fileout = '<a href="'.$file.'"><img src="'.$iconURL.'" alt="'.$fname.'" '.$fileSize.' title="'.$fname.'"></a>'; 
  363. return $fileout; 
  364.  
  365. /** 
  366. Deleting the uploaded file 
  367. */ 
  368.  
  369.  
  370. function delete_file($oid, $itm) 
  371.  
  372. $file = get_post_meta($oid, '_wc_uploaded_file_name_'.$itm, true); 
  373. if($file !='') 
  374.  
  375. $fileurl = explode($_SERVER['HTTP_HOST'], $this->fileurl); 
  376. $dir = $_SERVER['DOCUMENT_ROOT'].$fileurl[1].'/'.$oid.'/'.$file; 
  377. unlink($dir); 
  378. delete_post_meta($oid, '_wc_uploaded_file_path_'.$itm); 
  379. delete_post_meta($oid, '_wc_uploaded_file_name_' .$itm); 
  380.  
  381. $url = explode('?', $_SERVER['REQUEST_URI']);  
  382. echo "<script>location.href='".$url[0]."'</script>"; 
  383.  
  384. else 
  385.  
  386.