| | |
| | | namespace JVBase\integrations; |
| | | |
| | | use JVBase\integrations\Integrations; |
| | | use JVBase\meta\Meta; |
| | | use WP_Error; |
| | | use WP_REST_Request; |
| | | use WP_REST_Response; |
| | |
| | | |
| | | class Instagram extends Integrations |
| | | { |
| | | protected array $allowedContent = [ |
| | | 'post' |
| | | ]; |
| | | private string $ig_user_id = ''; |
| | | private string $page_id = ''; |
| | | private string $page_access_token = ''; |
| | |
| | | private const GRAPH_API_BASE = 'https://graph.facebook.com/'; |
| | | private const INSTAGRAM_BASE = 'https://graph.instagram.com/'; |
| | | |
| | | public function __construct(?int $userID = null) |
| | | protected static array $instances = []; |
| | | public static function getInstance(?int $userID = null):self |
| | | { |
| | | $key = is_null($userID) ? 'base' : $userID; |
| | | if (!array_key_exists($key, self::$instances)) { |
| | | self::$instances[$key] = new self($userID); |
| | | } |
| | | |
| | | return self::$instances[$key]; |
| | | } |
| | | |
| | | protected function __construct(?int $userID = null) |
| | | { |
| | | $this->service_name = 'instagram'; |
| | | $this->title = 'Instagram'; |
| | | $this->icon = 'instagram'; |
| | | $this->icon = 'instagram-logo'; |
| | | $this->apiBase = [ |
| | | 'graph' => self::GRAPH_API_BASE . self::API_VERSION, |
| | | 'instagram' => self::INSTAGRAM_BASE . self::API_VERSION |
| | |
| | | */ |
| | | public function handleTheSavePost(int $postID, \WP_Post $post, bool $update, array $settings): void |
| | | { |
| | | // Check if post has featured image (Instagram requirement) |
| | | |
| | | if (!$this->hasOAuthCredentials()) { |
| | | error_log('OAuth Not set up for '.$this->service_name); |
| | | return; |
| | | } |
| | | if (!has_post_thumbnail($postID)) { |
| | | $this->logError('Cannot post to Instagram without featured image', [ |
| | | 'post_id' => $postID |
| | | ]); |
| | | return; |
| | | } |
| | | |
| | | // Queue the Instagram post creation |
| | | $this->queueOperation('create_post', [ |
| | | 'post_id' => $postID, |
| | | 'type' => get_post_type($postID) |
| | | $this->queueOperation(self::$syncTo, [ |
| | | 'items' => [$postID], |
| | | 'user' => user_can($post->post_author, 'manage_options') ? null : $post->post_author |
| | | ], [ |
| | | 'priority' => 'normal', |
| | | 'delay' => 60 // Wait 1 minute to ensure all metadata is saved |
| | | 'delay' => 60 |
| | | ]); |
| | | |
| | | update_post_meta($postID, BASE . '_instagram_sync_status', 'queued'); |
| | | Meta::forPost($postID)->set('_'.$this->service_name.'_sync_status', 'queued'); |
| | | } |
| | | |
| | | /** |