PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.36
OttoKit: All-in-One Automation Platform v1.1.36
1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / memberpress / triggers / membership-signup-completed.php
suretriggers / src / Integrations / memberpress / triggers Last commit date
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