<?php
/**
 * Sanl Envato Api
 *
 * @package Elite_Notification_Lite\Api
 */

namespace Elite_Notification_Lite\Api;

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

/**
 * Class Sanl_Envato_Api
 *
 * @package Elite_Notification_Lite\Api
 */
class Sanl_Envato_Api {
	/**
	 * Its property bearer_token
	 *
	 * @var string
	 */
	private $bearer_token = '';

	/**
	 * Sanl_Envato_Api constructor.
	 *
	 * @param string $token it is token param.
	 */
	public function __construct( $token = '' ) {
		if ( ! empty( $token ) ) {
			$this->set_token( $token );
		}
	}

	/**
	 * The set token is generated by appsbd
	 *
	 * @param mixed $token It is token param.
	 */
	public function set_token( $token ) {
		$this->bearer_token = $token;
	}

	/**
	 * The get sales is generated by appsbd
	 *
	 * @param int $page It is page param.
	 *
	 * @return mixed
	 */
	public function get_sales( $page = 1 ) {
		$url  = 'https://api.envato.com/v3/market/author/sales?page=' . $page;
		$data = $this->apicall( $url );
		$i    = 1;
		foreach ( $data as &$d ) {
			unset( $d->item->description );
			unset( $d->item->attributes );
		}
		return $data;
	}

	/**
	 * The check api is generated by appsbd
	 *
	 * @param mixed $error It is error param.
	 *
	 * @return bool
	 */
	public function check_api( &$error ) {
		$url  = 'https://api.envato.com/v1/market/private/user/username.json';
		$data = $this->apicall( $url );
		if ( ! empty( $data->error ) ) {
			$error = $data->error;
			return false;
		}
		return true;
	}

	/**
	 * The apicall is generated by appsbd
	 *
	 * @param mixed $url It is url param.
	 * @param array $postarray It is postarray param.
	 *
	 * @return mixed
	 */
	private function apicall( $url, $postarray = array() ) {
		$is_post = false;
		if ( count( $postarray ) > 0 ) {
			$is_post = true;
		}
		if ( function_exists( 'wp_remote_post' ) ) {
			$rq_params = array(
				'method'      => $is_post ? 'POST' : 'GET',
				'sslverify'   => true,
				'timeout'     => 120,
				'redirection' => 5,
				'httpversion' => '1.0',
				'blocking'    => true,
				'headers'     => array(
					'Authorization' => "Bearer {$this->bearer_token}",
				),
				'cookies'     => array(),
			);
			if ( $is_post ) {
				$rq_params['body'] = $postarray;
			}
			$server_response = wp_remote_post( $url, $rq_params );
			if ( is_wp_error( $server_response ) ) {
				$rq_params['sslverify'] = false;
				$server_response        = wp_remote_post( $url, $rq_params );
				if ( ! is_wp_error( $server_response ) ) {
					$error = $server_response->get_error_message();
				} elseif ( ! empty( $server_response['body'] ) && ( is_array( $server_response ) && 200 === (int) wp_remote_retrieve_response_code( $server_response ) ) && 'GET404' != $server_response['body'] ) {
						return json_decode( trim( $server_response['body'] ) );
				}
			} elseif ( ! empty( $server_response['body'] ) && ( is_array( $server_response ) && 200 === (int) wp_remote_retrieve_response_code( $server_response ) ) && 'GET404' != $server_response['body'] ) {
					return json_decode( trim( $server_response['body'] ) );
			}
		}
	}
}
