Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function subscription_status_changed( $old_status, $new_status, $subscription ) {
- // Don't run on pending subscriptions
- if ( 'pending' == $new_status ) {
- return;
- }
- // Don't require the checkout callback
- remove_action( 'mepr-event-transaction-completed', array( $this, 'apply_tags_checkout' ) );
- // Get subscription data
- $data = $subscription->get_values();
- 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>' );
- // Get WPF settings
- $settings = get_post_meta( $data['product_id'], 'wpf-settings-memberpress', true );
- if ( empty( $settings ) ) {
- $settings = array();
- }
- $defaults = array(
- 'apply_tags_registration' => array(),
- 'remove_tags' => false,
- 'tag_link' => array(),
- 'apply_tags_cancelled' => array(),
- 'apply_tags_expired' => array(),
- 'apply_tags_payment_failed' => array(),
- 'apply_tags_corporate_accounts' => array(),
- 'apply_tags_trial' => array(),
- 'apply_tags_converted' => array(),
- );
- $settings = wp_parse_args( $settings, $defaults );
- $apply_tags = array();
- $remove_tags = array();
- // New subscriptions
- if ( 'active' == $new_status ) {
- // Apply tags
- $apply_tags = array_merge( $apply_tags, $settings['apply_tags_registration'], $settings['tag_link'] );
- // Remove cancelled / expired tags
- $remove_tags = array_merge( $remove_tags, $settings['apply_tags_cancelled'], $settings['apply_tags_expired'] );
- // Update data
- $payment_method = $subscription->payment_method();
- $update_data = array(
- 'mepr_reg_date' => $data['created_at'],
- 'mepr_payment_method' => $payment_method->name,
- 'mepr_membership_level' => get_the_title( $data['product_id'] ),
- );
- // Add expiration only if applicable
- if ( strtotime( $subscription->get_expires_at() ) >= 0 ) {
- $update_data['mepr_expiration'] = date( 'Y-m-d H:i:s', $subscription->get_expires_at() );
- }
- // Sync trial duration
- if ( $subscription->trial ) {
- $update_data['mepr_trial_duration'] = $subscription->trial_days;
- }
- // Coupon used
- if ( ! empty( $subscription->coupon_id ) ) {
- $update_data['mepr_coupon'] = get_the_title( $subscription->coupon_id );
- $coupon_settings = get_post_meta( $subscription->coupon_id, 'wpf-settings', true );
- if ( ! empty( $coupon_settings ) && ! empty( $coupon_settings['apply_tags_coupon'] ) ) {
- $apply_tags = array_merge( $apply_tags, $coupon_settings['apply_tags_coupon'] );
- }
- }
- // Get all meta as well to get any updated custom field values during upgrades
- $user_meta = array_map( array( wp_fusion()->user, 'map_user_meta' ), get_user_meta( $data['user_id'] ) );
- $update_data = array_merge( $user_meta, $update_data );
- wp_fusion()->user->push_user_meta( $data['user_id'], $update_data );
- }
- // Other status changes
- if ( $subscription->is_expired() && ! in_array( $new_status, array( 'active', 'pending' ) ) ) {
- // Expired subscription
- $remove_tags = array_merge( $remove_tags, $settings['tag_link'] );
- $apply_tags = array_merge( $apply_tags, $settings['apply_tags_expired'] );
- if ( ! empty( $settings['remove_tags'] ) ) {
- $remove_tags = array_merge( $remove_tags, $settings['apply_tags_registration'] );
- }
- } elseif ( 'cancelled' == $new_status ) {
- // Cancelled subscription
- $remove_tags = array_merge( $remove_tags, $settings['tag_link'] );
- $apply_tags = array_merge( $apply_tags, $settings['apply_tags_cancelled'] );
- if ( ! empty( $settings['remove_tags'] ) ) {
- $remove_tags = array_merge( $remove_tags, $settings['apply_tags_registration'] );
- }
- } elseif ( $subscription->in_trial() ) {
- // If is in a trial and isn't cancelled / expired
- $apply_tags = array_merge( $apply_tags, $settings['apply_tags_trial'] );
- }
- // Prevent looping when tags are modified
- remove_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
- // Remove any tags
- if ( ! empty( $remove_tags ) ) {
- wp_fusion()->user->remove_tags( $remove_tags, $data['user_id'] );
- }
- // Apply any tags
- if ( ! empty( $apply_tags ) ) {
- wp_fusion()->user->apply_tags( $apply_tags, $data['user_id'] );
- }
- add_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
- }
Add Comment
Please, Sign In to add comment