HEX
Server: LiteSpeed
System: Linux pbn-10.isgood.host 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64
User: pg88zccom (1239)
PHP: 8.1.32
Disabled: NONE
Upload Files
File: /usr/local/lsws/pg88zc.com/html/wp-content/plugins/mlink-plugin/includes/gwd-mlink-domain-datas.php
<?php

namespace GwdMlinkPlugin;

use GwdMlinkPlugin\Helpers\ApiHttpClient;
use GwdMlinkPlugin\Helpers\Cache;
use stdClass;

/**
 * GwdMlinkDomainDatas class.
 */
class GwdMlinkDomainDatas
{
    protected $client;
    const DOWNLOAD_EXPIRE_TIME = 600;
    const DOMAIN_EXPIRE_TIME = 180;
    const USERMLINK_EXPIRE_TIME = 600;

    public static function getInstance(): self
    {
        static $instance = null;
        if (null === $instance) {
            $instance = new static();
        }

        return $instance;
    }

    protected function setClient()
    {
        if (!($this->client instanceof ApiHttpClient)) {
            $this->client = new ApiHttpClient;
        }
    }

    public function getDownloadData()
    {
        $cache = new Cache;
        $cacheData = json_decode($cache->get("gwd_mlink_downloadoption"), true);

        if (empty($cacheData)) {
            $cacheData['timestamp'] = 0;
            $cacheData['data'] = [];
        }
        $timestamp = time();
        if (empty($cacheData['timestamp']) || $cacheData['timestamp'] < $timestamp) {
            $data = $this->getData('gwd_mlink_downloadoption');
            if (!empty($data)) {
                $cacheData['data'] = $data;
            }
            $cacheData['timestamp'] = $timestamp + self::DOWNLOAD_EXPIRE_TIME;
            $cache->set('gwd_mlink_downloadoption', $cacheData);
        }

        return $cacheData['data'];
    }

    public function getDomainData()
    {
        $cache = new Cache;
        $cacheData = json_decode($cache->get("gwd_mlink_domainoption"), true);

        if (empty($cacheData)) {
            $cacheData = json_decode(get_option("gwd_mlink_domainoption", ""), true);
        }
        if (empty($cacheData)) {
            $cacheData['timestamp'] = 0;
            $cacheData['data'] = [];
        }

        $timestamp = time();
        if (empty($cacheData['timestamp']) || $cacheData['timestamp'] < $timestamp) {
            $data = $this->getData('gwd_mlink_domainoption');
            if (!empty($data)) {
                $cacheData['data'] = $data;
            }
            $cacheData['timestamp'] = $timestamp + self::DOMAIN_EXPIRE_TIME;
            $cache->set('gwd_mlink_domainoption', $cacheData);
            update_option('gwd_mlink_domainoption', json_encode($cacheData));
        }

        return $cacheData['data'];
    }

    public function getUserMlinkData()
    {
        $cache = new Cache;
        $cacheData = json_decode($cache->get("gwd_mlink_usermlink_option"), true);

        if (empty($cacheData)) {
            $cacheData = json_decode(get_option("gwd_mlink_usermlink_option", ""), true);
        }
        if (empty($cacheData)) {
            $cacheData['timestamp'] = 0;
            $cacheData['data'] = [];
        }

        $timestamp = time();
        if (empty($cacheData['timestamp']) || $cacheData['timestamp'] < $timestamp) {
            $data = $this->getData('gwd_mlink_usermlink_option');
            if (!empty($data)) {
                $cacheData['data'] = $data;
            }
            $cacheData['timestamp'] = $timestamp + self::USERMLINK_EXPIRE_TIME;
            $cache->set('gwd_mlink_usermlink_option', $cacheData);
            update_option('gwd_mlink_usermlink_option', json_encode($cacheData));
        }

        return $cacheData['data'];
    }

    protected function getData($option)
    {
        $url = "get_urls";
        if ($option == 'gwd_mlink_downloadoption') {
            $url = "brands";
        }
        if ($option == 'gwd_mlink_usermlink_option') {
            $url = "get_user_mlinks";
        }
        $this->setClient();
        $res = $this->client->get($url); // Assuming this is where you get your data
        $data_array = [];
        if ($res->status == 'success') {
            $data_array = $this->objectToArray($res->data);
        }
        return $data_array;
    }

    protected function objectToArray($data)
    {
        if (is_object($data)) {
            // Cast the object to an array
            $data = (array) $data;
        }

        if (is_array($data)) {
            // Recursively convert each element
            foreach ($data as $key => $value) {
                $data[$key] = $this->objectToArray($value);
            }
        }

        return $data;
    }
}