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 / create-post.php

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

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