Jake Vanderwerf
10 days ago de317675a8069b747cb253ba3e2b5dc394ca36ef
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
<?php
namespace JVBase\integrations;
 
use Exception;
use JVBase\meta\Meta;
use JVBase\meta\Sanitizer;
use JVBase\registrar\Registrar;
use WP_Error;
use WP_Post;
use WP_Term;
use WP_User;
 
if (!defined('ABSPATH')) {
    exit;
}
 
trait SyncTo {
    use SyncHelpers, UserConnection;
    /**
     * Must be defined according to how each service needs it to be
     * @param int $itemID
     * @param string $type
     * @return array
     * @throws Exception
     */
    abstract protected function formatForService(int $itemID, string $type = 'post'):array;
 
    /**
     * Defaults to an array of formatted items. Can be overridden in child classes
     * @param array $itemIDs
     * @param string $type
     * @return array
     */
    protected function formatItems(array $itemIDs, string $type):array
    {
        $items = [];
        foreach ($itemIDs as $ID) {
            try {
                $items[] = $this->formatForService($ID, $type);
            } catch (Exception $e){
                $this->logError('formatItems', 'Could not format Item for service',[
                    'itemID' => $ID,
                    'type'  => $type,
                    'message'   => $e->getMessage(),
                ]);
            }
        }
        return $items;
    }
    /***************************************************************
     * Item creation
    ***************************************************************/
    public function createBatchToService($data):array
    {
        $type = $data['type']??'post';
 
        //Check if any of the submitted ids are already created
        $created = array_filter($data['items'],
            function($ID) use ($type) {
                return !empty($this->getServiceItemID($ID, $type));
            });
 
        $updated = [];
        if (!empty($created)) {
            $updateData = $data;
            $updateData['items'] = $created;
            $updated = $this->updateBatchToService($data);
 
            //remove any updated items from the original items to process
            $data['items'] = array_filter($data['items'], function ($ID) use ($created) {
                return !in_array($ID, $created);
            });
        }
 
        $items = $this->formatItems($data['items'], $type);
        if (empty($items)) {
            return $this->noCreatedItems($updated);
        }
 
        if ($this->hasBatchCreate) {
            $response = $this->sendBatchCreate($items);
            if (!is_wp_error($response)) {
                $result = $this->processBatchCreateResponse($data, $response);
            } else {
                $this->logError('createBatchToService','Batch create failed',[
                    'method'    => 'createBatchToService',
                    'item_ids'  => $data['items'],
                    'error'     => $response
                ]);
                $this->updateItemStatus($data['items'], 'error');
 
                $result = [
                    'outcome'   => 'failed_permanent',
                    'result'    => 'Could not update items'
                ];
            }
        } else {
            $success = $errors = [];
            foreach ($items as $item) {
                $itemResult = $this->createOne($item);
                if (!is_wp_error($itemResult)) {
                    $success[] = $itemResult;
                } else {
                    $errors[] = $itemResult;
                }
            }
            $result = [
                'outcome'   => empty($errors) ? 'success' : (empty($success) ? 'failed' : 'partial'),
                'result'    => [
                    'success'   => $success,
                    'errors'    => $errors
                ]
            ];
        }
 
        return array_merge($result, $updated);
    }
 
    /**
     * To be implemented by integration extension. Updates a single item
     * @param array $item
     * @return array|WP_Error
     */
    protected function createOne(array $item):array|WP_Error
    {
        return new WP_Error('invalid', 'Integration '.$this->service_name.' must implement its own createOne');
    }
 
    /**
     * Overridden by child classes
     * @param array $items
     * @return array|WP_Error
     */
    protected function sendBatchCreate(array $items):array|WP_Error
    {
        return new WP_Error('invalid', 'Integration '.$this->service_name.' must implement its own sendBatchCreate');
    }
 
    /**
     * To be implemented by extensions
     * @param array $data
     * @param array $response
     * @return array
     */
    protected function processBatchCreateResponse(array $data, array $response):array
    {
        return [
            'outcome'   => 'failed',
            'result'    => [
                'message'   => $this->service_name.' should implement processBatchCreateResponse.'
            ]
        ];
    }
 
    /*****************************************************************
     * ITEM UPDATING
    *****************************************************************/
    public function updateBatchToService(array $data):array
    {
        $type = $data['type']??'post';
 
        $newlyCreated = [];
        if (!$this->canCreateOnUpdate) {
            $created = array_filter($data['items'], function($ID) use ($type) {
                return !empty($this->getServiceItemID($ID, $type));
            });
 
            //Test to see if we have any that haven't been created yet.
            //For some services, items may have to be created before they can be updated
            if (count($created) !== count($data['items'])) {
                $notCreated = array_filter($data['items'], function($ID) use ($type) {
                    return empty($this->getServiceItemID($ID, $type));
                });
                $newData = $data;
                $newData['items'] = $notCreated;
                $newlyCreated = $this->createBatchToService($newData);
            }
 
            // If we don't have any that are created, just send the noUpdatedItems response, with any newly created items added
            if (empty($created)) {
                return $this->noUpdatedItems($newlyCreated);
            }
            $data['items'] = $created;
        }
 
 
        $items = $this->formatItems($data['items'], $type);
 
        if (empty($items)) {
            return $this->noUpdatedItems($newlyCreated);
        }
 
        if ($this->hasBatchUpdate) {
            $response = $this->sendBatchUpdate($items);
            if (!is_wp_error($response)) {
                $result = $this->processBatchUpdateResponse($data, $response);
            } else {
                $this->logError('updateBatchToService','Batch update failed',[
                    'method'    => 'updateBatchToService',
                    'item_ids'  => $data['items'],
                    'error'     => $response
                ]);
                $this->updateItemStatus($data['items'], 'error');
 
                $result = [
                    'outcome'   => 'failed_permanent',
                    'result'    => 'Could not update items'
                ];
            }
        } else {
            $success = $errors = [];
            //Does not have batch update, manually update each one
            foreach ($items as $item) {
                $itemResult = $this->updateOne($item);
                if (!is_wp_error($itemResult)) {
                    $success[] = $itemResult;
                } else {
                    $errors[] = $itemResult;
                }
            }
 
            $result = [
                'outcome'   => empty($errors) ? 'success' : (empty($success) ? 'failed' : 'partial'),
                'result'    => [
                    'success'   => $success,
                    'errors'    => $errors
                ]
            ];
        }
 
        return array_merge($result, $newlyCreated);
    }
 
    /**
     * To be implemented by integration extension. Updates a single item
     * @param array $item
     * @return array|WP_Error
     */
    protected function updateOne(array $item):array|WP_Error
    {
        return new WP_Error('invalid', 'Integration '.$this->service_name.' must implement its own updateOne method');
    }
 
    /**
     * Overridden by child classes
     * @param array $items
     * @return array|WP_Error
     */
    protected function sendBatchUpdate(array $items):array|WP_Error
    {
        return new WP_Error('invalid', 'Integration '.$this->service_name.' must implement its own sendBatchUpdate');
    }
 
 
    /**
     * To be implemented by extensions
     * @param array $data
     * @param array $response
     * @return array
     */
    protected function processBatchUpdateResponse(array $data, array $response):array
    {
        return [
            'outcome'   => 'failed',
            'result'    => [
                'message'   => $this->service_name.' should implement processBatchUpdateResponse.'
            ]
        ];
    }
    /*****************************************************************
     * UTILITY
    *****************************************************************/
    protected function noUpdatedItems(array $created):array
    {
        $result =  [
            'outcome'   => 'success',
            'result'    => [
                'message'   => 'No items to update',
                'updated'   => [],
                'errors'    => [],
            ]
        ];
 
        if (!empty($created)) {
            error_log('Result before: '.print_r($result, true));
            $result = array_merge($result, $created);
            error_log('Result after merge: '.print_r($result, true));
        }
        return $result;
    }
 
    protected function noCreatedItems(array $updated):array
    {
        $result = [
            'outcome'   => 'success',
            'result'    => [
                'message'   => 'No items to create',
                'created'   => [],
                'errors'    => [],
            ]
        ];
        if (!empty($updated)) {
            $result = array_merge($result, $updated);
        }
        return $result;
    }
 
    /****************************************************************
     * UTILITY
    ****************************************************************/
    protected function getSyncFields(int $itemID, string $type, array $additionalFields = []):array
    {
        $meta = match ($type) {
            'post'  => Meta::forPost($itemID),
            'term'  => Meta::forTerm($itemID),
            'user'  => Meta::forUser($itemID),
            default => false
        };
        if (!$meta) {
            return [];
        }
 
        $fields =  [
            'share_to_' . $this->service_name,
            '_keep_synced_' . $this->service_name,
            "_{$this->service_name}_item_id",
            "_{$this->service_name}_last_sync",
            "_{$this->service_name}_shared_at",
            "_{$this->service_name}_sync_status",
            "_{$this->service_name}_scheduled_at",
            ... $additionalFields
        ];
        return $meta->getAll($fields);
    }
    /****************************************************************
     * POST SYNC
    ****************************************************************/
    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'
            ]
        );
    }
    /****************************************************************
     * TERM SYNC
     ****************************************************************/
    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'
            ]
        );
    }
 
    /****************************************************************
     * USER SYNC
     ****************************************************************/
    public function addSaveUser():void
    {
        if (empty($this->syncUsers)) {
            return;
        }
        if (!has_action('profile_update', [$this, 'handleUpdateUser'])) {
            add_action('profile_update', [$this, 'handleUpdateUser'], 20, 1);
        }
        if (!has_action('user_register', [$this, 'handleUpdateUser'])) {
            add_action('user_register', [$this, 'handleUpdateUser'], 20, 1);
        }
    }
    public function removeSaveUser():void
    {
        if (empty($this->syncUsers)) {
            return;
        }
        remove_action('profile_update', [$this, 'handleUpdateUser'], 20);
        remove_action('user_register', [$this, 'handleUpdateUser'], 20);
    }
    protected function handleUpdateUser(int $userID):void
    {
        $user = $this->getOrCreateUser($userID);
        if (!$user) {
            return;
        }
 
        $fields = $this->getSyncFields($userID, 'user');
        if (!empty($fields["_{$this->service_name}_item_id"])) {
            return;
        }
 
        $this->removeSaveUser();
        $this->handleTheSaveUser($userID);
        $this->addSaveTerm();
    }
 
    protected function getOrCreateUser(int $userID):string|false
    {
        $user = get_userdata($userID);
        if (!$user || is_wp_error($user)) {
            return false;
        }
        $role = jvbUserRole($userID);
        if (!in_array(jvbNoBase($role), $this->syncUsers)) {
            return false;
        }
        $fields = $this->getSyncFields($userID, 'user');
        if (!empty($fields["_{$this->service_name}_item_id"])) {
            return $fields["_{$this->service_name}_item_id"];
        }
 
        $meta = Meta::forUser($userID);
        $serviceUserID = $this->searchServiceForUser($user->user_email??'');
        if ($serviceUserID) {
            $meta->set("{$this->service_name}_item_id", $serviceUserID);
            return $serviceUserID;
        }
 
        $created = $this->createServiceUser($userID, $fields);
        if ($created) {
            return $created;
        }
        return false;
    }
    public function searchServiceForUser(string $email):string|false
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            return false;
        }
        return $this->handleEmailSearch($email);
    }
 
    /**
     * Searches for existing user from sanitized email
     * @param string $email
     * @return string|false The found service's User ID or false on failure
     */
    abstract public function handleEmailSearch(string $email):string|false;
 
    public function createServiceUser(int $userID, array $fields):string|false
    {
        $checked = $this->validateUserFields($userID, $fields);
        $response = $this->handleCreateUser($checked);
        if ($response['success']) {
            $meta = Meta::forUser($userID);
            $meta->set("{$this->service_name}_item_id", $response['result']['id']);
        }
        return $response['success'] ? $response['customer']??false : false;
    }
 
    protected function validateUserFields(int $userID, array $fields):array|false
    {
        foreach ($fields as $f => $v) {
            $v = match ($f) {
                'email' => filter_var($v, FILTER_SANITIZE_EMAIL),
                'phone' => Sanitizer::sanitizePhone($v),
                default => sanitize_text_field($v),
            };
            if (empty($v) && in_array($f, $this->requiredUserFields())) {
                return false;
            }
            $fields[$f] = $v;
        }
        return $fields;
    }
    protected function requiredUserFields():array
    {
        return [];
    }
 
    /**
     * @param array $data User Data
     * @return array The created user ID or false on failure
     */
        abstract protected function handleCreateUser(array $data):array;
 
    /**
     * @param int $userID
     * @return void
     */
    protected function handleTheSaveUser(int $userID):void
    {
        error_log('==== ['.$this->title.']::handleTheSaveUser ====');
        //TODO: This is likely only for stuff like customers.
        //If we have multiple stores connected, we may have to queue operations to update every connection's customer account if they made an order with that store
        $role = jvbUserRole($userID);
        $registrar = Registrar::getInstance($role);
        if (!$registrar || !$registrar->hasIntegration($this->service_name)) {
            return;
        }
 
        $this->queueOperation(self::$syncTo, [
            'users' => [$userID],
        ]);
    }
 
    public function addDeleteUser():void
    {
        if (!has_action('delete_user', [$this, 'handleDeleteTerm'])) {
            add_action('delete_user', [$this, 'handleDeleteTerm']);
        }
    }
    public function removeDeleteUser():void
    {
        remove_action('delete_user', [$this, 'handleDeleteTerm']);
    }
    public function handleDeleteUser(int $userID):void
    {
        if (!$this->canSync['delete']) {
            return;
        }
 
        $fields = $this->getSyncFields($userID, 'user');
        if (empty($fields["_{$this->service_name}_item_id"])) {
            return;
        }
        JVB()->queue()->add(
            self::$deleteFrom,
            0,
            [
                'fields'    => [$userID => $fields],
                'service'   => $this->service_name,
                'type'      => 'user'
            ]
        );
    }
}