| | |
| | | |
| | | } |
| | | |
| | | protected function getTermFieldMapping(string $taxonomy): array |
| | | { |
| | | return apply_filters( |
| | | "jvb_{$this->service_name}_term_field_mapping", |
| | | [], |
| | | $taxonomy, |
| | | $this |
| | | ); |
| | | } |
| | | |
| | | /** |
| | | * Share post to external service (override in child classes) |
| | | */ |
| | | protected function sharePost(WP_Post $post): ?string |
| | | { |
| | | throw new Exception('sharePost must be implemented by child class'); |
| | | } |
| | | |
| | | /** |
| | | * Update shared post on external service (override in child classes) |
| | | */ |
| | | protected function updateSharedPost(WP_Post $post, string $external_id): void |
| | | { |
| | | throw new Exception('updateSharedPost must be implemented by child class'); |
| | | } |
| | | |
| | | /** |
| | | * Remove post from external service (override in child classes) |
| | | */ |
| | | protected function unsharePost(WP_Post $post, string $external_id): void |
| | | { |
| | | throw new Exception('unsharePost must be implemented by child class'); |
| | | } |
| | | |
| | | /** |
| | | * Get sync capabilities |
| | | */ |
| | | public function getSyncCapabilities(): array |
| | | { |
| | | return $this->canSync; |
| | | } |
| | | |
| | | /** |
| | | * Check if a specific sync capability is supported |
| | | */ |
| | | public function supportsSyncCapability(string $capability): bool |
| | | { |
| | | return $this->canSync[$capability] ?? false; |
| | | } |
| | | |
| | | /** |
| | | * Get posts shared to this service |
| | | */ |
| | | public function getSharedPosts(array $args = []): array |
| | | { |
| | | $defaults = [ |
| | | 'post_type' => $this->syncPostTypes, |
| | | 'meta_query' => [ |
| | | [ |
| | | 'key' => BASE."_{$this->service_name}_item_id", |
| | | 'compare' => 'EXISTS' |
| | | ] |
| | | ], |
| | | 'posts_per_page' => -1 |
| | | ]; |
| | | |
| | | $args = wp_parse_args($args, $defaults); |
| | | return get_posts($args); |
| | | } |
| | | |
| | | /** |
| | | * Check if post is shared to this service |
| | | */ |
| | | public function isPostShared(int $postID): bool |
| | | { |
| | | $external_id = get_post_meta($postID, BASE."_{$this->service_name}_item_id", true); |
| | | return $external_id !== ''; |
| | | } |
| | | |
| | | /** |
| | | * Get external ID for a post |
| | | */ |
| | | public function getPostExternalId(int $postID): ?string |
| | | { |
| | | $external_id = get_post_meta($postID, BASE."_{$this->service_name}_item_id", true); |
| | | return $external_id ?: null; |
| | | } |
| | | |
| | | /******************************************************************* |
| | | * UTILITIES |
| | |
| | | return strtolower($this->service_name); |
| | | } |
| | | |
| | | /** |
| | | * Refresh credentials from storage and re-initialize |
| | | */ |
| | | public function refreshCredentials(): void |
| | | { |
| | | $this->credentials = []; |
| | | $this->ensureInitialized(); |
| | | } |
| | | |
| | | /** |
| | | * Enhanced webhook handling with common patterns |
| | |
| | | /********************************************************************************* |
| | | * Syncing WP items from Service |
| | | *********************************************************************************/ |
| | | /** |
| | | * @param array $itemData |
| | | * @return bool |
| | | * @throws Exception |
| | | */ |
| | | protected function createItemFromService(array $itemData):bool |
| | | { |
| | | throw new Exception('createItemFromService must be implemented by child class '.$this->service_name); |
| | | } |
| | | |
| | | /** |
| | | * @param array $itemData |
| | | * @return bool |
| | | * @throws Exception |
| | | */ |
| | | protected function updateItemFromService(array $itemData):bool |
| | | { |
| | | throw new Exception('updateItemFromService must be implemented by child class '.$this->service_name); |
| | | } |
| | | |
| | | /** |
| | | * @param array $itemData |
| | | * @return bool |
| | | * @throws Exception |
| | | */ |
| | | protected function deleteItemFromService(array $itemData):bool |
| | | { |
| | | throw new Exception('deleteItemFromService must be implemented by child class '.$this->service_name); |
| | | } |
| | | |
| | | /**************************************************************** |
| | | * CONNECT to a user's integration |
| | | ****************************************************************/ |
| | | protected array $connections = []; |
| | | public function connectAs(?int $userID = null):bool |
| | | { |
| | | if (!$this->userCanConnect($userID)) { |
| | | return false; |
| | | } |
| | | $this->cleanup(); |
| | | $this->userID = $userID; |
| | | $this->ensureInitialized(); |
| | | return true; |
| | | } |
| | | |
| | | public function userCanConnect(?int $userID):bool |
| | | { |
| | | $user = get_userdata($userID); |
| | | if (!$user || is_wp_error($user)) { |
| | | return false; |
| | | } |
| | | if (current_user_can('manage_options')) { |
| | | return true; |
| | | } |
| | | $role = jvbUserRole($userID); |
| | | $registrar = Registrar::getInstance($role); |
| | | if (!$registrar || !$registrar->hasIntegration($this->service_name)) { |
| | | return false; |
| | | } |
| | | |
| | | return (bool) Meta::forUser($userID)->get('integration_'.$this->service_name.'_connected'); |
| | | } |
| | | |
| | | protected function cleanup():void |
| | | { |
| | | $this->userID = false; |
| | | $this->credentials = []; |
| | | $this->request_history = []; |
| | | $this->resetTokenRefreshFlag(); |
| | | } |
| | | } |