<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
<?php
/**
 * Zozothemes API class.
 *
 */

// Do not allow directly accessing this file.
if ( ! defined( 'ABSPATH' ) ) {
	exit( 'Direct script access denied.' );
}

/**
 * Creates the Zozothemes API connection.
 *
 * @class Zozothemes_API
 * @version 1.0.0
 * @since 1.0.0
 */
 
class Zozothemes_API {
	
	private $api_url;
	
	public function __construct(){
		$this->api_url = 'http://demo.zozothemes.com/pro-plugins/cea/cea-response.php';
	}
	
	public function request( $url ) {
		
		$args = array(
			'timeout' => 300
		);
		
		// Make an API request.
		$response = wp_remote_get( esc_url_raw( $url ), $args );

		// Check the response code.
		$response_code    = wp_remote_retrieve_response_code( $response );
		if ( 200 !== $response_code && ! empty( $response_message ) ) {
			return new WP_Error( $response_code, $response_message );
		}
		if ( 200 !== $response_code ) {
			return new WP_Error( $response_code, esc_html__( 'An unknown API error occurred.', 'beruco' ) );
		}
		$body_data = json_decode( wp_remote_retrieve_body( $response ), true );
		if ( null === $body_data ) {
			return new WP_Error( 'api_error', esc_html__( 'An unknown API error occurred.', 'beruco' ) );
		}
		
		return $body_data;
		
	}
	
	public function get_response(){
		$data = '';
		if ( false === ( $data = get_transient( 'cea_api_results' ) ) ) {
			$data = $this->request( $this->api_url );
			if( !is_wp_error( $data ) ){
				set_transient( 'cea_api_results', $data, 6 * HOUR_IN_SECONDS );
			}
		}		
		return $data;
	}
	
}