WCML_Downloadable_Products

The WooCommerce Multilingual WCML Downloadable Products class.

Defined (1)

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

/inc/translation-editor/class-wcml-downloadable-products.php  
  1. class WCML_Downloadable_Products{ 
  2.  
  3. private $woocommerce_wpml; 
  4. private $sitepress; 
  5.  
  6. /** 
  7. * WCML_Downloadable_Products constructor. 
  8. * @param woocommerce_wpml $woocommerce_wpml 
  9. * @param SitePress $sitepress 
  10. */ 
  11. public function __construct( &$woocommerce_wpml, &$sitepress ) { 
  12. $this->woocommerce_wpml = &$woocommerce_wpml; 
  13. $this->sitepress = &$sitepress; 
  14.  
  15. //downloadable files custom 
  16. add_action( 'woocommerce_product_options_downloads', array( $this, 'product_options_downloads_custom_option' ) ); 
  17. add_action( 'woocommerce_variation_options_download', array( $this, 'product_options_downloads_custom_option' ), 10, 3 ); 
  18.  
  19. /** 
  20. * @param bool $loop 
  21. * @param bool $variation_data 
  22. * @param bool|WP_post $variation 
  23. */ 
  24. public function product_options_downloads_custom_option( $loop = false, $variation_data = false, $variation = false ) { 
  25. global $pagenow; 
  26.  
  27. $product_id = false; 
  28. $is_variation = false; 
  29. if( $pagenow === 'post.php' && isset( $_GET['post'] ) ) { 
  30. $product_id = $_GET['post']; 
  31. }elseif( isset( $_POST['product_id'] ) ) { 
  32. $product_id = $_POST['product_id']; 
  33.  
  34. if ( ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'product' && isset( $_GET['source_lang'] ) ) 
  35. || ( get_post_type( $product_id ) !== 'product' || ! $this->woocommerce_wpml->products->is_original_product( $product_id ) ) 
  36. ) { 
  37. return; 
  38.  
  39. $this->load_custom_files_js_css(); 
  40.  
  41. if( $variation ) { 
  42. $product_id = $variation->ID; 
  43. $is_variation = true; 
  44.  
  45. $download_options = new WCML_Custom_Files_UI( $this->woocommerce_wpml, $product_id, $is_variation ); 
  46. $download_options->show(); 
  47.  
  48.  
  49. public function load_custom_files_js_css() { 
  50. wp_register_style( 'wpml-wcml-files', WCML_PLUGIN_URL . '/res/css/wcml-files.css', null, WCML_VERSION ); 
  51. wp_register_script( 'wcml-scripts-files', WCML_PLUGIN_URL . '/res/js/files' . WCML_JS_MIN . '.js', array( 'jquery' ), WCML_VERSION ); 
  52.  
  53. wp_enqueue_style('wpml-wcml-files'); 
  54. wp_enqueue_script('wcml-scripts-files'); 
  55.  
  56. public function sync_files_to_translations( $original_id, $trnsl_id, $data ) { 
  57.  
  58. $custom_product_sync = get_post_meta( $original_id, 'wcml_sync_files', true ); 
  59.  
  60. if ( ( $custom_product_sync && $custom_product_sync === 'self' ) || ( ! $custom_product_sync && ! $this->woocommerce_wpml->settings['file_path_sync'] ) ) { 
  61. if( $data ) { 
  62. $orig_var_files = $this->get_files_data( $original_id ); 
  63. $file_paths_array = array(); 
  64. foreach( $orig_var_files as $key => $var_file ) { 
  65. $key_file = md5( $data[ md5( 'file-url'.$key.$original_id ) ].$data[ md5( 'file-name'.$key.$original_id ) ] ); 
  66. $file_paths_array[ $key_file ][ 'name' ] = $data[ md5( 'file-name'.$key.$original_id ) ]; 
  67. $file_paths_array[ $key_file ][ 'file' ] = $data[ md5( 'file-url'.$key.$original_id ) ]; 
  68. update_post_meta( $trnsl_id, '_downloadable_files', $file_paths_array ); 
  69. } elseif ( ( $custom_product_sync && $custom_product_sync === 'auto' ) || $this->woocommerce_wpml->settings['file_path_sync'] ) { 
  70. $orig_file_path = maybe_unserialize( get_post_meta( $original_id, '_downloadable_files', true ) ); 
  71. update_post_meta( $trnsl_id, '_downloadable_files', $orig_file_path ); 
  72.  
  73. public function save_files_option( $post_id ) { 
  74. $nonce = filter_input( INPUT_POST, 'wcml_save_files_option_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); 
  75.  
  76. if ( isset( $_POST['wcml_file_path_option'], $nonce ) && wp_verify_nonce( $nonce, 'wcml_save_files_option' ) ) { 
  77. if( isset( $_POST[ 'wcml_file_path_option' ][ $post_id ] ) ) { 
  78. if ( $_POST['wcml_file_path_option'][ $post_id ] === 'on' ) { 
  79. update_post_meta( $post_id, 'wcml_sync_files', $_POST[ 'wcml_file_path_sync' ][ $post_id ] ); 
  80. }else{ 
  81. delete_post_meta( $post_id, 'wcml_sync_files' ); 
  82.  
  83. public function get_files_data( $product_id ) { 
  84.  
  85. $files = maybe_unserialize( get_post_meta( $product_id, '_downloadable_files', true ) ); 
  86. $files_data = array(); 
  87. if ( $files ) { 
  88. foreach ($files as $file) { 
  89. $files_info = array(); 
  90. $files_info['value'] = $file['file']; 
  91. $files_info['label'] = $file['name']; 
  92. $files_data[] = $files_info; 
  93.  
  94. return $files_data;