/** * Save downloadable files. * * @param WC_Product $product Product instance. * @param array $downloads Downloads data. * @param int $deprecated Deprecated since 2.7. * @return WC_Product */ private function save_downloadable_files($product, $downloads, $deprecated = 0) { if ($deprecated) { wc_deprecated_argument('variation_id', '2.7', 'save_downloadable_files() not requires a variation_id anymore.'); } $files = array(); foreach ($downloads as $key => $file) { if (empty($file['file'])) { continue; } $download = new WC_Product_Download(); $download->set_id($key); $download->set_name($file['name'] ? $file['name'] : wc_get_filename_from_url($file['file'])); $download->set_file(apply_filters('woocommerce_file_download_path', $file['file'], $product, $key)); $files[] = $download; } $product->set_downloads($files); return $product; }
/** * Read downloads from post meta. * * @param WC_Product * @since 2.7.0 */ protected function read_downloads(&$product) { $meta_values = array_filter((array) maybe_unserialize(get_post_meta($product->get_id(), '_downloadable_files', true))); if ($meta_values) { $downloads = array(); foreach ($meta_values as $key => $value) { $download = new WC_Product_Download(); $download->set_id($key); $download->set_name($value['name'] ? $value['name'] : wc_get_filename_from_url($value['file'])); $download->set_file(apply_filters('woocommerce_file_download_path', $value['file'], $product, $key)); $downloads[] = $download; } $product->set_downloads($downloads); } }
/** * Set downloads. * * @since 2.7.0 * @param $downloads_array array of WC_Product_Download objects or arrays. */ public function set_downloads($downloads_array) { $downloads = array(); $errors = array(); foreach ($downloads_array as $download) { if (is_a($download, 'WC_Product_Download')) { $download_object = $download; } else { $download_object = new WC_Product_Download(); $download_object->set_id(md5($download['file'])); $download_object->set_name($download['name']); $download_object->set_file($download['file']); if (isset($download['previous_hash'])) { $download_object->set_previous_hash($download['previous_hash']); } } // Validate the file extension if (!$download_object->is_allowed_filetype()) { $errors[] = sprintf(__('The downloadable file %1$s cannot be used as it does not have an allowed file type. Allowed types include: %2$s', 'woocommerce'), '<code>' . basename($download_object->get_file()) . '</code>', '<code>' . implode(', ', array_keys($download_object->get_allowed_mime_types())) . '</code>'); continue; } // Validate the file exists. if (!$download_object->file_exists()) { $errors[] = sprintf(__('The downloadable file %s cannot be used as it does not exist on the server.', 'woocommerce'), '<code>' . $download_object->get_file() . '</code>'); continue; } $downloads[$download_object->get_id()] = $download_object; } if ($errors) { $this->error('product_invalid_download', $errors[0]); } $this->set_prop('downloads', $downloads); }