Skip to content

Commit

Permalink
Adding MMPU compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dparker1005 committed Oct 6, 2022
1 parent 1fc02db commit 44795c3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
67 changes: 40 additions & 27 deletions pmpro-membership-card.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ function pmpro_membership_card_wp()
if(function_exists("pmpro_getMembershipLevelForUser"))
$pmpro_membership_card_user->membership_level = pmpro_getMembershipLevelForUser($pmpro_membership_card_user->ID);

/**
* For MMPU compatibility, let's also set $pmpro_membership_card_user->membership_levels.
*/
if ( function_exists( 'pmpro_getMembershipLevelsForUser' ) ) {
$pmpro_membership_card_user->membership_levels = pmpro_getMembershipLevelsForUser( $pmpro_membership_card_user->ID );
}

/*
Make sure that the current user can "edit" the user being viewed.
*/
Expand Down Expand Up @@ -303,12 +310,18 @@ function pmpro_membership_card_return_user_name( $pmpro_membership_card_user ){
}

/**
* Returns the members expiration date for their membership
* Returns the members most distant expiration date for their memberships.
*/
function pmpro_membership_card_return_end_date( $pmpro_membership_card_user ){
$furthest_enddate = null;
foreach ( $pmpro_membership_card_user->membership_levels as $level ) {
if ( $furthest_enddate == null || $level->enddate > $furthest_enddate ) {
$furthest_enddate = $level->enddate;
}
}

if(isset( $pmpro_membership_card_user->membership_level->enddate ) && $pmpro_membership_card_user->membership_level->enddate)
return date_i18n(get_option('date_format'), $pmpro_membership_card_user->membership_level->enddate);
if( ! empty( $furthest_enddate ) )
return date_i18n( get_option('date_format'), $furthest_enddate );
else
return __('Never', 'pmpro-membership-card');

Expand All @@ -320,33 +333,25 @@ function pmpro_membership_card_return_end_date( $pmpro_membership_card_user ){
* @param object $pmpro_membership_card_user The membership user.
*/
function pmpro_membership_card_output_levels_for_user( $pmpro_membership_card_user ) {
$levels = pmpro_membership_card_get_levels_for_user( $pmpro_membership_card_user );

// Get the user's current levels.
$levels = $pmpro_membership_card_user->membership_levels;
if ( empty( $levels ) ) {
return false;
return _e( 'None', 'pmpro-membership-card' );
}

if ( is_array( $levels ) ) {
$level_names = array();
foreach ( $levels as $level ) {
$level_names[] = $level->name;
}
sort( $level_names );

$display = '';

if ( count( $level_names ) > 1 ) {
$display = '<ul>';
foreach ( $level_names as $level_name ) {
$display .= '<li><p>' . esc_html( $level_name ) . '</p></li>';
}
$display .= '</ul>';
} else {
$level_name = current( $level_names );
$display = esc_html( $level_name );
}
// Get the level names.
$level_names = wp_list_pluck( $levels, 'name' );
sort( $level_names );

// Output the level names.
$display = '';
if ( count( $level_names ) > 1 ) {
$display = '<ul>';
$display .= '<li>' . implode( '</li><li>', $level_names ) . '</li>';
$display .= '</ul>';
} else {
$display = $levels;
$level_name = current( $level_names );
$display = esc_html( $level_name );
}

echo apply_filters( 'pmpro_membership_card_mmpu_output', $display, $levels, $pmpro_membership_card_user );
Expand All @@ -355,11 +360,15 @@ function pmpro_membership_card_output_levels_for_user( $pmpro_membership_card_us
/**
* Returns member's active levels.
*
* @deprecated TBD No longer necessary.
*
* @param object $pmpro_membership_card_user The membership user.
*
* @return array User Levels.
*/
function pmpro_membership_card_get_levels_for_user( $pmpro_membership_card_user ){
// Show deprecation message.
_deprecated_function( __FUNCTION__, 'TBD', 'pmpro_membership_card_output_levels_for_user' );

if ( ! isset( $pmpro_membership_card_user->ID ) ) {
return false;
Expand All @@ -381,8 +390,12 @@ function pmpro_membership_card_get_levels_for_user( $pmpro_membership_card_user

/**
* Returns member's level name
*
* @deprecated TBD No longer necessary.
*/
function pmpro_membership_card_return_level_name( $pmpro_membership_card_user ){
// Show deprecation message.
_deprecated_function( __FUNCTION__, 'TBD', 'pmpro_membership_card_output_levels_for_user' );

return isset( $pmpro_membership_card_user->membership_level->name ) ? $pmpro_membership_card_user->membership_level->name : __( 'None', 'pmpro-membership-card' );

Expand All @@ -396,7 +409,7 @@ function pmpro_membership_card_return_qr_code_data( $pmpro_membership_card_user,
if( $option == 'ID' ){
$data = isset( $pmpro_membership_card_user->ID ) ? intval( $pmpro_membership_card_user->ID ) : '';
} elseif ( $option == 'level' ){
$data = isset( $pmpro_membership_card_user->membership_level->ID ) ? intval( $pmpro_membership_card_user->membership_level->ID ) : null;
$data = isset( $pmpro_membership_card_user->membership_levels ) ? implode( ',', wp_list_pluck( $pmpro_membership_card_user->membership_levels, 'id' ) ) : null;
} elseif ( $option == 'email' ){
$data = isset( $pmpro_membership_card_user->data->user_email ) ? sanitize_text_field( $pmpro_membership_card_user->data->user_email ) : '';
} else {
Expand Down
2 changes: 1 addition & 1 deletion templates/membership-card.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<?php
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if(function_exists("pmpro_getMemberStartDate") && isset( $pmpro_membership_card_user->ID ) )
$since = pmpro_getMemberStartDate($pmpro_membership_card_user->ID);
$since = pmpro_getMemberStartDate($pmpro_membership_card_user->ID); // Will get the lowest membership_user ID, which should be the oldest startdate.
else
$since = isset( $pmpro_membership_card_user->user_registered ) ? $pmpro_membership_card_user->user_registered : '';
?>
Expand Down

0 comments on commit 44795c3

Please sign in to comment.