Skip to content

Instantly share code, notes, and snippets.

@eighty20results
Last active September 3, 2016 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eighty20results/9fd55c024c7fa823b9b167768381cb65 to your computer and use it in GitHub Desktop.
Save eighty20results/9fd55c024c7fa823b9b167768381cb65 to your computer and use it in GitHub Desktop.
One time use trial membership level(s). Has filter `pmprosut_set_trial_level_ids` to configure which membership levels to make into one-time trial memberships
<?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: .2
Author: Thomas Sjolshagen @ Stranger Studios <thomas@eighty20results.com>
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_ids' 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;
}
/* TODO: Uncomment the add_filter below to enable all free membership levels as one-time trials */
// add_filter('pmprosut_set_trial_level_ids', 'pmprosut_set_trial_levels', 1, 1);
/**
* Return a trial membership level to grant single-usage to.
*
* @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;
}
/*
* TODO: Alternatively replace 50 with the ID of the membership level you want to use as a single-use Trial Membership
* and uncomment the add_filter (below)
*/
// add_filter('pmprosut_set_trial_level_ids', '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_ids', 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_ids', 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_ids', 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