MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.123.2.lve.el8.x86_64 #1 SMP Thu May 7 23:17:13 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //home/cafsindia/www/wp-content/plugins/imageseo/src/Services/ClientApi.php
<?php

namespace ImageSeoWP\Services;

if (!defined('ABSPATH')) {
    exit;
}

use ImageSeo\Client\Client;

class ClientApi
{
    protected $client = null;

    public function __construct()
    {
        $this->optionService = imageseo_get_service('Option');
    }

    public function getHeaders($apiKey = null)
    {
        $headers = [
            'Content-Type' => 'application/json',
        ];

        if ($apiKey) {
            $headers['Authorization'] = $apiKey;
        } else {
            $headers['Authorization'] = $this->optionService->getOption('api_key');
        }

        return $headers;
    }

    /**
     * @return ImageSeo\Client\Client
     */
    public function getClient($apiKey = null)
    {
        if (null === $apiKey) {
            $apiKey = $this->optionService->getOption('api_key');
        }

        $options = [];
        if (defined('IMAGESEO_API_URL')) {
            $options['host'] = IMAGESEO_API_URL;
        }

        if ($this->client) {
            return $this->client;
        }

        $this->client = new Client($apiKey, $options);

        return $this->client;
    }

    /**
     * @param string $apiKey
     *
     * @return array
     */
    public function getOwnerByApiKey()
    {
        if (!$this->optionService->getOption('api_key')) {
            return null;
        }

        try {
            $response = wp_remote_get(IMAGESEO_API_URL . '/v1/external/projects/owner', [
                'headers' => $this->getHeaders(),
                'timeout' => 50,
            ]);
            $body = json_decode(wp_remote_retrieve_body($response), true);
        } catch (\Exception $e) {
            return null;
        }
        if (!$body['success']) {
            return null;
        }

        return $body['result'];
    }

    /**
     * @param string $apiKey
     *
     * @return array
     */
    public function validateApiKey($apiKey = null)
    {
        try {
            $headers = $this->getHeaders();
            $headers['Authorization'] = $apiKey;

            $response = wp_remote_get(IMAGESEO_API_URL . '/v1/external/projects/owner', [
                'headers' => $headers,
                'timeout' => 50,
            ]);
            $body = json_decode(wp_remote_retrieve_body($response), true);
        } catch (\Exception $e) {
            return null;
        }
        if (!$body['success']) {
            return null;
        }

        return $body['result'];
    }

    /**
     * @return array
     */
    public function getLanguages()
    {
        $apiKey = $this->optionService->getOption('api_key');

        $response = $this->getClient($apiKey)->getResource('Languages')->getLanguages();

        if (!$response['success']) {
            return null;
        }

        return $response['result'];
    }

    /**
     * @param array $data
     *
     * @return Image
     */
    public function generateSocialMediaImage($data)
    {
        if (!$this->optionService->getOption('api_key')) {
            return null;
        }

        list($body) = $this->getClient()->getResource('SocialMedia')->generateSocialMediaImage($data);

        return $body;
    }
}