PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.4
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.4
6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 All 35 releases
← All changes | integrations/woocommerce/includes/triggers/membership-created.php +230 -0 5.8.2 → 6.0.4 View file →
@@ -1,0 +1,230 @@
1 +<?php
2 +/**
3 + * Membership Created
4 + *
5 + * @package AutomatorWP\Integrations\WooCommerce\Triggers\Membership_Created
6 + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]>
7 + * @since 1.0.0
8 + */
9 +// Exit if accessed directly
10 +if( !defined( 'ABSPATH' ) ) exit;
11 +
12 +class AutomatorWP_WooCommerce_Membership_Created extends AutomatorWP_Integration_Trigger {
13 +
14 + public $integration = 'woocommerce';
15 + public $trigger = 'woocommerce_membership_created';
16 +
17 + /**
18 + * Register the trigger
19 + *
20 + * @since 1.0.0
21 + */
22 + public function register() {
23 +
24 + automatorwp_register_trigger( $this->trigger, array(
25 + 'integration' => $this->integration,
26 + 'label' => __( 'User is added to a membership', 'automatorwp' ),
27 + 'select_option' => __( 'User is <strong>added</strong> to a <strong>membership</strong>', 'automatorwp' ),
28 + /* translators: %1$s: Post title. %2$s: Number of times. */
29 + 'edit_label' => sprintf( __( 'User is added to %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ),
30 + /* translators: %1$s: Post title. */
31 + 'log_label' => sprintf( __( 'User is added to %1$s', 'automatorwp' ), '{post}' ),
32 + 'action' => array(
33 + 'wc_memberships_user_membership_created',
34 + 'wc_memberships_user_membership_saved'
35 + ),
36 + 'function' => array( $this, 'listener' ),
37 + 'priority' => 999,
38 + 'accepted_args' => 2,
39 + 'options' => array(
40 + 'post' => automatorwp_utilities_post_option( array(
41 + 'name' => __( 'Membership:', 'automatorwp' ),
42 + 'option_none_label' => __( 'any membership', 'automatorwp' ),
43 + 'post_type' => 'wc_membership_plan'
44 + ) ),
45 + 'times' => automatorwp_utilities_times_option(),
46 + ),
47 + 'tags' => array_merge(
48 + automatorwp_utilities_post_tags( __( 'Membership', 'automatorwp' ) ),
49 + automatorwp_woocommerce_order_tags(),
50 + automatorwp_utilities_times_tag()
51 + )
52 + ) );
53 +
54 + }
55 +
56 + /**
57 + * Trigger listener
58 + *
59 + * @since 1.0.0
60 + *
61 + * @param \WC_Memberships_Membership_Plan $membership_plan
62 + * @param array $args
63 + */
64 + public function listener( $membership_plan, $args ) {
65 +
66 + // Bail if the callback is for a membership updated hook
67 + if ( isset( $args['is_update'] ) && $args['is_update'] === true ) {
68 + return;
69 + }
70 +
71 + $user_id = absint( $args['user_id'] );
72 + $user_membership_id = absint( $args['user_membership_id'] );
73 +
74 + // Bail if not user provided
75 + if( $user_id === 0 ) {
76 + return;
77 + }
78 +
79 + // Bail if required function does not exists
80 + if( ! function_exists( 'wc_memberships_get_user_membership' ) ) {
81 + return;
82 + }
83 +
84 + $user_membership = wc_memberships_get_user_membership( $user_membership_id );
85 +
86 + // Bail if user membership is not active
87 + if( ! $user_membership->is_active() ) {
88 + return;
89 + }
90 +
91 + // Try to recover the membership plan if not found
92 + if( ! $membership_plan instanceof WC_Memberships_Membership_Plan ) {
93 + $membership_plan = $user_membership->get_plan();
94 + }
95 +
96 + // Get the order ID
97 + $order_id = 0;
98 + $access_method = get_post_meta( $membership_plan->id, '_access_method', true );
99 +
100 + if ( $access_method === 'purchase' ) {
101 + $order_id = get_post_meta( $user_membership_id, '_order_id', true );
102 + }
103 +
104 + automatorwp_trigger_event( array(
105 + 'trigger' => $this->trigger,
106 + 'user_id' => $user_id,
107 + 'post_id' => $membership_plan->id,
108 + 'order_id' => $order_id,
109 + ) );
110 +
111 + }
112 +
113 + /**
114 + * Admin trigger listener for manual assignations
115 + *
116 + * @since 1.0.0
117 + *
118 + * @param string $new_status
119 + * @param string $old_status
120 + * @param WP_Post $post
121 + */
122 + public function admin_listener( $new_status, $old_status, $post ) {
123 +
124 + // Bail if not is a WC Membership
125 + if( $post->post_type !== 'wc_user_membership' ) {
126 + return;
127 + }
128 +
129 + if( $old_status === 'auto-draft' ) {
130 + return;
131 + }
132 +
133 + $user_membership = wc_memberships_get_user_membership( $post->ID );
134 +
135 + if( ! $user_membership ) {
136 + return;
137 + }
138 +
139 + $order_id = 0;
140 + $access_method = get_post_meta( $post->ID, '_access_method', true );
141 +
142 + if ( $access_method === 'purchase' ) {
143 + $order_id = get_post_meta( $post->ID, '_order_id', true );
144 + }
145 +
146 + automatorwp_trigger_event( array(
147 + 'trigger' => $this->trigger,
148 + 'user_id' => $user_membership->get_user_id(),
149 + 'post_id' => $user_membership->get_plan_id(),
150 + 'order_id' => $order_id,
151 + ) );
152 +
153 + }
154 +
155 + /**
156 + * User deserves check
157 + *
158 + * @since 1.0.0
159 + *
160 + * @param bool $deserves_trigger True if user deserves trigger, false otherwise
161 + * @param stdClass $trigger The trigger object
162 + * @param int $user_id The user ID
163 + * @param array $event Event information
164 + * @param array $trigger_options The trigger's stored options
165 + * @param stdClass $automation The trigger's automation object
166 + *
167 + * @return bool True if user deserves trigger, false otherwise
168 + */
169 + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
170 +
171 + // Don't deserve if post is not received
172 + if( ! isset( $event['post_id'] ) ) {
173 + return false;
174 + }
175 +
176 + // Don't deserve if post doesn't match with the trigger option
177 + if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) {
178 + return false;
179 + }
180 +
181 + return $deserves_trigger;
182 +
183 + }
184 +
185 + /**
186 + * Register the required hooks
187 + *
188 + * @since 1.0.0
189 + */
190 + public function hooks() {
191 +
192 + // Log meta data
193 + add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
194 +
195 + // admin listener
196 + add_action( 'transition_post_status', array( $this, 'admin_listener' ), 999, 3 );
197 +
198 + parent::hooks();
199 + }
200 +
201 + /**
202 + * Trigger custom log meta
203 + *
204 + * @since 1.0.0
205 + *
206 + * @param array $log_meta Log meta data
207 + * @param stdClass $trigger The trigger object
208 + * @param int $user_id The user ID
209 + * @param array $event Event information
210 + * @param array $trigger_options The trigger's stored options
211 + * @param stdClass $automation The trigger's automation object
212 + *
213 + * @return array
214 + */
215 + function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
216 +
217 + // Bail if action type don't match this action
218 + if( $trigger->type !== $this->trigger ) {
219 + return $log_meta;
220 + }
221 +
222 + $log_meta['order_id'] = ( isset( $event['order_id'] ) ? $event['order_id'] : 0 );
223 +
224 + return $log_meta;
225 +
226 + }
227 +
228 +}
229 +
230 +new AutomatorWP_WooCommerce_Membership_Created();