Last active
April 7, 2021 02:41
-
-
Save strangerstudios/6c664091480da8f67c017ca5e6709b88 to your computer and use it in GitHub Desktop.
A report to show membership level changes (upgrades or downgrades) from initial level to current level.
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 | |
/* | |
Changes Report for Paid Memberships Pro | |
Title: pmpro_reports_changes | |
Slug: pmpro_reports_changes | |
*/ | |
//update this array for your desired reports. the format is: "report name" => array( initial_level_id, current_level_id ), | |
global $pmpro_reports_level_changes; | |
$pmpro_reports_level_changes = array( | |
"Members upgrading from Level 1 to 2" => array( 1, 2), | |
"Members downgrading from Level 3 to 2" => array( 3, 2), | |
); | |
global $pmpro_reports; | |
$pmpro_reports['changes'] = __('Membership Level Changes', 'pmpro-reports-changes'); | |
function pmpro_report_changes_widget() { | |
?> | |
<table class="widefat striped"> | |
<thead> | |
<tr> | |
<th><?php _e( 'Name', 'pmpro-reports-changes' ); ?></th> | |
<th><?php _e( 'Initial Level', 'pmpro-reports-changes' ); ?></th> | |
<th><?php _e( 'Current Level', 'pmpro-reports-changes' ); ?></th> | |
<th><?php _e( 'Count', 'pmpro-reports-changes' ); ?></th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
global $wpdb, $pmpro_reports_level_changes; | |
foreach( $pmpro_reports_level_changes as $key => $pmpro_report_level_changes ) { | |
$current_level = pmpro_getLevel( $pmpro_report_level_changes[1] ); | |
$initial_level = pmpro_getLevel( $pmpro_report_level_changes[0] ); | |
$changes_count_query = $wpdb->prepare(" | |
SELECT COUNT(mu1.id) | |
FROM $wpdb->pmpro_memberships_users mu1 | |
LEFT JOIN $wpdb->pmpro_memberships_users mu2 ON mu1.user_id = mu2.user_id AND mu2.membership_id = %d AND mu2.id < mu1.id | |
WHERE mu1.membership_id = %d AND mu1.status = 'active' AND mu2.id IS NOT NULL", $initial_level->id, $current_level->id); | |
$changes_count = $wpdb->get_var( $changes_count_query); | |
?> | |
<tr> | |
<th scope="row"><?php echo $key; ?></th> | |
<td><?php | |
if( empty( $initial_level ) ) { | |
echo '-'; | |
} else { | |
echo $initial_level->name; | |
} | |
?></td> | |
<td><?php echo $current_level->name; ?></td> | |
<td> <strong><?php echo number_format_i18n( $changes_count ); ?></strong></td> | |
</tr> | |
<?php | |
} | |
?> | |
</tbody> | |
</table> | |
<?php | |
} | |
function pmpro_report_changes_page() { | |
?> | |
<h1><?php _e( 'Membership Level Changes Report', 'pmpro-reports-changes' ); ?></h1> | |
<table class="widefat striped"> | |
<thead> | |
<tr> | |
<th><?php _e( 'Name', 'pmpro-reports-changes' ); ?></th> | |
<th><?php _e( 'Initial Level', 'pmpro-reports-changes' ); ?></th> | |
<th><?php _e( 'Current Level', 'pmpro-reports-changes' ); ?></th> | |
<th><?php _e( 'Count', 'pmpro-reports-changes' ); ?></th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
global $wpdb, $pmpro_reports_level_changes; | |
foreach( $pmpro_reports_level_changes as $key => $pmpro_report_level_changes ) { | |
$current_level = pmpro_getLevel( $pmpro_report_level_changes[1] ); | |
$initial_level = pmpro_getLevel( $pmpro_report_level_changes[0] ); | |
$changes_count_query = $wpdb->prepare(" | |
SELECT COUNT(mu1.id) | |
FROM $wpdb->pmpro_memberships_users mu1 | |
LEFT JOIN $wpdb->pmpro_memberships_users mu2 ON mu1.user_id = mu2.user_id AND mu2.membership_id = %d AND mu2.id < mu1.id | |
WHERE mu1.membership_id = %d AND mu1.status = 'active' AND mu2.id IS NOT NULL", $initial_level->id, $current_level->id); | |
$changes_count = $wpdb->get_var( $changes_count_query); | |
?> | |
<tr> | |
<th scope="row"><?php echo $key; ?></th> | |
<td><?php | |
if( empty( $initial_level ) ) { | |
echo '-'; | |
} else { | |
echo $initial_level->name; | |
} | |
?></td> | |
<td><?php echo $current_level->name; ?></td> | |
<td> <strong><?php echo number_format_i18n( $changes_count ); ?></strong></td> | |
</tr> | |
<?php | |
} | |
?> | |
</tbody> | |
</table> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "New Report to View Membership Level Changes (Upgrades or Downgrades)" at Paid Memberships Pro here: https://www.paidmembershipspro.com/new-report-view-membership-level-changes-upgrades-downgrades/