Your IP : 216.73.216.68


Current Path : /home/jeromecohp/www/plugins/system/yooessentials/modules/api/src/Facebook/
Upload File :
Current File : /home/jeromecohp/www/plugins/system/yooessentials/modules/api/src/Facebook/FacebookApi.php

<?php
/**
 * @package   Essentials YOOtheme Pro (Freemium) 2.0.19 build 1026.0920
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials\Api\Facebook;

class FacebookApi extends FacebookBaseApi implements FacebookApiInterface
{
    const TYPE_PROFILE = 'profile';
    const TYPE_UPLOADED = 'uploaded';
    const DEFAULT_LIMIT = 20;

    // prettier-ignore
    const PAGE_FIELDS = ['id', 'name', 'username', 'link', 'website', 'about', 'category', 'whatsapp_number', 'description', 'description_html', 'general_info', 'overall_star_rating', 'rating_count', 'followers_count', 'talking_about_count', 'birthday', 'personal_interests', 'affiliation'];

    // prettier-ignore
    const POST_FIELDS = ['id', 'parent_id', 'created_time', 'updated_time', 'from{name}', 'actions', 'is_expired', 'is_hidden', 'is_popular', 'is_published', 'full_picture', 'reactions.summary(true)', 'comments.summary(true)', 'message', 'message_tags{name}', 'permalink_url', 'likes.summary(true)', 'shares'];

    // prettier-ignore
    const REVIEW_FIELDS = ['created_time', 'has_rating', 'has_review', 'rating', 'recommendation_type', 'review_text', 'reviewer{id,first_name,last_name,middle_name,name,name_format,picture,short_name}'];

    // prettier-ignore
    const PHOTO_FIELDS = ['id', 'name', 'album', 'width', 'from', 'height', 'link', 'images', 'event', 'name_tags{name}', 'place', 'alt_text', 'alt_text_custom', 'backdated_time', 'created_time', 'target', 'updated_time', 'comments.summary(true)', 'likes.summary(true)', 'shares'];

    public function page(string $pageId): array
    {
        return $this->get($pageId, [
            'fields' => implode(',', self::PAGE_FIELDS),
        ]);
    }

    public function pages(string $userId): array
    {
        $accounts = $this->get("$userId/accounts", ['limit' => 100]);

        $pages = array_map(function ($page) {
            $pageId = $page['id'] ?? '';

            if (!$pageId) {
                return null;
            }

            // You can't manage the page, so no sense displaying it
            $token = $page['access_token'] ?? null;
            if (!$token) {
                return null;
            }

            return [
                'id' => $pageId,
                'name' => $page['name'] ?? null,
            ];
        }, $accounts['data'] ?? []);

        return array_values(array_filter($pages));
    }

    public function posts(string $userOrPageId, array $filters = []): array
    {
        $this->withAccessToken($this->getPageAccessToken($userOrPageId));

        $result = $this->get("{$userOrPageId}/feed", [
            'fields' => implode(',', self::POST_FIELDS),
            'limit' => $filters['limit'] ?? self::DEFAULT_LIMIT,
        ]);

        return $result['data'] ?? [];
    }

    public function reviews(string $userOrPageId, array $filters = []): array
    {
        $this->withAccessToken($this->getPageAccessToken($userOrPageId));

        $result = $this->get("{$userOrPageId}/ratings", [
            'fields' => implode(',', self::REVIEW_FIELDS),
            'limit' => $filters['limit'] ?? self::DEFAULT_LIMIT,
        ]);

        return $result['data'] ?? [];
    }

    public function photos(string $userOrPageId, array $filters = []): array
    {
        $this->withAccessToken($this->getPageAccessToken($userOrPageId));

        $result = $this->get("{$userOrPageId}/photos", [
            'fields' => implode(',', self::PHOTO_FIELDS),
            'limit' => $filters['limit'] ?? self::DEFAULT_LIMIT,
            'type' => $filters['type'] ?? self::TYPE_UPLOADED,
        ]);

        return $result['data'] ?? [];
    }

    public function getPageAccessToken(string $pageId): ?string
    {
        return $this->get($pageId, ['fields' => 'access_token'])['access_token'] ?? null;
    }
}