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