/inc/umf-funct.php

  1. <?php 
  2.  
  3. if ( ! class_exists( 'WOO_Check' ) ) 
  4. require_once 'class-check-woocommerce.php'; 
  5.  
  6. /** 
  7. * WC Detection 
  8. */ 
  9. if ( ! function_exists( 'is_woocommerce_active' ) ) { 
  10. function is_woocommerce_active() { 
  11. return WOO_Check::woocommerce_active_check(); 
  12.  
  13. /**admin messages*/ 
  14. if(!function_exists('showMessage')) { 
  15. function showMessage($message, $errormsg = false) 
  16. if ($errormsg) { echo '<div id="message" class="error">';} 
  17. else {echo '<div id="message" class="updated fade">';} 
  18. echo "<p>$message</p></div>"; 
  19. function showAdminMessages() {showMessage(__( 'WooCommerce is not active. Please activate plugin before using WooCommerce Upload My File plugin.', 'woocommerce_umf'), true);} 
  20.  
  21. /** WordPress Administration Menu 
  22. Toont een nieuw menu item als submenu van WooCommerce 
  23. */ 
  24. function woocommerce_umf_admin_menu() { 
  25. $page = add_submenu_page('woocommerce', __( 'Upload My File', 'woocommerce-umf' ), __( 'Upload My File', 'woocommerce-umf' ), 'manage_woocommerce', 'woocommerce_umf', 'woocommerce_umf_page' ); 
  26.  
  27. /** Add meta boxes to pages 
  28. Toont een nieuwe box op de product pagina en op de bestel pagina. 
  29. */ 
  30. function woocommerce_umf_add_box() { 
  31. add_meta_box( 'woocommerce-umf-box-product', __( 'Upload Files', 'woocommerce-umf' ), 'woocommerce_umf_box_product', 'product', 'side', 'default' ); 
  32. add_meta_box( 'woocommerce-umf-box-order-detail', __( 'Uploaded Files', 'woocommerce-umf' ), 'woocommerce_umf_box_order_detail', 'shop_order', 'side', 'default' ); 
  33.  
  34. function is_localhost() { 
  35. $whitelist = array( '127.0.0.1', '::1' ); 
  36. if( in_array( $_SERVER['REMOTE_ADDR'], $whitelist) ) 
  37. return true; 
  38.  
  39. /** Inhoud van de box op de order-detail pagina*/ 
  40. function woocommerce_umf_box_order_detail($post) { 
  41. $order=new WC_Order($post->ID); 
  42.  
  43. $j=1; 
  44. /** per product een formulier met gegevens */ 
  45. foreach ( $order->get_items() as $order_item ) { 
  46. $max_upload_count=0; 
  47. $max_upload_count=get_max_upload_count($order, $order_item['product_id']); 
  48. if($max_upload_count!=0) { 
  49. $product=new WC_Product($order_item['product_id']); 
  50. //$item_meta = new WC_Order_Item_Meta( $order_item['item_meta'], $order_item['product_id'] ); 
  51. $item_meta = new WC_Order_Item_Meta( $order_item, $product ); 
  52. $forproduct=$order_item['name'].' ('.$item_meta->display($flat=true, $return=true).')'; 
  53. echo '<strong>'; 
  54. printf( __('File for product: %s:', 'woocommerce-umf'), $forproduct); 
  55. echo '</strong><br>'; 
  56.  
  57. /** Controle of er al een bestand is geupload */ 
  58. $i=1; 
  59. $upload_count=0; 
  60. echo '<ul>'; 
  61. while ($i <= $max_upload_count) { 
  62. echo '<li>'; 
  63. $name = get_post_meta( $post->ID, '_woo_umf_uploaded_file_name_' . $j, true ); 
  64.  
  65. if (is_localhost()) { 
  66.  
  67. $url = get_post_meta( $post->ID, '_woo_umf_uploaded_file_path_' . $j, true ); 
  68.  
  69. } else { 
  70.  
  71. $url = site_url( str_replace( ABSPATH, '', get_post_meta( $post->ID, '_woo_umf_uploaded_file_path_' . $j, true ) ) ); 
  72.  
  73. $forproduct = get_post_meta( $post->ID, '_woo_umf_uploaded_product_name_' . $j, true ); 
  74. /** geen bestand geupload, dus toon upload velden */ 
  75. if( !empty( $url ) && !empty( $name ) ) { 
  76. printf( '<a href="%s" target="_blank">%s</a>', $url, $name ); 
  77. $upload_count++; 
  78. } else { 
  79. echo '<span style="color:red;">'; 
  80. printf( __('File #%s has not been uploaded.', 'woocommerce-umf'), $i ); 
  81. echo '</span>'; 
  82. $i++; 
  83. $j++; 
  84. echo '</li>'; 
  85. echo '</ul>'; 
  86. /** toon aantal nog aan te leveren bestanden */ 
  87. $upload_count=$max_upload_count-$upload_count; 
  88. echo '<p>'; 
  89. printf( __('Files to be uploaded for this item: %s', 'woocommerce-umf'), $upload_count ); 
  90. echo '</p>'; 
  91.  
  92. function umf_my_orders_columns($columns) { 
  93.  
  94. if (is_array($columns)) { 
  95.  
  96. $new_columns = array(); 
  97.  
  98. foreach ($columns AS $column_key => $column_title) { 
  99.  
  100. $new_columns[$column_key] = $column_title; 
  101.  
  102. if ($column_key == 'order-total') { 
  103. $new_columns['order-uploads'] = __( 'Files', 'woocommerce-umf'); 
  104.  
  105.  
  106.  
  107. return $new_columns; 
  108.  
  109. function umf_my_orders_uploads_column($order) 
  110.  
  111. if(check_for_files($order->id)=='upload') { 
  112. echo '<a href="'.$order->get_view_order_url().'">'.__( 'Upload', 'woocommerce-umf' ).'</a>'; 
  113. } elseif(check_for_files($order->id)=='blank') { echo '-'; } 
  114.  
  115.  
  116. function umf_order_actions($actions, $order) { 
  117.  
  118. if(check_for_files($order->id)=='upload') { 
  119.  
  120. $actions['upload'] = array( 
  121. 'url' => $order->get_view_order_url(),  
  122. 'name' => __( 'Upload files', 'woocommerce-umf' ) 
  123. ); 
  124.  
  125. return $actions; 
  126.  
  127.  
  128. /** Inhoud van de box op de product bewerk pagina*/ 
  129. function woocommerce_umf_box_product($post) { 
  130. wp_nonce_field( 'woo_umf_nonce', 'woo_umf_nonce' ); 
  131. echo '<p>'; 
  132. echo '<label for="_woo_umf_enable">' . __('Enable', 'woocommerce-umf' ) . ': </label> '; 
  133. echo '<input type="hidden" name="_woo_umf_enable" value="0" />'; 
  134. $myarray=get_post_meta( $post->ID, '_woo_umf_enable'); 
  135. $checked=checked( get_post_meta( $post->ID, '_woo_umf_enable', true ), 1, false ); 
  136. echo '<input type="checkbox" id="_woo_umf_enable" class="checkbox" name="_woo_umf_enable" value="1" ' . $checked . ' />'; 
  137. echo '</p>'; 
  138.  
  139. /** Instellingen bewaren*/ 
  140. function save_meta_settings( $post_id ) { 
  141. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; 
  142. if ( !isset( $_POST['woo_umf_nonce'] ) || !wp_verify_nonce( $_POST['woo_umf_nonce'], 'woo_umf_nonce' ) ) return; 
  143. update_post_meta( $post_id, '_woo_umf_enable', (int) $_POST['_woo_umf_enable'] ); 
  144.  
  145. /** functie om de producten te tonen*/ 
  146. function woocommerce_umf_get_product( $product_id, $args = array() ) { 
  147. $product = null; 
  148. if ( version_compare( WOOCOMMERCE_VERSION, "2.0.0" ) >= 0 ) { 
  149. // WC 2.0 
  150. $product = get_product( $product_id, $args ); 
  151. } else { 
  152. // old style, get the product or product variation object 
  153. if ( isset( $args['parent_id'] ) && $args['parent_id'] ) { 
  154. $product = new WC_Product_Variation( $product_id, $args['parent_id'] ); 
  155. } else { 
  156. // get the regular product, but if it has a parent, return the product variation object 
  157. $product = new WC_Product( $product_id ); 
  158. if ( $product->get_parent() ) { 
  159. $product = new WC_Product_Variation( $product->id, $product->get_parent() ); 
  160. return $product; 
  161.  
  162. /** functie om de product eigenschappen te tonen*/ 
  163. if (!function_exists('woocommerce_umf_get_product_meta')) { 
  164. function woocommerce_umf_get_product_meta( $product, $field_name ) { 
  165.  
  166. if ( version_compare( WOOCOMMERCE_VERSION, "2.0.0" ) >= 0 ) { 
  167. // even in WC >= 2.0 product variations still use the product_custom_fields array apparently 
  168. if ( $product->variation_id && isset( $product->product_custom_fields[ '_' . $field_name ][0] ) && $product->product_custom_fields[ '_' . $field_name ][0] !== '' ) { 
  169. return $product->product_custom_fields[ '_' . $field_name ][0]; 
  170. // use magic __get 
  171. return maybe_unserialize( $product->$field_name ); 
  172. } else { 
  173. // use product custom fields array 
  174.  
  175. // variation support: return the value if it's defined at the variation level 
  176. if ( isset( $product->variation_id ) && $product->variation_id ) { 
  177.  
  178. if ( ( $value = get_post_meta( $product->variation_id, '_' . $field_name, true ) ) !== '' ) return $value; 
  179. // otherwise return the value from the parent 
  180. return get_post_meta( $product->id, '_' . $field_name, true ); 
  181.  
  182. // regular product 
  183. return isset( $product->product_custom_fields[ '_' . $field_name ][0] ) ? $product->product_custom_fields[ '_' . $field_name ][0] : null; 
  184.  
  185. function get_max_upload_count($order, $order_item=0) { 
  186. $max_upload_count=0; 
  187. //product specifiek 
  188. if( (( is_array( get_option( 'woocommerce_umf_status' ) ) && in_array( $order->status, get_option( 'woocommerce_umf_status' ) ) ) ) || $order->status == get_option( 'woocommerce_umf_status' ) ) { 
  189. if($order_item!=0) { 
  190. $product = woocommerce_umf_get_product($order_item); 
  191. if( woocommerce_umf_get_product_meta($product, 'woo_umf_enable') == 1) { 
  192. $max_upload_count=1; 
  193. } else { 
  194. // order totaal 
  195. foreach ( $order->get_items() as $order_item ) { 
  196. $product = woocommerce_umf_get_product($order_item['product_id']); 
  197. $limit=1; 
  198. if( woocommerce_umf_get_product_meta($product, 'woo_umf_enable') == 1 && $limit > 0 ) { 
  199. $max_upload_count+=$limit; 
  200. return $max_upload_count; 
  201.  
  202. /** functie controleert of bestanden zijn geupload of niet */ 
  203. function check_for_files( $order_id ) { 
  204. $upload=false; 
  205. $order = new WC_Order( $order_id ); 
  206. if( (( is_array( get_option( 'woocommerce_umf_status' ) ) && in_array( $order->status, get_option( 'woocommerce_umf_status' ) ) ) ) || $order->status == get_option( 'woocommerce_umf_status' ) ) { 
  207. if(get_max_upload_count( $order ) >0 ) { 
  208. for ($i = 1; $i <= get_max_upload_count($order); $i++) { 
  209. $name = get_post_meta( $order_id, '_woo_umf_uploaded_file_name_' . $i, true ); 
  210. if( empty( $name ) ) {$upload = true;} 
  211. if($upload==true) {return 'upload';} else { return 'done'; } 
  212. } else { return 'blank';} 
  213.  
  214.  
  215. /** show upload fields on order detail page 
  216. Deze functie toont de upload velden op de order-detail pagina van de klant 
  217. */ 
  218. function upload_files_field( $order_id ) { 
  219. $order = new WC_Order( $order_id ); 
  220.  
  221. /** upload handler */ 
  222. if( isset( $_FILES ) && isset($_POST['upload']) && $_POST['upload']) { 
  223.  
  224. $upload_dir=wp_upload_dir(); 
  225. $path = trailingslashit( trailingslashit( $upload_dir['basedir'].'/umf/' ) . $order_id ); 
  226.  
  227. foreach( $_FILES as $key => $umf_file ) { 
  228. if( empty( $umf_file['name'] ) ) continue; 
  229. wp_mkdir_p( $path ); 
  230. $umf_filepath = $path . $order_id.'_'.$key.'_'.$umf_file['name']; 
  231. $ext = strtolower( pathinfo( $umf_filepath, PATHINFO_EXTENSION ) ); 
  232. $doctypes = explode( ', ', str_replace('.', '', get_option( 'woocommerce_umf_allowed_file_types' ) )); 
  233. foreach($doctypes as $k => $v) { $doctypes[$k] = strtolower( trim( $v ) ); } 
  234. if( in_array( $ext, $doctypes ) ) $typeallow = true; 
  235. else $typeallow = false; 
  236. if($typeallow==false) { 
  237. $upload_error.= '<li>' . sprintf( __( 'The %s file type is not allowed.', 'woocommerce-umf'), $ext ) . '</li>'; 
  238. $filesize=(int)get_option( 'woocommerce_umf_max_uploadsize' ); 
  239. $max_upload = (int)(ini_get('upload_max_filesize')); 
  240. $max_post = (int)(ini_get('post_max_size')); 
  241. $memory_limit = (int)(ini_get('memory_limit')); 
  242. $max_mb = min($max_upload, $max_post, $memory_limit); 
  243. if($filesize > $max_mb) { $filesize=$max_mb;} 
  244. if($_FILES[$key]["size"]< ($filesize*1024*1024)) 
  245. {$sizeallow=true; } else {$sizeallow=false; $upload_error.= '<li">' . sprintf( __( 'The file "%s" is to big.', 'woocommerce-umf'), $umf_file['name'] ) . '</li>';} 
  246.  
  247. if( $typeallow == true && $sizeallow == true ) { 
  248. if( copy( $umf_file['tmp_name'], $umf_filepath ) ) { 
  249. $success=true; 
  250. update_post_meta( $order_id, '_woo_umf_uploaded_file_name_' . $key, $umf_file['name'] ); 
  251. update_post_meta( $order_id, '_woo_umf_uploaded_file_path_' . $key, $umf_filepath ); 
  252. update_post_meta( $order_id, '_woo_umf_uploaded_product_name_' . $key, $_POST['uploaded_product_name'][$key] ); 
  253. } else { 
  254. $upload_error.= '<li>' . __( 'There was an error while uploading your file(s).', 'woocommerce-umf') . '</li>'; 
  255. if($success==true && isset($upload_error) && $upload_error!="") { 
  256. echo '<p class="woocommerce-info woo-umf-success updated">' . __( 'There was a problem while uploading your files.', 'woocommerce-umf') . '<br>' . __( 'Not all files have been successfully uploaded.', 'woocommerce-umf') . '</p>'; 
  257. if($success==true && isset($upload_error) && $upload_error=="") { 
  258. echo '<p class="woocommerce-message woo-umf-success success">' . __( 'Your file(s) were uploaded successfully.', 'woocommerce-umf') . '</p>'; 
  259. if(isset($upload_error) && $upload_error!="") { 
  260. echo '<div class="woocommerce-error woo-umf-error error"><ul>'; 
  261. echo $upload_error; 
  262. echo '</ul></div>'; 
  263.  
  264. /** wanneer er meer dan 1 bestand geupload kan worden, ga verder */ 
  265. if(get_max_upload_count($order) > 0) { 
  266. echo '<h2>' . __( 'Upload files', 'woocommerce-umf' ) . '</h2>'; 
  267.  
  268. /**begin form*/ 
  269. echo '<form enctype="multipart/form-data" action="" method="POST" class=woo-umf-form>'; 
  270.  
  271. $j=1; 
  272. /** per product een formulier met gegevens */ 
  273. foreach ( $order->get_items() as $order_item ) { 
  274. $max_upload_count=0; 
  275. $max_upload_count=get_max_upload_count($order, $order_item['product_id']); 
  276. $product=new WC_Product($order_item['product_id']); 
  277. $item_meta = new WC_Order_Item_Meta( $order_item, $product ); 
  278. if($max_upload_count!=0) { 
  279. // start fieldset per product item 
  280. echo '<fieldset class="woo-umf-product">'; 
  281. if($item_meta->display($flat=true, $return=true)!="") {$variation='<span class=woo-umf-variations>'.$item_meta->display($flat=true, $return=true).'</span>';} else {$variation='';} 
  282. echo '<legend>'.$order_item['name'].' '.$variation.'</span></legend>'; 
  283. /** Controle of er al een bestand is geupload */ 
  284. $i=1; 
  285. $upload_count=0; 
  286. while ($i <= $max_upload_count) { 
  287. $name = get_post_meta( $order_id, '_woo_umf_uploaded_file_name_' . $j, true ); 
  288. /** geen bestand geupload, dus toon upload velden */ 
  289. if($name=="") { 
  290. echo '<div class=woo-umf-item-box><input type="file" name="'.$j.'" /> <span class="umf-btn umf-icon-info"><em>'.woo_um_get_allowed_filetypes($order_item['product_id']).'</em></span></div>'; 
  291. echo '<input type="hidden" value="'.strip_tags($order_item['name'].' - '.$item_meta->display($flat=true, $return=true)).'" name="uploaded_product_name['.$j.']">'; 
  292. $upload=true; 
  293. } else { 
  294. /** alle bestanden zijn geupload, toon bestandsnamen */ 
  295. echo '<p>'.$name.'</p>'; 
  296. $upload_count++; 
  297. $i++; 
  298. $j++; 
  299. echo '</fieldset>'; 
  300.  
  301. /** knoppen */ 
  302. if( isset($upload) ) { echo '<input type="submit" name=upload class="button uploadfiles" value="' . __( 'Upload', 'woocommerce-umf' ) . '" />';} 
  303.  
  304. /** upload info */ 
  305. echo '<legend>'; 
  306. $max_upload = (int)(ini_get('upload_max_filesize')); 
  307. $max_post = (int)(ini_get('post_max_size')); 
  308. $memory_limit = (int)(ini_get('memory_limit')); 
  309. $max_mb = min($max_upload, $max_post, $memory_limit); 
  310.  
  311. if(get_option( 'woocommerce_umf_max_uploadsize' )) {$upload_mb=(int)(get_option( 'woocommerce_umf_max_uploadsize' ));} else { $upload_mb = $max_mb; } 
  312. if($upload_mb > $max_mb) { $upload_mb = $max_mb;} 
  313.  
  314. echo '<p>' . sprintf( __( 'Max upload size per file: %s', 'woocommerce-umf' ), $upload_mb ) . 'MB. '; 
  315. echo sprintf( __( 'Max total upload size: %s', 'woocommerce-umf' ), $max_mb ) . 'MB</p>'; 
  316. echo '</legend>'; 
  317.  
  318. echo '</form>'; 
  319. /** eind form*/ 
  320. /** 
  321. * woo_umf_styles 
  322. * Add basic frontend styling if is choosen in admin 
  323. * Since 0.2 
  324. */ 
  325. function woo_umf_styles() { 
  326. wp_enqueue_style('woo-umf-style', plugins_url('css/woo-umf.css', dirname(__FILE__))); 
  327. /** 
  328. * Get allowed or disallowed filetypes and corresponding language strings 
  329. * @since 0.2 
  330. */ 
  331. function woo_um_get_allowed_filetypes($product_id) { 
  332. if(get_post_meta($product_id, '_woo_umf_filetypes', true )=="") { 
  333. $filetypes = get_option( 'woocommerce_umf_allowed_file_types' ); 
  334. } else { 
  335. $filetypes = get_post_meta( $product_id, '_woo_umf_filetypes', true ); 
  336. return __('Allowed filetypes:', 'woocommerce-umf').' '.$filetypes; 
  337.  
  338. /** 
  339. * attach email link to emails, this removes the email templates inside /woocommerce-upload-my-file/templates/ 
  340. * @since 0.1 
  341. */ 
  342. function umf_mail($order) { 
  343. $count=get_max_upload_count($order); 
  344. if($count>0) { 
  345. if(check_for_files($order->id)=='upload') { 
  346. echo '<h2>'._n('Upload file', 'Upload files', $count, 'woocommerce-umf' ).'</h2>'; 
  347.  
  348. if ( version_compare( WOOCOMMERCE_VERSION, "2.1" ) <= 0 ) { 
  349. echo '<p><a href="'. esc_url( add_query_arg('order', $order->id, get_permalink(woocommerce_get_page_id('view_order'))) ).'" ><span>'._n( 'Login to upload your file and attach it to your order.', 'Login to upload your files and attach them to your order.', $count, 'woocommerce-umf' ).'</span></a>'; 
  350. } else { 
  351. echo '<p><a href="'. esc_url( $order->get_view_order_url() ).'" ><span>'._n( 'Login to upload your file and attach it to your order.', 'Login to upload your files and attach them to your order.', $count, 'woocommerce-umf' ).'</span></a>'; 
  352. ?> 
.