PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.32
OttoKit: All-in-One Automation Platform v1.1.32
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 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / suredash / actions / suredash-create-space.php
suretriggers / src / Integrations / suredash / actions Last commit date
suredash-add-badges-to-user.php 5 months ago suredash-create-space.php 4 months ago suredash-post-to-discussion-space.php 6 months ago suredash-remove-badges-from-user.php 5 months ago
suredash-create-space.php
296 lines
1 <?php
2 /**
3 * SureDashCreateSpace.
4 *
5 * @category SureDashCreateSpace
6 * @package SureTriggers
7 * @author BSF <username@example.com>
8 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
9 * @link https://www.brainstormforce.com/
10 * @since 1.0.0
11 */
12
13 namespace SureTriggers\Integrations\SureDash\Actions;
14
15 use SureTriggers\Integrations\AutomateAction;
16 use SureTriggers\Traits\SingletonLoader;
17 use Exception;
18
19 /**
20 * SureDashCreateSpace
21 *
22 * @category SureDashCreateSpace
23 * @package SureTriggers
24 * @author BSF <username@example.com>
25 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
26 * @link https://www.brainstormforce.com/
27 * @since 1.0.0
28 */
29 class SureDashCreateSpace extends AutomateAction {
30
31 /**
32 * Integration type.
33 *
34 * @var string
35 */
36 public $integration = 'SureDash';
37
38 /**
39 * Action name.
40 *
41 * @var string
42 */
43 public $action = 'suredash_create_space';
44
45 use SingletonLoader;
46
47 /**
48 * Register a action.
49 *
50 * @param array $actions actions.
51 * @return array
52 */
53 public function register( $actions ) {
54 $actions[ $this->integration ][ $this->action ] = [
55 'label' => __( 'Create Space', 'suretriggers' ),
56 'action' => 'suredash_create_space',
57 'function' => [ $this, 'action_listener' ],
58 ];
59
60 return $actions;
61 }
62
63 /**
64 * Action listener.
65 *
66 * @param int $user_id user_id.
67 * @param int $automation_id automation_id.
68 * @param array $fields fields.
69 * @param array $selected_options selectedOptions.
70 *
71 * @return array|bool
72 * @throws Exception Error.
73 */
74 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
75 if ( ! defined( 'SUREDASHBOARD_VER' ) ) {
76 return [
77 'status' => 'error',
78 'message' => __( 'SureDash plugin is not active or properly configured.', 'suretriggers' ),
79 ];
80 }
81
82 if ( ! function_exists( 'sd_wp_insert_post' ) ) {
83 return [
84 'status' => 'error',
85 'message' => __( 'SureDash version does not support programmatic space creation.', 'suretriggers' ),
86 ];
87 }
88
89 $space_title = ! empty( $selected_options['space_title'] ) ? sanitize_text_field( $selected_options['space_title'] ) : '';
90 $space_type = ! empty( $selected_options['space_type'] ) ? sanitize_text_field( $selected_options['space_type'] ) : 'posts_discussion';
91 $space_group = ! empty( $selected_options['space_group'] ) ? sanitize_text_field( $selected_options['space_group'] ) : '';
92
93 if ( empty( $space_title ) ) {
94 return [
95 'status' => 'error',
96 'message' => __( 'Space title is required.', 'suretriggers' ),
97 ];
98 }
99
100 $allowed_types = [ 'single_post', 'posts_discussion', 'link', 'resource_library', 'course', 'collection', 'events' ];
101 if ( ! in_array( $space_type, $allowed_types, true ) ) {
102 return [
103 'status' => 'error',
104 'message' => __( 'Invalid space type. Allowed types: single_post, posts_discussion, link, resource_library, course, collection, events.', 'suretriggers' ),
105 ];
106 }
107
108 $allowed_statuses = [ 'publish', 'draft', 'private', 'pending' ];
109 $post_status = isset( $selected_options['space_status'] ) && in_array( $selected_options['space_status'], $allowed_statuses, true )
110 ? $selected_options['space_status']
111 : 'publish';
112
113 $taxonomy = defined( 'SUREDASHBOARD_TAXONOMY' ) ? SUREDASHBOARD_TAXONOMY : 'portal_group';
114 $term_id = 0;
115
116 // Resolve or create the space group.
117 if ( ! empty( $space_group ) ) {
118 if ( is_numeric( $space_group ) ) {
119 $term = get_term( absint( $space_group ), $taxonomy );
120 if ( $term && ! is_wp_error( $term ) ) {
121 $term_id = $term->term_id;
122 } else {
123 return [
124 'status' => 'error',
125 'message' => __( 'Space group not found with the provided ID.', 'suretriggers' ),
126 ];
127 }
128 } else {
129 $term = term_exists( $space_group, $taxonomy );
130 if ( is_array( $term ) ) {
131 $term_id = (int) $term['term_id'];
132 } else {
133 // Create the space group.
134 $new_term = wp_insert_term( $space_group, $taxonomy );
135 if ( ! is_wp_error( $new_term ) ) {
136 $term_id = (int) $new_term['term_id'];
137 }
138 }
139 }
140 } else {
141 // Default to "Uncategorized" group. Note: on non-English sites SureDash may
142 // name the default group differently; this will create a new term if not found.
143 $term = term_exists( 'Uncategorized', $taxonomy );
144 if ( is_array( $term ) ) {
145 $term_id = (int) $term['term_id'];
146 } else {
147 $new_term = wp_insert_term( 'Uncategorized', $taxonomy );
148 if ( ! is_wp_error( $new_term ) ) {
149 $term_id = (int) $new_term['term_id'];
150 }
151 }
152 }
153
154 if ( 0 === $term_id ) {
155 return [
156 'status' => 'error',
157 'message' => __( 'Failed to resolve or create the space group.', 'suretriggers' ),
158 ];
159 }
160
161 $post_type = defined( 'SUREDASHBOARD_POST_TYPE' ) ? SUREDASHBOARD_POST_TYPE : 'portal';
162
163 $post_author = $user_id ? $user_id : get_current_user_id();
164 if ( ! empty( $selected_options['space_author'] ) ) {
165 $post_author = absint( $selected_options['space_author'] );
166 }
167
168 $post_attr = [
169 'post_title' => $space_title,
170 'post_name' => sanitize_title( $space_title ),
171 'post_type' => $post_type,
172 'post_status' => $post_status,
173 'post_author' => $post_author,
174 ];
175
176 if ( ! empty( $selected_options['space_description'] ) ) {
177 $post_attr['post_content'] = wp_kses_post( $selected_options['space_description'] );
178 }
179
180 $space_id = sd_wp_insert_post( $post_attr );
181
182 if ( is_wp_error( $space_id ) || 0 === $space_id ) {
183 return [
184 'status' => 'error',
185 'message' => __( 'Failed to create the space.', 'suretriggers' ),
186 ];
187 }
188
189 // Assign the space to the group.
190 $terms_result = wp_set_post_terms( $space_id, [ $term_id ], $taxonomy );
191 if ( is_wp_error( $terms_result ) ) {
192 return [
193 'status' => 'error',
194 'message' => __( 'Space created but group assignment failed.', 'suretriggers' ),
195 ];
196 }
197
198 // Set the integration/space type meta.
199 if ( function_exists( 'sd_update_post_meta' ) ) {
200 sd_update_post_meta( $space_id, 'integration', $space_type );
201 } else {
202 update_post_meta( $space_id, 'integration', $space_type );
203 }
204
205 // For discussion spaces, create a forum category linked to the space.
206 if ( 'posts_discussion' === $space_type ) {
207 $feed_taxonomy = defined( 'SUREDASHBOARD_FEED_TAXONOMY' ) ? SUREDASHBOARD_FEED_TAXONOMY : 'community-forum';
208 $forum_term = term_exists( $space_title, $feed_taxonomy );
209
210 if ( is_array( $forum_term ) ) {
211 $feed_group_id = (int) $forum_term['term_id'];
212 } else {
213 $new_forum_term = wp_insert_term( $space_title, $feed_taxonomy );
214 $feed_group_id = ! is_wp_error( $new_forum_term ) ? (int) $new_forum_term['term_id'] : 0;
215 }
216
217 if ( $feed_group_id > 0 ) {
218 if ( function_exists( 'sd_update_post_meta' ) ) {
219 sd_update_post_meta( $space_id, 'feed_group_id', $feed_group_id );
220 } else {
221 update_post_meta( $space_id, 'feed_group_id', $feed_group_id );
222 }
223 }
224 }
225
226 // Set featured image if provided — validate URL to prevent SSRF.
227 if ( ! empty( $selected_options['featured_image'] ) ) {
228 $image_url = esc_url_raw( $selected_options['featured_image'] );
229 $parsed = wp_parse_url( $image_url );
230
231 if ( ! empty( $image_url ) && isset( $parsed['scheme'] ) && in_array( $parsed['scheme'], [ 'http', 'https' ], true ) ) {
232 require_once ABSPATH . 'wp-admin/includes/media.php';
233 require_once ABSPATH . 'wp-admin/includes/file.php';
234 require_once ABSPATH . 'wp-admin/includes/image.php';
235
236 $attachment_id = media_sideload_image( $image_url, $space_id, null, 'id' );
237
238 if ( ! is_wp_error( $attachment_id ) && $attachment_id ) {
239 set_post_thumbnail( $space_id, (int) $attachment_id );
240 }
241 }
242 }
243
244 // Set custom meta fields if provided.
245 if ( ! empty( $selected_options['space_meta'] ) && is_array( $selected_options['space_meta'] ) ) {
246 foreach ( $selected_options['space_meta'] as $meta ) {
247 if ( isset( $meta['metaKey'] ) && isset( $meta['metaValue'] ) ) {
248 $meta_key = sanitize_key( $meta['metaKey'] );
249 $meta_value = sanitize_text_field( $meta['metaValue'] );
250 if ( function_exists( 'sd_update_post_meta' ) ) {
251 sd_update_post_meta( $space_id, $meta_key, $meta_value );
252 } else {
253 update_post_meta( $space_id, $meta_key, $meta_value );
254 }
255 }
256 }
257 }
258
259 $space_post = get_post( $space_id );
260
261 if ( ! $space_post ) {
262 return [
263 'status' => 'error',
264 'message' => __( 'Failed to retrieve created space.', 'suretriggers' ),
265 ];
266 }
267
268 $response_data = [
269 'status' => 'success',
270 'message' => __( 'Space created successfully.', 'suretriggers' ),
271 'space_id' => $space_post->ID,
272 'space_title' => $space_post->post_title,
273 'space_type' => $space_type,
274 'space_status' => $space_post->post_status,
275 'space_author' => $space_post->post_author,
276 'space_date' => $space_post->post_date,
277 'space_permalink' => get_permalink( $space_id ),
278 'space_group_id' => $term_id,
279 ];
280
281 $group_term = get_term( $term_id, $taxonomy );
282 if ( $group_term && ! is_wp_error( $group_term ) ) {
283 $response_data['space_group_name'] = $group_term->name;
284 }
285
286 $featured_image_url = get_the_post_thumbnail_url( $space_id, 'full' );
287 if ( $featured_image_url ) {
288 $response_data['featured_image_url'] = $featured_image_url;
289 }
290
291 return $response_data;
292 }
293 }
294
295 SureDashCreateSpace::get_instance();
296