Skip to content

Instantly share code, notes, and snippets.

@raftaar1191
Created July 2, 2020 04:33
Show Gist options
  • Save raftaar1191/a4ef19365d175e8816a494985b1d2896 to your computer and use it in GitHub Desktop.
Save raftaar1191/a4ef19365d175e8816a494985b1d2896 to your computer and use it in GitHub Desktop.
Woo Subscription Update Download permissions
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Set the schedule action hook for update the permission
*/
function file_update_permission_as_schedule_recurring_action( $product_id, $new_download_ids ) {
$strtotime = strtotime( 'now' );
$strtotime = $strtotime + 10;
/**
* Schedule an action with the hook 'eg_midnight_log2' to run at midnight each day
* so that our callback is run then.
*/
as_schedule_single_action(
$strtotime,
'woocommerce_process_product_file_download_paths_ajax',
array(
'product_id' => $product_id,
'new_download_ids' => $new_download_ids,
),
'bb-theme-woocommerce'
);
}
/**
* Add custom function to give the user download permission
*/
function woocommerce_process_product_file_download_paths_callback( $product_id, $variation_id, $downloadable_files ) {
$product_id = ( $variation_id ) ? $variation_id : $product_id;
$product = wc_get_product( $product_id );
$existing_download_ids = array_keys( (array) wcs_get_objects_property( $product, 'downloads' ) );
$downloadable_ids = array_keys( (array) $downloadable_files );
$new_download_ids = array_filter( array_diff( $downloadable_ids, $existing_download_ids ) );
if ( ! empty( $new_download_ids ) ) {
delete_post_meta( $product_id, 'process_product_file_status' );
file_update_permission_as_schedule_recurring_action( $product_id, $new_download_ids );
}
}
add_action( 'woocommerce_process_product_file_download_paths', 'woocommerce_process_product_file_download_paths_callback', 11, 3 );
/**
* A callback to run when the 'eg_midnight_log2' scheduled action is run.
*/
function woocommerce_process_product_file_download_paths_ajax_callback( $product_id, $new_download_ids ) {
global $wpdb;
$product_id = absint( $product_id );
$product_file_status = array();
$product_file_status_old = get_post_meta( $product_id, 'process_product_file_status', true );
if ( ! empty( $product_file_status_old ) ) {
$product_file_status = $product_file_status_old;
}
$limit = 100;
$total_page = 1;
$offset = 100;
if ( empty( $product_file_status ) ) {
$all_subscriptions = wcs_get_subscriptions_for_product( $product_id );
$all_subscriptions_count = count( $all_subscriptions );
$product_file_status['total'] = $all_subscriptions_count;
} else {
$all_subscriptions_count = $product_file_status['total'];
$offset = $product_file_status['offset'];
}
if ( $all_subscriptions_count > $limit ) {
$total_page = $all_subscriptions_count / $limit;
}
$product_file_status['total_page'] = ceil( $total_page );
$existing_permissions = $wpdb->get_results( $wpdb->prepare( "SELECT order_id, download_id from {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE product_id = %d", $product_id ) );
$subscriptions = wcs_get_subscriptions_for_product(
$product_id,
'ids',
array(
'limit' => 100,
'offset' => $offset,
)
);
// Arrange download id permissions by order id
$permissions_by_order_id = array();
foreach ( $existing_permissions as $permission_data ) {
$permissions_by_order_id[ $permission_data->order_id ][] = $permission_data->download_id;
}
$permissions_by_order_ids = array_keys( $permissions_by_order_id );
foreach ( $subscriptions as $subscription_id ) {
// Grant permissions to subscriptions which have no permissions for this product, pre WC3.0, or all subscriptions, post WC3.0, as WC doesn't grant them retrospectively anymore.
if ( ! in_array( $subscription_id, $permissions_by_order_ids ) || false === WC_Subscriptions::is_woocommerce_pre( '3.0' ) ) {
$subscription = wcs_get_subscription( $subscription_id );
foreach ( $new_download_ids as $download_id ) {
$has_permission = isset( $permissions_by_order_id[ $subscription_id ] ) && in_array( $download_id, $permissions_by_order_id[ $subscription_id ] );
if ( $subscription && ! $has_permission && apply_filters( 'woocommerce_process_product_file_download_paths_grant_access_to_new_file', true, $download_id, $product_id, $subscription ) ) {
wc_downloadable_file_permission( $download_id, $product_id, $subscription );
}
}
}
}
if ( $offset <= ( $total_page * $limit ) ) {
$offset = $offset + $limit;
$product_file_status['offset'] = $offset;
update_post_meta( $product_id, 'process_product_file_status', $product_file_status );
file_update_permission_as_schedule_recurring_action( $product_id, $new_download_ids );
} else {
delete_post_meta( $product_id, 'process_product_file_status' );
}
}
add_action( 'woocommerce_process_product_file_download_paths_ajax', 'woocommerce_process_product_file_download_paths_ajax_callback', 10, 2 );
@raftaar1191
Copy link
Author

Testing

function woocommerce_process_product_file_download_paths_ajax() {
if ( empty( get_option( 'woocommerce_process_product_file_download_paths_ajax9', false ) ) ) {
$args = array(
6 => 'e2119603-a91e-4a4c-95cc-23df016490a6',
);
file_update_permission_as_schedule_recurring_action( 79641, $args );
}

update_option( 'woocommerce_process_product_file_download_paths_ajax9', true );

}
add_action( 'admin_init', 'woocommerce_process_product_file_download_paths_ajax' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment