Skip to content

Instantly share code, notes, and snippets.

@lordspace
Created November 4, 2023 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lordspace/f3c56d64fd6e22f7e1126d9fd0472257 to your computer and use it in GitHub Desktop.
Save lordspace/f3c56d64fd6e22f7e1126d9fd0472257 to your computer and use it in GitHub Desktop.
fix for https://github.com/Automattic/woocommerce-subscriptions-core/ Site crashes with out of memory error when adding a downloadable product and hitting Update button
// file:
///plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/includes/class-wcs-download-handler.php
/**
* Grant downloadable file access to any newly added files on any existing subscriptions
* which don't have existing permissions pre WC3.0 and all subscriptions post WC3.0.
*
* @param int $product_id
* @param int $variation_id
* @param array $downloadable_files product downloadable files
* @since 1.0.0 - Migrated from WooCommerce Subscriptions v2.0.18
*/
public static function grant_new_file_product_permissions( $product_id, $variation_id, $downloadable_files ) {
if ( wcs_is_woocommerce_pre( '3.0' ) ) {
return;
}
global $wpdb;
$product_id = !empty($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 ) ) {
return;
}
$subscriptions = wcs_get_subscriptions_for_product( $product_id );
if ( empty( $subscriptions ) ) { // No subscriptions
return;
}
foreach ( $subscriptions as $subscription_id ) {
// $subscription_id is technically an order id for a subscription product
$permission_by_order_id = $wpdb->get_var(
$wpdb->prepare( "SELECT order_id FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d LIMIT 1",
$subscription_id, $product_id
)
);
if ( empty( $permission_by_order_id ) ) {
continue;
}
$subscription = wcs_get_subscription( $subscription_id );
if ( empty( $subscription ) ) {
continue;
}
foreach ( $new_download_ids as $download_id ) {
// WooCommerce generates random IDs and this is almost pointless.
// I used the debugger and stopped the debugging session right after wc_downloadable_file_permission() was called.
$permission_found = $wpdb->get_var(
$wpdb->prepare( "SELECT download_id FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE order_id = %d AND product_id = %d AND download_id = %s LIMIT 1",
$subscription_id, $product_id, $download_id
)
);
if ( ! empty( $permission_found ) ) {
continue;
}
// 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 ( 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 );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment