as $child_id => $child_download_data ) { $file_url = $parent_download_data['file']; $must_create_permission = // The variation offers the same file as the parent for download... in_array( $file_url, array_keys( $child_download_data['download_ids_by_file_url'] ), true ) && // ...but no equivalent download permission (same file URL, order id and user id) exists. ! array_key_exists( $parent_file_order_and_user, $child_download_data['permission_data_by_file_order_user'] ); if ( $must_create_permission ) { // The new child download permission is a copy of the parent's, // but with the product and download ids changed to match those of the variation. $new_download_data = $parent_download_data['data']; $new_download_data['product_id'] = $child_id; $new_download_data['download_id'] = $child_download_data['download_ids_by_file_url'][ $file_url ]; $this->downloads_data_store->create_from_data( $new_download_data ); } } } } /** * Get the existing downloadable files and download permissions for a given product. * The returned value is an array with two keys: * * - download_ids_by_file_url: an associative array of file url => download_id. * - permission_data_by_file_order_user: an associative array where key is "file_url:customer_id:order_id" and value is the full permission data set. * * @param \WC_Product $product The product to get the downloadable files and permissions for. * @return array[] Information about the downloadable files and permissions for the product. */ private function get_download_files_and_permissions( \WC_Product $product ) { $result = array( 'permission_data_by_file_order_user' => array(), 'download_ids_by_file_url' => array(), ); $downloads = $product->get_downloads(); foreach ( $downloads as $download ) { $result['download_ids_by_file_url'][ $download->get_file() ] = $download->get_id(); } $permissions = $this->downloads_data_store->get_downloads( array( 'product_id' => $product->get_id() ) ); foreach ( $permissions as $permission ) { $permission_data = (array) $permission->data; if ( array_key_exists( $permission_data['download_id'], $downloads ) ) { $file = $downloads[ $permission_data['download_id'] ]->get_file(); $data = array( 'file' => $file, 'data' => (array) $permission->data, ); $result['permission_data_by_file_order_user'][ "{$file}:{$permission_data['user_id']}:{$permission_data['order_id']}" ] = $data; } } return $result; } }