Forked from cartpauj/memberpress-corporate-all-sub-accounts.php
Last active
July 9, 2020 16:24
-
-
Save X-Raym/a2c5a2eb05b9be3c04ee9972876aa2df to your computer and use it in GitHub Desktop.
Get all sub-accounts for a parent user in MemberPress Corporate Accounts
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 | |
/** | |
* Plugin Name: MemberPress Corporate Sub Account Shortcode | |
* Plugin URI: https://gist.github.com/X-Raym/a2c5a2eb05b9be3c04ee9972876aa2df/edit | |
* Description: AMemberPress Corporate Sub Account Shortcode | |
* Author: X-Raym | |
* Author URI: https://www.extremraym.com/ | |
* License: GNU AGPLv3 | |
* License URI: http://www.gnu.org/licenses/agpl-3.0.html | |
* Date: 2020-07-09 | |
* Version: 1.0.2 | |
*/ | |
add_shortcode( 'membepress_coporate_subs', 'memberpress_corporate_subs_shortcode' ); | |
function memberpress_corporate_subs_shortcode( $atts, $tag ) { | |
if ( ! ( class_exists( 'MeprUtils' ) && class_exists( 'MPCA_Corporate_Account' ) && class_exists( 'MeprUser' ) ) ) | |
return "Memberclass or Memberclass corporate add-on is not installed."; | |
$user = MeprUtils::get_currentuserinfo(); | |
$sub_user_ids = memberpress_get_current_user_corporate_subs( $user ); | |
$output = ''; | |
if( is_array($sub_user_ids) && !empty($sub_user_ids)) { | |
$output .= '<h2>Your Sub Account Users</h2><ul>'; | |
foreach($sub_user_ids as $user_id) { | |
$user = new MeprUser($user_id); | |
$output .= '<li>' . $user->user_login . '</li>'; | |
} | |
$output .= '</ul>'; | |
} else { | |
$output .= '<p>' . $sub_user_ids . '</p>'; | |
} | |
return $output; | |
} | |
function memberpress_get_current_user_corporate_subs( $user ) { | |
$sub_user_ids = array(); | |
if($user !== false) { | |
$transactions = $user->active_product_subscriptions('transactions'); | |
if(!empty($transactions)) { | |
foreach($transactions as $txn) { | |
if(($sub = $txn->subscription()) !== false) { | |
//Recurring subscription | |
$ca = MPCA_Corporate_Account::find_corporate_account_by_obj_id($sub->id, 'subscriptions'); | |
} | |
else { | |
//Non Recurring subscription | |
$ca = MPCA_Corporate_Account::find_corporate_account_by_obj_id($txn->id, 'transactions'); | |
} | |
if(!empty($ca) && isset($ca->id) && !empty($ca->id)) { | |
$sub_users = $ca->sub_users(); | |
foreach($sub_users as $key => $user) { | |
//$sub_user_ids[$key] = $user->ID; | |
array_push ($sub_users, $user->ID); | |
} | |
} | |
} | |
$sub_user_ids = array_unique($sub_user_ids); | |
if( empty($sub_user_ids) ) { | |
$sub_user_ids = 'No Sub User IDs'; | |
} | |
} else { | |
$sub_user_ids = 'Transaction is empty for this user.'; | |
} | |
} else { | |
$sub_user_ids = 'Invalid User Paramater.'; | |
} | |
return $sub_user_ids; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment