Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active April 7, 2021 02:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save strangerstudios/6c664091480da8f67c017ca5e6709b88 to your computer and use it in GitHub Desktop.
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.
<?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&nbsp;Level', 'pmpro-reports-changes' ); ?></th>
<th><?php _e( 'Current&nbsp;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&nbsp;Level', 'pmpro-reports-changes' ); ?></th>
<th><?php _e( 'Current&nbsp;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
}
@LMNTL
Copy link

LMNTL commented Jun 6, 2019

updated version available here, fixes missing "details" button on current version of PMPro:
https://gist.github.com/LMNTL/e60c35c7176463227e370e51d89f6c32

@laurenhagan0306
Copy link

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment