<?php

/**
 * Plugin Name: GL - Page Builder
 * Description: Page builder for Gutenberg with modern web development features
 * Author: Wpsoul
 * Author URI: https://greenlightbuilder.pro
 * Plugin URI: https://greenlightbuilder.pro
 * Version: 3.3.3
 * Text Domain: gl-page-builder
 * License: GPL2+
 * License URI: https://www.gnu.org/licenses/gpl-2.0.txt
 */

// Exit if accessed directly.
if (!defined('ABSPATH')) {
	exit;
}

// Define Dir URL
define('GL_DIR_URL', plugin_dir_url(__FILE__));
define('GL_DIR_PATH', plugin_dir_path(__FILE__));
define('GL_PLUGIN_VERSION', '3.3.3');


require_once GL_DIR_PATH . 'init.php';
require_once GL_DIR_PATH . 'includes/helper.php';
require_once GL_DIR_PATH . 'settings.php';
require_once GL_DIR_PATH . 'includes/jsoptimization.php';
require_once GL_DIR_PATH . 'includes/importer.php';

//////////////////////////////////////////////////////////////////
// Filter Indexer Activator
//////////////////////////////////////////////////////////////////
register_activation_hook( __FILE__, 'gspb_filter_filters_activation' );
// Register the deactivation hook to remove the table
register_deactivation_hook( __FILE__, 'gspb_filter_filters_deactivation' );

//////////////////////////////////////////////////////////////////
// Init settings and EDD
//////////////////////////////////////////////////////////////////
require_once GL_DIR_PATH . '/edd/edd_start.php';
add_action('plugins_loaded', 'gspb_GreenLight_plugin_init');
function gspb_GreenLight_plugin_init()
{
	if (class_exists('EddLicensePage')) {
		new EddLicensePage();
	}
	if (class_exists('GSPB_GreenLight_Settings')) {
		new GSPB_GreenLight_Settings();
	}
}

//////////////////////////////////////////////////////////////////
// Updater
//////////////////////////////////////////////////////////////////
add_filter('plugins_api', 'gl_plugin_info', 20, 3);
add_filter('site_transient_update_plugins', 'gl_push_update');
add_action('upgrader_process_complete', 'gl_after_update', 10, 2);
add_action('after_plugin_row_' . plugin_basename(__FILE__), 'gl_after_plugin_row', 10, 3);

function gl_plugin_info($res, $action, $args)
{

    // do nothing if this is not about getting plugin information
    if ($action !== 'plugin_information') {
        return false;
    }

    // do nothing if it is not our plugin
    if (plugin_basename(__DIR__) !== $args->slug) {
        return $res;
    }

    // trying to get from cache first, to disable cache comment 23,33,34,35,36
    if (false == $remote = get_transient('gl_upgrade_pluginslug')) {

        // info.json is the file with the actual information about plug-in on your server
        $remote = wp_remote_get(
            EDD_GSPB_STORE_URL_UPDATE . '/get-info.php?slug=' . plugin_basename(__DIR__) . '&action=info',
            array(
                'timeout' => 15,
                'headers' => array(
                    'Accept' => 'application/json'
                )
            )
        );

        if (!is_wp_error($remote) && isset($remote['response']['code']) && $remote['response']['code'] == 200 && !empty($remote['body'])) {
            set_transient('gl_upgrade_pluginslug', $remote, 60000);
        }else{
            set_transient('gl_upgrade_pluginslug', 'error', 60000);
        }
    }

    if ($remote && !is_wp_error($remote) && $remote != 'error' && isset($remote->version)) {

        $remote = json_decode(wp_remote_retrieve_body($remote));

        $res = new stdClass();
        $res->name = $remote->name;
        $res->slug = $remote->slug;
        $res->version = $remote->version;
        $res->tested = $remote->tested;
        $res->requires = $remote->requires;
        $res->author = $remote->author;
        $res->author_profile = $remote->author_homepage;
        $res->download_link = $remote->download_link;
        $res->trunk = $remote->download_link;
        $res->last_updated = $remote->last_updated;

        if (isset($remote->sections)) {
            $res->sections = array(
                'description' => $remote->sections->description, // description tab
                'installation' => $remote->sections->installation, // installation tab
                'changelog' => isset($remote->sections->changelog) ? $remote->sections->changelog : '',
            );
        }
        if (isset($remote->banners)) {
            $res->banners = array(
                'low' => $remote->banners->low,
                'high' => $remote->banners->high,
            );
        }

        return $res;
    }

    return false;
}

function gl_push_update($transient)
{

    if (empty($transient->checked)) {
        return $transient;
    }

    // trying to get from cache first, to disable cache comment 11,20,21,22,23
    if (false == $remote = get_transient('gl_upgrade_pluginslug')) {
        // info.json is the file with the actual plugin information on your server
        $remote = wp_remote_get(
            EDD_GSPB_STORE_URL_UPDATE . '/get-info.php?slug=' . plugin_basename(__DIR__) . '&action=info',
            array(
                'timeout' => 10,
                'headers' => array(
                    'Accept' => 'application/json'
                )
            )
        );

        if (!is_wp_error($remote) && isset($remote['response']['code']) && $remote['response']['code'] == 200 && !empty($remote['body'])) {
            set_transient('gl_upgrade_pluginslug', $remote, 60000);
        }else{
            set_transient('gl_upgrade_pluginslug', 'error', 60000);
        }
    }

    if (!is_wp_error($remote) && $remote && $remote != 'error') {

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

        // your installed plugin version should be on the line below! You can obtain it dynamically of course
        if ($remote && isset($remote->version) && version_compare(GL_PLUGIN_VERSION, $remote->version, '<') && version_compare($remote->requires, get_bloginfo('version'), '<')) {
            $res = new stdClass();
            $res->slug = plugin_basename(__DIR__);
            $res->plugin = plugin_basename(__FILE__); // it could be just pluginslug.php if your plugin doesn't have its own directory
            $res->new_version = $remote->version;
            $res->tested = $remote->tested;
            $licenses = gl_edd_check_all_licenses();
            $is_active = gl_edd_has_premium_access($licenses);
            if ($is_active) {
                $res->package = $remote->download_link;
            }
            $transient->response[$res->plugin] = $res;
            //$transient->checked[$res->plugin] = $remote->version;
        }
    }
    return $transient;
}

function gl_after_update($upgrader_object, $options)
{
    if ($options['action'] == 'update' && $options['type'] === 'plugin') {
        // just clean the cache when new plugin version is installed
        delete_transient('gl_upgrade_pluginslug');
    }
}

function gl_after_plugin_row($plugin_file, $plugin_data, $status)
{
	$licenses = gl_edd_check_all_licenses();
	$is_active = gl_edd_has_premium_access($licenses);
	if (!$is_active) {
		echo sprintf('<tr class="active"><td colspan="4">%s <a href="%s">%s</a></td></tr>', 'Please enter a license to receive automatic updates', esc_url(admin_url('admin.php?page=' . EDD_GSPB_PLUGIN_LICENSE_PAGE)), 'Enter License.');
	}
}

function gl_change_action_links($links)
{

    $links = array_merge(array(
        '<a href="https://shop.greenshiftwp.com/update-plugin/gl-page-builder/readme.txt" style="color:#93003c" target="_blank">' . __('What\'s New', 'gl-page-builder') . '</a>'
    ), $links);

    return $links;
}

add_action('plugin_action_links_' . plugin_basename(__FILE__), 'gl_change_action_links');


function greenlight_activation_redirect($plugin)
{
	if ($plugin == plugin_basename(__FILE__)) {
		global $pagenow;
		if($pagenow != 'plugins.php' && $pagenow != 'themes.php'){
			return;
		}
		exit(wp_redirect(admin_url('admin.php?page=gl_dashboard')));
	}
}
add_action('activated_plugin', 'greenlight_activation_redirect');

register_deactivation_hook(__FILE__, 'greenlight_deactivation_hook_function');
function greenlight_deactivation_hook_function()
{
	$timestamp = wp_next_scheduled('gl_check_cron_hook');
	wp_unschedule_event($timestamp, 'gl_check_cron_hook');
}