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 / suremembers / includes / triggers / user-added-to-access-group.php

user-added-to-access-group.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.4, at integrations/suremembers/includes/triggers/user-added-to-access-group.php

206 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 * Access granted to a group
4 *
5 * @package AutomatorWP\Integrations\SureMembers\Triggers\User_Added_To_Access_Group
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_SureMembers_User_Added_To_Access_Group extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'suremembers';
15 public $trigger = 'suremembers_user_added_to_access_group';
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 added to an access group', 'automatorwp' ),
27 'select_option' => __( 'User added to <strong>an acccess group</strong>', 'automatorwp' ),
28 /* translators: %1$s: Group title. %2$s: Number of times. */
29 'edit_label' => sprintf( __( 'User added to %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ),
30 /* translators: %1$s: Group title. */
31 'log_label' => sprintf( __( 'User added to %1$s', 'automatorwp' ), '{post}' ),
32 'action' => 'suremembers_after_access_grant',
33 'function' => array( $this, 'listener' ),
34 'priority' => 10,
35 'accepted_args' => 2,
36 'options' => array(
37 'post' => automatorwp_utilities_post_option( array(
38 'name' => __( 'Group:', 'automatorwp' ),
39 'option_none_label' => __( 'any group', 'automatorwp' ),
40 'post_type' => 'wsm_access_group'
41 ) ),
42 'times' => automatorwp_utilities_times_option(),
43 ),
44 'tags' => array_merge(
45 automatorwp_utilities_post_tags( __( 'Group', 'automatorwp' ) ),
46 automatorwp_utilities_times_tag()
47 )
48 ) );
49
50 }
51
52 /**
53 * Trigger listener
54 *
55 * @since 1.0.0
56 *
57 * @param int $user_id
58 * @param array $access_group_ids
59 */
60 public function listener( $user_id, $access_group_ids ) {
61
62 // Login is required
63 if ( $user_id === 0 ) {
64 return;
65 }
66
67 foreach ( $access_group_ids as $key => $group_id ) {
68
69 // Used for hook
70 $group = array( $key => $group_id );
71
72 /**
73 * Excluded groups event by filter
74 *
75 * @since 1.0.0
76 *
77 * @param bool $exclude Whatever to exclude or not, by default false
78 * @param string $key Key
79 * @param mixed $group_id Group ID
80 * @param array $group Group setup array
81 */
82 if( apply_filters( 'automatorwp_suremembers_exclude_group', false, $key, $group_id, $group ) ) {
83 continue;
84 }
85
86 // Trigger submit form event
87 automatorwp_trigger_event( array(
88 'trigger' => $this->trigger,
89 'user_id' => $user_id,
90 'groups_id' => $access_group_ids,
91 'post_id' => $group_id
92 ) );
93 }
94
95
96 }
97
98 /**
99 * User deserves check
100 *
101 * @since 1.0.0
102 *
103 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
104 * @param stdClass $trigger The trigger object
105 * @param int $user_id The user ID
106 * @param array $event Event information
107 * @param array $trigger_options The trigger's stored options
108 * @param stdClass $automation The trigger's automation object
109 *
110 * @return bool True if user deserves trigger, false otherwise
111 */
112 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
113
114 // Don't deserve if group is not received
115 if( ! isset( $event['post_id'] ) ) {
116 return false;
117 }
118
119 // Bail if group doesn't match with the trigger option
120 if( $trigger_options['post'] !== 'any' && absint( $event['post_id'] ) !== absint( $trigger_options['post'] ) ) {
121 return false;
122 }
123
124 return $deserves_trigger;
125
126 }
127
128 /**
129 * Register the required hooks
130 *
131 * @since 1.0.0
132 */
133 public function hooks() {
134
135 // Log meta data
136 add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
137
138 // Log fields
139 add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
140
141 parent::hooks();
142 }
143
144 /**
145 * Trigger custom log meta
146 *
147 * @since 1.0.0
148 *
149 * @param array $log_meta Log meta data
150 * @param stdClass $trigger The trigger object
151 * @param int $user_id The user ID
152 * @param array $event Event information
153 * @param array $trigger_options The trigger's stored options
154 * @param stdClass $automation The trigger's automation object
155 *
156 * @return array
157 */
158 function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
159
160 // Bail if action type don't match this action
161 if( $trigger->type !== $this->trigger ) {
162 return $log_meta;
163 }
164
165 $log_meta['group_title'] = ( isset( $event['post_id'] ) ? $event['post_id'] : array() );
166
167 return $log_meta;
168
169 }
170
171 /**
172 * Action custom log fields
173 *
174 * @since 1.0.0
175 *
176 * @param array $log_fields The log fields
177 * @param stdClass $log The log object
178 * @param stdClass $object The trigger/action/automation object attached to the log
179 *
180 * @return array
181 */
182 public function log_fields( $log_fields, $log, $object ) {
183
184 // Bail if log is not assigned to an trigger
185 if( $log->type !== 'trigger' ) {
186 return $log_fields;
187 }
188
189 // Bail if trigger type don't match this trigger
190 if( $object->type !== $this->trigger ) {
191 return $log_fields;
192 }
193
194 $log_fields['group_title'] = array(
195 'name' => __( 'Group Title', 'automatorwp' ),
196 'desc' => __( 'Information about the group Title', 'automatorwp' ),
197 'type' => 'text',
198 );
199
200 return $log_fields;
201
202 }
203
204 }
205
206 new AutomatorWP_SureMembers_User_Added_To_Access_Group();