Last active
December 10, 2019 09:09
-
-
Save webzunft/3f7c906901ac01f07fc89caddb0aefb8 to your computer and use it in GitHub Desktop.
Suggestion for improved edd_all_access_user_has_pass() in EDD All Access
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check if a specific customer has a valid, specific All Access Pass. | |
* | |
* @since 1.0.0 | |
* @param int - The ID of the user | |
* @param mixed - The ID of the All Access product, separate multiple IDs using comma | |
* @param mixed - The price_id (price variation) of the All Access product, separate multiple IDs using comma, leave empty for all price IDs | |
* @param string - The status of the pass we want to check if the user has. | |
* @return object/int - The All Access Pass if it exists or false if not. | |
*/ | |
function edd_all_access_user_has_pass( $user_id, $download_id, $price_id = 0, $required_pass_status = 'active' ){ | |
$has_pass = false; | |
// Get the Customer | |
$customer = new EDD_Customer( $user_id, true ); | |
// If no customer was found (perhaps not logged in) | |
if ( 0 == $customer->id ){ | |
return $has_pass; | |
} | |
// Get the current customer's All Access Passes from the customer meta | |
$customer_all_access_passes = $customer->get_meta( 'all_access_passes' ); | |
// If the customer has no All Access Passes in their customer meta | |
if ( empty( $customer_all_access_passes ) || ! is_array( $customer_all_access_passes ) ){ | |
return $has_pass; | |
} | |
// Loop through each All Access Pass to see if any match the restricted_to and are active | |
foreach( $customer_all_access_passes as $purchased_download_id_price_id => $purchased_aa_data ){ | |
if ( ! isset( $purchased_aa_data['payment_id'] ) || ! isset( $purchased_aa_data['download_id'] ) || ! isset( $purchased_aa_data['price_id'] ) ){ | |
continue; | |
} | |
// Set up an All Access Pass Object for this | |
$all_access_pass = EDD_All_Access_Pass( $purchased_aa_data['payment_id'], $purchased_aa_data['download_id'], $purchased_aa_data['price_id'] ); | |
// If this All Access Pass matches the required status, check if it is one of the restricted_to products | |
if ( $all_access_pass->status == $required_pass_status ){ | |
$download_ids = explode( ',', $download_id ); | |
$price_ids = explode( ',', $price_id ); | |
// If this All Access Pass is the one that was purchased | |
if ( in_array( $purchased_aa_data['download_id'], $download_ids ) | |
&& ( | |
empty( $price_id ) // accept all price IDs | |
|| in_array( $purchased_aa_data['price_id'], $price_ids ) // accept also specific price IDs only | |
) ) { | |
// We can stop looping now because we found a valid pass with the required status. | |
return $has_pass = $all_access_pass; | |
} | |
}else{ | |
$has_pass = false; | |
} | |
} | |
return $has_pass; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment