<?php

namespace WPIS;

class Updater {

    const INFO_URL = 'https://wp.immosync.com/update/info.json';

    public function __construct() {
        add_filter('pre_set_site_transient_update_plugins', [$this, 'checkForUpdate']);
        add_filter('plugins_api', [$this, 'getInfo'], 20, 3);
    }

    public function checkForUpdate($checked_data){

        $plugin_slug = 'wp-immo-sync';

        if (empty($checked_data->checked))
            return $checked_data;

        $remote = wp_remote_get( self::INFO_URL, [
            'timeout' => 10,
            'headers' => [
                'Accept' => 'application/json'
            ]
        ]);

        if( ! is_wp_error( $remote ) && isset( $remote['response']['code'] ) && $remote['response']['code'] == 200 && ! empty( $remote['body'] ) ) {

            $plugin_data = get_plugin_data(__DIR__ . '/../wp-immo-sync.php');

            $body = json_decode($remote['body']);

            if( $plugin_data['Version'] !== $body->version) {

                $pluginUpdateData = (object) [
                    'slug'        => $plugin_slug,
                    'new_version' => $body->version,
                    'homepage'    => $body->homepage_url,
                    'package'     => $body->download_url,
                    'tested'      => $body->tested
                ];

                $checked_data->response[$plugin_slug .'/'. $plugin_slug .'.php'] = $pluginUpdateData;
            }
        }

        return $checked_data;
    }

    public function getInfo( $res, $action, $args ){

        if( 'plugin_information' !== $action ) {
            return false;
        }

        $plugin_slug = 'wp-immo-sync';

        if( $plugin_slug !== $args->slug ) {
            return false;
        }

        if( false == $remote = get_transient( 'wpis_update_' . $plugin_slug ) ) {

            $remote = wp_remote_get( self::INFO_URL, [
                'timeout' => 10,
                'headers' => [
                    'Accept' => 'application/json'
                ]
            ]);

            if ( ! is_wp_error( $remote ) && isset( $remote['response']['code'] ) && $remote['response']['code'] == 200 && ! empty( $remote['body'] ) ) {
                set_transient( 'wpis_update_' . $plugin_slug, $remote, 43200 ); // 12 hours cache
            }

        }

        if( ! is_wp_error( $remote ) && isset( $remote['response']['code'] ) && $remote['response']['code'] == 200 && ! empty( $remote['body'] ) ) {

            $remote             = json_decode( $remote['body'] );
            $res                = (object) [];
            $res->name          = $remote->name;
            $res->slug          = $plugin_slug;
            $res->version       = $remote->version;
            $res->tested        = $remote->tested;
            $res->requires      = $remote->requires;
            $res->author        = $remote->author;
            $res->download_link = $remote->download_url;
            $res->trunk         = $remote->download_url;
            $res->requires_php  = $remote->requires_php;
            $res->last_updated  = $remote->last_updated;
            $res->sections      = array(
                'description'  => $remote->sections->description,
                'installation' => $remote->sections->installation,
                'changelog'    => $remote->sections->changelog
            );

            // in case you want the screenshots tab, use the following HTML format for its content:
            // <ol><li><a href="IMG_URL" target="_blank"><img src="IMG_URL" alt="CAPTION" /></a><p>CAPTION</p></li></ol>

            if( !empty( $remote->sections->screenshots ) ) {
                $res->sections['screenshots'] = $remote->sections->screenshots;
            }

            $res->banners = [
                'low'  => $remote->banners->low,
                'high' => $remote->banners->high
            ];

            return $res;

        }

        return false;

    }
}

new Updater();