Skip to content

Instantly share code, notes, and snippets.

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 LMNTL/e60c35c7176463227e370e51d89f6c32 to your computer and use it in GitHub Desktop.
Save LMNTL/e60c35c7176463227e370e51d89f6c32 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() {
?>
<span id="pmpro_report_changes" class="pmpro_report-holder">
<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>
<p class="pmpro_report-button">
<a class="button button-primary" href="/wp-admin/admin.php?page=pmpro-reports&amp;report=changes">Details</a>
</p>
</span>
<?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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment