Skip to content

Instantly share code, notes, and snippets.

@ronalfy
Created November 5, 2020 21:07
Show Gist options
  • Save ronalfy/3ec5bc20b9b403434c6c5a5b2f10d03a to your computer and use it in GitHub Desktop.
Save ronalfy/3ec5bc20b9b403434c6c5a5b2f10d03a to your computer and use it in GitHub Desktop.
PMPro - Exclude TOS MMPU Group
<?php
/**
* Exclude certain levels from a terms of service. If you are on MMPU, you can exclude a group.
*
* 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/
*/
/**
* Exclude TOS from levels or groups.
*/
class PMPro_Exclude_Tos {
/**
* Exclude levels from the TOS page.
*
* @var array $exclude_levels Placeholder for excluded levels.
*/
private $exclude_levels = array();
/**
* If MMPU is enabled.
*
* @var bool $mmpu Whether this is MMPU or not.
*/
private $mmpu = false;
/**
* MMPU groups to exclude.
*
* @var array MMPU Grouups to exclude.
*/
private $groups_to_exclude = array();
/**
* Exclude levels and groups potentially.
*
* @param array $levels_to_exclude The levels to exclude.
* @param array $groups_to_exclude If using MMPU, set this array to exclude groups.
*/
public function __construct( $levels_to_exclude = array(), $groups_to_exclude = array() ) {
$this->exclude_levels = $levels_to_exclude;
$this->groups_to_exclude = $groups_to_exclude;
$this->run();
}
/**
* Returns true or false if MMPU is activated and there are groups to exclude.
*/
private function is_mmpu() {
if ( defined( 'PMPROMMPU_VER' ) && ! empty( $this->groups_to_exclude ) ) {
$this->is_mmpu = true;
return true;
}
$this->is_mmpu = false;
return false;
}
/**
* Initialize the class.
*/
private function run() {
add_action( 'plugins_loaded', array( $this, 'init_hooks' ) );
}
/**
* Skip TOS for certain groups/levels.
*/
public function maybe_skip_tos() {
global $tos, $tospage, $pmpro_review, $pmpro_level;
$pmpro_levels = array( absint( $pmpro_level->id ) ); // this portion doesn't work well if selecting multiple levels in MMPU.
// Skip groups.
if ( $this->is_mmpu() && ! empty( $this->groups_to_exclude ) ) {
$group = absint( pmprommpu_get_group_for_level( $pmpro_level->id ) );
if ( in_array( $group, $this->groups_to_exclude ) ) {
$tos = $tospage = false; // Skips the TOS on the page for MMPU.
}
}
// Now skip levels.
foreach ( $pmpro_levels as $level ) {
if ( in_array( $level, $this->exclude_levels ) ) {
$tospage = false;
$tos = $tospage = false; // Skips the TOS for a specific level.
return;
}
}
}
/**
* Initialize main actions/filters.
*/
public function init_hooks() {
$this->is_mmpu();
add_action( 'pmpro_checkout_after_payment_information_fields', array( $this, 'maybe_skip_tos' ) );
add_action( 'pmpro_checkout_after_parameters_set', array( $this, 'maybe_skip_tos' ) );
}
}
/* Levels don't work well with MMPU, use groups instead. */
$pmpro_levels_to_exclude = array();
$pmpro_groups_to_exclude = array( 7 );
new PMPro_Exclude_Tos( $pmpro_levels_to_exclude, $pmpro_groups_to_exclude );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment