Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ phpcs-report.txt
!/assets/css/vue-vendor.css
!/assets/css/vue-frontend.css
!/assets/css/wp-version-before-5-3.css
.vscode
45 changes: 44 additions & 1 deletion includes/REST/OrderControllerV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WeDevs\Dokan\REST;

use WC_Customer_Download;
use WC_Data_Store;
use WP_Error;
use WP_REST_Server;
Expand Down Expand Up @@ -59,6 +60,16 @@
'required' => true,
],
],
'download_remaining' => [
'type' => 'integer',
'description' => __( 'Download remaining.', 'dokan-lite' ),
'required' => false,
],
'access_expires' => [
'type' => 'string',
'description' => __( 'Access expires.', 'dokan-lite' ),
'required' => false,
],
],
],
[
Expand Down Expand Up @@ -152,7 +163,7 @@
$downloads = array_filter(
$download_permissions,
function ( $download ) use ( $existing_product_ids ) {
return in_array( $download->product_id, $existing_product_ids );

Check failure on line 166 in includes/REST/OrderControllerV2.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Not using strict comparison for in_array; supply true for $strict argument.
}
);

Expand Down Expand Up @@ -230,6 +241,8 @@
public function grant_order_downloads( $requests ) {
$order_id = intval( $requests->get_param( 'id' ) );
$product_ids = array_filter( array_map( 'absint', (array) wp_unslash( $requests->get_param( 'ids' ) ) ) );
$remaining = $requests->get_param( 'download_remaining' );
$expiry = $requests->get_param( 'access_expires' );
$file_counter = 0;
$order = dokan()->order->get( $order_id );
$data = [];
Expand All @@ -243,7 +256,37 @@
$files = $product->get_downloads();

foreach ( $files as $download_id => $file ) {
$inserted_id = wc_downloadable_file_permission( $download_id, $product_id, $order );
$data_store = WC_Data_Store::load( 'customer-download' );
$existing_permissions = $data_store->get_downloads(
[
'order_id' => $order->get_id(),
'product_id' => $product_id,
'download_id' => $download_id,
'limit' => 1,
]
);

$download = null;
$inserted_id = 0;

if ( ! empty( $existing_permissions ) ) {
$download = reset( $existing_permissions );
$inserted_id = $download->get_id();
} else {
$inserted_id = wc_downloadable_file_permission( $download_id, $product_id, $order );
if ( $inserted_id ) {
$download = new WC_Customer_Download( $inserted_id );
if ( $download ) {
if ( $remaining ) {
$download->set_downloads_remaining( $remaining );
}
if ( $expiry ) {
$download->set_access_expires( $expiry );
}
$download->save();
}
}
}

if ( $inserted_id ) {
++$file_counter;
Expand Down
Loading