From 1571ea4e6a053729f8b076971aadd255a85a889b Mon Sep 17 00:00:00 2001 From: maximilianoRicoTabo Date: Tue, 26 Sep 2023 18:18:35 -0300 Subject: [PATCH 1/2] Add support to comma separated levels --- includes/functions.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 503d97e6e..d220a97f0 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -783,13 +783,14 @@ function pmpro_showRequiresMembershipMessage() { * Function to check if a user has specified membership levels. * * pmpro_hasMembershipLevel() checks if the passed user is a member of the passed level - * $level may either be the ID or name of the desired membership_level. (or an array of such) + * $level may either be the ID or name of the desired membership_level. (or an array of such or a comma separated string) * If $user_id is omitted, the value will be retrieved from $current_user. * * Return values: * * Success returns boolean true. * * Failure returns a string containing the error message. * + * @since TBD Added support to pass comma separated value to $levels * @since 1.8.5 Added 'e' option for expired members. * @since 1.0.0 * @@ -819,7 +820,18 @@ function pmpro_hasMembershipLevel( $levels = null, $user_id = null ) { $return = ! empty( $membership_levels ); } else { if ( ! is_array( $levels ) ) { - $levels = array( $levels ); + //we'll assume is a string + $levels_str = (string)$levels; + // If no comma, just make the value and array how we used to + if ( strpos( $levels_str, ',' ) === false ) { + $levels = array( $levels ); + } else { + // We have a string with at least 1 comma in it, turn it into an array and check again. + $level_ids = explode( ',', $levels_str ); + // Trim whitespace from the levels ids or names. + $levels = array_map( 'trim', $level_ids ); + } + } if ( empty( $membership_levels ) ) { From 866be8cfa456d48cffd04b1fada3f30fc311c2a7 Mon Sep 17 00:00:00 2001 From: Jason Coleman Date: Wed, 4 Oct 2023 04:40:07 +1100 Subject: [PATCH 2/2] Rearranging --- includes/functions.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index d220a97f0..b6cbf94f9 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -820,18 +820,17 @@ function pmpro_hasMembershipLevel( $levels = null, $user_id = null ) { $return = ! empty( $membership_levels ); } else { if ( ! is_array( $levels ) ) { - //we'll assume is a string - $levels_str = (string)$levels; - // If no comma, just make the value and array how we used to - if ( strpos( $levels_str, ',' ) === false ) { - $levels = array( $levels ); - } else { - // We have a string with at least 1 comma in it, turn it into an array and check again. - $level_ids = explode( ',', $levels_str ); - // Trim whitespace from the levels ids or names. - $levels = array_map( 'trim', $level_ids ); - } - + // Check for a comma. + $levels_str = (string)$levels; + if ( strpos( $levels_str, ',' ) !== false ) { + // We have a string with at least 1 comma in it, turn it into an array. + $level_ids = explode( ',', $levels_str ); + // Trim whitespace from the levels ids or names. + $levels = array_map( 'trim', $level_ids ); + } else { + // No comma, but we want an array of levels. + $levels = array( $levels ); + } } if ( empty( $membership_levels ) ) {