Jake Vanderwerf
2026-07-12 c204185ae86a98994f80010abf35a190c9406739
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace JVBase\integrations;
 
use JVBase\meta\Meta;
use WP_Query;
 
if (!defined('ABSPATH')) {
    exit;
}
 
trait SyncHelpers {
    use _Base;
    /**
     * Queue types
     * These types match with IntegrationExecutor
     */
    protected static string $syncTo;
    protected static string $deleteFrom;
    protected static string $syncFrom;
    protected static string $syncCustomer;
    protected static string $import;
 
    protected function setQueueTypes():void
    {
        self::$syncTo = $this->service_name.'_sync_to';
        self::$deleteFrom = $this->service_name.'_delete_from';
        self::$syncFrom = $this->service_name.'_sync_from';
        //TODO: What is the difference between sync_from and import?
        self::$import = $this->service_name.'import_from';
    }
 
    public function getServiceItemID(int $itemID, string $type = 'post'):string
    {
        return match($type) {
            'post'  => Meta::forPost($itemID)->get("_{$this->service_name}_item_id"),
            'term','taxonomy'   => Meta::forTerm($itemID)->get("_{$this->service_name}_item_id"),
            'user'  => Meta::forUser($itemID)->get("_{$this->service_name}_item_id"),
            default => ''
        };
    }
    protected function updateItemStatus(int|array $items, string $status = 'pending', string $type = 'post'):void
    {
        if (!is_array($items)) {
            $items = [$items];
        }
        foreach ($items as $ID) {
            match ($type) {
                'post'  => Meta::forPost($ID)->set('_'.$this->service_name.'_sync_status', $status),
                'term'  => Meta::forTerm($ID)->set('_'.$this->service_name.'_sync_status', $status),
                'user'  => Meta::forUser($ID)->set('_'.$this->service_name.'_sync_status', $status),
                default => false
            };
        }
    }
 
 
    public function findWPItem(string $integrationID, ?string $type = null):array|false
    {
        if (empty($integrationID)) {
            error_log('Integrations::findWPItem No Integration ID received.');
            return false;
        }
        $types = ['post', 'term', 'user'];
        if (!is_null($type) && !in_array($type, $types)) {
            error_log('Integrations::findWPItem invalid type sent: '.$type.'; defaulting to checking all');
            $type = null;
        }
        if (!is_null($type)) {
            return $this->queryWPType($integrationID, $type);
        } else {
            foreach ($types as $type) {
                $check = $this->queryWPType($integrationID, $type);
                if ($check) {
                    return $check;
                }
            }
        }
        return false;
    }
 
    /**
     * @param string $integrationID
     * @param string|null $type
     * @return array|false if success, an array of $ID and $type
     */
    public function queryWPType(string $integrationID, ?string $type = null):array|false
    {
        if (!in_array($type, ['post', 'term', 'user'])) {
            return $this->findWPItem($integrationID);
        }
        return match($type) {
            'post'  => $this->findWPPost($integrationID),
            'term'  => $this->findWPTerm($integrationID),
            'user'  => $this->findWPUser($integrationID),
            default => false
        };
    }
    public function findWPPost(string $integrationID):array|false
    {
        if (empty($integrationID)) {
            return false;
        }
        $get = new WP_Query([
            'meta_query'    => [
                'key'   => BASE."_{$this->service_name}_item_id",
                'value' => $integrationID,
            ],
            'fields'    => 'ids',
            'posts_per_page' => 1,
        ]);
        return $get->have_posts() && !empty($get->posts) ? [$get->posts[0], 'post'] : false;
    }
    public function findWPTerm(string $integrationID):array|false
    {
        if (empty($integrationID)) {
            return false;
        }
        $get = get_terms([
            'meta_query'    => [
                'key'   => BASE."_{$this->service_name}_item_id",
                'value' => $integrationID,
            ],
            'fields'    => 'ids',
            'hide_empty'=> true,
            'number'    => 1
        ]);
        return is_wp_error($get) || empty($get) ? false : [$get[0], 'term'];
    }
    public function findWPUser(string $integrationID):array|false
    {
        if (empty($integrationID)) {
            return false;
        }
        $get = get_users([
            'meta_query'    => [
                'key'   => BASE."_{$this->service_name}_item_id",
                'value' => $integrationID,
            ],
            'fields'    => 'ids',
            'number'    => 1,
        ]);
        return is_wp_error($get) || empty($get) ? false : [$get[0], 'user'];
    }
}