Advertisement
verygoodplugins

Untitled

Aug 11th, 2020
3,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.74 KB | None | 0 0
  1.     /**
  2.      * Triggered when payment for membership / product is complete (for one-time or free billing)
  3.      *
  4.      * @access  public
  5.      * @return  void
  6.      */
  7.  
  8.     public function apply_tags_checkout( $event ) {
  9.  
  10.         // The mepr-signup hook passes a transaction already
  11.         if ( is_a( $event, 'MeprTransaction' ) ) {
  12.             $txn = $event;
  13.         } else {
  14.             $txn = $event->get_data();
  15.         }
  16.  
  17.         if ( 'complete' != $txn->status ) {
  18.             return;
  19.         }
  20.  
  21.         // No need to run this twice if another action fires
  22.         remove_action( 'mepr-signup', array( $this, 'apply_tags_checkout' ) );
  23.         remove_action( 'mepr-event-transaction-completed', array( $this, 'apply_tags_checkout' ) );
  24.         remove_action( 'mepr-txn-status-complete', array( $this, 'apply_tags_checkout' ) );
  25.  
  26.         // Log the action
  27.  
  28.         $action = 'unknown action';
  29.  
  30.         if ( doing_action( 'mepr-signup' ) ) {
  31.             $action = 'mepr-signup';
  32.         } elseif ( doing_action( 'mepr-event-transaction-completed' ) ) {
  33.             $action = 'mepr-event-transaction-completed';
  34.         } elseif ( doing_action( 'mepr-txn-status-complete' ) ) {
  35.             $action = 'mepr-txn-status-complete';
  36.         }
  37.  
  38.         // Logger
  39.         wpf_log( 'info', $txn->user_id, 'New MemberPress transaction <a href="' . admin_url( 'admin.php?page=memberpress-trans&action=edit&id=' . $txn->id ) . '" target="_blank">#' . $txn->id . '</a> (' . $action . ')' );
  40.  
  41.         //
  42.         // Get meta fields
  43.         //
  44.  
  45.         $payment_method = $txn->payment_method();
  46.         $product_id     = $txn->product_id;
  47.  
  48.         $update_data = array(
  49.             'mepr_membership_level' => get_the_title( $product_id ),
  50.             'mepr_reg_date'         => $txn->created_at,
  51.             'mepr_payment_method'   => $payment_method->name,
  52.         );
  53.  
  54.         // Add expiration only if applicable
  55.         if ( strtotime( $txn->expires_at ) >= 0 ) {
  56.             $update_data['mepr_expiration'] = $txn->expires_at;
  57.         }
  58.  
  59.         // Coupons
  60.         if ( ! empty( $txn->coupon_id ) ) {
  61.             $update_data['mepr_coupon'] = get_the_title( $txn->coupon_id );
  62.         }
  63.  
  64.         // Push all meta as well to get any updated custom field values during upgrades
  65.         $user_meta = array_map( array( wp_fusion()->user, 'map_user_meta' ), get_user_meta( $txn->user_id ) );
  66.  
  67.         $update_data = array_merge( $user_meta, $update_data );
  68.  
  69.         wp_fusion()->user->push_user_meta( $txn->user_id, $update_data );
  70.  
  71.         //
  72.         // Update tags based on the product purchased
  73.         //
  74.  
  75.         $apply_tags = array();
  76.  
  77.         $settings = get_post_meta( $txn->product_id, 'wpf-settings-memberpress', true );
  78.  
  79.         if ( ! empty( $settings ) ) {
  80.  
  81.             if ( ! empty( $settings['apply_tags_registration'] ) ) {
  82.                 $apply_tags = array_merge( $apply_tags, $settings['apply_tags_registration'] );
  83.             }
  84.  
  85.             if ( ! empty( $settings['tag_link'] ) ) {
  86.                 $apply_tags = array_merge( $apply_tags, $settings['tag_link'] );
  87.             }
  88.  
  89.             if ( ! empty( $settings['apply_tags_payment_failed'] ) ) {
  90.  
  91.                 // Remove any failed tags
  92.                 remove_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
  93.  
  94.                 wp_fusion()->user->remove_tags( $settings['apply_tags_payment_failed'], $txn->user_id );
  95.  
  96.                 add_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
  97.  
  98.             }
  99.  
  100.             // If this transaction was against a subscription that had a trial, and is no longer in a trial, consider it "converted"
  101.             $subscription = $txn->subscription();
  102.  
  103.             if ( false !== $subscription && true == $subscription->trial ) {
  104.  
  105.                 // Figure out if it's the first real payment
  106.  
  107.                 $first_payment = false;
  108.  
  109.                 if ( $subscription->trial_amount > 0.00 && $subscription->txn_count == 2 ) {
  110.                     $first_payment = true;
  111.                 } elseif ( $subscription->trial_amount == 0.00 && $subscription->txn_count == 1 ) {
  112.                     $first_payment = true;
  113.                 }
  114.  
  115.                 if ( true == $first_payment && ! empty( $settings['apply_tags_converted'] ) ) {
  116.  
  117.                     $apply_tags = array_merge( $apply_tags, $settings['apply_tags_converted'] );
  118.  
  119.                 }
  120.             }
  121.         }
  122.  
  123.         // Coupons
  124.         if ( ! empty( $txn->coupon_id ) ) {
  125.  
  126.             $coupon_settings = get_post_meta( $txn->coupon_id, 'wpf-settings', true );
  127.  
  128.             if ( ! empty( $coupon_settings ) && ! empty( $coupon_settings['apply_tags_coupon'] ) ) {
  129.                 $apply_tags = array_merge( $apply_tags, $coupon_settings['apply_tags_coupon'] );
  130.             }
  131.         }
  132.  
  133.         // Corporate accounts
  134.         $corporate_account = get_user_meta( $txn->user_id, 'mpca_corporate_account_id', true );
  135.  
  136.         if ( ! empty( $corporate_account ) && ! empty( $settings['apply_tags_corporate_accounts'] ) ) {
  137.             $apply_tags = array_merge( $apply_tags, $coupon_settings['apply_tags_corporate_accounts'] );
  138.         }
  139.  
  140.         if ( ! empty( $apply_tags ) ) {
  141.  
  142.             // Prevent looping when tags are applied
  143.             remove_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
  144.  
  145.             wp_fusion()->user->apply_tags( $apply_tags, $txn->user_id );
  146.  
  147.             add_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
  148.  
  149.         }
  150.  
  151.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement