Skip to content

Instantly share code, notes, and snippets.

@ronalfy
Created November 11, 2020 16:47
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 ronalfy/14ac27248643a46dd656e5d4d29a204f to your computer and use it in GitHub Desktop.
Save ronalfy/14ac27248643a46dd656e5d4d29a204f to your computer and use it in GitHub Desktop.
PMPro - Email Confirmation Add Body Classes
<?php
/**
* Set up classes for email confirmation.
*
* Available classes:
*
* 1. Logged in: pmpro-body-is-logged-in
* 2. Logged out: pmpro-body-is-logged-out
* 3. Logged in and and active member: pmpro-body-has-membership-level, pmpro-body-has-membership-level-n
* 4. Logged in and awaiting confirmation: pmpro-body-pending-confirmation
* 5. Logged in and no confirmation key present or needed: pmpro-body-no-confirmation-key
* 6. Logged in and confirmed: pmpro-body-approved-confirmation
*
* 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/
*/
/**
* Wrap a div around the confirmation message upon confirmation with email confirmation add-on.
*/
/**
* Check to see if the confirmatiton message is active.
*/
function my_pmpro_pmproec_pmpro_confirmation_message( $message ) {
if ( function_exists( 'pmproec_pmpro_confirmation_message' ) ) {
$message = sprintf(
'<div id="pmpro-email-confirmation-message">%s</div>',
$message
);
}
return $message;
}
add_filter( 'pmpro_confirmation_message', 'my_pmpro_pmproec_pmpro_confirmation_message', 50 );
function my_pmpro_pmproec_pmpro_body_classes( $body_classes ) {
global $current_user;
// Add logged in classes.
if ( is_user_logged_in() ) {
$body_classes[] = 'pmpro-body-is-logged-in';
} else {
$body_classes[] = 'pmpro-body-is-logged-out';
}
if ( function_exists( 'pmpro_hasMembershipLevel' ) && pmpro_hasMembershipLevel() ) {
$body_classes[] = 'pmpro-body-has-membership-level';
$body_classes[] = 'pmpro-body-has-membership-level-' . absint( $current_user->membership_level->ID );
}
return $body_classes;
}
add_filter( 'body_class', 'my_pmpro_pmproec_pmpro_body_classes' );
/**
* Assume has level is off for this function.
*/
function my_pmpro_add_no_confirmation_class( $classes ) {
if ( ! is_user_logged_in() ) {
return;
}
global $current_user;
$meta = get_user_meta( $user_id, 'pmpro_email_confirmation_key', true );
if ( ! $meta ) {
$classes[] = 'pmpro-body-no-confirmation-key';
} else {
$classes[] = 'pmpro-body-pending-confirmation';
}
return $classes;
}
/**
* Assume user has been approved and has level is true.
*/
function my_pmpro_add_confirmation_class( $classes ) {
global $current_user;
if ( ! is_user_logged_in() ) {
return $classes;
}
if ( has_filter( 'pmpro_has_membership_level', 'pmproec_pmpro_has_membership_level' ) ) {
$meta = get_user_meta( $current_user->ID, 'pmpro_email_confirmation_key', true );
if ( ! $meta ) {
$classes[] = 'pmpro-body-no-confirmation-key';
} else {
$classes[] = 'pmpro-body-approved-confirmation';
}
}
return $classes;
}
/**
* Run after the confirmation check. Checks to see if the confirmation member email has run.
*/
function my_pmproec_pmpro_has_membership_level( $has_level, $user_id, $level ) {
if ( has_filter( 'pmpro_has_membership_level', 'pmproec_pmpro_has_membership_level' ) ) {
if ( function_exists( 'pmproec_pmpro_has_membership_level' ) ) {
if ( ! $has_level ) {
add_filter( 'body_class', 'my_pmpro_add_no_confirmation_class' );
}
}
}
return $has_level;
}
add_action( 'pmpro_has_membership_level', 'my_pmproec_pmpro_has_membership_level', 11, 3 );
add_action( 'body_class', 'my_pmpro_add_confirmation_class' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment