<?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'
|
]
|
);
|
}
|
}
|