PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
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 5.6.1 All 34 releases
← All changes | integrations/automatorwp/triggers/all-users.php +362 -0 5.7.6 → 6.0.3 View file →
@@ -1,0 +1,362 @@
1 +<?php
2 +/**
3 + * All Users
4 + *
5 + * @package AutomatorWP\Integrations\AutomatorWP\Triggers\All_Users
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_AutomatorWP_All_Users extends AutomatorWP_Integration_Trigger {
13 +
14 + public $integration = 'automatorwp';
15 + public $trigger = 'automatorwp_all_users';
16 +
17 + /**
18 + * The field conditions
19 + *
20 + * @since 1.0.0
21 + *
22 + * @var array $field_conditions
23 + */
24 + public $field_conditions = array();
25 +
26 + /**
27 + * The meta conditions
28 + *
29 + * @since 1.0.0
30 + *
31 + * @var array $meta_conditions
32 + */
33 + public $meta_conditions = array();
34 +
35 + /**
36 + * Register the trigger
37 + *
38 + * @since 1.0.0
39 + */
40 + public function register() {
41 +
42 + automatorwp_register_trigger( $this->trigger, array(
43 + 'integration' => $this->integration,
44 + 'label' => __( 'Run automation on all users', 'automatorwp' ),
45 + 'select_option' => '',
46 + 'show_in_selector' => false,
47 + /* translators: %1$s: Automation title. %2$s: Number of times. */
48 + 'edit_label' => sprintf( __( 'Run automation on %1$s', 'automatorwp' ), '{all_users_conditions}' ),
49 + /* translators: %1$s: Automation title. */
50 + 'log_label' => sprintf( __( 'Run automation on %1$s', 'automatorwp' ), '{all_users_conditions}' ),
51 + 'options' => array(
52 + 'all_users_conditions' => array(
53 + 'default' => __( 'all users', 'automatorwp' ),
54 + 'fields' => array(
55 + 'field_conditions' => array(
56 + 'name' => __( 'Field Conditions:', 'automatorwp' ),
57 + 'desc' => __( 'Set conditions to filter users by their fields.', 'automatorwp' ),
58 + 'type' => 'group',
59 + 'classes' => 'automatorwp-fields-table',
60 + 'options' => array(
61 + 'add_button' => __( 'Add condition', 'automatorwp' ),
62 + 'remove_button' => '<span class="dashicons dashicons-no-alt"></span>',
63 + ),
64 + 'fields' => array(
65 + 'operator' => automatorwp_utilities_operator_field(),
66 + 'field' => array(
67 + 'name' => __( 'Field:', 'automatorwp' ),
68 + 'type' => 'select',
69 + 'options_cb' => 'automatorwp_options_cb_user_fields',
70 + 'option_none' => true,
71 + 'option_none_value' => '',
72 + 'option_none_label' => __( 'Choose a field', 'automatorwp' ),
73 + 'default' => ''
74 + ),
75 + 'condition' => automatorwp_utilities_condition_field(),
76 + 'value' => array(
77 + 'name' => __( 'Value:', 'automatorwp' ),
78 + 'type' => 'text',
79 + 'default' => ''
80 + ),
81 + ),
82 + ),
83 + 'meta_conditions' => array(
84 + 'name' => __( 'Meta Conditions:', 'automatorwp' ),
85 + 'desc' => __( 'Set conditions to filter users by their metas.', 'automatorwp' ),
86 + 'type' => 'group',
87 + 'classes' => 'automatorwp-fields-table',
88 + 'options' => array(
89 + 'add_button' => __( 'Add condition', 'automatorwp' ),
90 + 'remove_button' => '<span class="dashicons dashicons-no-alt"></span>',
91 + ),
92 + 'fields' => array(
93 + 'operator' => automatorwp_utilities_operator_field(),
94 + 'meta_key' => array(
95 + 'name' => __( 'Meta Key:', 'automatorwp' ),
96 + 'type' => 'text',
97 + 'default' => ''
98 + ),
99 + 'condition' => automatorwp_utilities_condition_field(),
100 + 'meta_value' => array(
101 + 'name' => __( 'Meta Value:', 'automatorwp' ),
102 + 'type' => 'text',
103 + 'default' => ''
104 + ),
105 + ),
106 + ),
107 + )
108 + ),
109 + ),
110 + 'tags' => array_merge(
111 + automatorwp_utilities_times_tag()
112 + )
113 + ) );
114 +
115 + }
116 +
117 + /**
118 + * Register the required hooks
119 + *
120 + * @since 1.0.0
121 + */
122 + public function hooks() {
123 +
124 + parent::hooks();
125 +
126 + add_filter( 'automatorwp_get_automation_item_option_replacement', array( $this, 'dynamic_item_option_replacement' ), 10, 5 );
127 + add_filter( 'automatorwp_get_all-users_automation_sql', array( $this, 'get_sql' ), 10, 7 );
128 +
129 + // Override edit item HTML
130 + add_filter( 'automatorwp_automation_item_edit_html_integration', array( $this, 'override_item_edit_html_integration' ), 10, 4 );
131 + add_filter( 'automatorwp_automations_column_triggers_integration', array( $this, 'override_item_edit_html_integration' ), 10, 4 );
132 + add_filter( 'automatorwp_get_log_integration_icon_integration', array( $this, 'override_item_edit_html_integration' ), 10, 4 );
133 + add_filter( 'automatorwp_automation_item_edit_html_classes', array( $this, 'override_item_edit_html_classes' ), 10, 4 );
134 + add_filter( 'automatorwp_automation_item_edit_html_actions', array( $this, 'override_item_edit_html_actions' ), 10, 4 );
135 +
136 + // Times tag, register as "no user trigger"
137 + add_filter( 'automatorwp_get_trigger_tag_replacement_times_no_user_triggers', array( $this, 'add_type_to_array' ) );
138 + add_filter( 'automatorwp_get_trigger_last_completion_log_no_user_types', array( $this, 'add_type_to_array' ) );
139 + add_filter( 'automatorwp_get_action_last_completion_log_no_user_types', array( $this, 'add_type_to_array' ) );
140 +
141 + }
142 +
143 + /**
144 + * Filters the option value for replacement on labels
145 + *
146 + * @since 1.0.0
147 + *
148 + * @param string $value The option value
149 + * @param stdClass $object The trigger/action object
150 + * @param string $item_type The item type (trigger|action)
151 + * @param string $option The option name
152 + * @param string $context The context this function is executed
153 + *
154 + * @return string
155 + */
156 + public function dynamic_item_option_replacement( $value, $object, $item_type, $option, $context ) {
157 +
158 + // Bail if not is our option
159 + if( $option !== 'all_users_conditions' ) {
160 + return $value;
161 + }
162 +
163 + ct_setup_table( "automatorwp_{$item_type}s" );
164 +
165 + // Setup vars
166 + $field_conditions = ct_get_object_meta( $object->id, 'field_conditions', true );
167 + $meta_conditions = ct_get_object_meta( $object->id, 'meta_conditions', true );
168 +
169 + $someting_in_fields = false;
170 + $someting_in_metas = false;
171 +
172 + // Check if there is any field condition
173 + if( is_array( $field_conditions ) ) {
174 + foreach( $field_conditions as $condition ) {
175 + if( ! empty( $condition['field'] ) && ! empty( $condition['condition'] ) && ! empty( $condition['value'] ) ) {
176 + $someting_in_fields = true;
177 + break;
178 + }
179 + }
180 + }
181 +
182 + // Check if there is any meta condition
183 + if( is_array( $meta_conditions ) ) {
184 + foreach( $meta_conditions as $condition ) {
185 + if( ! empty( $condition['meta_key'] ) && ! empty( $condition['condition'] ) && ! empty( $condition['meta_value'] ) ) {
186 + $someting_in_fields = true;
187 + break;
188 + }
189 + }
190 + }
191 +
192 + if( $someting_in_fields || $someting_in_metas ) {
193 + $value = __( 'some users', 'automatorwp' );
194 + }
195 +
196 + ct_reset_setup_table();
197 +
198 + return $value;
199 +
200 + }
201 +
202 + /**
203 + * Available filter to override the all users automation SQL
204 + *
205 + * @since 1.0.0
206 + *
207 + * @param string $sql The SQL query
208 + * @param stdClass $automation The automation object
209 + * @param stdClass $trigger The trigger object
210 + * @param bool $count True if is looking for the SQL to count the number of users
211 + * @param array $trigger_options The trigger's stored options
212 + * @param int $items_per_loop The automation items per loop option
213 + * @param int $loop The current loop
214 + */
215 + public function get_sql( $sql, $automation, $trigger, $count, $trigger_options, $items_per_loop, $loop ) {
216 +
217 + global $wpdb;
218 +
219 + // Setup vars
220 + $field_conditions = isset( $trigger_options['field_conditions'] ) ? $trigger_options['field_conditions'] : array();
221 + $meta_conditions = isset( $trigger_options['meta_conditions'] ) ? $trigger_options['meta_conditions'] : array();
222 + $allowed_fields = automatorwp_utilities_get_user_fields();
223 +
224 + $joins = array();
225 + $where = array();
226 +
227 + // Set up the user field conditions
228 + if( is_array( $field_conditions ) ) {
229 +
230 + $where = array( "( 1=1" );
231 +
232 + foreach( $field_conditions as $condition ) {
233 +
234 +
235 + if( ! isset( $condition['field'] ) ) {
236 + continue;
237 + }
238 +
239 + // Skip not allowed fields
240 + if( ! isset( $allowed_fields[$condition['field']] ) ) {
241 + continue;
242 + }
243 +
244 + // Initialize operator for backward compatibility
245 + if( ! isset( $condition['operator'] ) ) {
246 + $condition['operator'] = "AND";
247 + }
248 +
249 + // Ensure that operator is AND or OR, other values are not allowed
250 + if( ! in_array( $condition['operator'], array( "AND", "OR" ) ) ) {
251 + $condition['operator'] = "AND";
252 + }
253 +
254 + // Replace all tags by their replacements
255 + $condition = automatorwp_parse_automation_tags( $automation->id, 0, $condition );
256 +
257 + // Sanitize
258 + $field = sanitize_text_field( $condition['field'] );
259 + $value = sanitize_text_field( $condition['value'] );
260 +
261 + if( ! empty( $field ) ) {
262 + $where[] = $condition['operator'] . " (" . automatorwp_utilities_parse_condition_to_sql( 'u.' . $field, $condition['condition'], $value ) . ")";
263 + }
264 + }
265 + $where[] = ')';
266 + }
267 +
268 + // Set up the user meta conditions
269 + if( is_array( $meta_conditions ) ) {
270 + foreach( $meta_conditions as $condition ) {
271 +
272 + if( ! isset( $condition['meta_key'] ) ) {
273 + continue;
274 + }
275 +
276 + // Initialize operator for backward compatibility
277 + if( ! isset( $condition['operator'] ) ) {
278 + $condition['operator'] = "AND";
279 + }
280 +
281 + // Ensure that operator is AND or OR, other values are not allowed
282 + if( ! in_array( $condition['operator'], array( "AND", "OR" ) ) ) {
283 + $condition['operator'] = "AND";
284 + }
285 +
286 + // Replace all tags by their replacements
287 + $condition = automatorwp_parse_automation_tags( $automation->id, 0, $condition );
288 +
289 + // Sanitize
290 + $meta_key = sanitize_key( $condition['meta_key'] );
291 + $meta_key = esc_sql( $meta_key );
292 + $meta_value = sanitize_text_field( $condition['meta_value'] );
293 +
294 + if( ! empty( $meta_key ) ) {
295 + $index = count( $joins );
296 +
297 + $joins[] = "INNER JOIN {$wpdb->usermeta} AS um{$index} ON ( um{$index}.user_id = u.ID AND um{$index}.meta_key = '{$meta_key}' )";
298 +
299 + $where[] = $condition['operator'] . " (" .automatorwp_utilities_parse_condition_to_sql( "um{$index}.meta_value", $condition['condition'], $meta_value, false ) . ")";
300 + }
301 + }
302 + }
303 +
304 + // Turn arrays into strings
305 + $joins = implode( ' ', $joins );
306 + $where = ( ! empty( $where ) ? 'WHERE ' . implode( ' ', $where ) : '' );
307 +
308 + if( $count ) {
309 + // The count SQL query
310 + return "SELECT COUNT(*) FROM {$wpdb->users} AS u {$joins} {$where}";
311 + } else {
312 + $offset = $loop * $items_per_loop;
313 +
314 + // The normal SQL query
315 + return "SELECT u.ID FROM {$wpdb->users} AS u {$joins} {$where} LIMIT $offset, {$items_per_loop}";
316 + }
317 +
318 + }
319 +
320 + public function override_item_edit_html_integration( $integration, $object, $item_type, $automation ) {
321 +
322 + if( $item_type === 'trigger' && $object->type === $this->trigger ) {
323 + $integration['icon'] = AUTOMATORWP_URL . 'assets/img/automatorwp-all-users.svg';
324 + }
325 +
326 + return $integration;
327 +
328 + }
329 +
330 + public function override_item_edit_html_classes( $classes, $object, $item_type, $automation ) {
331 +
332 + if( $automation->type === 'all-users' && $object->type === $this->trigger ) {
333 + $classes[] = 'automatorwp-no-grab';
334 + }
335 +
336 + return $classes;
337 +
338 + }
339 +
340 + public function override_item_edit_html_actions( $actions, $object, $item_type, $automation ) {
341 +
342 + if( $automation->type === 'all-users' && $object->type === $this->trigger ) {
343 + unset( $actions['move-up'] );
344 + unset( $actions['move-down'] );
345 + unset( $actions['delete'] );
346 + }
347 +
348 + return $actions;
349 +
350 + }
351 +
352 + public function add_type_to_array( $no_user_triggers ) {
353 +
354 + $no_user_triggers[] = $this->trigger;
355 +
356 + return $no_user_triggers;
357 +
358 + }
359 +
360 +}
361 +
362 +new AutomatorWP_AutomatorWP_All_Users();