Advertisement
Guest User

Untitled

a guest
Feb 28th, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 KB | None | 0 0
  1.  
  2. /**
  3.  * Retrieves affiliate id for the given user.
  4.  *
  5.  * @param int $user_id user id.
  6.  *
  7.  * @return string|null
  8.  */
  9. function bdx_get_affiliate_id( $user_id ) {
  10.  
  11.     global $wpdb;
  12.     $table = $wpdb->prefix . 'uap_referrals';
  13.  
  14.     return $wpdb->get_var( $wpdb->prepare( "SELECT affiliate_id FROM {$table} WHERE refferal_wp_uid =%d", $user_id ) );
  15. }
  16.  
  17. /**
  18.  * Make all newly signing user's folllow their sponsor automatically(the affiliate who invited them).
  19.  */
  20. add_action( 'user_register', function ( $user_id ) {
  21.  
  22.     // 1. check if follow exists.
  23.     if ( ! function_exists( 'bp_follow_is_following' ) ) {
  24.         return;
  25.     }
  26.  
  27.     global $indeed_db;
  28.  
  29.     if ( empty( $indeed_db ) || ! method_exists( $indeed_db, 'get_uid_by_affiliate_id' ) ) {
  30.         return;
  31.     }
  32.  
  33.     // check if wp affiliates pro exists.
  34.     $affiliate_id = bdx_get_affiliate_id( $user_id );
  35.     if ( ! $affiliate_id ) {
  36.         return;
  37.     }
  38.  
  39.     $affiliate_wp_user_id = $indeed_db->get_uid_by_affiliate_id( $affiliate_id );
  40.     if ( ! $affiliate_wp_user_id ) {
  41.         return;
  42.     }
  43.  
  44.     $args = array(
  45.         'leader_id'   => absint( $affiliate_wp_user_id ),
  46.         'follower_id' => $user_id,
  47.     );
  48.  
  49.     if ( ! bp_follow_is_following( $args ) ) {
  50.         bp_follow_start_following( $args );
  51.     }
  52.  
  53. }, 100 );
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement