Skip to content
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

Add 'apply_filters' to 'EDD_Download' class 'support_buy_now()' function return value #9679

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions includes/class-edd-download.php
Original file line number Diff line number Diff line change
Expand Up @@ -939,44 +939,61 @@ public function supports_buy_now( $price_id = null ) {
$recurring_active = function_exists( 'edd_recurring' );
$free_downloads_active = function_exists( 'edd_free_downloads_use_modal' );

// If Recurring and Free Downloads are not present, we can return true.
if ( false === $recurring_active && false === $free_downloads_active ) {
// Check if Buy Now is supported.
$supports_buy_now = $this->is_buy_now_supported( $recurring_active, $free_downloads_active, $price_id );

return apply_filters( 'edd_download_supports_buy_now', $supports_buy_now, $this->ID, $price_id );
}

/**
* Needed checks to determine if Buy Now is supported.
*
* @since 3.2.4
* @param bool $recurring_active Whether or not Recurring is active.
* @param bool $free_downloads_active Whether or not Free Downloads is active.
* @param int $price_id The price ID to check for.
*
* @return bool True if Buy Now is supported, false otherwise.
*/
private function is_buy_now_supported( $recurring_active, $free_downloads_active, $price_id ) {
// If Recurring and Free Downloads are not present, Buy Now is supported.
if ( ! $recurring_active && ! $free_downloads_active ) {
return true;
}

// Free downloads does not support Buy Now.
// Free downloads do not support Buy Now.
if ( $free_downloads_active && ! $this->has_variable_prices() ) {
$price = get_post_meta( $this->ID, 'edd_price', true );
// If the download is free, we can return false. This check bypasses the is_free() method, to omit the filter.

// If the download is free, Buy Now is not supported.
if ( empty( $price ) && edd_free_downloads_use_modal( $this->ID ) ) {
return false;
}
}

// Subscription products cannot support Buy Now.
if ( $recurring_active ) {
if ( $this->has_variable_prices() ) {
// Parse if we have a price ID passed in.
$price_id = is_numeric( $price_id ) ? intval( $price_id ) : null;
// If no Price ID was passed in, and the product has variable prices, return false if any of the prices are recurring.

if ( null === $price_id ) {
foreach ( $this->get_prices() as $key => $price ) {
if ( edd_recurring()->is_price_recurring( $this->ID, $key ) ) {
return false;
}
}
}

$is_recurring = edd_recurring()->is_price_recurring( $this->ID, $price_id );
} else {
$is_recurring = edd_recurring()->is_recurring( $this->ID );
}

if ( $is_recurring ) {
return false;
}
}

return true;
}
}