Jake Vanderwerf
5 days ago 0dfe1d8afafc59c4a5559c498342668d5a58d6ef
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
<?php
namespace JVBase\integrations;
 
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
 
if (!defined('ABSPATH')) {
    exit;
}
 
trait SyncToTerm
{
    use SyncTo, SyncHelpers, UserConnection;
    public function addSaveTerm():void
    {
        if (empty($this->syncTaxonomies)) {
            return;
        }
        if (!has_action('saved_term', [$this, 'handleSaveTerm'])) {
            add_action('saved_term', [$this, 'handleSaveTerm'], 20, 3);
        }
    }
    public function removeSaveTerm():void
    {
        if (empty($this->syncTaxonomies)) {
            return;
        }
        remove_action('saved_term', [$this, 'handleSaveTerm'], 20, 3);
    }
    protected function handleSaveTerm(int $termID, int $tt_id, string $taxonomy, bool $update, array $args):void
    {
        $tax = jvbNoBase($taxonomy);
        if (!in_array($tax, $this->syncTaxonomies)) {
            return;
        }
        $registrar = Registrar::getInstance($tax);
        if (!$registrar) {
            return;
        }
 
        $settings = $registrar->getIntegrationConfig($this->service_name);
        if (!$settings) {
            return;
        }
 
        $fields = $this->getSyncFields($termID, 'term');
        if (!$fields['share_to_'.$this->service_name]) {
            return;
        }
 
        $isShared = array_key_exists("_{$this->service_name}_item_id", $fields) && !empty($fields["_{$this->service_name}_item_id"]);
 
        if ($isShared && $update && !$fields['_keep_synced_'.$this->service_name]) {
            return;
        }
        $this->removeSaveTerm();
        $this->handleTheSaveTerm($termID, $update, $taxonomy, $settings);
        $this->addSaveTerm();
    }
 
    /**
     * @param int $termID
     * @param bool $update
     * @param array $settings
     * @return void
     */
    protected function handleTheSaveTerm(int $termID, bool $update, string $taxonomy, array $settings):void
    {
        error_log('==== ['.$this->title.']::handleTheSaveTerm ====');
        //TODO: Figure out some sort of permissions for if the user can share this term, particularly for content types
        //TODO: If this is a content type, it likely has its own integration. This is an edmonton.ink problem, so I'm offloading it for now
        $this->queueOperation(self::$syncTo, [
            'terms'   => [$termID],
            'user'      => get_current_user_id(),
        ], [
            'priority' => 'high',
            'delay'    => 30,
        ]);
        Meta::forTerm($termID)->set('_'.$this->service_name.'_sync_status', 'queued');
    }
 
    public function addDeleteTerm():void
    {
        if (!has_action('pre_delete_term', [$this, 'handleDeleteTerm'])) {
            add_action('pre_delete_term', [$this, 'handleDeleteTerm']);
        }
    }
    public function removeDeleteTerm():void
    {
        remove_action('pre_delete_term', [$this, 'handleDeleteTerm']);
    }
    public function handleDeleteTerm(int $termID, string $taxonomy):void
    {
        if (!$this->canSync['delete']) {
            return;
        }
        $tax = jvbNoBase($taxonomy);
        if (!in_array($tax, $this->syncTaxonomies)) {
            return;
        }
        $fields = $this->getSyncFields($termID, 'term');
        if (empty($fields["_{$this->service_name}_item_id"])) {
            return;
        }
        JVB()->queue()->add(
            self::$deleteFrom,
            0,
            [
                'fields'    => [$termID => $fields],
                'service'   => $this->service_name,
                'type'      => 'term'
            ]
        );
    }
}