<?php

/**
 * @copyright Copyright (c) 2009-2022 ThemeCatcher (https://www.themecatcher.net)
 */
class Quform_Updater
{
    /**
     * How long to cache the latest version information
     * @var int
     */
    const CACHE_TIME = 43200; // 12 hours

    /**
     * @var Quform_Api
     */
    protected $api;

    /**
     * @var Quform_License
     */
    protected $license;

    public function __construct(Quform_Api $api, Quform_License $license)
    {
        $this->api = $api;
        $this->license = $license;
    }

    /**
     * Add the update information to the transient object if an update exists
     *
     * @param  object $transient
     * @return object
     */
    public function setUpdateTransient($transient)
    {
        if (empty($transient) || !is_object($transient)) {
            return $transient;
        }

        unset($transient->response[QUFORM_BASENAME]);

        $transient->no_update[QUFORM_BASENAME] = (object) array(
            'id' => QUFORM_NAME,
            'slug' => QUFORM_NAME,
            'plugin' => QUFORM_BASENAME,
            'new_version' => QUFORM_VERSION,
            'url' => 'https://www.quform.com/',
            'package' => ''
        );

        return $transient;
    }

    /**
     * Validate the request to check for an update
     */
    protected function validateCheckForUpdateRequest()
    {
        if ( ! current_user_can('quform_settings')) {
            wp_send_json(array(
                'type'    => 'error',
                'message' => __('Insufficient permissions', 'quform')
            ));
        }

        if ( ! check_ajax_referer('quform_manual_update_check', false, false)) {
            wp_send_json(array(
                'type'    => 'error',
                'message' => __('Nonce check failed', 'quform')
            ));
        }
    }

    /**
     * Checks the for the latest version information for the Settings page
     */
    public function checkForUpdate()
    {
        $this->validateCheckForUpdateRequest();

        wp_send_json(array(
            'type' => 'success',
            'message' => __('You are using the latest version.', 'quform')
        ));
    }

    /**
     * Get the latest version information
     *
     * @param   boolean $cache    Whether the information should be fetched from the cache if available
     * @return  boolean|StdClass  The version information object or false on failure
     */
    protected function getLatestVersionInfo($cache = true)
    {
        return false;
    }

    /**
     * Get plugin information
     *
     * @param   boolean         $false   Returned if the method call is not for this action or plugin
     * @param   string          $action  The action to perform
     * @param   object          $args    The current plugin data
     * @return  boolean|object           The plugin information data or the $false argument
     */
    public function pluginInformation($false, $action, $args)
    {
        return $false;
    }
}
