| | |
| | | class Facebook extends Integrations |
| | | { |
| | | // Facebook-specific properties |
| | | protected array $allowedContent = [ |
| | | 'post', |
| | | 'photo', |
| | | 'video', |
| | | 'event', |
| | | 'offer', |
| | | 'note', |
| | | 'milestone' |
| | | ]; |
| | | private string $page_id = ''; |
| | | private string $page_access_token = ''; |
| | | private array $permissions = []; |
| | |
| | | 'milestone' => 'milestones' |
| | | ]; |
| | | |
| | | 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 = 'facebook'; |
| | | $this->title = 'Facebook'; |
| | |
| | | */ |
| | | protected function handleTheSavePost(int $postID, WP_Post $post, bool $update, array $settings): void |
| | | { |
| | | $post_type = $settings['fb_type'] ?? 'post'; |
| | | $sync_immediately = $settings['immediate'] ?? false; |
| | | |
| | | // Determine the Facebook post type |
| | | $fb_endpoint = self::FB_POST_TYPES[$post_type] ?? 'feed'; |
| | | |
| | | $operation_data = [ |
| | | 'post_id' => $postID, |
| | | 'fb_type' => $post_type, |
| | | 'endpoint' => $fb_endpoint, |
| | | 'page_id' => $this->page_id |
| | | ]; |
| | | |
| | | // Check for scheduled posting |
| | | $schedule_time = get_post_meta($postID, BASE . 'schedule_facebook', true); |
| | | $options = []; |
| | | |
| | | if ($schedule_time && strtotime($schedule_time) > time()) { |
| | | $options['scheduled'] = strtotime($schedule_time); |
| | | } elseif (!$sync_immediately) { |
| | | $options['delay'] = 300; // 5 minute delay for batching |
| | | if (!$this->hasOAuthCredentials()) { |
| | | error_log('OAuth Not set up for '.$this->service_name); |
| | | return; |
| | | } |
| | | |
| | | $operation = $update ? 'update_post' : 'create_post'; |
| | | $this->queueOperation($operation, $operation_data, $options); |
| | | $this->queueOperation(self::$syncTo, [ |
| | | 'items' => [$postID], |
| | | 'user' => user_can($post->post_author, 'manage_options') ? null : $post->post_author |
| | | ], [ |
| | | 'delay' => 60 |
| | | ]); |
| | | Meta::forPost($postID)->set('_'.$this->service_name.'_sync_status', 'queued'); |
| | | } |
| | | |
| | | /** |
| | |
| | | public function processOperation(WP_Error|array $result, object $operation, array $data): WP_Error|array |
| | | { |
| | | try { |
| | | $fb = (array_key_exists('user', $data)) ? new self((int)$data['user']) : $this; |
| | | $fb = (array_key_exists('user', $data) && $data['user'] !== 0) ? $this->getInstance((int)$data['user']) : $this; |
| | | switch ($operation->type) { |
| | | case 'facebook_create_post': |
| | | return $fb->createFacebookPost($data); |