Filter
uncanny-automator
automator_surecart_product_download_link
Filters the download link for a SureCart product, allowing modification before it's displayed to the user.
add_filter( 'automator_surecart_product_download_link', $callback, 10, 1 );
Description
Filters the download link for SureCart products, allowing developers to modify the URL, title, or text before it's displayed. This hook is applied when generating download links for purchased SureCart products, offering control over how users access their digital goods.
Usage
add_filter( 'automator_surecart_product_download_link', 'your_function_name', 10, 1 );
Parameters
-
$download_link(mixed) - This parameter holds the download link for a SureCart product.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the automator_surecart_product_download_link filter.
* This example appends a query parameter to the download link,
* for example, to track the source of the download.
*
* @param string $download_link The original download link HTML.
* @return string The modified download link HTML.
*/
add_filter( 'automator_surecart_product_download_link', function( $download_link ) {
// Check if the download_link is a string and not empty.
if ( ! is_string( $download_link ) || empty( $download_link ) ) {
return $download_link;
}
// We assume the download_link is an <a> tag and we need to extract the href.
// A more robust solution might involve DOMDocument, but for a realistic example,
// we'll use a simple string manipulation or regex.
// For simplicity, let's assume the href is directly accessible after 'href="'.
// In a real-world scenario, you'd want to parse the HTML more reliably.
$href_start_pos = strpos( $download_link, 'href="' );
if ( $href_start_pos === false ) {
return $download_link; // Could not find href attribute.
}
$href_start_pos += strlen( 'href="' );
$href_end_pos = strpos( $download_link, '"', $href_start_pos );
if ( $href_end_pos === false ) {
return $download_link; // Malformed href.
}
$original_url = substr( $download_link, $href_start_pos, $href_end_pos - $href_start_pos );
// Append a custom query parameter to track the download source.
// In a real scenario, you might get this tracking info from $purchase_data
// or other available context if the filter provided it.
$tracking_source = 'automator-plugin';
$modified_url = add_query_arg( 'download_source', $tracking_source, $original_url );
// Reconstruct the download link HTML with the modified URL.
$new_download_link = str_replace( $original_url, $modified_url, $download_link );
return $new_download_link;
}, 10, 1 ); // Priority 10, accepts 1 argument.
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:602
src/integrations/surecart/tokens/surecart-tokens-new-framework.php:746
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 );
}