diff --git a/classes/class-pmprogroupacct-group.php b/classes/class-pmprogroupacct-group.php index ae99f74..2eeecda 100644 --- a/classes/class-pmprogroupacct-group.php +++ b/classes/class-pmprogroupacct-group.php @@ -393,20 +393,4 @@ protected static function generate_group_checkout_code() { return $new_group_checkout_code; } - - /** - * Checks whether a specific level ID can be claimed by joining this group. - * - * @since TBD - * - * @param int $level_id The ID of the membership level to check. - * @return bool True if the level can be claimed by joining this group, false otherwise. - */ - public function can_claim_level( $level_id ) { - $settings = pmprogroupacct_get_settings_for_level( $this->group_parent_level_id ); - if ( empty( $settings ) ) { - return false; - } - return in_array( (int)$level_id, $settings['child_level_ids'], true ); - } } diff --git a/includes/admin.php b/includes/admin.php index 3ea4219..7061fb8 100644 --- a/includes/admin.php +++ b/includes/admin.php @@ -217,6 +217,22 @@ function pmprogroupacct_plugin_row_meta($links, $file) { add_filter('plugin_row_meta', 'pmprogroupacct_plugin_row_meta', 10, 2); + +/* + Code to import group account member associations when using the Import Users From CSV plugin. + + Step 0. Have your group account levels properly configured + + Step 1. Add the leader members with following columns to your import CSV: + * pmprogroupacct_group_parent_level_id: Id of the parent level. + * pmprogroupacct_group_checkout_code: The code that can be distributed to create child accounts. + * pmprogroupacct_group_total_seats: Amount of seats or child accounts that can be created. + Step 2. Add the child members with following columns to your import CSV: + * pmprogroupacct_group_child_user_id: Id of the user that owns a parent level. + * pmprogroupacct_group_child_level_id: Id of the child level + * pmprogroupacct_group_group_id: Id of the wp_pmpro_groups group +*/ + /** * Import group account member associations when using the Import Users From CSV plugin. * @@ -225,71 +241,51 @@ function pmprogroupacct_plugin_row_meta($links, $file) { * @since TBD */ function pmprogroupacct_pmproiucsv_post_user_import( $user_id ) { - //get the user + global $wpdb; $user = get_userdata( $user_id ); - //It's a parent account, so let's update the number of seats. The add on created the group account already. - if( ! empty( $user->pmprogroupacct_group_total_seats ) ) { - //Check if the user level is a group account level - $existing_group = PMProGroupAcct_Group::get_group_by_parent_user_id_and_parent_level_id( $user_id, $user->membership_id ); - if ( ! empty( $existing_group ) ) { - //Let's update the number of seats. intval will set to 0 if blank or any oddity - $existing_group->update_group_total_seats( intval( $user->pmpro_groupseats ) ); + + //Is this a parent user? + if ( ! empty( $user->pmprogroupacct_group_parent_level_id ) ) { + + //bail if this row has not all the necessary data + if ( empty( $user->pmprogroupacct_group_total_seats ) ) { return; } - // it's a child account let's update the DB to reflect that. - } else if (! empty( $user->pmprogroupacct_group_account_owner ) ) { - //Get user's group account owner by id, login name or email - if( is_numeric( $user->pmprogroupacct_group_account_owner ) ) { - $group_account_owner = get_userdata( $user->pmprogroupacct_group_account_owner ); - } else if ( strpos($sponsor, "@") !== false ) { - $group_account_owner = get_user_by( 'email', $user->pmprogroupacct_group_account_owner ); - } else { - $group_account_owner = get_user_by( 'login', $user->pmprogroupacct_group_account_owner ); - } - //bail if we can't find the group account owner - if( empty( $group_account_owner ) ) { + + //If the checkout code isn't given we assume the importer want to genereate a new code. + if ( empty( $user->pmprogroupacct_group_checkout_code ) ) { + $parent_group = PMProGroupAcct_Group::create( $user_id, $user->pmprogroupacct_group_parent_level_id, $user->pmprogroupacct_group_total_seats ); + //line above will insert the record, no need to run the insert below return; } + //insert the parent record + $wpdb->insert( + $wpdb->pmprogroupacct_groups, + array( + 'group_parent_user_id' => (int) $user->ID, + 'group_parent_level_id' => (int) $user->pmprogroupacct_group_parent_level_id, + 'group_checkout_code' => $user->pmprogroupacct_group_checkout_code, + 'group_total_seats' => (int) $user->pmprogroupacct_group_total_seats, + ) + ); + //Is this a child user? + } else if ( ! empty( $user->pmprogroupacct_group_child_level_id ) ) { - // Get the membership levels for the group account owner. - $owner_levels = pmpro_getMembershipLevelsForUser( $group_account_owner->ID ); - //bail if the owner doesn't have any levels - if( empty( $owner_levels ) ) { + //bail if this row has not all the necessary data + if ( empty( $user->pmprogroupacct_group_child_level_id ) || empty( $user->pmprogroupacct_group_group_id ) || empty( $user->pmprogroupacct_group_child_status ) ) { return; } - foreach( $owner_levels as $owner_level ) { - $group = PMProGroupAcct_Group::get_group_by_parent_user_id_and_parent_level_id( $group_account_owner->ID, $group_account_owner->membership_id ); - //bail is if null - if ( is_null( $group ) ) { - break; - } - - //chck if the passed level is a child of the parent level - $child_level_ids = pmprogroupacct_get_child_level_ids( $group ); - if ( ! in_array( $user->membership_id, $child_level_ids ) ) { - // This is not a valid group code. Bail. - break; - } - - // Check if the user somehow is already in the DB - $group_member_query_args = array( - 'group_id' => $group->id, - 'group_child_user_id' => $user_id, - ); - - $group_members = PMProGroupAcct_Group_Member::get_group_members( $group_member_query_args ); - if ( ! empty( $group_members ) ) { - // This user was previously a member of this group. Update their status to active. - foreach ( $group_members as $group_member ) { - $group_member->update_group_child_status( 'active' ); - } - return; - } else { - // This user was not previously a member of this group. Add them to the group. - PMProGroupAcct_Group_Member::create( $user_id, $user->membership_id, $group->id ); - } - } + //insert or updaate the child record + $wpdb->insert( + $wpdb->pmprogroupacct_group_members, + array( + 'group_child_user_id' => $user->ID, + 'group_child_level_id' => $user->pmprogroupacct_group_child_level_id, + 'group_id' => $user->pmprogroupacct_group_group_id, + 'group_child_status' => $user->pmprogroupacct_group_child_status, + ) + ); } } diff --git a/includes/children.php b/includes/children.php index cf37cc3..4ce8cff 100644 --- a/includes/children.php +++ b/includes/children.php @@ -22,12 +22,9 @@ function pmprogroupacct_checkout_before_form_child() { if ( ! empty( $group_code ) ) { // Check if the group code is valid. $group = PMProGroupAcct_Group::get_group_by_checkout_code( $group_code ); - if ( ! empty( $group ) && $group->is_accepting_signups() && $group->can_claim_level( $checkout_level->id ) ) { + if ( ! empty( $group ) && $group->is_accepting_signups() ) { // Show a message that the group code has been applied. pmpro_setMessage( esc_html__( 'Group code applied.', 'pmpro-group-accounts' ), 'pmpro_success' ); - } elseif ( ! $group->can_claim_level( $checkout_level->id ) ) { - // Show a message that the group code cannot be used to claim this level. - pmpro_setMessage( esc_html__( 'This group code cannot be used to claim this level.', 'pmpro-group-accounts' ), 'pmpro_error' ); } elseif ( ! empty( $group ) ) { // Show a message that the group code is not accepting signups. pmpro_setMessage( esc_html__( 'This group is no longer accepting signups.', 'pmpro-group-accounts' ), 'pmpro_error' ); @@ -63,7 +60,7 @@ function pmprogroupacct_checkout_after_level_cost_child() { if ( ! empty( $group_code ) ) { // Check if the group code is valid. $group = PMProGroupAcct_Group::get_group_by_checkout_code( $group_code ); - if ( ! empty( $group ) && $group->is_accepting_signups() && $group->can_claim_level( $checkout_level->id ) ) { + if ( ! empty( $group ) && $group->is_accepting_signups() ) { // Add a hidden field to the checkout form with the group code and return so that we don't show the group code field. ?>
@@ -124,7 +121,7 @@ function pmprogroupacct_pmpro_checkout_level_child( $level ) { if ( ! empty( $group_code ) ) { // Check if the group code is valid. $group = PMProGroupAcct_Group::get_group_by_checkout_code( $group_code ); - if ( ! empty( $group ) && $group->is_accepting_signups() && $group->can_claim_level( $level->id ) ) { + if ( ! empty( $group ) && $group->is_accepting_signups() ) { // Set the level cost to 0. $level->initial_payment = 0; $level->billing_amount = 0; @@ -158,7 +155,7 @@ function pmprogroupacct_pmpro_level_cost_text_child_checkout( $level_cost_text, if ( ! empty( $group_code ) ) { // Check if the group code is valid. $group = PMProGroupAcct_Group::get_group_by_checkout_code( $group_code ); - if ( ! empty( $group ) && $group->is_accepting_signups() && $group->can_claim_level( $level->id ) ) { + if ( ! empty( $group ) && $group->is_accepting_signups() ) { // Unset the level cost text. $level_cost_text = ''; } @@ -209,13 +206,6 @@ function pmprogroupacct_pmpro_registration_checks_child( $pmpro_continue_registr return false; } - // Make sure that the group code can claim the level being checked out for. - if ( ! $group->can_claim_level( $checkout_level->id ) ) { - // This group code cannot claim this level. Show an error message. - pmpro_setMessage( esc_html__( 'This group code cannot be used to claim this level.', 'pmpro-group-accounts' ), 'pmpro_error' ); - return false; - } - // Check if this group is accepting signups. if ( ! $group->is_accepting_signups() ) { // This group is not accepting signups. Show an error message. diff --git a/includes/functions.php b/includes/functions.php index 9ab2d95..cfe8848 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -50,19 +50,3 @@ function pmprogroupacct_level_can_be_claimed_using_group_codes( $level_id ) { } return false; } - -/** - * Given parent level ID, return the child level IDs. - * - * @since TBD - * - * @param int $parent_level_id The ID of the parent membership level. - * @return array The IDs of the child membership levels. - */ -function pmprogroupacct_get_child_level_ids( $parent_level_id ) { - // Get the group account settings for the level. - $settings = pmprogroupacct_get_settings_for_level( $parent_level_id ); - - // Return the child level IDs. - return empty( $settings['child_level_ids'] ) ? array() : $settings['child_level_ids']; -} diff --git a/includes/parents.php b/includes/parents.php index 11f36e0..7c7a1f0 100644 --- a/includes/parents.php +++ b/includes/parents.php @@ -330,17 +330,12 @@ function pmprogroupacct_pmpro_after_all_membership_level_changes_parent( $old_us // Get the new level for this user. $new_levels = pmpro_getMembershipLevelsForUser( $user_id ); - $new_level_ids = wp_list_pluck( $new_levels, 'id' ); - - // Make sure the user has a group for any group parent levels they gained. - $received_level_ids = array_diff( $new_level_ids, $old_level_ids ); - foreach ( $received_level_ids as $received_level_id ) { - pmprogroupacct_create_free_group_if_needed( $user_id, $received_level_id ); - } + $new_level_ids = wp_list_pluck( $new_levels, 'id' ); + // Get the levels that the user lost. + $lost_level_ids = array_diff( $old_level_ids, $new_level_ids ); // Check if the parent has a group for any of the levels they lost. - $lost_level_ids = array_diff( $old_level_ids, $new_level_ids ); foreach ( $lost_level_ids as $lost_level_id ) { $existing_group = PMProGroupAcct_Group::get_group_by_parent_user_id_and_parent_level_id( $user_id, $lost_level_id ); if ( ! empty( $existing_group ) ) { @@ -405,63 +400,3 @@ function pmprogroupacct_pmpro_invoice_bullets_bottom_parent( $invoice ) { ID ); - - // Loop through the user's levels and create empty groups if needed. - foreach ( $levels as $level ) { - pmprogroupacct_create_free_group_if_needed( $user->ID, $level->id ); - } -} -add_action( 'wp_login', 'pmprogroupacct_wp_login_parent', 10, 2 ); - - -/** - * Creates an "free" group if needed for a given parent user and level. - * - * A "free" group is a new, empty group that is created with the maximum number - * of free seats for the level. We do not want to give paid seats to a group - * until the user has completed checkout and paid for them. - * - * @since TBD - * - * @param int $user_id The ID of the parent user. - * @param int $level_id The ID of the level. - */ -function pmprogroupacct_create_free_group_if_needed( $user_id, $level_id ) { - // Get the group settings for this level. - $settings = pmprogroupacct_get_settings_for_level( $level_id ); - - // If there are no settings, then this is not a group parent level. Bail. - if ( empty( $settings ) ) { - return; - } - - // Check if there is already a group for this user and level. - $existing_group = PMProGroupAcct_Group::get_group_by_parent_user_id_and_parent_level_id( $user_id, $level_id ); - if ( ! empty( $existing_group ) ) { - return; - } - - // Create a group for this user and level. - // If the pricing model is free, give them the max seats. - // Otherwise, give them them 0. The `pmpro_after_checkout` filter will update the seats at chekout if needed. - $seats = ( $settings['pricing_model'] === 'none' ) ? $settings['max_seats'] : 0; - PMProGroupAcct_Group::create( $user_id, $level_id, $seats ); -}