verygoodplugins

Untitled

Jun 15th, 2020
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.63 KB | None | 0 0
  1.     public function subscription_status_changed( $old_status, $new_status, $subscription ) {
  2.  
  3.         // Don't run on pending subscriptions
  4.         if ( 'pending' == $new_status ) {
  5.             return;
  6.         }
  7.  
  8.         // Don't require the checkout callback
  9.         remove_action( 'mepr-event-transaction-completed', array( $this, 'apply_tags_checkout' ) );
  10.  
  11.         // Get subscription data
  12.         $data = $subscription->get_values();
  13.  
  14.         wpf_log( 'info', $data['user_id'], 'MemberPress subscription <a href="' . admin_url( 'admin.php?page=memberpress-subscriptions&action=edit&id=' . $subscription->id ) . '" target="_blank">#' . $subscription->id . '</a> status changed from <strong>' . ucwords( $old_status ) . '</strong> to <strong>' . ucwords( $new_status ) . '</strong>' );
  15.  
  16.         // Get WPF settings
  17.         $settings = get_post_meta( $data['product_id'], 'wpf-settings-memberpress', true );
  18.  
  19.         if ( empty( $settings ) ) {
  20.             $settings = array();
  21.         }
  22.  
  23.         $defaults = array(
  24.             'apply_tags_registration'       => array(),
  25.             'remove_tags'                   => false,
  26.             'tag_link'                      => array(),
  27.             'apply_tags_cancelled'          => array(),
  28.             'apply_tags_expired'            => array(),
  29.             'apply_tags_payment_failed'     => array(),
  30.             'apply_tags_corporate_accounts' => array(),
  31.             'apply_tags_trial'              => array(),
  32.             'apply_tags_converted'          => array(),
  33.         );
  34.  
  35.         $settings = wp_parse_args( $settings, $defaults );
  36.  
  37.         $apply_tags = array();
  38.  
  39.         $remove_tags = array();
  40.  
  41.         // New subscriptions
  42.         if ( 'active' == $new_status ) {
  43.  
  44.             // Apply tags
  45.             $apply_tags = array_merge( $apply_tags, $settings['apply_tags_registration'], $settings['tag_link'] );
  46.  
  47.             // Remove cancelled / expired tags
  48.             $remove_tags = array_merge( $remove_tags, $settings['apply_tags_cancelled'], $settings['apply_tags_expired'] );
  49.  
  50.             // Update data
  51.             $payment_method = $subscription->payment_method();
  52.  
  53.             $update_data = array(
  54.                 'mepr_reg_date'         => $data['created_at'],
  55.                 'mepr_payment_method'   => $payment_method->name,
  56.                 'mepr_membership_level' => get_the_title( $data['product_id'] ),
  57.             );
  58.  
  59.             // Add expiration only if applicable
  60.             if ( strtotime( $subscription->get_expires_at() ) >= 0 ) {
  61.                 $update_data['mepr_expiration'] = date( 'Y-m-d H:i:s', $subscription->get_expires_at() );
  62.             }
  63.  
  64.             // Sync trial duration
  65.             if ( $subscription->trial ) {
  66.                 $update_data['mepr_trial_duration'] = $subscription->trial_days;
  67.             }
  68.  
  69.             // Coupon used
  70.             if ( ! empty( $subscription->coupon_id ) ) {
  71.  
  72.                 $update_data['mepr_coupon'] = get_the_title( $subscription->coupon_id );
  73.  
  74.                 $coupon_settings = get_post_meta( $subscription->coupon_id, 'wpf-settings', true );
  75.  
  76.                 if ( ! empty( $coupon_settings ) && ! empty( $coupon_settings['apply_tags_coupon'] ) ) {
  77.                     $apply_tags = array_merge( $apply_tags, $coupon_settings['apply_tags_coupon'] );
  78.                 }
  79.             }
  80.  
  81.             // Get all meta as well to get any updated custom field values during upgrades
  82.             $user_meta = array_map( array( wp_fusion()->user, 'map_user_meta' ), get_user_meta( $data['user_id'] ) );
  83.  
  84.             $update_data = array_merge( $user_meta, $update_data );
  85.  
  86.             wp_fusion()->user->push_user_meta( $data['user_id'], $update_data );
  87.  
  88.         }
  89.  
  90.         // Other status changes
  91.         if ( $subscription->is_expired() && ! in_array( $new_status, array( 'active', 'pending' ) ) ) {
  92.  
  93.             // Expired subscription
  94.             $remove_tags = array_merge( $remove_tags, $settings['tag_link'] );
  95.             $apply_tags  = array_merge( $apply_tags, $settings['apply_tags_expired'] );
  96.  
  97.             if ( ! empty( $settings['remove_tags'] ) ) {
  98.                 $remove_tags = array_merge( $remove_tags, $settings['apply_tags_registration'] );
  99.             }
  100.         } elseif ( 'cancelled' == $new_status ) {
  101.  
  102.             // Cancelled subscription
  103.             $remove_tags = array_merge( $remove_tags, $settings['tag_link'] );
  104.             $apply_tags  = array_merge( $apply_tags, $settings['apply_tags_cancelled'] );
  105.  
  106.             if ( ! empty( $settings['remove_tags'] ) ) {
  107.                 $remove_tags = array_merge( $remove_tags, $settings['apply_tags_registration'] );
  108.             }
  109.         } elseif ( $subscription->in_trial() ) {
  110.  
  111.             // If is in a trial and isn't cancelled / expired
  112.             $apply_tags = array_merge( $apply_tags, $settings['apply_tags_trial'] );
  113.  
  114.         }
  115.  
  116.         // Prevent looping when tags are modified
  117.         remove_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
  118.  
  119.         // Remove any tags
  120.         if ( ! empty( $remove_tags ) ) {
  121.             wp_fusion()->user->remove_tags( $remove_tags, $data['user_id'] );
  122.         }
  123.  
  124.         // Apply any tags
  125.         if ( ! empty( $apply_tags ) ) {
  126.             wp_fusion()->user->apply_tags( $apply_tags, $data['user_id'] );
  127.         }
  128.  
  129.         add_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
  130.  
  131.     }
Add Comment
Please, Sign In to add comment