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
automatorwp / integrations / paid-membership-subscriptions / includes / triggers / purchase-subscription.php

purchase-subscription.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.4, at integrations/paid-membership-subscriptions/includes/triggers/purchase-subscription.php

202 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Purchase Subscription
4 *
5 * @package AutomatorWP\Integrations\Paid_Membership_Subscriptions\Triggers\Purchase_Subscription
6 * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.0.0
8 */
9
10 // Exit if accessed directly
11 if (!defined('ABSPATH'))
12 exit;
13
14 class AutomatorWP_PMS_Purchase_Subscription extends AutomatorWP_Integration_Trigger
15 {
16
17 public $integration = 'paid_membership_subscriptions';
18 public $trigger = 'paid_membership_subscriptions_purchase_subscription';
19
20 /**
21 * Register the trigger
22 *
23 * @since 1.0.0
24 */
25 public function register()
26 {
27
28 automatorwp_register_trigger(
29 $this->trigger,
30 array(
31 'integration' => $this->integration,
32 'label' => __('User purchases a subscription of a membership level', 'automatorwp'),
33 'select_option' => __('User <strong>purchase</strong> a subscription plan', 'automatorwp'),
34 /* translators: %1$s: Content title. %2$s: Number of times. */
35 'edit_label' => sprintf(__('User purchase %1$s %2$s time(s)', 'automatorwp'), '{subscription_plan}', '{times}'),
36 /* translators: %1$s: Content title. */
37 'log_label' => sprintf(__('User purchase a subscription plan', 'automatorwp')),
38 'action' => 'pms_member_subscription_insert',
39 'function' => array($this, 'listener'),
40 'priority' => 10,
41 'accepted_args' => 2,
42 'options' => array(
43 'subscription_plan' => automatorwp_utilities_post_option(
44 array(
45 'name' => __('Subscription plan:', 'automatorwp'),
46 'option_none_label' => __('any subscription plan', 'automatorwp'),
47 'post_type' => 'pms-subscription'
48 )
49 ),
50 'times' => automatorwp_utilities_times_option(),
51 ),
52 'tags' => array_merge(
53 automatorwp_pms_get_subscription_tags(),
54 automatorwp_utilities_times_tag()
55 )
56 )
57 );
58
59 }
60
61 /**
62 * Trigger listener
63 *
64 * @since 1.0.0
65 *
66 *
67 */
68 public function listener($id, $data) {
69
70 $user_id = $data['user_id'];
71
72 // Login is required
73 if ($user_id === 0) {
74 return;
75 }
76
77 // Subscription tags
78 $subscription_plan_id = $data['subscription_plan_id'];
79 $subscription_plan_name = automatorwp_pms_get_subscription_name( $subscription_plan_id );
80
81 automatorwp_trigger_event(
82 array(
83 'trigger' => $this->trigger,
84 'user_id' => $user_id,
85 'subscription_plan_id' => $subscription_plan_id,
86 'subscription_plan_name' => $subscription_plan_name
87 )
88 );
89
90 }
91
92 /**
93 * User deserves check
94 *
95 * @since 1.0.0
96 *
97 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
98 * @param stdClass $trigger The trigger object
99 * @param int $user_id The user ID
100 * @param array $event Event information
101 * @param array $trigger_options The trigger's stored options
102 * @param stdClass $automation The trigger's automation object
103 *
104 * @return bool True if user deserves trigger, false otherwise
105 */
106 public function user_deserves_trigger($deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation) {
107
108
109 // Don't deserve if membership is not received
110 if (!isset ($event['subscription_plan_id'])) {
111 return false;
112 }
113
114 // Bail if post doesn't match with the trigger option
115 if( $trigger_options['post'] !== 'any' && absint( $event['subscription_plan_id'] ) !== absint( $trigger_options['post'] ) ) {
116 return false;
117 }
118
119 return $deserves_trigger;
120
121 }
122
123 /**
124 * Register the required hooks
125 *
126 * @since 1.0.0
127 */
128 public function hooks() {
129
130 // Log meta data
131 add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
132
133 // Log fields
134 add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
135
136 parent::hooks();
137 }
138
139 /**
140 * Trigger custom log meta
141 *
142 * @since 1.0.0
143 *
144 * @param array $log_meta Log meta data
145 * @param stdClass $trigger The trigger object
146 * @param int $user_id The user ID
147 * @param array $event Event information
148 * @param array $trigger_options The trigger's stored options
149 * @param stdClass $automation The trigger's automation object
150 *
151 * @return array
152 */
153 function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
154
155 // Bail if action type don't match this action
156 if( $trigger->type !== $this->trigger ) {
157 return $log_meta;
158 }
159
160 $log_meta['subscription_plan'] = ( isset( $event['subscription_plan_id'] ) ? $event['subscription_plan_id'] : array() );
161 $log_meta['subscription_plan_id'] = ( isset( $event['subscription_plan_id'] ) ? $event['subscription_plan_id'] : array() );
162 $log_meta['subscription_plan_name'] = ( isset( $event['subscription_plan_name'] ) ? $event['subscription_plan_name'] : array() );
163
164 return $log_meta;
165
166 }
167
168 /**
169 * Action custom log fields
170 *
171 * @since 1.0.0
172 *
173 * @param array $log_fields The log fields
174 * @param stdClass $log The log object
175 * @param stdClass $object The trigger/action/automation object attached to the log
176 *
177 * @return array
178 */
179 public function log_fields( $log_fields, $log, $object ) {
180
181 // Bail if log is not assigned to an trigger
182 if( $log->type !== 'trigger' ) {
183 return $log_fields;
184 }
185
186 // Bail if trigger type don't match this trigger
187 if( $object->type !== $this->trigger ) {
188 return $log_fields;
189 }
190
191 $log_fields['subscription_plan'] = array(
192 'name' => __( 'Subscription plan', 'automatorwp' ),
193 'desc' => __( 'Information about the subscription plan purchased.', 'automatorwp' ),
194 'type' => 'text',
195 );
196
197 return $log_fields;
198
199 }
200 }
201
202 new AutomatorWP_PMS_Purchase_Subscription();