Filter uncanny-automator

automator_surecart_product_download_url_expiration

Filters the download URL expiration time for SureCart products.

add_filter( 'automator_surecart_product_download_url_expiration', $callback, 10, 2 );

Description

Filters the expiration time for SureCart product download URLs. Developers can modify the default 24-hour expiration to set custom access durations for downloaded files. This hook is applied before generating the download link, allowing for dynamic expiration based on purchase or product data.


Usage

add_filter( 'automator_surecart_product_download_url_expiration', 'your_function_name', 10, 2 );

Parameters

$download (mixed)
This parameter represents the default expiration time for the download URL in seconds.
$purchase_data (mixed)
This parameter represents the SureCart product download object currently being processed, containing details about the downloadable item.

Return Value

The filtered value.


Examples

add_filter(
	'automator_surecart_product_download_url_expiration',
	function ( $expiration_seconds, $download, $purchase_data ) {
		// Example: Extend the download URL expiration for specific products.
		// In this example, we'll check if the product ID is 'prod_123xyz'
		// and if so, we'll set the expiration to 7 days instead of the default 1 day.

		// Ensure $download and $purchase_data are valid objects before accessing properties.
		if ( ! is_object( $download ) || ! property_exists( $download, 'product_id' ) || ! is_object( $purchase_data ) ) {
			return $expiration_seconds; // Return default if data is invalid
		}

		$product_id = $download->product_id;
		$new_expiration_seconds = $expiration_seconds; // Default to original

		// Check if the current product is one we want to modify the expiration for.
		if ( 'prod_123xyz' === $product_id ) {
			// Set expiration to 7 days (7 * 24 * 60 * 60 seconds)
			$new_expiration_seconds = 7 * 24 * 60 * 60;
		}

		// You could also add logic based on purchase data if needed, for example:
		// if ( isset( $purchase_data->user_id ) && 123 === $purchase_data->user_id ) {
		//     $new_expiration_seconds = 30 * 24 * 60 * 60; // 30 days for a specific user
		// }

		return $new_expiration_seconds;
	},
	10, // Priority
	3  // Accepted args
);

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/surecart/tokens/surecart-tokens.php:598
src/integrations/surecart/tokens/surecart-tokens-new-framework.php:742

public function get_download_links( $purchase_data ) {

		$downloads = $this->get_downloads( $purchase_data );

		$download_links = array();

		if ( empty( $downloads ) ) {
			return '';
		}

		foreach ( $downloads as $download ) {
			$expose_for       = apply_filters( 'automator_surecart_product_download_url_expiration', 24 * 60 * 60, $download, $purchase_data );
			$media            = SureCartModelsMedia::where( array( 'expose_for' => $expose_for ) )->find( $download->media->id );
			$format           = '<a href="%s" title="%s">%s</a>';
			$download_link    = sprintf( $format, $media->url, $media->filename, esc_html_x( 'Download', 'Surecart', 'uncanny-automator' ) );
			$download_links[] = apply_filters( 'automator_surecart_product_download_link', $download_link );
		}

		return implode( ', ', $download_links );
	}


Scroll to Top