PluginProbe
OttoKit: All-in-One Automation Platform / 1.1.38
OttoKit: All-in-One Automation Platform v1.1.38
1.1.38 1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 All 124 releases
suretriggers / src / Integrations / voxel / actions / new-post.php
new-post.php
143 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * NewPost.
4 * php version 5.6
5 *
6 * @category NewPost
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Integrations\Voxel\Actions;
15
16 use SureTriggers\Integrations\AutomateAction;
17 use SureTriggers\Traits\SingletonLoader;
18 use SureTriggers\Integrations\Voxel\Voxel;
19 use Exception;
20
21 /**
22 * NewPost
23 *
24 * @category NewPost
25 * @package SureTriggers
26 * @author BSF <username@example.com>
27 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
28 * @link https://www.brainstormforce.com/
29 * @since 1.0.0
30 */
31 class NewPost extends AutomateAction {
32
33 /**
34 * Integration type.
35 *
36 * @var string
37 */
38 public $integration = 'Voxel';
39
40 /**
41 * Action name.
42 *
43 * @var string
44 */
45 public $action = 'voxel_create_new_post';
46
47 use SingletonLoader;
48
49 /**
50 * Register action.
51 *
52 * @param array $actions action data.
53 * @return array
54 */
55 public function register( $actions ) {
56 $actions[ $this->integration ][ $this->action ] = [
57 'label' => __( 'Create New Post', 'suretriggers' ),
58 'action' => 'voxel_create_new_post',
59 'function' => [ $this, 'action_listener' ],
60 ];
61
62 return $actions;
63 }
64
65 /**
66 * Action listener.
67 *
68 * @param int $user_id user_id.
69 * @param int $automation_id automation_id.
70 * @param array $fields fields.
71 * @param array $selected_options selectedOptions.
72 *
73 * @throws Exception Exception.
74 *
75 * @return bool|array
76 */
77 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
78 $user_email = $selected_options['post_author_email'];
79
80 if ( ! class_exists( 'Voxel\Post' ) ) {
81 return false;
82 }
83
84 // Get the post type.
85 $post_type = isset( $selected_options['voxel_post_type'] ) && '' !== $selected_options['voxel_post_type'] ? $selected_options['voxel_post_type'] : 'post';
86
87 $post_fields = [];
88 foreach ( $selected_options['field_row_repeater'] as $key => $field ) {
89 $field_name = $field['value']['name'];
90 if ( 'repeater' == $field['value']['type'] ) {
91 if ( 'work-hours' == $field['value']['name'] ) {
92 $arr_value = $selected_options['field_row'][ $key ][ $field_name ];
93 foreach ( $arr_value as $key => $val ) {
94 $post_fields[ $field_name ][ $key ]['days'] = $val['work_days'];
95 $post_fields[ $field_name ][ $key ]['status'] = $val['work_status'];
96 if ( '' != $val['work_hours'] ) {
97 $hours = explode( '-', $val['work_hours'] );
98 $post_fields[ $field_name ][ $key ]['hours'][] = [
99 'from' => $hours[0],
100 'to' => $hours[1],
101 ];
102 }
103 }
104 } else {
105 $arr_value = $selected_options['field_row'][ $key ][ $field_name ];
106 foreach ( $arr_value as $key => $val ) {
107 $post_fields[ $field_name ][ $key ] = $val;
108 }
109 }
110 } else {
111 $value = trim( $selected_options['field_row'][ $key ][ $field_name ] );
112 $post_fields[ $field_name ] = $value;
113 }
114 }
115
116 $data = [
117 'post_type' => $post_type,
118 'post_title' => isset( $post_fields['title'] ) && ! is_array( $post_fields['title'] ) ? (string) $post_fields['title'] : '',
119 'post_status' => isset( $selected_options['post_status'] ) && '' !== $selected_options['post_status'] ? $selected_options['post_status'] : 'draft',
120 ];
121
122 if ( is_email( $user_email ) ) {
123 $user = get_user_by( 'email', $user_email );
124 $user_id = $user ? $user->ID : 1;
125 $data['post_author'] = $user_id;
126 }
127 $post_id = wp_insert_post( $data );
128
129 // Update Collection fields.
130 Voxel::voxel_update_post( $post_fields, $post_id, $post_type );
131
132 return [
133 'success' => true,
134 'message' => esc_attr__( 'Post created successfully', 'suretriggers' ),
135 'post_id' => $post_id,
136 'post_url' => get_permalink( $post_id ),
137 ];
138 }
139
140 }
141
142 NewPost::get_instance();
143