WC_Naguro_Downloader

The WooCommerce Naguro Connect WC Naguro Downloader class.

Defined (1)

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

/includes/woocommerce/core/class-wc-naguro-downloader.php  
  1. class WC_Naguro_Downloader { 
  2. public function __construct() { 
  3. add_action( 'init', array( $this, 'rewrite_rules' ), 1 ); 
  4. add_action( 'template_redirect', array( $this, 'listener' ) ); 
  5.  
  6. public function upload_mimes( $types ) { 
  7. $types['html'] = 'text/html'; 
  8. return $types; 
  9.  
  10. public function rewrite_rules() { 
  11. add_rewrite_endpoint( 'naguro-editor', EP_ROOT ); 
  12.  
  13. public function listener() { 
  14. add_filter( 'upload_mimes', array( $this, 'upload_mimes' ) ); 
  15.  
  16. if ( 'download' != get_query_var( 'naguro-editor' ) ) { 
  17. return; 
  18.  
  19. include(ABSPATH . 'wp-admin/includes/file.php' ); 
  20.  
  21. $data = $_POST['data']; 
  22. $store_array = array(); 
  23.  
  24. $type_arr = array( 'html' => 'url', 'css' => 'url', 'js' => 'url' ); 
  25.  
  26. foreach ( $type_arr as $key => $value_to_store ) { 
  27. $assets_api_url = apply_filters( 'wc_naguro_assets_api_endpoint_url', 'http://api.naguro.com/' ); 
  28. $file_url = $assets_api_url . $data[$key]; 
  29. $temp_file = download_url( $file_url, 600 ); 
  30.  
  31. // array based on $_FILE as seen in PHP file uploads 
  32. $file = array( 
  33. 'name' => basename($file_url),  
  34. 'type' => 'image/png',  
  35. 'tmp_name' => $temp_file,  
  36. 'error' => 0,  
  37. 'size' => filesize($temp_file),  
  38. ); 
  39.  
  40. $overrides = array( 
  41. 'test_form' => false,  
  42. 'test_size' => true,  
  43. 'test_upload' => true,  
  44. ); 
  45.  
  46. // move the temporary file into the uploads directory 
  47. $results = wp_handle_sideload( $file, $overrides ); 
  48.  
  49. if (!empty($results['error'])) { 
  50. exit; 
  51. } else { 
  52. $store_array[$key] = $results[ $value_to_store ]; 
  53.  
  54. update_option( 'naguro_editor_' . $data['hash'], $store_array ); 
  55.  
  56. remove_filter( 'upload_mimes', array( $this, 'upload_mimes' ) ); 
  57.  
  58. exit;