-
Notifications
You must be signed in to change notification settings - Fork 214
Feat/downloadable permission rest #3092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 3 commits
e8f6775
481973b
13a0230
e4311dd
d918ae0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ | |
|
|
||
| namespace WeDevs\Dokan\REST; | ||
|
|
||
| use WC_Customer_Download; | ||
| use WC_Data; | ||
| use WC_Data_Store; | ||
| use WC_Product; | ||
| use WC_Product_Variation; | ||
| use WP_Error; | ||
|
|
@@ -316,6 +318,73 @@ public function register_routes() { | |
| ], | ||
| ] | ||
| ); | ||
|
|
||
| register_rest_route( | ||
| $this->namespace, '/' . $this->base . '/grant-downloadable-access', [ | ||
| [ | ||
| 'methods' => WP_REST_Server::CREATABLE, | ||
| 'callback' => [ $this, 'grant_downloadable_access' ], | ||
| 'permission_callback' => [ $this, 'get_product_permissions_check' ], | ||
| 'args' => [ | ||
| 'order_id' => [ | ||
| 'description' => __( 'Order ID', 'dokan-lite' ), | ||
| 'type' => 'integer', | ||
| 'required' => true, | ||
| ], | ||
| 'product_ids' => [ | ||
| 'description' => __( 'Product IDs', 'dokan-lite' ), | ||
| 'type' => 'array', | ||
| 'items' => [ | ||
| 'type' => 'integer', | ||
| ], | ||
| 'required' => true, | ||
| ], | ||
| 'download_remaining' => [ | ||
| 'description' => __( 'Download Remaining', 'dokan-lite' ), | ||
| 'type' => 'integer', | ||
| 'required' => false, | ||
| ], | ||
| 'access_expires' => [ | ||
| 'description' => __( 'Access Expires', 'dokan-lite' ), | ||
| 'type' => 'date', | ||
| 'required' => false, | ||
| ], | ||
| ], | ||
| ], | ||
| ] | ||
| ); | ||
|
|
||
| register_rest_route( | ||
| $this->namespace, '/' . $this->base . '/revoke-downloadable-access', [ | ||
| [ | ||
| 'methods' => WP_REST_Server::CREATABLE, | ||
| 'callback' => [ $this, 'revoke_access_to_download' ], | ||
| 'permission_callback' => [ $this, 'get_product_permissions_check' ], | ||
| 'args' => [ | ||
| 'download_id' => [ | ||
| 'description' => __( 'Download ID', 'dokan-lite' ), | ||
| 'type' => 'string', | ||
| 'required' => true, | ||
| ], | ||
| 'product_id' => [ | ||
| 'description' => __( 'Product ID', 'dokan-lite' ), | ||
| 'type' => 'integer', | ||
| 'required' => true, | ||
| ], | ||
| 'order_id' => [ | ||
| 'description' => __( 'Order ID', 'dokan-lite' ), | ||
| 'type' => 'integer', | ||
| 'required' => true, | ||
| ], | ||
| 'permission_id' => [ | ||
| 'description' => __( 'Permission ID', 'dokan-lite' ), | ||
| 'type' => 'integer', | ||
| 'required' => true, | ||
| ], | ||
| ], | ||
| ], | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1739,6 +1808,128 @@ public function get_multistep_categories() { | |
| return rest_ensure_response( $categories ); | ||
| } | ||
|
|
||
| public function grant_downloadable_access( $request ) { | ||
| $order_id = $request->get_param( 'order_id' ); | ||
| $product_ids = $request->get_param( 'product_ids' ); | ||
| $remaining = $request->get_param( 'download_remaining' ); | ||
| $expiry = $request->get_param( 'access_expires' ); | ||
|
|
||
| if ( ! is_array( $product_ids ) ) { | ||
| $product_ids = [ $product_ids ]; | ||
| } | ||
|
|
||
| $order = dokan()->order->get( $order_id ); | ||
| $granted_files = []; | ||
|
|
||
| foreach ( $product_ids as $product_id ) { | ||
| $product = wc_get_product( $product_id ); | ||
| if ( ! $product ) { | ||
| continue; | ||
| } | ||
| $files = $product->get_downloads(); | ||
|
|
||
| if ( ! $order->get_billing_email() ) { | ||
| continue; | ||
| } | ||
|
|
||
|
||
| if ( $files ) { | ||
| foreach ( $files as $download_id => $file ) { | ||
| $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(); | ||
| $granted_files[] = [ | ||
| 'name' => $file->get_name(), | ||
| 'file' => $file->get_file(), | ||
| 'download_id' => $download->get_download_id(), | ||
| 'permission_id' => $download->get_id(), | ||
| 'download_name' => $file->get_name(), | ||
| 'order_id' => $download->get_order_id(), | ||
| 'order_key' => $download->get_order_key(), | ||
| 'remaining' => $download->get_downloads_remaining(), | ||
| 'access_expires' => $download->get_access_expires() ? wc_rest_prepare_date_response( $download->get_access_expires() ) : null, | ||
| ]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return rest_ensure_response( | ||
| [ | ||
| 'success' => true, | ||
| 'message' => __( 'Downloadable access granted successfully.', 'dokan-lite' ), | ||
| 'files' => $granted_files, | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Revoke file download access for customer. | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @param WP_REST_Request $request Request object. | ||
| * | ||
| * @return WP_REST_Response|WP_Error | ||
| */ | ||
| public function revoke_access_to_download( $request ) { | ||
| $download_id = $request->get_param( 'download_id' ); | ||
| $product_id = $request->get_param( 'product_id' ); | ||
| $order_id = $request->get_param( 'order_id' ); | ||
| $permission_id = $request->get_param( 'permission_id' ); | ||
|
|
||
| if ( empty( $permission_id ) ) { | ||
| return new WP_Error( | ||
| 'dokan_rest_missing_permission_id', | ||
| __( 'Permission ID is required.', 'dokan-lite' ), | ||
| [ 'status' => 400 ] | ||
| ); | ||
| } | ||
|
|
||
| $data_store = WC_Data_Store::load( 'customer-download' ); | ||
| $data_store->delete_by_id( absint( $permission_id ) ); | ||
|
|
||
| do_action( 'woocommerce_ajax_revoke_access_to_product_download', $download_id, $product_id, $order_id, $permission_id ); | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return rest_ensure_response( | ||
| [ | ||
| 'success' => true, | ||
| 'message' => __( 'Download access revoked successfully.', 'dokan-lite' ), | ||
| 'download_id' => $download_id, | ||
| 'product_id' => $product_id, | ||
| 'order_id' => $order_id, | ||
| 'permission_id' => $permission_id, | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the Product's schema, conforming to JSON Schema. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
WordPress REST API register_rest_route argument schema type date datetime supported values💡 Result:
In
register_rest_route( ... 'args' => ... ),date/datetimeare not valid JSON Schematypevalues in WordPress. For a date/time argument you should use:Supported
typevalues (WP REST arg schema) are:array,object,string,number,integer,boolean,null. [3]Supported string
formatvalues in WordPress are:date-time(RFC3339),uri,email,ip,uuid,hex-color. [1]🏁 Script executed:
Repository: getdokan/dokan
Length of output: 95
🏁 Script executed:
Repository: getdokan/dokan
Length of output: 1980
Fix REST argument type for
access_expires.Line 349 uses
'type' => 'date', which is not a supported WordPress REST API argument type. Use'type' => 'string'with'format' => 'date-time'instead. Valid REST arg types are:array,object,string,number,integer,boolean,null. The current type will cause REST schema validation failures.♻️ Proposed fix
'access_expires' => [ 'description' => __( 'Access Expires', 'dokan-lite' ), - 'type' => 'date', + 'type' => 'string', + 'format' => 'date-time', 'required' => false, ],📝 Committable suggestion
🤖 Prompt for AI Agents