| | |
| | | <?php |
| | | namespace JVBase\integrations; |
| | | |
| | | use JVBase\inc\managers\ErrorHelper; |
| | | use JVBase\managers\Cache; |
| | | use JVBase\managers\Errors; |
| | | use JVBase\managers\IconsManager; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | |
| | | } |
| | | |
| | | trait _Base { |
| | | use ErrorHelper; |
| | | /** |
| | | * @var string Unique identifier for this service (ex: square, facebook) |
| | | */ |
| | |
| | | */ |
| | | protected string $apiVersion; |
| | | protected Cache $cache; |
| | | //Use Trait "Requests" to set this |
| | | protected bool $hasRequests = false; |
| | | protected ?RateLimits $requestLimiter = null; |
| | | //Use Trait "OAuth" to set this |
| | | protected ?RateLimits $oauthLimiter = null; |
| | | protected bool $hasOAuth = false; |
| | | //Use trait "Webhooks" to set this true |
| | | protected bool $hasWebhooks = false; |
| | | protected ?string $cacheName = null; |
| | | |
| | | |
| | | /** |
| | | * @var array Each item in the array becomes a li element in the ordered list of instructions |
| | |
| | | * @var bool Flag to indicate there are extra options that warrant a dedicated options page for this service |
| | | * Usually used for default field values for syncable services |
| | | */ |
| | | public bool $hasExtraOptions = false; |
| | | public static bool $hasExtraOptions = false; |
| | | public bool $hasBatchCreate = false; |
| | | public bool $hasBatchUpdate = false; |
| | | public bool $hasBatchDelete = false; |
| | | public bool $canCreateOnUpdate = false; |
| | | /** |
| | | * Post Syncing Capabilities |
| | | * Define what sync operations this integration supports |
| | | */ |
| | | protected array $canSync = [ |
| | | 'initial' => false, // Can share new posts to the service |
| | | 'update' => false, // Can update already-shared posts |
| | | 'delete' => false, // Can remove posts from the service |
| | | ]; |
| | | |
| | | /** |
| | | * @var array // Post types that can be synced (e.g., ['artwork', 'tattoo']): usually built by Registrar.php if the integration name exists as a key in [ 'integrations' => []] |
| | | */ |
| | | protected array $syncPostTypes = []; |
| | | /** |
| | | * @var array Taxonomies that can be synced (e.g., ['category', 'section']): usually built by Registrar.php if the integration name exists as a key in [ 'integrations' => []] |
| | | */ |
| | | protected array $syncTaxonomies = []; |
| | | /** |
| | | * @var array User roles that can be synced (e.g., ['customer']): usually built by Registrar.php if the integration name exists as a key in [ 'integrations' => []] |
| | | */ |
| | | protected array $syncUsers = []; |
| | | /** |
| | | * @var array Integration's specific content types. An array of [ TYPE => [$fields] ] Set by child classes' setContentTypes |
| | | */ |
| | | protected array $contentTypes = []; |
| | | |
| | | protected array $credentials; |
| | | |
| | | |
| | | /** |
| | |
| | | */ |
| | | public function isSetUp(): bool |
| | | { |
| | | return !empty($this->loadCredentials()); |
| | | $tmp = null; |
| | | if ($this->userID) { |
| | | $tmp = $this->userID; |
| | | $this->switchUser(); |
| | | } |
| | | $isSetUp = !empty($this->loadCredentials()??[]); |
| | | if ($tmp) { |
| | | $this->switchUser($tmp); |
| | | } |
| | | return $isSetUp; |
| | | } |
| | | |
| | | public function loadCredentials():array |
| | | { |
| | | return Auth::getInstance()->getCredentials($this->service_name, $this->userID); |
| | | if (!isset($this->credentials)) { |
| | | $this->credentials = Auth::getInstance()->getCredentials($this->service_name, $this->userID); |
| | | } |
| | | return $this->credentials; |
| | | } |
| | | |
| | | protected function logError(string $method, string $message, array $context = []):void |
| | | { |
| | | |
| | | $context['service'] = $this->service_name; |
| | | JVB()->error()->log('['.self::class.']'.$method, $message, $context); |
| | | Errors::log($message, array_merge( |
| | | [ |
| | | 'service' => $this->service_name, |
| | | 'method' => $method, |
| | | 'class' => static::class, |
| | | ], |
| | | $context |
| | | )); |
| | | } |
| | | |
| | | protected function getReturnURL():string |
| | |
| | | { |
| | | if (is_admin()) { |
| | | $icons = IconsManager::for('admin'); |
| | | return $icons->getRawSvg($icon); |
| | | return $icons->getRawSvg($icon)??''; |
| | | } |
| | | return jvbDashIcon($icon); |
| | | } |
| | | |
| | | protected function initializeRateLimiters():void |
| | | { |
| | | if (method_exists($this, 'initializeRequestLimiter')) { |
| | | $this->initializeRequestLimiter(); |
| | | } |
| | | if (method_exists($this, 'initializeOAuthRateLimiter')) { |
| | | $this->initializeOAuthRateLimiter(); |
| | | } |
| | | } |
| | | |
| | | public function response(bool $success, string $message, array $data = []):array |
| | | { |
| | | $response = [ |
| | | 'success' => $success, |
| | | 'message' => $message |
| | | ]; |
| | | if (!empty($data)) { |
| | | $response['data'] = $data; |
| | | } |
| | | return $response; |
| | | } |
| | | |
| | | public function getAllowedContent():array |
| | | { |
| | | return array_keys($this->contentTypes); |
| | | } |
| | | |
| | | public function getServiceName():string |
| | | { |
| | | return $this->service_name; |
| | | } |
| | | } |