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 / voxel / voxel.php
suretriggers / src / Integrations / voxel Last commit date
actions 5 months ago triggers 8 months ago voxel.php 6 months ago
voxel.php
984 lines
1 <?php
2 /**
3 * Voxel integrations file
4 *
5 * @since 1.0.0
6 * @package SureTrigger
7 */
8
9 namespace SureTriggers\Integrations\Voxel;
10
11 use SureTriggers\Controllers\IntegrationsController;
12 use SureTriggers\Integrations\Integrations;
13 use SureTriggers\Traits\SingletonLoader;
14 use SureTriggers\Integrations\WordPress\WordPress;
15
16 /**
17 * Class SureTrigger
18 *
19 * @package SureTriggers\Integrations\Voxel
20 */
21 class Voxel extends Integrations {
22
23 use SingletonLoader;
24
25 /**
26 * ID
27 *
28 * @var string
29 */
30 protected $id = 'Voxel';
31
32 /**
33 * SureTrigger constructor.
34 */
35 public function __construct() {
36 $this->name = __( 'Voxel', 'suretriggers' );
37 $this->description = __( 'Voxel is a complete no code solution in a single packageto create WordPress dynamic sites.', 'suretriggers' );
38 $this->icon_url = SURE_TRIGGERS_URL . 'assets/icons/voxel.svg';
39
40 add_action( 'init', [ $this, 'suretriggers_voxel_follow_post' ], 10 );
41 parent::__construct();
42 }
43
44 /**
45 * Update post.
46 *
47 * @access public
48 * @since 1.0
49 * @param array $fields Workflow step fields.
50 * @param int $post_id Post ID.
51 * @param string $post_type Post type.
52 * @return array|bool|string
53 */
54 public static function voxel_update_post( $fields, $post_id, $post_type ) {
55 if ( ! class_exists( 'Voxel\Post' ) || ! class_exists( 'Voxel\Post_Type' ) ) {
56 return [];
57 }
58 $post_title = isset( $fields['title'] ) && '' !== $fields['title'] ? $fields['title'] : '';
59 $post = \Voxel\Post::force_get( $post_id );
60 if ( ! $post ) {
61 return wp_json_encode(
62 [
63 'success' => false,
64 'message' => esc_attr__( 'Post not found', 'suretriggers' ),
65
66 ]
67 );
68 }
69 if ( $post_title ) {
70 $args = [
71 'ID' => $post_id,
72 'post_title' => $post_title,
73 ];
74 if ( isset( $fields['post_status'] ) && '' !== $fields['post_status'] ) {
75 $args['post_status'] = $fields['post_status'];
76 }
77 wp_update_post( $args );
78 }
79 $post_type = \Voxel\Post_Type::get( $post_type );
80 $post_fields = $post_type->get_fields();
81 $field_opts = self::get_fields();
82
83 foreach ( $post_fields as $key => $field ) {
84 $post_fields[ $key ] = $field->get_props();
85 }
86
87 // Loop through the post fields.
88 foreach ( $post_fields as $key => $field ) {
89 $field_key = $key;
90 $field_type = $field['type'];
91 $post_field = $post->get_field( $field_key );
92 // If field is not available, then skip.
93 if ( ! $post_field ) {
94 continue;
95 }
96
97 // If field is ui-step, ui-html and ui-heading, then skip.
98 if ( in_array( $field_type, [ 'ui-step', 'ui-html', 'ui-heading', 'ui-image' ], true ) ) {
99 continue;
100 }
101
102 // Update the repeater field data.
103 if ( 'repeater' === $field_type ) {
104 $repeater_fields = $field['fields'];
105 $repeater_values_final = [];
106 if ( count( $repeater_fields ) > 1 ) {
107 foreach ( $fields[ $field_key ] as $row_index => $row_values ) {
108 $repeater_value = [];
109
110 foreach ( $repeater_fields as $input_key => $input_field ) {
111 if ( isset( $row_values[ $input_field['key'] ] ) ) {
112 $repeater_value[ $input_field['key'] ] = $row_values[ $input_field['key'] ];
113 }
114 }
115
116 if ( ! empty( $repeater_value ) ) {
117 $repeater_values_final[] = $repeater_value;
118 }
119 }
120 }
121
122 if ( ! empty( $repeater_values_final ) ) {
123 $post_field->update( $repeater_values_final );
124 }
125
126 continue;
127 }
128
129 $field_inputs = $field_opts[ $field_type ]['fields'];
130 $field_value = '';
131
132 // If input field is location, then update the location.
133 if ( 'location' === $field_key ) {
134 $location_values = $field_opts[ $field_key ]['value'];
135 $gallery_value = [];
136
137 foreach ( $location_values as $value_item => $item_value ) {
138 if ( isset( $fields[ $field_key . '_' . $value_item ] ) ) {
139 $location_value[ $value_item ] = $fields[ $field_key . '_' . $value_item ];
140 }
141 }
142
143 if ( ! empty( $location_value ) ) {
144 $post_field->update( $location_value );
145 }
146
147 continue;
148 }
149
150 // If field inputs are more than one, then get the value from the inputs.
151 if ( count( $field_inputs ) > 1 ) {
152 $field_value = [];
153 foreach ( $field_inputs as $input_key => $input_field ) {
154 if ( isset( $fields[ $field_key . '_' . $input_field['key'] ] ) ) {
155 $field_value[ $field_key ] = $fields[ $field_key . '_' . $input_field['key'] ];
156 }
157 }
158 }
159
160 // Update work hours field.
161 if ( 'work-hours' === $field_type ) {
162 $schedules = [];
163
164 foreach ( $fields[ $field_key ] as $schedule ) {
165 $work_days_value = [];
166 $field_value = [];
167 if ( isset( $schedule['work_days'] ) ) {
168 $work_days_value = explode( ',', $schedule['work_days'] );
169 $field_value['days'] = $work_days_value;
170 }
171 if ( array_key_exists( 'work_hours', $schedule ) && isset( $schedule['work_hours'] ) ) {
172 $work_hours_array = explode( '-', $schedule['work_hours'] );
173 $formatted_work_hours = [
174 [
175 'from' => isset( $work_hours_array[0] ) ? $work_hours_array[0] : '',
176 'to' => isset( $work_hours_array[1] ) ? $work_hours_array[1] : '',
177 ],
178 ];
179 $field_value['hours'] = $formatted_work_hours;
180 }
181 if ( isset( $schedule['work_status'] ) ) {
182 $field_value['status'] = $schedule['work_status'];
183 }
184
185 if ( ! empty( $field_value ) ) {
186 $schedules[] = $field_value;
187 }
188 }
189 if ( ! empty( $schedules ) ) {
190 $post_field->update( $schedules );
191 }
192
193 continue;
194 }
195
196 // Update event-date field.
197 if ( 'recurring-date' === $field_type || 'event-date' === $field_type ) {
198 $event_date_value = [];
199
200 if ( isset( $fields[ $field_key ] ) ) {
201 $json_value = $fields[ $field_key ];
202 if ( is_string( $json_value ) && ( strpos( $json_value, '[{' ) === 0 || strpos( $json_value, '{"' ) === 0 ) ) {
203 $decoded_data = json_decode( $json_value, true );
204 if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded_data ) ) {
205 if ( isset( $decoded_data[0] ) ) {
206 $event = $decoded_data[0];
207 } else {
208 $event = $decoded_data;
209 }
210
211 if ( isset( $event['start'] ) ) {
212 $event_date_value['start'] = $event['start'];
213 }
214 if ( isset( $event['end'] ) ) {
215 $event_date_value['end'] = $event['end'];
216 }
217 if ( isset( $event['multiday'] ) ) {
218 $event_date_value['multiday'] = $event['multiday'];
219 }
220 if ( isset( $event['allday'] ) ) {
221 $event_date_value['allday'] = $event['allday'];
222 }
223 }
224 }
225 }
226
227 if ( empty( $event_date_value ) ) {
228 if ( isset( $fields[ $field_key . '_event_start_date' ] ) ) {
229 $event_date_value['start'] = $fields[ $field_key . '_event_start_date' ];
230 }
231
232 if ( isset( $fields[ $field_key . '_event_end_date' ] ) ) {
233 $event_date_value['end'] = $fields[ $field_key . '_event_end_date' ];
234 }
235
236 if ( isset( $fields[ $field_key . '_event_frequency' ] ) ) {
237 $event_date_value['frequency'] = $fields[ $field_key . '_event_frequency' ];
238 }
239
240 if ( isset( $fields[ $field_key . '_repeat_every' ] ) ) {
241 $event_date_value['unit'] = $fields[ $field_key . '_repeat_every' ];
242 }
243
244 if ( isset( $fields[ $field_key . '_event_until' ] ) ) {
245 $event_date_value['until'] = $fields[ $field_key . '_event_until' ];
246 }
247 }
248
249 if ( ! empty( $event_date_value ) ) {
250 $post_field->update( [ $event_date_value ] );
251 }
252
253 continue;
254 }
255
256 // If field is available in the fields, then update the post field.
257 if ( isset( $fields[ $field_key ] ) ) {
258 $field_value = $fields[ $field_key ];
259 if ( '' != $field_value ) {
260 if ( in_array( $field_type, [ 'file', 'profile-avatar' ], true ) || ( 'image' === $field_type && 'gallery' !== $field_key ) ) {
261 $field_value = [
262 [
263 'source' => 'existing',
264 'file_id' => (int) $field_value,
265 ],
266 ];
267 } elseif ( 'image' === $field_type && 'gallery' === $field_key ) {
268 // Handle gallery field with multiple comma-separated image IDs.
269 $image_ids = array_map( 'trim', explode( ',', $field_value ) );
270 $field_value = array_map(
271 function( $image_id ) {
272 return [
273 'source' => 'existing',
274 'file_id' => (int) $image_id,
275 ];
276 },
277 $image_ids
278 );
279 } elseif ( 'post-relation' === $field_type ) {
280 $field_value = array_map(
281 function( $post_id ) {
282 return (int) $post_id;
283 },
284 explode( ',', $field_value )
285 );
286 } elseif ( 'taxonomy' === $field_type ) {
287 $field_value = explode( ',', $field_value );
288 }
289 // If value is boolean false, then set it to false.
290 if ( 'false' === $field_value ) {
291 $field_value = false;
292 }
293 $post_field->update( $field_value );
294 }
295 }
296 }
297 return true;
298 }
299
300 /**
301 * Voxel fields with types and input requirements.
302 *
303 * @access public
304 * @since 1.0
305 * @return array
306 */
307 public static function get_fields() {
308 return [
309 // Post type fields.
310 'title' => [
311 'type' => 'text',
312 'value' => '',
313 'fields' => [
314 [
315 'key' => 'title',
316 'label' => esc_attr__( 'Title', 'suretriggers' ),
317 'type' => 'text',
318 ],
319 ],
320 ],
321 'description' => [
322 'type' => 'text',
323 'value' => '',
324 'fields' => [
325 [
326 'key' => 'description',
327 'label' => esc_attr__( 'Description', 'suretriggers' ),
328 'type' => 'text',
329 ],
330 ],
331 ],
332 'timezone' => [
333 'type' => 'text',
334 'value' => '',
335 'fields' => [
336 [
337 'key' => 'timezone',
338 'label' => esc_attr__( 'Timezone', 'suretriggers' ),
339 'type' => 'text',
340 ],
341 ],
342 ],
343 'location' => [
344 'type' => 'array',
345 'value' => [
346 'address' => '',
347 'latitude' => '',
348 'longitude' => '',
349 ],
350 'fields' => [
351 [
352 'key' => 'address',
353 'label' => esc_attr__( 'Address', 'suretriggers' ),
354 'type' => 'text',
355 ],
356 [
357 'key' => 'latitude',
358 'label' => esc_attr__( 'Latitude', 'suretriggers' ),
359 'type' => 'text',
360 ],
361 [
362 'key' => 'longitude',
363 'label' => esc_attr__( 'Longitude', 'suretriggers' ),
364 'type' => 'text',
365 ],
366 ],
367 ],
368 'email' => [
369 'type' => 'text',
370 'value' => '',
371 'fields' => [
372 [
373 'key' => 'email',
374 'label' => esc_attr__( 'Email', 'suretriggers' ),
375 'type' => 'text',
376 ],
377 ],
378 ],
379 'logo' => [
380 'type' => 'array',
381 'value' => [ 0 ],
382 'fields' => [
383 [
384 'key' => 'image_id',
385 'label' => esc_attr__( 'Image ID', 'suretriggers' ),
386 'type' => 'text',
387 'help' => esc_attr__( 'Provide Image ID', 'suretriggers' ),
388
389 ],
390 ],
391 ],
392 'cover-image' => [
393 'type' => 'array',
394 'value' => [ 0 ],
395 'fields' => [
396 [
397 'key' => 'image_id',
398 'label' => esc_attr__( 'Image ID', 'suretriggers' ),
399 'type' => 'text',
400 'help' => esc_attr__( 'Provide Image ID', 'suretriggers' ),
401
402 ],
403 ],
404 ],
405 'gallery' => [
406 'type' => 'array',
407 'value' => [
408 [ 0 ],
409 ],
410 'fields' => [
411 [
412 'key' => 'image_id',
413 'label' => esc_attr__( 'Image ID', 'suretriggers' ),
414 'type' => 'text',
415 'help' => esc_attr__( 'Provide Image IDs, separated by comma', 'suretriggers' ),
416
417 ],
418 ],
419 ],
420 'featured-image' => [
421 'type' => 'array',
422 'value' => [ 0 ],
423 'fields' => [
424 [
425 'key' => 'image_id',
426 'label' => esc_attr__( 'Image ID', 'suretriggers' ),
427 'type' => 'text',
428 'help' => esc_attr__( 'Provide Image IDs, separated by comma', 'suretriggers' ),
429
430 ],
431 ],
432 ],
433 'website' => [
434 'type' => 'text',
435 'value' => '',
436 'fields' => [
437 [
438 'key' => 'website',
439 'label' => esc_attr__( 'Website URL', 'suretriggers' ),
440 'type' => 'text',
441 ],
442 ],
443 ],
444 'phone-number' => [
445 'type' => 'text',
446 'value' => '',
447 'fields' => [
448 [
449 'key' => 'phone_number',
450 'label' => esc_attr__( 'Phone Number', 'suretriggers' ),
451 'type' => 'text',
452 ],
453 ],
454 ],
455 'event-date' => [
456 'type' => 'array',
457 'value' => [
458 'start' => '',
459 'end' => '',
460 'frequency' => '',
461 'unit' => '',
462 'until' => '',
463 ],
464 'fields' => [
465 [
466 'key' => 'event_start_date',
467 'label' => esc_attr__( 'Event Start Date', 'suretriggers' ),
468 'type' => 'text',
469 ],
470 [
471 'key' => 'event_end_date',
472 'label' => esc_attr__( 'Event End Date', 'suretriggers' ),
473 'type' => 'text',
474 ],
475 [
476 'key' => 'event_frequency',
477 'label' => esc_attr__( 'Event Frequency', 'suretriggers' ),
478 'type' => 'text',
479 ],
480 [
481 'key' => 'repeat_every',
482 'label' => esc_attr__( 'Event Unit', 'suretriggers' ),
483 'type' => 'text',
484 'help' => esc_attr__( 'Accepted values: day, week, month, year', 'suretriggers' ),
485
486 ],
487 [
488 'key' => 'event_until',
489 'label' => esc_attr__( 'Event Until', 'suretriggers' ),
490 'type' => 'text',
491 ],
492 ],
493 ],
494 'work-hours' => [
495 'type' => 'array',
496 'value' => [
497 [
498 'days',
499 'status',
500 'hours',
501 ],
502 ],
503 'fields' => [
504 [
505 'key' => 'work_days',
506 'label' => esc_attr__( 'Work Days', 'suretriggers' ),
507 'type' => 'text',
508 'help' => esc_attr__( 'Accepted values: mon, tue, wed, thu, fri, sat, sun', 'suretriggers' ),
509
510 ],
511 [
512 'key' => 'work_hours',
513 'label' => esc_attr__( 'Work Hours', 'suretriggers' ),
514 'type' => 'text',
515 'help' => esc_attr__( 'Enter value pairs as start and end time, separated by dash. For multiple pairs, use comma separator. Eg. 09:00-17:00, 09:00-12:00', 'suretriggers' ),
516
517 ],
518 [
519 'key' => 'work_status',
520 'label' => esc_attr__( 'Work Status', 'suretriggers' ),
521 'type' => 'text',
522 'help' => esc_attr__( 'Accepted values: hours, open, close, appointments_only', 'suretriggers' ),
523
524 ],
525 ],
526 ],
527 'profile-picture' => [
528 'type' => 'array',
529 'value' => [ 0 ],
530 'fields' => [
531 [
532 'key' => 'image_id',
533 'label' => esc_attr__( 'Image ID', 'suretriggers' ),
534 'type' => 'text',
535 'help' => esc_attr__( 'Provide Image ID', 'suretriggers' ),
536
537 ],
538 ],
539 ],
540 'profile-avatar' => [
541 'type' => 'array',
542 'value' => [ 0 ],
543 'fields' => [
544 [
545 'key' => 'image_id',
546 'label' => esc_attr__( 'Image ID', 'suretriggers' ),
547 'type' => 'text',
548 'help' => esc_attr__( 'Provide Image ID', 'suretriggers' ),
549
550 ],
551 ],
552 ],
553 'profile-name' => [
554 'type' => 'text',
555 'value' => '',
556 'fields' => [
557 [
558 'key' => 'profile_name',
559 'label' => esc_attr__( 'Profile Name', 'suretriggers' ),
560 'type' => 'text',
561 ],
562 ],
563 ],
564 // Custom field types.
565 'text' => [
566 'type' => 'text',
567 'value' => '',
568 'fields' => [
569 [
570 'key' => 'text',
571 'label' => esc_attr__( 'Text', 'suretriggers' ),
572 'type' => 'text',
573 ],
574 ],
575 ],
576 'number' => [
577 'type' => 'text',
578 'value' => '',
579 'fields' => [
580 [
581 'key' => 'number',
582 'label' => esc_attr__( 'Number', 'suretriggers' ),
583 'type' => 'text',
584 ],
585 ],
586 ],
587 'switcher' => [
588 'type' => 'switcher',
589 'value' => true,
590 'fields' => [
591 [
592 'key' => 'switcher',
593 'label' => esc_attr__( 'Switcher', 'suretriggers' ),
594 'type' => 'yes/no',
595 ],
596 ],
597 ],
598 'texteditor' => [
599 'type' => 'text',
600 'value' => '',
601 'fields' => [
602 [
603 'key' => 'text_editor',
604 'label' => esc_attr__( 'Text Editor', 'suretriggers' ),
605 'type' => 'textarea',
606 ],
607 ],
608 ],
609 'taxonomy' => [
610 'type' => 'array',
611 'value' => [],
612 'fields' => [
613 [
614 'key' => 'taxonomy',
615 'label' => esc_attr__( 'Taxonomy Slug', 'suretriggers' ),
616 'type' => 'text',
617 'help' => esc_attr__( 'Provide related taxonomy slug(s). If multiples allowed, separate with comma.', 'suretriggers' ),
618 ],
619 ],
620 ],
621 'product' => [
622 'type' => 'array',
623 'value' => [],
624 'fields' => [
625 [
626 'key' => 'product',
627 'label' => esc_attr__( 'Product ID', 'suretriggers' ),
628 'type' => 'text',
629 'help' => esc_attr__( 'Provide Product ID', 'suretriggers' ),
630
631 ],
632 ],
633 ],
634 'phone' => [
635 'type' => 'text',
636 'value' => '',
637 'fields' => [
638 [
639 'key' => 'phone',
640 'label' => esc_attr__( 'Phone', 'suretriggers' ),
641 'type' => 'text',
642 ],
643 ],
644 ],
645 'url' => [
646 'type' => 'text',
647 'value' => '',
648 'fields' => [
649 [
650 'key' => 'url',
651 'label' => esc_attr__( 'URL', 'suretriggers' ),
652 'type' => 'text',
653 ],
654 ],
655 ],
656 'image' => [
657 'type' => 'array',
658 'value' => [
659 'url' => '',
660 'alt' => '',
661 ],
662 'fields' => [
663 [
664 'key' => 'image_id',
665 'label' => esc_attr__( 'Image ID', 'suretriggers' ),
666 'type' => 'text',
667 'help' => esc_attr__( 'Provide Image IDs. Separate with comma.', 'suretriggers' ),
668
669 ],
670 ],
671 ],
672 'file' => [
673 'type' => 'array',
674 'value' => [
675 'url' => '',
676 'alt' => '',
677 ],
678 'fields' => [
679 [
680 'key' => 'file_id',
681 'label' => esc_attr__( 'File ID', 'suretriggers' ),
682 'type' => 'text',
683 'help' => esc_attr__( 'Provide File ID', 'suretriggers' ),
684
685 ],
686 ],
687 ],
688 'repeater' => [
689 'type' => 'array',
690 'value' => [
691 [
692 'url' => '',
693 'alt' => '',
694 ],
695 ],
696 'fields' => [
697 [
698 'key' => 'repeater',
699 'label' => esc_attr__( 'Repeater', 'suretriggers' ),
700 'type' => 'repeater',
701 ],
702 ],
703 ],
704 'recurring-date' => [
705 'type' => 'array',
706 'value' => [
707 [
708 'start' => '',
709 'end' => '',
710 'frequency' => '',
711 'unit' => '',
712 'until' => '',
713 ],
714 ],
715 'fields' => [
716 [
717 'key' => 'event_start_date',
718 'label' => esc_attr__( 'Event Start Date', 'suretriggers' ),
719 'type' => 'text',
720 ],
721 [
722 'key' => 'event_end_date',
723 'label' => esc_attr__( 'Event End Date', 'suretriggers' ),
724 'type' => 'text',
725 ],
726 [
727 'key' => 'event_frequency',
728 'label' => esc_attr__( 'Event Frequency', 'suretriggers' ),
729 'type' => 'text',
730 ],
731 [
732 'key' => 'repeat_every',
733 'label' => esc_attr__( 'Event Unit', 'suretriggers' ),
734 'type' => 'text',
735 'help' => esc_attr__( 'Accepted values: day, week, month, year', 'suretriggers' ),
736
737 ],
738 [
739 'key' => 'event_until',
740 'label' => esc_attr__( 'Event Until', 'suretriggers' ),
741 'type' => 'text',
742 ],
743 ],
744 ],
745 'post-relation' => [
746 'type' => 'array',
747 'value' => [ 0 ],
748 'fields' => [
749 [
750 'key' => 'post_id',
751 'label' => esc_attr__( 'Post ID', 'suretriggers' ),
752 'type' => 'text',
753 'help' => esc_attr__( 'Provide Post ID', 'suretriggers' ),
754
755 ],
756 ],
757 ],
758 'date' => [
759 'type' => 'text',
760 'value' => '',
761 'fields' => [
762 [
763 'key' => 'date',
764 'label' => esc_attr__( 'Date', 'suretriggers' ),
765 'type' => 'text',
766 ],
767 ],
768 ],
769 'select' => [
770 'type' => 'text',
771 'value' => '',
772 'fields' => [
773 [
774 'key' => 'select',
775 'label' => esc_attr__( 'Select', 'suretriggers' ),
776 'type' => 'select',
777 ],
778 ],
779 ],
780 'color' => [
781 'type' => 'color',
782 'value' => '',
783 'fields' => [
784 [
785 'key' => 'color',
786 'label' => esc_attr__( 'Color', 'suretriggers' ),
787 'type' => 'text',
788 ],
789 ],
790 ],
791 // Layout fields.
792 'ui-image' => [
793 'type' => 'array',
794 'value' => [ 0 ],
795 'fields' => [
796 [
797 'key' => 'image_id',
798 'label' => esc_attr__( 'Image IDs', 'suretriggers' ),
799 'type' => 'text',
800 'help' => esc_attr__( 'Provide Image IDs. Separate with comma.', 'suretriggers' ),
801
802 ],
803 ],
804 ],
805 ];
806 }
807
808 /**
809 * Sanitize the content.
810 *
811 * @access public
812 * @since 1.0
813 * @param string $content Content to sanitize.
814 * @return string|bool
815 */
816 public static function sanitize_content( $content ) {
817 if ( ! class_exists( 'Voxel\Timeline\Fields\Status_Message_Field' ) ) {
818 return false;
819 }
820 $field = new \Voxel\Timeline\Fields\Status_Message_Field();
821 $content = $field->sanitize( $content );
822 $field->validate( $content );
823
824 return $content;
825 }
826
827 /**
828 * Sanitize the files.
829 *
830 * @access public
831 * @since 1.0
832 * @param array $files Files to sanitize.
833 * @return array
834 */
835 public static function sanitize_files( $files ) {
836 if ( ! function_exists( 'Voxel\get' ) ) {
837 return [];
838 }
839 if ( ! \Voxel\get( 'settings.timeline.posts.images.enabled', true ) || ! class_exists( 'Voxel\Timeline\Fields\Status_Files_Field' ) ) {
840 return [];
841 }
842
843 $field = new \Voxel\Timeline\Fields\Status_Files_Field();
844 $files = $field->sanitize( $files );
845 $field->validate( $files );
846 $file_ids = $field->prepare_for_storage( $files );
847
848 return $file_ids;
849 }
850
851 /**
852 * Get Post Fields and it's values.
853 *
854 * @access public
855 * @since 1.0
856 * @param int $post_id Post ID.
857 * @return array
858 */
859 public static function get_post_fields( $post_id ) {
860 if ( ! class_exists( 'Voxel\Post' ) ) {
861 return [];
862 }
863 $post = \Voxel\Post::force_get( $post_id );
864 $context = [];
865 $context_data = [];
866 if ( ! empty( $post ) ) {
867 $fields = $post->get_fields();
868 if ( is_array( $fields ) && ! empty( $fields ) ) {
869 foreach ( $fields as $field ) {
870 $field_key = $field->get_key();
871 $field_type = $field->get_type();
872 $field_value = $field->get_value();
873 $field_content = null;
874 switch ( $field_type ) {
875 case 'taxonomy':
876 $field_content = join(
877 ', ',
878 array_map(
879 function( $term ) {
880 return $term->get_label();
881 },
882 $field_value
883 )
884 );
885 break;
886 case 'location':
887 $field_content = $field_value['address'];
888 break;
889 case 'work-hours':
890 $hours = [];
891 if ( is_array( $field_value ) && ! empty( $field_value ) ) {
892 foreach ( $field_value as $work_hour ) {
893 if ( 'hours' === $work_hour['status'] ) {
894 foreach ( $work_hour['days'] as $day ) {
895 foreach ( $work_hour['hours'] as $hour_key => $hour ) {
896 $hours[ $day . '_' . $hour_key ] = $hour['from'] . '-' . $hour['to'];
897 }
898 }
899 }
900 }
901 }
902 $field_content = $field_value;
903 $context[ $field_key . '_simplified' ] = wp_json_encode( $hours );
904 break;
905 case 'file':
906 case 'image':
907 case 'profile-avatar':
908 case 'gallery':
909 if ( is_array( $field_value ) ) {
910 foreach ( $field_value as $file_key => $file_id ) {
911 $field_content[ $field_key . '_' . $file_key . '_url' ] = wp_get_attachment_url( $file_id );
912 }
913 } else {
914 $field_content[ $field_key . '_url' ] = wp_get_attachment_url( $field_value );
915 }
916 break;
917 default:
918 $field_content = $field_value;
919 break;
920 }
921 $context[ $field_key ] = $field_content;
922 }
923 }
924 }
925 if ( ! empty( $context ) ) {
926 foreach ( $context as $key => $value ) {
927 $context_data[ 'field_' . $key ] = $value;
928 }
929 }
930 return $context_data;
931 }
932
933 /**
934 * Custom hook for Follow post and UnFollow Post triggers.
935 *
936 * @access public
937 * @since 1.0
938 * @return void
939 */
940 public function suretriggers_voxel_follow_post() {
941 if ( ! function_exists( 'Voxel\current_user' ) || ! class_exists( 'Voxel\Post' ) ) {
942 return;
943 }
944 if ( isset( $_GET['_wpnonce'] ) ) {
945 if ( ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'vx_user_follow' ) ) {
946 return;
947 }
948 }
949 if ( isset( $_GET['action'] ) && 'user.follow_post' === $_GET['action'] ) {
950 $current_user = \Voxel\current_user();
951 $post_id = ! empty( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : null;
952 if ( ! $post_id ) {
953 return;
954 }
955 $post = \Voxel\Post::get( $post_id );
956
957 if ( $post && $current_user ) {
958 $current_status = $current_user->get_follow_status( 'post', $post->get_id() );
959 $new_status = ( 1 === $current_status ) ? 'unfollow' : 'follow';
960 $follow_data = [
961 'post_id' => $post_id,
962 'user_id' => $current_user->get_id(),
963 'status' => $new_status,
964 ];
965 $follow_data = array_merge( $follow_data, self::get_post_fields( $post_id ), WordPress::get_post_context( $post_id ) );
966 $action_to_perform = ( 'follow' === $new_status ) ? 'st_voxel_post_followed' : 'st_voxel_post_unfollowed';
967 do_action( $action_to_perform, $follow_data );
968 }
969 }
970 }
971
972 /**
973 * Is Plugin depended plugin is installed or not.
974 *
975 * @return bool
976 */
977 public function is_plugin_installed() {
978 $bricks_theme = wp_get_theme( 'voxel' );
979 return $bricks_theme->exists();
980 }
981 }
982
983 IntegrationsController::register( Voxel::class );
984