Created
June 12, 2016 13:27
-
-
Save eighty20results/47f8c9d286af2eedee7bb72eb6cb9ed7 to your computer and use it in GitHub Desktop.
Configure one or more membership level(s) as "single-use" trial memberships (i.e. prevent the user from signing up more than once)
This file contains hidden or 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 | |
/* | |
Plugin Name: PMPro Single Use Trial Memberships | |
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/ | |
Description: Allow a member to sign up for the trial membership level once | |
Version: .1 | |
Author: Thomas Sjolshagen @ Stranger Studios <[email protected]> | |
Author URI: http://www.eighty20results.com/thomas-sjolshagen/ | |
*/ | |
/* | |
Only allow users to use the trial level once. | |
By default, this add-on cam treat all free membership levels as single-use trial membership | |
(re-add the 'add_filter("pmprosut_set_trial_level_id", "pmprosut_set_trial_levels", 1, 1);' line below) | |
You'll need to have an expiration date configured for the trial level(s). | |
NOTE: Use a filter ('pmprosut_set_trial_level_id') to add/remove level IDs from the list of trial levels | |
*/ | |
/** | |
* Filter to return all free levels as "single-use trial membership levels" | |
* | |
* @param array $level_array Array of level IDs (one or more). | |
* @return array Array of level ID(s). | |
*/ | |
function pmprosut_set_trial_levels( $level_array ) { | |
if (function_exists('pmpro_isLevelFree')) { | |
$all_levels = pmpro_getAllLevels(true, true); | |
// Add all free levels (trials?) to the filter array | |
foreach($all_levels as $level_id => $level ) { | |
if ( pmpro_isLevelFree($level)) { | |
$level_array[]= $level_id; | |
} | |
} | |
} | |
return $level_array; | |
} | |
// add_filter('pmprosut_set_trial_level_id', 'pmprosut_set_trial_levels', 1, 1); | |
/** | |
* Return a trial membership level to grant single-usage to. | |
* TODO: Replace 50 with the ID of the membership level you want to use as a single-use Trial Membership | |
* | |
* @param $level_array | |
* | |
* @return array | |
*/ | |
function pmprosut_set_single_trial_level( $level_array ) { | |
// Set this to the level ID for your (single) trial membership level | |
$level_array[] = 50; | |
return $level_array; | |
} | |
add_filter('pmprosut_set_trial_level_id', 'pmprosut_set_single_trial_level', 10, 1); | |
//record when users gain the trial level | |
function pmprosut_after_change_membership_level($level_id, $user_id) | |
{ | |
//trial level(s) to allow single sign-ups | |
$trial_levels = apply_filters('pmprosut_set_trial_level_id', array()); | |
if(in_array($level_id, $trial_levels) ) | |
{ | |
//add user meta to record the fact that this user has had this level before | |
update_user_meta($user_id, "pmpro_trial_level_{$level_id}_used", true); | |
} | |
} | |
add_action("pmpro_after_change_membership_level", "pmprosut_after_change_membership_level", 10, 2); | |
//check at checkout if the user has used the trial level already | |
function pmprosut_registration_checks($value) | |
{ | |
global $current_user; | |
// array of trial levels | |
$trial_levels = apply_filters('pmprosut_set_trial_level_id', array()); | |
$level_id = intval($_REQUEST['level']); | |
if($current_user->ID && in_array( $level_id, $trial_levels) ) | |
{ | |
//check if the current user has already used the trial level | |
$already = get_user_meta($current_user->ID, "pmpro_trial_level_{$level_id}_used", true); | |
//yup, don't let them checkout | |
if($already) | |
{ | |
global $pmpro_msg, $pmpro_msgt; | |
$pmpro_msg = "You have already used up your trial membership. Please select a full membership to checkout."; | |
$pmpro_msgt = "pmpro_error"; | |
$value = false; | |
} | |
} | |
return $value; | |
} | |
add_filter("pmpro_registration_checks", "pmprosut_registration_checks"); | |
//swap the expiration text if the user has used the trial | |
function pmprosut_level_expiration_text($text, $level) | |
{ | |
global $current_user; | |
$level_id = $level->id; | |
$trial_levels = apply_filters('pmprosut_set_trial_level_id', array()); | |
$has_used = get_user_meta($current_user->ID, "pmpro_trial_level_{$level_id}_used", true); | |
if( !empty($current_user->ID) && !empty($has_used) && in_array($level_id, $trial_levels)) | |
{ | |
$text = "You have already used up your trial membership. Please select a full membership to checkout."; | |
} | |
return $text; | |
} | |
add_filter("pmpro_level_expiration_text", "pmprosut_level_expiration_text", 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment