Plugin Directory

Changeset 3154426


Ignore:
Timestamp:
09/19/2024 10:00:47 AM (19 months ago)
Author:
expresstechsoftware
Message:

Upgraded the plugin to version 1.2.21. Implemented SSO functionality, added checks to avoid re-adding members to Discord guild if already present, and updated the changelog.

Location:
pmpro-discord-add-on
Files:
144 added
4 edited

Legend:

Unmodified
Added
Removed
  • pmpro-discord-add-on/trunk/CHANGELOG.txt

    r3139125 r3154426  
     1= 1.2.21 =
     2- Implemented Single Sign-On (SSO) feature with Discord - allowing users to authenticate via the "Connect with Discord" button without needing to manually create an account if one already exists
     3- Added logic to check if a member has already been added to the Discord guild to prevent duplicate additions - optimizing role management and avoiding unnecessary API calls
     4
    15= 1.2.20 =
    26- Added handling for cases where Discord user email does not exist, ensuring a temporary email is used.
  • pmpro-discord-add-on/trunk/includes/classes/class-discord-api.php

    r3139125 r3154426  
    450450        $guild_response         = wp_remote_post( $guilds_memeber_api_url, $guild_args );
    451451
     452        $response_arr = json_decode( wp_remote_retrieve_body( $guild_response ), true );
     453
    452454        ets_pmpro_discord_log_api_response( $user_id, $guilds_memeber_api_url, $guild_args, $guild_response );
    453455        if ( ets_pmpro_discord_check_api_errors( $guild_response ) ) {
     
    460462
    461463        update_user_meta( $user_id, '_ets_pmpro_discord_role_id', $discord_role );
     464
    462465        if ( $discord_role && $discord_role != 'none' && isset( $user_id ) ) {
    463466            $this->put_discord_role_api( $user_id, $discord_role );
     
    636639                            wp_signon( $credentials, '' );
    637640                            $discord_user_id = sanitize_text_field( trim( get_user_meta( $user_id, '_ets_pmpro_discord_user_id', true ) ) );
    638                             $this->add_discord_member_in_guild( $discord_user_id, $user_id, $access_token );
     641
     642                            //Do not call add member to guild if the member is connected via discord, check for usermeta.
     643                            $last_joined_date = trim( get_user_meta( $user_id, '_ets_pmpro_discord_join_date', true) );
     644
     645                            if(!$last_joined_date){
     646                                $this->add_discord_member_in_guild( $discord_user_id, $user_id, $access_token );
     647                            }
    639648                            if ( $_COOKIE['ets_discord_page'] ) {
    640649                                wp_safe_redirect( urldecode_deep( $_COOKIE['ets_discord_page'] ) );
     
    657666        $access_token          = sanitize_text_field( trim( $res_body['access_token'] ) );
    658667        update_user_meta( $user_id, '_ets_pmpro_discord_access_token', $access_token );
     668
    659669        if ( array_key_exists( 'refresh_token', $res_body ) ) {
    660670            $refresh_token = sanitize_text_field( trim( $res_body['refresh_token'] ) );
    661671            update_user_meta( $user_id, '_ets_pmpro_discord_refresh_token', $refresh_token );
    662672        }
     673
    663674        if ( array_key_exists( 'expires_in', $res_body ) ) {
    664675            $expires_in = $res_body['expires_in'];
     
    668679            update_user_meta( $user_id, '_ets_pmpro_discord_expires_in', $token_expiry_time );
    669680        }
     681
    670682        $user_body = $this->get_discord_current_user( $access_token );
    671683
     
    676688            update_user_meta( $user_id, '_ets_pmpro_discord_username', $discord_user_name_with_number );
    677689        }
     690
    678691        if ( is_array( $user_body ) && array_key_exists( 'id', $user_body ) ) {
    679692            $_ets_pmpro_discord_user_id = sanitize_text_field( trim( $user_body['id'] ) );
    680             if ( $discord_exist_user_id == $_ets_pmpro_discord_user_id ) {
     693
     694            if ( $discord_exist_user_id == $_ets_pmpro_discord_user_id ) {             
    681695                $_ets_pmpro_discord_role_id = sanitize_text_field( trim( get_user_meta( $user_id, '_ets_pmpro_discord_role_id', true ) ) );
    682                 if ( ! empty( $_ets_pmpro_discord_role_id ) && $_ets_pmpro_discord_role_id != 'none' ) {
     696               
     697                // Get the expected role based on the current membership level
     698                $expected_role_id = $this->get_expected_discord_role( $user_id );
     699
     700                // Only delete and reassign if the current role doesn't match the expected role
     701                if ( !empty($_ets_pmpro_discord_role_id) && $_ets_pmpro_discord_role_id != 'none' && $_ets_pmpro_discord_role_id != $expected_role_id ) {
    683702                    $this->delete_discord_role( $user_id, $_ets_pmpro_discord_role_id );
    684703                }
    685704            }
     705
     706            // Update the user meta with the Discord user ID
    686707            update_user_meta( $user_id, '_ets_pmpro_discord_user_id', $_ets_pmpro_discord_user_id );
    687708        }
    688 
    689     }
     709    }
     710
     711    /*
     712    * Function to get the expected Discord role based on the user's current membership level
     713    */
     714    private function get_expected_discord_role( $user_id ) {
     715        $current_membership_level_id = pmpro_getMembershipLevelForUser( $user_id )->ID;
     716        $role_mapping = json_decode( get_option( 'ets_pmpor_discord_role_mapping' ), true );
     717       
     718        if ( isset( $role_mapping['pmpro_level_id_' . $current_membership_level_id] ) ) {
     719            return sanitize_text_field( $role_mapping['pmpro_level_id_' . $current_membership_level_id] );
     720        }
     721        return 'none';
     722    }
     723
     724
    690725
    691726    /**
     
    746781        delete_user_meta( $user_id, '_ets_pmpro_discord_username' );
    747782        delete_user_meta( $user_id, '_ets_pmpro_discord_expires_in' );
     783        delete_user_meta( $user_id, '_ets_pmpro_discord_join_date' );
    748784
    749785    }
  • pmpro-discord-add-on/trunk/pmpro-discord.php

    r3139125 r3154426  
    44 * Plugin URI:  https://www.expresstechsoftwares.com/step-by-step-documentation-guide-on-how-to-connect-pmpro-and-discord-server-using-discord-addon
    55 * Description: Connect your PaidMebershipPro site to your discord server, enable your members to be part of your community.
    6  * Version: 1.2.20
     6 * Version: 1.2.21
    77 * Author: ExpressTech Software Solutions Pvt. Ltd., Strangers Studios
    88 * Author URI: https://www.expresstechsoftwares.com
     
    1515
    1616// create plugin version constant.
    17 define( 'ETS_PMPRO_VERSION', '1.2.20' );
     17define( 'ETS_PMPRO_VERSION', '1.2.21' );
    1818
    1919// create plugin url constant.
  • pmpro-discord-add-on/trunk/readme.txt

    r3139125 r3154426  
    88Tested up to: 6.5
    99Requires PHP: 7.0
    10 Stable tag: 1.2.20
     10Stable tag: 1.2.21
    1111License: GPLv2
    1212License URI: https://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset for help on using the changeset viewer.