<?php

/**
 * Storzen
 *
 * @package     storzen
 * phpcs:ignore trademarked_term -- Plugin slug is historical, extends WooCommerce.
 * @author      devhasib
 * @copyright   2026 devhasib
 * @license     GPL-2.0-or-later
 *
 * Plugin Name: Storzen – Elementor Builder with Widgets & Conversion Modules for WooCommerce
 * Plugin URI:  https://storzen.webtend.net/
 * Description: Build converting WooCommerce stores with Elementor — 46+ widgets + 11 free conversion modules: variation swatches, wishlist, quick view, back in stock, upsells, stock display, reviews & more.
 * Version:     1.1.3
 * Requires at least: 6.5
 * Requires PHP: 8.1
 * Author:      WebTend
 * Author URI:  https://webtend.net/
 * Text Domain: storzen
 * License:     GPL v2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 *
 */

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

/*
 * Bail out early if the premium "Storzen Pro" plugin is active.
 *
 * Both plugins share the same constants, classes, hooks and assets, so running
 * them together would cause fatal "already defined / declared" conflicts. When
 * Pro is present it supersedes the free version, so we simply stop loading here.
 *
 * We check by plugin path (not by constant) because plugin load order is not
 * guaranteed — the free plugin may boot before Pro has had a chance to define
 * its STORZEN_PRO_* constants.
 *
 * @since 1.1.1
 */
if (! function_exists('storzen_is_pro_active')) {
	function storzen_is_pro_active() {
		// Already loaded in this request (Pro booted before us).
		if ( defined( 'STORZEN_PRO_VERSION' ) || class_exists( 'Storzen_Pro_Init', false ) ) {
			return true;
		}

		if (! function_exists('is_plugin_active')) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		$pro = 'storzen-pro/storzen-pro.php';
		// Covers both single-site and network (multisite) activation.
		return is_plugin_active($pro) || (is_multisite() && is_plugin_active_for_network($pro));
	}
}

if (storzen_is_pro_active()) {
	return;
}

/*
 * Define Plugin Dir Path
 * @since 1.0.0
 */
define('STORZEN_ROOT_PATH', plugin_dir_path(__FILE__));
define('STORZEN_ROOT_URL',  plugin_dir_url(__FILE__));
define('STORZEN_INC',       STORZEN_ROOT_PATH . 'inc');
define('STORZEN_CSS',       STORZEN_ROOT_URL  . 'assets/css');
define('STORZEN_JS',        STORZEN_ROOT_URL  . 'assets/js');
define('STORZEN_IMG',       STORZEN_ROOT_URL  . 'assets/img');
define('STORZEN_ELEMENTOR', STORZEN_ROOT_PATH . 'elementor');

/*
 * Alias the bare Storzen_* constants used by shared module classes.
 * Modules copied from the free plugin reference STORZEN_ROOT_URL, STORZEN_INC,
 * and STORZEN_VERSION directly — aliasing here lets us keep those files
 * byte-identical with the free plugin so future updates merge cleanly.
 *
 * Guarded with defined() in case the standalone free plugin tried to define
 * them first (the free plugin self-disables when Pro is active, but the
 * guard is defensive and harmless).
 *
 * @since 1.0.9
 */
if (! defined('STORZEN_ROOT_PATH')) define('STORZEN_ROOT_PATH', STORZEN_ROOT_PATH);
if (! defined('STORZEN_ROOT_URL'))  define('STORZEN_ROOT_URL',  STORZEN_ROOT_URL);
if (! defined('STORZEN_INC'))       define('STORZEN_INC',       STORZEN_INC);
if (! defined('STORZEN_CSS'))       define('STORZEN_CSS',       STORZEN_CSS);
if (! defined('STORZEN_JS'))        define('STORZEN_JS',        STORZEN_JS);
// Determine license status from cache / stored data — no remote call at boot.
function _storzen_is_licensed()
{
	$data = get_transient('storzen_license_cache');
	if ($data === false) {
		$data = get_option('storzen_license_data', []);
	}
	if (! is_array($data) || empty($data['status']) || $data['status'] !== 'active') {
		return false;
	}
	$host = (string) wp_parse_url(home_url(), PHP_URL_HOST);
	$acts = is_array($data['activations'] ?? null) ? $data['activations'] : [];
	return in_array($host, $acts, true);
}
define('STORZEN_IS_PRO', _storzen_is_licensed() ? 'yes' : 'no');

/** Plugin version **/
define('STORZEN_VERSION', '1.1.3');

// Shared modules reference STORZEN_VERSION — alias it to the Pro version so
// cache-busting query strings stay accurate across Pro updates.
if (! defined('STORZEN_VERSION')) {
	define('STORZEN_VERSION', STORZEN_VERSION);
}

// ── Storzen framework (bundled inside lib/storzen/) ──────────────────────
// Registers its own PK_* constants only if standalone Storzen is NOT active.
if (! defined('SZ_VERSION')) {
	define('SZ_VERSION', '3.1.0');
	define('SZ_FILE',    __FILE__);
	define('SZ_DIR',     STORZEN_ROOT_PATH . 'lib/storzen/');
	define('SZ_URL',     STORZEN_ROOT_URL  . 'lib/storzen/');
}

// PSR-4 autoloader for Storzen namespace
if (! class_exists('Storzen\\Panel')) {
	spl_autoload_register(function ($class) {
		if (strpos($class, 'Storzen\\') !== 0) {
			return;
		}
		$rel  = str_replace(array('Storzen\\', '\\'), array('', '/'), $class);
		$path = SZ_DIR . 'framework/' . $rel . '.php';
		if (file_exists($path)) {
			require_once $path;
		}
	});
}

/**
 * Load plugin textdomain.
 */
// Translation loading is handled automatically by WordPress (since WP 4.6) using the plugin slug.

/*
 * Require plugin init file
 */
if (file_exists(STORZEN_INC . '/class-storzen-init.php')) {
	require_once STORZEN_INC . '/class-storzen-init.php';
}

/**
 * Add "Settings" and "Go Pro" action links on the WordPress Plugins screen,
 * shown directly under the plugin name (alongside Activate / Deactivate).
 *
 * The "Go Pro" link is only added when the free version is running. When the
 * Pro license is active (STORZEN_IS_PRO === 'yes'), only the Settings link
 * appears, which is the correct behaviour for paying customers.
 *
 * @since 1.1.1
 *
 * @param string[] $links Existing action links.
 * @return string[] Modified action links.
 */
function storzen_plugin_action_links($links) {
	$settings_link = sprintf(
		'<a href="%s">%s</a>',
		esc_url(admin_url('admin.php?page=storzen')),
		esc_html__('Settings', 'storzen')
	);

	// Prepend Settings link so it appears first.
	array_unshift($links, $settings_link);

	// Only show "Go Pro" when running the free / unlicensed version.
	if (! defined('STORZEN_IS_PRO') || STORZEN_IS_PRO !== 'yes') {
		$go_pro_link = sprintf(
			'<a href="%s" target="_blank" rel="noopener noreferrer" style="color:#f5610b;font-weight:600;">%s</a>',
			esc_url('https://storzen.webtend.net/pricing/'),
			esc_html__('Go Pro', 'storzen')
		);

		// Insert "Go Pro" right after Settings (index 1).
		array_splice($links, 1, 0, array($go_pro_link));
	}

	return $links;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'storzen_plugin_action_links');

