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/meta-box/includes/triggers/post-field-updated.php +244 -0 5.7.7 → 6.0.4 View file →
@@ -1,0 +1,244 @@
1 +<?php
2 +/**
3 + * Post Field Updated
4 + *
5 + * @package AutomatorWP\Integrations\Meta_Box\Triggers\Post_Field_Updated
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_Meta_Box_Post_Field_Updated extends AutomatorWP_Integration_Trigger {
13 +
14 + public $integration = 'meta_box';
15 + public $trigger = 'meta_box_post_field_updated';
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 updates field on a post with a value', 'automatorwp' ),
27 + 'select_option' => __( 'User updates <strong>field</strong> on a post with a value', 'automatorwp' ),
28 + /* translators: %1$s: Field name. %2$s: Post title. %3$s: Field value. %4$s: Number of times. */
29 + 'edit_label' => sprintf( __( 'User updates field %1$s on %2$s with %3$s %4$s time(s)', 'automatorwp' ), '{field_name}', '{post}', '{field_value}', '{times}' ),
30 + /* translators: %1$s: Field name. %2$s: Post title. %3$s: Field value. */
31 + 'log_label' => sprintf( __( 'User updates field %1$s on %2$s with %3$s', 'automatorwp' ), '{field_name}', '{post}', '{field_value}' ),
32 + 'action' => array(
33 + 'added_post_meta',
34 + 'updated_post_meta'
35 + ),
36 + 'function' => array( $this, 'listener' ),
37 + 'priority' => 10,
38 + 'accepted_args' => 4,
39 + 'options' => array(
40 + 'field_name' => automatorwp_utilities_ajax_selector_option( array(
41 + 'field' => 'field_name',
42 + 'name' => __( 'Field:', 'automatorwp' ),
43 + 'option_none_value' => 'any',
44 + 'option_none_label' => __( 'any field', 'automatorwp' ),
45 + 'action_cb' => 'automatorwp_meta_box_get_post_fields',
46 + 'options_cb' => 'automatorwp_meta_box_options_cb_post_fields',
47 + 'default' => 'any'
48 + ) ),
49 + 'field_value' => array(
50 + 'from' => 'field_value',
51 + 'default' => __( 'any value', 'automatorwp' ),
52 + 'fields' => array(
53 + 'field_value' => array(
54 + 'name' => __( 'Value:', 'automatorwp' ),
55 + 'type' => 'text',
56 + 'default' => ''
57 + ),
58 + )
59 + ),
60 + 'post' => automatorwp_utilities_post_option( array(
61 + 'option_none_label' => __( 'any post', 'automatorwp' ),
62 + 'post_type' => 'any'
63 + ) ),
64 + 'times' => automatorwp_utilities_times_option(),
65 + ),
66 + 'tags' => array_merge(
67 + automatorwp_meta_box_get_tags(),
68 + automatorwp_utilities_times_tag()
69 + )
70 + ) );
71 +
72 + }
73 +
74 +
75 + /**
76 + * Trigger listener
77 + *
78 + * @since 1.0.0
79 + *
80 + * @param int $meta_id ID of updated metadata entry
81 + * @param int $object_id ID of the object metadata is for
82 + * @param string $meta_key Metadata key
83 + * @param mixed $_meta_value Metadata value
84 + */
85 + public function listener( $meta_id, $object_id, $meta_key, $_meta_value ) {
86 +
87 + $user_id = get_current_user_id();
88 +
89 + // Bail if user is not logged
90 + if ($user_id === 0) {
91 + return;
92 + }
93 +
94 + // Trigger the tag removed
95 +
96 + automatorwp_trigger_event( array(
97 + 'trigger' => $this->trigger,
98 + 'user_id' => $user_id,
99 + 'post_id' => $object_id,
100 + 'meta_key' => $meta_key,
101 + 'meta_value' => $_meta_value,
102 + ) );
103 +
104 + }
105 +
106 + /**
107 + * User deserves check
108 + *
109 + * @since 1.0.0
110 + *
111 + * @param bool $deserves_trigger True if user deserves trigger, false otherwise
112 + * @param stdClass $trigger The trigger object
113 + * @param int $user_id The user ID
114 + * @param array $event Event information
115 + * @param array $trigger_options The trigger's stored options
116 + * @param stdClass $automation The trigger's automation object
117 + *
118 + * @return bool True if user deserves trigger, false otherwise
119 + */
120 + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
121 +
122 + // Don't deserve if post is not received
123 + if( ! isset( $event['post_id'] ) ) {
124 + return false;
125 + }
126 +
127 + // Don't deserve if meta key is not received
128 + if( ! isset( $event['meta_key'] ) ) {
129 + return false;
130 + }
131 +
132 + // Ensure modified meta is a meta box
133 + $fields_allowed = array_keys( rwmb_get_object_fields( $event['post_id'] ) );
134 +
135 + // Don't deserve if meta_key is not allowed
136 + if ( ! in_array( $event['meta_key'], $fields_allowed, true ) ) {
137 + return false;
138 + }
139 +
140 + // Don't deserve if post doesn't match with the trigger option
141 + if( $trigger_options['post'] !== 'any' && absint( $trigger_options['post'] ) !== absint( $event['post_id'] ) ) {
142 + return false;
143 + }
144 +
145 + // Don't deserve if meta_key doesn't match with the trigger option
146 + if( $trigger_options['field_name'] !== 'any' && $trigger_options['field_name'] !== $event['meta_key'] ) {
147 + return false;
148 + }
149 +
150 + // Don't deserve if value doesn't matches with the trigger option
151 + if( $trigger_options['field_value'] !== '' && $trigger_options['field_value'] !== $event['meta_value'] ) {
152 + return false;
153 + }
154 +
155 + return $deserves_trigger;
156 +
157 + }
158 +
159 + /**
160 + * Register the required hooks
161 + *
162 + * @since 1.0.0
163 + */
164 + public function hooks() {
165 +
166 + // Log meta data
167 + add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
168 +
169 + // Log fields
170 + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
171 +
172 + parent::hooks();
173 + }
174 +
175 + /**
176 + * Trigger custom log meta
177 + *
178 + * @since 1.0.0
179 + *
180 + * @param array $log_meta Log meta data
181 + * @param stdClass $trigger The trigger object
182 + * @param int $user_id The user ID
183 + * @param array $event Event information
184 + * @param array $trigger_options The trigger's stored options
185 + * @param stdClass $automation The trigger's automation object
186 + *
187 + * @return array
188 + */
189 + function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
190 +
191 + // Bail if action type don't match this action
192 + if( $trigger->type !== $this->trigger ) {
193 + return $log_meta;
194 + }
195 +
196 + $log_meta['updated_meta_key'] = ( isset( $event['meta_key'] ) ? $event['meta_key'] : '' );
197 + $log_meta['updated_meta_value'] = ( isset( $event['meta_value'] ) ? $event['meta_value'] : '' );
198 +
199 + return $log_meta;
200 +
201 + }
202 +
203 + /**
204 + * Action custom log fields
205 + *
206 + * @since 1.0.0
207 + *
208 + * @param array $log_fields The log fields
209 + * @param stdClass $log The log object
210 + * @param stdClass $object The trigger/action/automation object attached to the log
211 + *
212 + * @return array
213 + */
214 + public function log_fields( $log_fields, $log, $object ) {
215 +
216 + // Bail if log is not assigned to an trigger
217 + if( $log->type !== 'trigger' ) {
218 + return $log_fields;
219 + }
220 +
221 + // Bail if trigger type don't match this trigger
222 + if( $object->type !== $this->trigger ) {
223 + return $log_fields;
224 + }
225 +
226 + $log_fields['updated_meta_key'] = array(
227 + 'name' => __( 'Updated field', 'automatorwp' ),
228 + 'desc' => __( 'Key of the updated field.', 'automatorwp' ),
229 + 'type' => 'text',
230 + );
231 +
232 + $log_fields['updated_meta_value'] = array(
233 + 'name' => __( 'Updated value', 'automatorwp' ),
234 + 'desc' => __( 'Value of the updated field.', 'automatorwp' ),
235 + 'type' => 'text',
236 + );
237 +
238 + return $log_fields;
239 +
240 + }
241 +
242 +}
243 +
244 +new AutomatorWP_Meta_Box_Post_Field_Updated();