Jake Vanderwerf
2025-09-30 2cb91676044ecd0abd9c45b4835abb8b0d042312
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace JVBase\registry\providers;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class IntegrationFieldProvider implements FieldProviderInterface
{
    protected array $allowed = [];
    public function __construct() {
        $allowed = [];
        if (jvbSiteUsesGMB()) {
            $allowed['gmb'] = 'Google My Business';
        }
        if (jvbSiteUsesFacebook()) {
            $allowed['facebook'] = 'Facebook';
        }
        if (jvbSiteUsesSquare()) {
            $allowed['square'] = 'Square';
        }
        if (jvbSiteUsesInstagram()) {
            $allowed['instagram'] = 'Instagram';
        }
        if (jvbSiteUsesBluesky()) {
            $allowed['bluesky'] = 'BlueSky';
        }
        if (jvbSiteUsesHelcim()) {
            $allowed['helcim'] = 'Helcim';
        }
        $this->allowed = $allowed;
    }
    public function getFields(...$args): array
    {
        $args = $args[0];
        if (!array_key_exists('integrations', $args)) {
            return [];
        }
        $fields = [];
        $settings = $args['integrations'];
        foreach ($settings as $slug => $setting) {
            if (!array_key_exists($slug, $this->allowed)){
                continue;
            }
            $fields = array_merge($fields, $this->getIntegrationFields($slug, $args));
        }
        return $fields;
    }
 
    public function getIntegrationFields(string $integration, $config):array
    {
        $fields = [];
        $fields['share_to_' . $integration] = [
            'type' => 'true_false',
            'label' => 'Share to ' . $this->allowed[$integration],
            'section' => 'sync',
            'bulkEdit' => true,
        ];
        $setting = $config['integrations'][$integration];
        if (jvbCheck('update', $setting)) {
            $fields['_keep_synced_'.$integration] = [
                'type'  => 'true_false',
                'label' => 'Keep Synced with '. $this->allowed[$integration],
                'section' => 'sync',
                'condition' => [
                    'field' => 'share_to_'.$integration,
                    'value' => 1,
                    'operator'=> '==',
                ],
                'bulkEdit' => true,
            ];
        }
 
        $fields['schedule_'.$integration] = [
            'type'  => 'datetime',
            'label' => 'Schedule for later?',
            'condition' => [
                'field' => 'share_to_'.$integration,
                'operator' => '==',
                'value' => 1
            ]
        ];
        $fields["_{$integration}_item_id"] = [
            'type'      => 'text',
            'label'     => $this->allowed[$integration].' ID',
            'section'   => 'sync',
            'hidden'    => true,
        ];
        $fields["_{$integration}_shared_at"] = [
            'type'      => 'datetime',
            'label'     => $this->allowed[$integration].' Shared at:',
            'section'   => 'sync',
            'hidden'    => true,
        ];
        $fields["_{$integration}_last_sync"] = [
            'type'      => 'datetime',
            'label'     => $this->allowed[$integration].' Last Sync',
            'section'   => 'sync',
            'hidden'    => true
        ];
        $fields["_{$integration}_sync_status"] = [
            'type'  => 'select',
            'label' => 'Sync Status',
            'options' => [
                'synced' => 'Synced',
                'pending' => 'Pending',
                'failed' => 'Failed',
                'unpublished' => 'Unpublished',
                'scheduled' => 'Scheduled'
            ],
            'section' => 'sync',
            'hidden' => true,
        ];
 
        $integration = JVB()->connect($integration);
        if ($integration && $integration->hasContent()) {
            $type = (array_key_exists('content_type', $setting)) ? $setting['content_type'] : $integration->getDefaultContentType();
            $fields = array_merge($fields,$integration->getContentTypes($type));
        }
        return $fields;
    }
}