Last active
January 19, 2021 09:48
-
-
Save ipokkel/4179b1902f581c4967f5d61d4dde52c3 to your computer and use it in GitHub Desktop.
Helper function that returns an array of a user's previous paid membership levels.
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 | |
/** | |
* Helper function that returns an array of a user's previous paid | |
* membership levels. | |
* | |
* Set your free level ids in the $free_levels_ids array. If there are | |
* no free levels set this to | |
* $free_levels_ids = array( 0 ); | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmpro_get_previous_paid_levels_1611046034511( $user_id = null ) { | |
// Is PMPro active? | |
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) { | |
return false; | |
} | |
global $wpdb, $current_user, $pmpro_levels; | |
// Get user id | |
if ( empty( $user_id ) ) { | |
$user_id = $current_user->ID; | |
} | |
if ( ! $user_id ) { | |
return false; | |
} | |
$all_levels_ids = array_keys( $pmpro_levels ); | |
$free_levels_ids = array( 1 ); // set your free levels ids here. | |
$paid_levels_ids = $all_levels_ids; | |
// remove free levels | |
if ( ! empty( $free_levels_ids ) && is_array( $free_levels_ids ) ) { | |
$paid_levels_ids = array_diff( $all_levels_ids, $free_levels_ids ); | |
} | |
$member_previous_levels = $wpdb->get_results( "SELECT membership_id FROM $wpdb->pmpro_memberships_users WHERE user_id = '$user_id' ORDER BY id DESC", ARRAY_N ); | |
// Bail if new member | |
if ( empty( $member_previous_levels ) ) { | |
return false; | |
} | |
$member_previous_levels = array_merge( ...$member_previous_levels ); | |
$member_previous_levels_paid = array_intersect( $paid_levels_ids, $member_previous_levels ); | |
$member_previous_levels_paid = array_values( $member_previous_levels_paid ); | |
if ( empty( $member_previous_levels_paid ) ) { | |
return false; | |
} | |
return $member_previous_levels_paid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment