PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 5.8.5
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v5.8.5
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
automatorwp / integrations / wordpress / triggers / post-meta-update.php

post-meta-update.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 5.8.5, at integrations/wordpress/triggers/post-meta-update.php

298 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post Meta Update
4 *
5 * @package AutomatorWP\Integrations\WordPress\Triggers\Post_Meta_Update
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_WordPress_Post_Meta_Update extends AutomatorWP_Integration_Trigger {
13
14 /**
15 * Initialize the trigger
16 *
17 * @since 1.0.0
18 */
19 public function __construct( $integration ) {
20
21 $this->integration = $integration;
22 $this->trigger = $integration . '_post_meta_update';
23
24 parent::__construct();
25
26 }
27
28 /**
29 * Register the trigger
30 *
31 * @since 1.0.0
32 */
33 public function register() {
34
35 automatorwp_register_trigger( $this->trigger, array(
36 'integration' => $this->integration,
37 'label' => __( 'Post meta gets updated with a value', 'automatorwp' ),
38 'select_option' => __( 'Post <strong>meta gets updated</strong> with a value', 'automatorwp' ),
39 /* translators: %1$s: Post title. %2$s: Key. %3$s: Condition. %4$s: Value. %5$s: Number of times. */
40 'edit_label' => sprintf( __( '%1$s meta %2$s gets updated with a value %3$s %4$s %5$s time(s)', 'automatorwp' ), '{post}', '{meta_key}', '{condition}', '{meta_value}', '{times}' ),
41 /* translators: %1$s: Post title. %2$s: Key. %3$s: Condition. %4$s: Value. */
42 'log_label' => sprintf( __( '%1$s meta %2$s gets updated with a value %3$s %4$s', 'automatorwp' ), '{post}', '{meta_key}', '{condition}', '{meta_value}' ),
43 'action' => 'updated_post_meta',
44 'function' => array( $this, 'listener' ),
45 'priority' => 10,
46 'accepted_args' => 4,
47 'options' => array(
48 'condition' => automatorwp_utilities_condition_option(),
49 'post' => automatorwp_utilities_post_option( array(
50 'post_type' => 'any',
51 'option_none_label' => __( 'Any post', 'automatorwp' ),
52 ) ),
53 'meta_key' => array(
54 'from' => 'meta_key',
55 /* translators: Refers to meta key */
56 'default' => __( 'with any key', 'automatorwp' ),
57 'fields' => array(
58 'meta_key' => array(
59 'name' => __( 'Meta key:', 'automatorwp' ),
60 'type' => 'text',
61 'default' => ''
62 ),
63 )
64 ),
65 'meta_value' => array(
66 'from' => 'meta_value',
67 'default' => __( 'any value', 'automatorwp' ),
68 'fields' => array(
69 'meta_value' => array(
70 'name' => __( 'Meta value:', 'automatorwp' ),
71 'type' => 'text',
72 'default' => ''
73 ),
74 )
75 ),
76 'times' => automatorwp_utilities_times_option(),
77 ),
78 'tags' => array_merge(
79 array(
80 'updated_meta_key' => array(
81 'label' => __( 'Updated meta key', 'automatorwp' ),
82 'type' => 'text',
83 'preview' => __( 'Key of the updated meta', 'automatorwp' ),
84 ),
85 'updated_meta_value' => array(
86 'label' => __( 'Updated meta value', 'automatorwp' ),
87 'type' => 'text',
88 'preview' => __( 'Value of the updated meta', 'automatorwp' ),
89 ),
90 ),
91 automatorwp_utilities_post_tags(),
92 automatorwp_utilities_times_tag()
93 )
94 ) );
95
96 }
97
98 /**
99 * Trigger listener
100 *
101 * @since 1.0.0
102 *
103 * @param int $meta_id ID of updated metadata entry.
104 * @param int $object_id ID of the object metadata is for.
105 * @param string $meta_key Metadata key.
106 * @param mixed $meta_value Metadata value. Serialized if non-scalar.
107 */
108 public function listener( $meta_id, $object_id, $meta_key, $meta_value ) {
109
110 $post = get_post( $object_id );
111
112 /**
113 * User ID for post updated triggers
114 *
115 * @since 1.0.0
116 *
117 * @param int $user_id The user ID
118 * @param int $post_ID The post ID
119 * @param string $trigger The trigger
120 */
121 $user_id = apply_filters( 'automatorwp_post_updated_user_id', $post->post_author, $object_id, $this->trigger );
122
123 automatorwp_trigger_event( array(
124 'trigger' => $this->trigger,
125 'user_id' => $user_id,
126 'post_id' => $object_id,
127 'meta_key' => $meta_key,
128 'meta_value' => $meta_value,
129 ) );
130
131 }
132
133 /**
134 * User deserves check
135 *
136 * @since 1.0.0
137 *
138 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
139 * @param stdClass $trigger The trigger object
140 * @param int $user_id The user ID
141 * @param array $event Event information
142 * @param array $trigger_options The trigger's stored options
143 * @param stdClass $automation The trigger's automation object
144 *
145 * @return bool True if user deserves trigger, false otherwise
146 */
147 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
148
149 // Don't deserve if post is not received
150 if( ! isset( $event['post_id'] ) && ! isset( $event['meta_key'] ) && ! isset( $event['meta_value'] ) ) {
151 return false;
152 }
153
154 $post = get_post( absint( $event['post_id'] ) );
155
156 // Don't deserve if post doesn't exists
157 if( ! $post ) {
158 return false;
159 }
160
161 // Don't deserve if key doesn't matches with the trigger option
162 if( $trigger_options['meta_key'] !== '' && $trigger_options['meta_key'] !== $event['meta_key'] ) {
163 return false;
164 }
165
166 // Don't deserve if value doesn't matches with the trigger option
167 if ( $trigger_options['meta_value'] !== '' && ! automatorwp_condition_matches( $event['meta_value'], $trigger_options['meta_value'], $trigger_options['condition'] ) ) {
168 return false;
169 }
170
171 return $deserves_trigger;
172
173 }
174
175 /**
176 * Register the required hooks
177 *
178 * @since 1.0.0
179 */
180 public function hooks() {
181
182 // Tags replacement
183 add_filter( 'automatorwp_get_trigger_tag_replacement', array( $this, 'tags_replacement' ), 10, 6 );
184
185 // Log meta data
186 add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
187
188 // Log fields
189 add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
190
191 parent::hooks();
192 }
193
194 /**
195 * Trigger custom tags replacement
196 *
197 * @since 1.0.0
198 *
199 * @param string $replacement The tag replacement
200 * @param string $tag_name The tag name (without "{}")
201 * @param stdClass $trigger The trigger object
202 * @param int $user_id The user ID
203 * @param string $content The content to parse
204 * @param stdClass $log The last trigger log object
205 *
206 * @return string
207 */
208 function tags_replacement( $replacement, $tag_name, $trigger, $user_id, $content, $log ) {
209
210 // Bail if action type don't match this action
211 if( $trigger->type !== $this->trigger ) {
212 return $replacement;
213 }
214
215 switch( $tag_name ) {
216 case 'updated_meta_key':
217 $replacement = automatorwp_get_log_meta( $log->id, 'updated_meta_key', true );
218 break;
219 case 'updated_meta_value':
220 $replacement = automatorwp_get_log_meta( $log->id, 'updated_meta_value', true );
221 break;
222 }
223
224 return $replacement;
225
226 }
227
228 /**
229 * Trigger custom log meta
230 *
231 * @since 1.0.0
232 *
233 * @param array $log_meta Log meta data
234 * @param stdClass $trigger The trigger object
235 * @param int $user_id The user ID
236 * @param array $event Event information
237 * @param array $trigger_options The trigger's stored options
238 * @param stdClass $automation The trigger's automation object
239 *
240 * @return array
241 */
242 function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
243
244 // Bail if action type don't match this action
245 if( $trigger->type !== $this->trigger ) {
246 return $log_meta;
247 }
248
249 $log_meta['updated_meta_key'] = ( isset( $event['updated_meta_key'] ) ? $event['updated_meta_key'] : '' );
250 $log_meta['updated_meta_value'] = ( isset( $event['updated_meta_value'] ) ? $event['updated_meta_value'] : '' );
251
252 return $log_meta;
253
254 }
255
256 /**
257 * Action custom log fields
258 *
259 * @since 1.0.0
260 *
261 * @param array $log_fields The log fields
262 * @param stdClass $log The log object
263 * @param stdClass $object The trigger/action/automation object attached to the log
264 *
265 * @return array
266 */
267 public function log_fields( $log_fields, $log, $object ) {
268
269 // Bail if log is not assigned to an trigger
270 if( $log->type !== 'trigger' ) {
271 return $log_fields;
272 }
273
274 // Bail if trigger type don't match this trigger
275 if( $object->type !== $this->trigger ) {
276 return $log_fields;
277 }
278
279 $log_fields['updated_meta_key'] = array(
280 'name' => __( 'Updated meta key', 'automatorwp' ),
281 'desc' => __( 'Key of the updated meta.', 'automatorwp' ),
282 'type' => 'text',
283 );
284
285 $log_fields['updated_meta_value'] = array(
286 'name' => __( 'Updated meta value', 'automatorwp' ),
287 'desc' => __( 'Value of the updated meta.', 'automatorwp' ),
288 'type' => 'text',
289 );
290
291 return $log_fields;
292
293 }
294
295 }
296
297 new AutomatorWP_WordPress_Post_Meta_Update( 'wordpress' );
298 new AutomatorWP_WordPress_Post_Meta_Update( 'posts' );