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
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
<?php
namespace JVBase\integrations;
 
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
use WP_Post;
 
if (!defined('ABSPATH')) {
    exit;
}
 
trait SyncToPost
{
    use SyncTo, SyncHelpers, UserConnection;
 
    public function addSavePost():void
    {
        if (!has_action('save_post', [$this, 'handleSavePost'])) {
            add_action('save_post', [$this, 'handleSavePost'], 20, 3);
        }
    }
    public function removeSavePost():void
    {
        remove_action('save_post', [$this, 'handleSavePost'], 20, 3);
    }
    public function handleSavePost(int $postID, WP_Post $post, bool $update):void
    {
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
        if (wp_is_post_revision($postID)) return;
 
        error_log('=== ['.$this->service_name.']::handleSavePost called');
 
        $postType = jvbNoBase($post->post_type);
        if (!in_array($postType, $this->syncPostTypes)) {
            error_log('Not handling save for '.$this->service_name.' because there are no syncPostTypes: '.print_r($this->syncPostTypes, true));
            return;
        }
 
        $registrar = Registrar::getInstance($postType);
        //Should not happen, as syncPostTypes is defined by Registrar instances
        if (!$registrar) {
            return;
        }
 
        $settings = $registrar->getIntegrationConfig($this->service_name);
        if (!$settings) {
            error_log('Not handling save for '.$this->service_name.' because of no integration config '.print_r($settings, true));
            return;
        }
 
        $fields = $this->getSyncFields($postID, 'post');
        if (!$fields['share_to_'.$this->service_name]) {
            error_log('Not handling save for '.$this->service_name.' because of no share_to_'.$this->service_name.' '.print_r($fields, true));
            return;
        }
 
        $isShared = array_key_exists("_{$this->service_name}_item_id", $fields) && !empty($fields["_{$this->service_name}_item_id"]);
 
        if ($post->post_status !== 'publish' && !$isShared) {
            error_log('Not handling save for '.$this->service_name.' because post status is not publish, and it is not already shared.');
            return;
        }
 
        if ($isShared && $update && !$fields['_keep_synced_'.$this->service_name]) {
            error_log('Not handling save for '.$this->service_name.' because it is already shared, and not set to keep synced. ');
            return;
        }
        error_log('==== Sending to integration\'s handleTheSavePost '.$this->service_name.' ====');
        $this->removeSavePost();
        $this->handleTheSavePost($postID, $post, $update, $settings);
        $this->addSavePost();
    }
    /**
     * Handle post save for syncing
     *
     * Override to implement custom sync logic when posts are saved.
     * Check the $settings array for post type specific configuration.
     *
     * @param int $postID The post ID
     * @param WP_Post $post The post object
     * @param bool $update Whether this is an update
     * @param array $settings Post type integration settings
     * @return void
     */
    protected function handleTheSavePost(int $postID, WP_Post $post, bool $update, array $settings):void
    {
        error_log('==== ['.$this->title.']::handleTheSavePost ====');
        $this->queueOperation(self::$syncTo, [
            'posts'   => [$postID],
            'user'      => user_can($post->post_author, 'manage_options') ? null : $post->post_author
        ], [
            'priority' => 'high',
            'delay'    => 30,
        ]);
        Meta::forPost($postID)->set('_'.$this->service_name.'_sync_status', 'queued');
    }
 
    public function addDeletePost():void
    {
        if (!has_action('before_delete_post', [$this, 'handleDeletePost'])) {
            add_action('before_delete_post', [$this, 'handleDeletePost'], 20, 3);
        }
    }
    public function removeDeletePost():void
    {
        remove_action('before_delete_post', [$this, 'handleSavePost'], 20, 3);
    }
    public function handleDeletePost(int $postID):void
    {
        if (!$this->canSync['delete']) {
            return;
        }
        $postType = get_post_type($postID);
        if (!in_array(jvbNoBase($postType), $this->syncPostTypes)) {
            return;
        }
        $fields = $this->getSyncFields($postID, 'post');
        if (empty($fields["_{$this->service_name}_item_id"])) {
            return;
        }
        $post = get_post($postID);
        if (!$post) {
            return;
        }
        $userID = $this->determineUserID($post->post_author);
        if (!$userID) {
            return;
        }
 
        JVB()->queue()->add(
            self::$deleteFrom,
            $userID,
            [
                'fields'    => [$postID => $fields],
                'service'   => $this->service_name,
                'type'      => 'post'
            ]
        );
    }
}