coupon-code-redeemed.php
1 month ago
membership-cancelled.php
2 years ago
membership-paused.php
2 years ago
membership-signup-completed.php
2 months ago
purchase-membership.php
1 year ago
membership-signup-completed.php
218 lines
| 1 | <?php |
| 2 | /** |
| 3 | * MembershipCreated. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category MembershipCreated |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\MemberPress\Triggers; |
| 15 | |
| 16 | use MeprTransaction; |
| 17 | use SureTriggers\Controllers\AutomationController; |
| 18 | use SureTriggers\Integrations\MemberPress\MemberPress; |
| 19 | use SureTriggers\Integrations\WordPress\WordPress; |
| 20 | use SureTriggers\Traits\SingletonLoader; |
| 21 | |
| 22 | if ( ! class_exists( 'MembershipCreated' ) ) : |
| 23 | |
| 24 | /** |
| 25 | * MembershipCreated |
| 26 | * |
| 27 | * @category MembershipCreated |
| 28 | * @package SureTriggers |
| 29 | * @author BSF <username@example.com> |
| 30 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 31 | * @link https://www.brainstormforce.com/ |
| 32 | * @since 1.0.0 |
| 33 | */ |
| 34 | class MembershipCreated { |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Integration type. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | public $integration = 'MemberPress'; |
| 43 | |
| 44 | /** |
| 45 | * Trigger name. |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
| 49 | public $trigger = 'mepr-event-member-signup-completed'; |
| 50 | |
| 51 | use SingletonLoader; |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Constructor |
| 56 | * |
| 57 | * @since 1.0.0 |
| 58 | */ |
| 59 | public function __construct() { |
| 60 | add_filter( 'sure_trigger_register_trigger', [ $this, 'register' ] ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Register action. |
| 65 | * |
| 66 | * @param array $triggers trigger data. |
| 67 | * @return array |
| 68 | */ |
| 69 | public function register( $triggers ) { |
| 70 | |
| 71 | $triggers[ $this->integration ][ $this->trigger ] = [ |
| 72 | 'label' => __( 'Membership Created', 'suretriggers' ), |
| 73 | 'action' => $this->trigger, |
| 74 | 'function' => [ $this, 'trigger_listener' ], |
| 75 | 'priority' => 10, |
| 76 | 'accepted_args' => 1, |
| 77 | ]; |
| 78 | |
| 79 | return $triggers; |
| 80 | |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Trigger listener |
| 86 | * This will trigger only for initial member signup completion, not recurring payments. |
| 87 | * |
| 88 | * @param object $event Event data. |
| 89 | * |
| 90 | * @return void |
| 91 | */ |
| 92 | public function trigger_listener( $event ) { |
| 93 | if ( ! class_exists( 'MeprEvent' ) || ! $event instanceof \MeprEvent ) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | $user_id = $event->evt_id; |
| 98 | if ( empty( $user_id ) ) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | $user = get_user_by( 'ID', $user_id ); |
| 103 | if ( ! $user ) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | if ( ! class_exists( 'MeprTransaction' ) ) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | $membership_id = 0; |
| 112 | $membership_context = null; |
| 113 | $transaction_id = 0; |
| 114 | |
| 115 | // MemberPress records the triggering transaction as JSON in $event->args. |
| 116 | // For offline/free/lifetime one-time payments the txn has status='complete'. |
| 117 | // For Stripe subscriptions, record_create_sub() fires this event BEFORE |
| 118 | // record_sub_payment() runs, so the event's txn is a subscription_confirmation |
| 119 | // (status='confirmed'). In that case we pull financial data from the subscription. |
| 120 | $txn_args = ! empty( $event->args ) ? json_decode( $event->args ) : null; |
| 121 | $txn_id = ( $txn_args instanceof \stdClass && isset( $txn_args->id ) && is_numeric( $txn_args->id ) ) |
| 122 | ? absint( $txn_args->id ) |
| 123 | : 0; |
| 124 | |
| 125 | if ( $txn_id ) { |
| 126 | $txn = new \MeprTransaction( $txn_id ); |
| 127 | |
| 128 | if ( $txn->id ) { |
| 129 | if ( \MeprTransaction::$complete_str === $txn->status ) { |
| 130 | // One-time / offline / free payment: transaction is already complete. |
| 131 | $membership_id = (int) $txn->product_id; |
| 132 | $membership_context = MemberPress::get_membership_context( $txn ); |
| 133 | $transaction_id = (int) $txn->id; |
| 134 | } elseif ( |
| 135 | \MeprTransaction::$confirmed_str === $txn->status |
| 136 | && ! empty( $txn->subscription_id ) |
| 137 | && class_exists( 'MeprSubscription' ) |
| 138 | ) { |
| 139 | // Stripe subscription: the confirmation txn exists but the payment |
| 140 | // txn is created later. Pull financial data from the subscription. |
| 141 | $sub = new \MeprSubscription( (int) $txn->subscription_id ); |
| 142 | if ( $sub instanceof \MeprSubscription && $sub->id ) { |
| 143 | $membership_id = (int) $txn->product_id; |
| 144 | $sub_ctx = MemberPress::get_subscription_context( $sub ); |
| 145 | $sub_ctx['trans_num'] = $txn->trans_num; |
| 146 | $sub_ctx['transaction_id'] = $txn->id; |
| 147 | $membership_context = $sub_ctx; |
| 148 | $transaction_id = (int) $txn->id; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Fallback for any other payment flow: scan all user transactions. |
| 155 | if ( null === $membership_context ) { |
| 156 | $all_txns = \MeprTransaction::get_all_by_user_id( $user_id ); |
| 157 | foreach ( (array) $all_txns as $raw_txn ) { |
| 158 | if ( \MeprTransaction::$complete_str === $raw_txn->status ) { |
| 159 | $txn_obj = new \MeprTransaction( (int) $raw_txn->id ); |
| 160 | $membership_id = (int) $txn_obj->product_id; |
| 161 | $membership_context = MemberPress::get_membership_context( $txn_obj ); |
| 162 | $transaction_id = (int) $txn_obj->id; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if ( null === $membership_context || empty( $membership_id ) ) { |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if ( ! get_post( $membership_id ) ) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | // Collect MemberPress custom/account field values for this user. |
| 177 | $custom_fields_context = []; |
| 178 | if ( class_exists( 'MeprUser' ) ) { |
| 179 | $mepr_user = new \MeprUser( $user_id ); |
| 180 | if ( method_exists( $mepr_user, 'custom_profile_values' ) ) { |
| 181 | $custom_field_values = $mepr_user->custom_profile_values( true ); |
| 182 | if ( is_array( $custom_field_values ) ) { |
| 183 | foreach ( $custom_field_values as $field_key => $field_value ) { |
| 184 | $custom_fields_context[ sanitize_key( $field_key ) ] = $field_value; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | $membership_context['transaction_id'] = $transaction_id; |
| 191 | |
| 192 | $context = array_merge( |
| 193 | WordPress::get_user_context( $user_id ), |
| 194 | $membership_context, |
| 195 | $custom_fields_context, |
| 196 | [ 'signup_date' => $event->created_at ] |
| 197 | ); |
| 198 | $context['membership_id'] = $membership_id; |
| 199 | |
| 200 | AutomationController::sure_trigger_handle_trigger( |
| 201 | [ |
| 202 | 'trigger' => $this->trigger, |
| 203 | 'context' => $context, |
| 204 | ] |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Ignore false positive |
| 212 | * |
| 213 | * @psalm-suppress UndefinedMethod |
| 214 | */ |
| 215 | MembershipCreated::get_instance(); |
| 216 | |
| 217 | endif; |
| 218 |