PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.2
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.2
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 5.6.2 All 33 releases
automatorwp / integrations / wordpress / actions / update-post.php

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

517 lines 20.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Update Post
4 *
5 * @package AutomatorWP\Integrations\WordPress\Actions\Update_Post
6 * @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 class AutomatorWP_WordPress_Update_Post extends AutomatorWP_Integration_Action {
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->action = $integration . '_update_post';
23
24 parent::__construct();
25
26 }
27
28 /**
29 * The new inserted post ID
30 *
31 * @since 1.0.0
32 *
33 * @var int|WP_Error $post_id
34 */
35 public $post_id = 0;
36
37 /**
38 * The post meta
39 *
40 * @since 1.0.0
41 *
42 * @var array $post_meta
43 */
44 public $post_meta = array();
45
46 /**
47 * Register the trigger
48 *
49 * @since 1.0.0
50 */
51 public function register() {
52
53 $post_type_options = array();
54
55 foreach( get_post_types( array(), 'objects' ) as $post_type ) {
56 /* translators: %1$s: Post type key (post, page). %2$s: Post type name (Post, Page). */
57 $post_type_options[] = sprintf( __( '<code>%1$s</code> for %2$s', 'automatorwp' ), $post_type->name, $post_type->labels->name );
58 }
59
60 $post_status_options = array();
61
62 foreach( get_post_statuses() as $post_status => $post_status_label ) {
63 /* translators: %1$s: Post status key (draft, pending). %2$s: Post status label (Draft, Pending Review). */
64 $post_status_options[] = sprintf( __( '<code>%1$s</code> for %2$s', 'automatorwp' ), $post_status, $post_status_label );
65 }
66
67 automatorwp_register_action( $this->action, array(
68 'integration' => $this->integration,
69 'label' => __( 'Update a post', 'automatorwp' ),
70 'select_option' => __( 'Update <strong>a post</strong>', 'automatorwp' ),
71 /* translators: %1$s: Post. */
72 'edit_label' => sprintf( __( 'Update %1$s', 'automatorwp' ), '{post}' ),
73 /* translators: %1$s: Post. */
74 'log_label' => sprintf( __( 'Update %1$s', 'automatorwp' ), '{post}' ),
75 'options' => array(
76 'post' => array(
77 'default' => __( 'a post', 'automatorwp' ),
78 'from' => 'post_id',
79 'fields' => array(
80 'post_id' => automatorwp_utilities_post_field( array(
81 'name' => __( 'Post to update:', 'automatorwp' ),
82 'post_type' => 'any',
83 'placeholder' => __( 'Select a post', 'automatorwp' ),
84 'option_none_label' => __( 'a post', 'automatorwp' ),
85 'option_custom' => true,
86 'option_custom_desc' => __( 'Post ID', 'automatorwp' ),
87 ) ),
88 'post_id_custom' => automatorwp_utilities_custom_field( array(
89 'option_custom_desc' => __( 'Post ID', 'automatorwp' ),
90 ) ),
91 'post_title' => array(
92 'name' => __( 'Title:', 'automatorwp' ),
93 'desc' => __( 'The post title.', 'automatorwp' )
94 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
95 'type' => 'text',
96 'default' => ''
97 ),
98 'post_name' => array(
99 'name' => __( 'URL slug:', 'automatorwp' ),
100 'desc' => __( 'The last part of the URL.', 'automatorwp' )
101 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' )
102 . ' ' . sprintf( __( '<a href="" target="_blank">Read about permalinks</a>', 'automatorwp' ), 'https://wordpress.org/support/article/writing-posts/#post-field-descriptions' ),
103 'type' => 'text',
104 'default' => ''
105 ),
106 'post_type' => array(
107 'name' => __( 'Type:', 'automatorwp' ),
108 'desc' => __( 'The post type.', 'automatorwp' )
109 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' )
110 . ' ' . automatorwp_toggleable_options_list( $post_type_options ),
111 'type' => 'text',
112 'default' => ''
113 ),
114 'post_status' => array(
115 'name' => __( 'Status:', 'automatorwp' ),
116 'desc' => __( 'The post status.', 'automatorwp' )
117 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' )
118 . ' ' . automatorwp_toggleable_options_list( $post_status_options ),
119 'type' => 'text',
120 'default' => ''
121 ),
122 'post_date' => array(
123 'name' => __( 'Date:', 'automatorwp' ),
124 'desc' => __( 'The date of the post. Supports "YYYY-MM-DD HH:MM:SS" and "YYYY-MM-DD" formats.', 'automatorwp' )
125 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
126 'type' => 'text',
127 'default' => ''
128 ),
129 'post_author' => array(
130 'name' => __( 'Author:', 'automatorwp' ),
131 'desc' => __( 'The ID of the user who added this post.', 'automatorwp' )
132 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
133 'type' => 'text',
134 'default' => ''
135 ),
136 'taxonomy' => automatorwp_utilities_ajax_selector_field( array(
137 'field' => 'taxonomy',
138 'option_default' => __( 'taxonomy', 'automatorwp' ),
139 'placeholder' => __( 'Select a taxonomy', 'automatorwp' ),
140 'name' => __( 'Taxonomy:', 'automatorwp' ),
141 'desc' => __( 'The post taxonomy.', 'automatorwp' ),
142 'action_cb' => 'automatorwp_wordpress_get_taxonomies',
143 'options_cb' => 'automatorwp_wordpress_options_cb_taxonomy',
144 'default' => ''
145 ) ),
146 'post_terms' => automatorwp_utilities_ajax_selector_field( array(
147 'field' => 'post_terms',
148 'option_default' => __( 'terms', 'automatorwp' ),
149 'placeholder' => __( 'Select a term', 'automatorwp' ),
150 'name' => __( 'Term:', 'automatorwp' ),
151 'desc' => __( 'The post term from taxonomy.', 'automatorwp' ),
152 'action_cb' => 'automatorwp_wordpress_get_terms',
153 'options_cb' => 'automatorwp_wordpress_options_cb_term',
154 'default' => ''
155 ) ),
156 'post_content' => array(
157 'name' => __( 'Content:', 'automatorwp' ),
158 'desc' => __( 'The post content.', 'automatorwp' )
159 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
160 'type' => 'wysiwyg',
161 'default' => ''
162 ),
163 'post_excerpt' => array(
164 'name' => __( 'Excerpt:', 'automatorwp' ),
165 'desc' => __( 'The post excerpt.', 'automatorwp' )
166 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
167 'type' => 'textarea',
168 'default' => ''
169 ),
170 'post_parent' => array(
171 'name' => __( 'Parent:', 'automatorwp' ),
172 'desc' => __( 'The post parent.', 'automatorwp' )
173 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
174 'type' => 'text',
175 'default' => ''
176 ),
177 'menu_order' => array(
178 'name' => __( 'Menu order:', 'automatorwp' ),
179 'desc' => __( 'The post menu order.', 'automatorwp' )
180 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
181 'type' => 'text',
182 'default' => ''
183 ),
184 'post_password' => array(
185 'name' => __( 'Password:', 'automatorwp' ),
186 'desc' => __( 'The password to access this post.', 'automatorwp' )
187 . ' ' . __( 'Leave empty to no update this field.', 'automatorwp' ),
188 'type' => 'text',
189 'default' => ''
190 ),
191 'post_meta' => array(
192 'name' => __( 'Post Meta:', 'automatorwp' ),
193 'desc' => __( 'The post meta values keyed by their post meta key.', 'automatorwp' ),
194 'type' => 'group',
195 'classes' => 'automatorwp-fields-table',
196 'options' => array(
197 'add_button' => __( 'Add meta', 'automatorwp' ),
198 'remove_button' => '<span class="dashicons dashicons-no-alt"></span>',
199 ),
200 'fields' => array(
201 'meta_key' => array(
202 'name' => __( 'Meta Key:', 'automatorwp' ),
203 'type' => 'text',
204 'default' => ''
205 ),
206 'meta_value' => array(
207 'name' => __( 'Meta Value:', 'automatorwp' ),
208 'type' => 'text',
209 'default' => ''
210 ),
211 ),
212 ),
213 )
214 )
215 ),
216 'tags' => automatorwp_utilities_post_tags(),
217 ) );
218
219 }
220
221 /**
222 * Action execution function
223 *
224 * @since 1.0.0
225 *
226 * @param stdClass $action The action object
227 * @param int $user_id The user ID
228 * @param array $action_options The action's stored options (with tags already passed)
229 * @param stdClass $automation The action's automation object
230 */
231 public function execute( $action, $user_id, $action_options, $automation ) {
232
233 $post_id = absint( $action_options['post_id'] );
234 $this->post_id = $post_id;
235
236 // Bail if not post ID provided
237 if( $post_id === 0 ) {
238 return;
239 }
240
241 // Setup post data
242 $post_data = array(
243 'ID' => $post_id,
244 );
245
246 $post_fields = array(
247 'post_title',
248 'post_name',
249 'post_type',
250 'post_status',
251 'post_date',
252 'post_author',
253 'post_content',
254 'post_excerpt',
255 'post_parent',
256 'menu_order',
257 'post_password',
258 );
259
260 $post_taxonomy = $action_options['taxonomy'];
261 $post_term = $action_options['post_terms'];
262
263 foreach( $post_fields as $post_field ) {
264 if( ! empty( $action_options[$post_field] ) ) {
265 $post_data[$post_field] = $action_options[$post_field];
266 }
267 }
268
269 $post_data['ID'] = $this->post_id;
270
271 // Update the post
272 wp_update_post( $post_data );
273
274 // Add to term if is selected
275 if ( $post_taxonomy !== 'any' || $post_term !== 'any' ){
276 $term_data = get_term( $post_term );
277 wp_set_object_terms( $this->post_id, absint( $term_data->term_id ), $term_data->taxonomy, false );
278 }
279
280 if( is_array( $action_options['post_meta'] ) ) {
281
282 foreach( $action_options['post_meta'] as $i => $meta ) {
283
284 // Parse automation tags replacements to both, key and value
285 $meta_key = automatorwp_parse_automation_tags( $automation->id, $user_id, $meta['meta_key'] );
286 $meta_value = automatorwp_parse_automation_tags( $automation->id, $user_id, $meta['meta_value'] );
287
288 // Sanitize
289 $meta_key = sanitize_text_field( $meta_key );
290 $meta_value = sanitize_text_field( $meta_value );
291
292 // Update post meta
293 update_post_meta( $this->post_id, $meta_key, $meta_value );
294
295 $this->post_meta[$meta_key] = $meta_value;
296
297 // Update action options to be passed on upcoming hooks
298 $action_options['post_meta'][$i] = array(
299 'meta_key' => $meta_key,
300 'meta_value' => $meta_value,
301 );
302
303 }
304
305 }
306
307 }
308
309 /**
310 * Register required hooks
311 *
312 * @since 1.0.0
313 */
314 public function hooks() {
315
316 // Log post ID
317 add_filter( 'automatorwp_user_completed_action_post_id', array( $this, 'post_id' ), 10, 6 );
318
319 // Log meta data
320 add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 );
321
322 // Log fields
323 add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
324
325 parent::hooks();
326 }
327
328 /**
329 * Action custom log post ID
330 *
331 * @since 1.0.0
332 *
333 * @param int $post_id The post ID, by default 0
334 * @param stdClass $action The action object
335 * @param int $user_id The user ID
336 * @param array $event Event information
337 * @param array $action_options The action's stored options (with tags already passed)
338 * @param stdClass $automation The action's automation object
339 *
340 * @return int
341 */
342 public function post_id( $post_id, $action, $user_id, $event, $action_options, $automation ) {
343
344 // Bail if action type don't match this action
345 if( $action->type !== $this->action ) {
346 return $post_id;
347 }
348
349 if( $this->post_id ) {
350 $post_id = $this->post_id;
351 }
352
353 return $post_id;
354
355 }
356
357 /**
358 * Action custom log meta
359 *
360 * @since 1.0.0
361 *
362 * @param array $log_meta Log meta data
363 * @param stdClass $action The action object
364 * @param int $user_id The user ID
365 * @param array $action_options The action's stored options (with tags already passed)
366 * @param stdClass $automation The action's automation object
367 *
368 * @return array
369 */
370 public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) {
371
372 // Bail if action type don't match this action
373 if( $action->type !== $this->action ) {
374 return $log_meta;
375 }
376
377 // Store post fields
378 $post_fields = array(
379 'post_title',
380 'post_name',
381 'post_type',
382 'post_status',
383 'post_date',
384 'post_author',
385 'post_content',
386 'post_excerpt',
387 'post_parent',
388 'menu_order',
389 'post_password',
390 );
391
392 foreach( $post_fields as $post_field ) {
393 $log_meta[$post_field] = $action_options[$post_field];
394 }
395
396 // Store post meta
397 $log_meta['post_meta'] = $this->post_meta;
398
399 // Store result
400 if( $this->post_id ) {
401 $log_meta['result'] = __( 'Post updated successfully', 'automatorwp' );
402 } else if( is_wp_error( $this->post_id ) ) {
403 $log_meta['result'] = $this->post_id->get_error_message();
404 } else {
405 $log_meta['result'] = __( 'Could not update the post', 'automatorwp' );
406 }
407
408 return $log_meta;
409 }
410
411 /**
412 * Action custom log fields
413 *
414 * @since 1.0.0
415 *
416 * @param array $log_fields The log fields
417 * @param stdClass $log The log object
418 * @param stdClass $object The trigger/action/automation object attached to the log
419 *
420 * @return array
421 */
422 public function log_fields( $log_fields, $log, $object ) {
423
424 // Bail if log is not assigned to an action
425 if( $log->type !== 'action' || $object->type !== $this->action )
426 return $log_fields;
427
428 $log_fields['post_info'] = array(
429 'name' => __( 'Post Information', 'automatorwp' ),
430 'desc' => __( 'Information about the post created.', 'automatorwp' ),
431 'type' => 'title',
432 );
433
434 $log_fields['post_title'] = array(
435 'name' => __( 'Title:', 'automatorwp' ),
436 'desc' => __( 'The post title.', 'automatorwp' ),
437 'type' => 'text',
438 );
439
440 $log_fields['post_name'] = array(
441 'name' => __( 'URL slug:', 'automatorwp' ),
442 'desc' => __( 'The post URL slug.', 'automatorwp' ),
443 'type' => 'text',
444 );
445
446 $log_fields['post_type'] = array(
447 'name' => __( 'Type:', 'automatorwp' ),
448 'desc' => __( 'The post type.', 'automatorwp' ),
449 'type' => 'text',
450 );
451
452 $log_fields['post_status'] = array(
453 'name' => __( 'Status:', 'automatorwp' ),
454 'desc' => __( 'The post status.', 'automatorwp' ),
455 'type' => 'text',
456 );
457
458 $log_fields['post_date'] = array(
459 'name' => __( 'Date:', 'automatorwp' ),
460 'desc' => __( 'The date of the post.', 'automatorwp' ),
461 'type' => 'text',
462 );
463
464 $log_fields['post_author'] = array(
465 'name' => __( 'Author:', 'automatorwp' ),
466 'desc' => __( 'The ID of the user who added this post.', 'automatorwp' ),
467 'type' => 'text',
468 );
469
470 $log_fields['post_content'] = array(
471 'name' => __( 'Content:', 'automatorwp' ),
472 'desc' => __( 'The post content.', 'automatorwp' ),
473 'type' => 'text',
474 );
475
476 $log_fields['post_excerpt'] = array(
477 'name' => __( 'Excerpt:', 'automatorwp' ),
478 'desc' => __( 'The post excerpt.', 'automatorwp' ),
479 'type' => 'text',
480 );
481
482 $log_fields['post_parent'] = array(
483 'name' => __( 'Parent:', 'automatorwp' ),
484 'desc' => __( 'The post parent.', 'automatorwp' ),
485 'type' => 'text',
486 );
487
488 $log_fields['menu_order'] = array(
489 'name' => __( 'Menu order:', 'automatorwp' ),
490 'desc' => __( 'The post menu order.', 'automatorwp' ),
491 'type' => 'text',
492 );
493
494 $log_fields['post_password'] = array(
495 'name' => __( 'Password:', 'automatorwp' ),
496 'desc' => __( 'The password to access this post.', 'automatorwp' ),
497 'type' => 'text',
498 );
499
500 $log_fields['post_meta'] = array(
501 'name' => __( 'Post Meta:', 'automatorwp' ),
502 'desc' => __( 'The post meta values keyed by their post meta key.', 'automatorwp' ),
503 'type' => 'text',
504 );
505
506 $log_fields['result'] = array(
507 'name' => __( 'Result:', 'automatorwp' ),
508 'type' => 'text',
509 );
510
511 return $log_fields;
512 }
513
514 }
515
516 new AutomatorWP_WordPress_Update_Post( 'wordpress' );
517 new AutomatorWP_WordPress_Update_Post( 'posts' );