Last active
August 4, 2021 19:11
-
-
Save dparker1005/a518eca25d3a78daa79e3fe68c7134ce to your computer and use it in GitHub Desktop.
Sample code for using the pmpro_after_all_membership_level_changes action.
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 | |
/** | |
* Sample code for using the pmpro_after_all_membership_level_changes action. | |
* | |
* @param array $old_user_levels $user_id => array $old_levels | |
*/ | |
function my_pmpro_after_all_membership_level_changes_sample( $old_user_levels ) { | |
// Step 1: Loop through $old_user_levels. | |
foreach ( $old_user_levels as $user_id => $old_levels ) { | |
// Step 2: Get the user's current membership levels. | |
$new_levels = pmpro_getMembershipLevelsForUser( $user_id ); | |
// Step 3: Build arrays of "options" associated with old and new membership levels. | |
$old_options = array(); | |
$new_options = array(); | |
// Populate $old_options. | |
foreach ( $old_levels as $old_level ) { | |
$old_level_options = array(); // TODO: Get array of options for $old_level. | |
if ( ! empty( $old_level_options ) && is_array( $old_level_options ) ) { | |
$old_options = array_merge( $old_options, $old_level_options ); | |
} | |
} | |
// Populate new_options. | |
foreach ( $new_levels as $new_level ) { | |
$new_level_options = array(); // TODO: Get array of options for $new_level. | |
if ( ! empty( $new_level_options ) && is_array( $new_level_options ) ) { | |
$new_options = array_merge( $new_options, $new_level_options ); | |
} | |
} | |
// Step 4: Remove duplicates in the array of old and new options. | |
$new_options = array_unique( $old_options ); | |
$old_options = array_unique( $new_options ); | |
// Step 5: Build the final array of options that needs to be added/removed for the current user. | |
$add_options = array_values( array_diff( $new_options, $old_options ) ); | |
$remove_options = array_values( array_diff( $old_options, $new_options ) ); | |
// Step 6: Execute changes. | |
foreach ( $add_options as $add_option ) { | |
// TODO: Add the option for the user. | |
} | |
foreach ( $remove_options as $remove_option ) { | |
// TODO: Remove the option from the user. | |
} | |
} | |
} | |
add_action( 'pmpro_after_all_membership_level_changes', 'my_pmpro_after_all_membership_level_changes_sample', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment