<?php
/**
 * Plugin main file.
 *
 * @package     Analog
 * @copyright   2026 SmallTownDev.
 * @link        https://analogwp.com
 *
 * @wordpress-plugin
 * Plugin Name: Style Kits for Elementor
 * Plugin URI:  https://analogwp.com/
 * Description: Style Kits extends the Elementor theme styles editor with more global styling options. Boost your design workflow in Elementor with intuitive global controls and theme style presets.
 * Version:     2.6.5
 * Author:      AnalogWP
 * Author URI:  https://analogwp.com/
 * License:     GPL2
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: analogwp-templates
 * Requires at least: 6.0
 * Requires PHP: 7.4
 *
 * Elementor tested up to: 4.0.8
 * Elementor Pro tested up to: 4.0.4
 */

defined( 'ABSPATH' ) || exit;

define( 'ANG_ELEMENTOR_MINIMUM', '3.32.0' );
define( 'ANG_PHP_MINIMUM', '7.4' );
define( 'ANG_WP_MINIMUM', '6.0' );
define( 'ANG_VERSION', '2.6.5' );
define( 'ANG_TEXT_DOMAIN', 'analogwp-templates' );
define( 'ANG_LEGACY_TEXT_DOMAIN', 'ang' );
define( 'ANG_PLUGIN_FILE', __FILE__ );
define( 'ANG_PLUGIN_URL', plugin_dir_url( ANG_PLUGIN_FILE ) );
define( 'ANG_PLUGIN_DIR', plugin_dir_path( ANG_PLUGIN_FILE ) );
define( 'ANG_PLUGIN_BASE', plugin_basename( ANG_PLUGIN_FILE ) );

/**
 * Handles plugin activation.
 *
 * Throws an error if the plugin is activated on an older version than PHP 7.4.
 *
 * @since 1.6.0
 * @access private
 * @return void
 */
function analog_activate_plugin() {
	if ( version_compare( PHP_VERSION, ANG_PHP_MINIMUM, '<' ) ) {
		wp_die(
			/* translators: %s: version number */
			esc_html( sprintf( __( 'Style Kit for Elementor requires PHP version %s', 'analogwp-templates' ), ANG_PHP_MINIMUM ) ),
			esc_html__( 'Error Activating', 'analogwp-templates' )
		);
	}

	do_action( 'analog_activation' );
}

register_activation_hook( __FILE__, 'analog_activate_plugin' );

/**
 * Handles plugin deactivation.
 *
 * @since 1.6.0
 * @access private
 * @return void
 */
function analog_deactivate_plugin() {
	if ( version_compare( PHP_VERSION, ANG_PHP_MINIMUM, '<' ) ) {
		return;
	}

	do_action( 'analog_deactivation' );
}

register_deactivation_hook( __FILE__, 'analog_deactivate_plugin' );

/**
 * Fail loading, if WordPress version requirements not met.
 *
 * @since 1.1
 * @return void
 */
function analog_fail_wp_version() {
	/* translators: %s: WordPress version */
	$message      = sprintf( esc_html__( 'Style Kits requires WordPress version %s+. Because you are using an earlier version, the plugin is currently NOT RUNNING.', 'analogwp-templates' ), ANG_WP_MINIMUM );
	$html_message = sprintf( '<div class="error">%s</div>', wpautop( $message ) );

	echo wp_kses_post( $html_message );
}

/**
 * Elementor version requirements are not met.
 *
 * @return mixed
 */
function analog_require_minimum_elementor() {
	$file_path           = 'elementor/elementor.php';
	$allowed_notice_html = array(
		'div' => array(
			'class' => array(),
		),
		'p'   => array(),
		'a'   => array(
			'href'  => array(),
			'class' => array(),
			'style' => array(),
		),
	);

	$link = add_query_arg(
		array(
			'action' => 'upgrade-plugin',
			'plugin' => $file_path,
		),
		admin_url( 'update.php' )
	);

	$update_url = wp_nonce_url( $link, 'upgrade-plugin_' . $file_path );

	/* translators: %s: Minimum required Elementor version. */
	$minimum_elementor_message = __( 'Style Kits requires Elementor v%s or newer in order to work. Please update Elementor to the latest version.', 'analogwp-templates' );

	$message = sprintf(
		'<p>%s</p>',
		esc_html( sprintf( $minimum_elementor_message, ANG_ELEMENTOR_MINIMUM ) )
	);

	$versions = get_transient( 'ang_rollback_versions_' . ANG_VERSION );
	/* translators: %s: Version number. */
	$rollback_version_label = __( 'Rollback to v%s', 'analogwp-templates' );

	$message .= '<p>';
	/* translators: %s: Link to update Elementor. */
	$message .= sprintf( '<a href="%s" class="button-primary">%s</a>', esc_url( $update_url ), esc_html__( 'Update Elementor Now', 'analogwp-templates' ) );
	/* translators: %s: Link to rollback plugin to previous version. */
	$message .= sprintf(
		'<a href="%s" class="button-secondary" style="margin-left:10px">%s</a>',
		esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=analog_style_kits_rollback&version=' . $versions[0] ), 'analog_style_kits_rollback' ) ),
		esc_html( sprintf( $rollback_version_label, $versions[0] ) )
	);
	$message .= '</p>';

	echo wp_kses( '<div class="error">' . $message . '</div>', $allowed_notice_html );
}

/**
 * Fail plugin initiialization if requirements are not met.
 *
 * @return mixed|bool
 */
function analog_fail_load() {
	if ( ! function_exists( 'get_current_screen' ) ) {
		require_once ABSPATH . 'wp-admin/includes/screen.php';
	}

	$screen = get_current_screen();

	if ( isset( $screen->parent_file ) && 'plugins.php' === $screen->parent_file && 'update' === $screen->id ) {
		return;
	}

	$file_path           = 'elementor/elementor.php';
	$allowed_notice_html = array(
		'div' => array(
			'class' => array(),
		),
		'p'   => array(),
		'a'   => array(
			'href'  => array(),
			'class' => array(),
		),
	);

	$is_not_activated = false;
	$is_not_installed = false;

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

	$installed_plugins = get_plugins();
	$elementor         = isset( $installed_plugins[ $file_path ] );

	if ( $elementor ) {
		$is_not_activated = true;
	} else {
		$is_not_installed = true;
	}

	if ( $is_not_activated ) {
		if ( ! current_user_can( 'activate_plugins' ) ) {
			return;
		}

		$activation_url = wp_nonce_url( 'plugins.php?action=activate&amp;plugin=' . $file_path . '&amp;plugin_status=all&amp;paged=1&amp;s', 'activate-plugin_' . $file_path );
		$message        = sprintf( '<p>%s</p>', esc_html__( 'Style Kits is not working because you need to activate the Elementor plugin.', 'analogwp-templates' ) );
		$message       .= sprintf( '<p><a href="%s" class="button-primary">%s</a></p>', esc_url( $activation_url ), esc_html__( 'Activate Elementor Now', 'analogwp-templates' ) );
	} elseif ( $is_not_installed ) {
		if ( ! current_user_can( 'install_plugins' ) ) {
			return;
		}

		$install_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=elementor' ), 'install-plugin_elementor' );
		$message     = sprintf( '<p>%s</p>', esc_html__( 'Style Kits is not working because you need to install the Elementor plugin.', 'analogwp-templates' ) );
		$message    .= sprintf( '<p><a href="%s" class="button-primary">%s</a></p>', esc_url( $install_url ), esc_html__( 'Install Elementor Now', 'analogwp-templates' ) );
	}

	echo wp_kses( '<div class="error">' . $message . '</div>', $allowed_notice_html );
}

// Third party dependencies.
if ( is_readable( __DIR__ . '/third-party/vendor/scoper-autoload.php' ) ) {
	require_once __DIR__ . '/third-party/vendor/scoper-autoload.php';
}

if ( ! function_exists( 'sk_fs' ) ) {
	/**
	 * Create a helper function for easy SDK access.
	 */
	function sk_fs() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
		global $sk_fs;

		if ( ! isset( $sk_fs ) ) {
			$freemius_bootstrap_paths = array(
				__DIR__ . '/vendor/freemius/wordpress-sdk/start.php',
				__DIR__ . '/vendor/freemius/start.php',
				__DIR__ . '/freemius/start.php',
			);

			$freemius_bootstrap = '';

			foreach ( $freemius_bootstrap_paths as $bootstrap_path ) {
				if ( is_readable( $bootstrap_path ) ) {
					$freemius_bootstrap = $bootstrap_path;
					break;
				}
			}

			// Include Freemius SDK.
			require_once $freemius_bootstrap;

			$sk_fs = fs_dynamic_init( // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
				array(
					'id'             => '17032',
					'slug'           => 'analogwp-templates',
					'type'           => 'plugin',
					'public_key'     => 'pk_e05579cda0ad78db31e94616185cc',
					'is_premium'     => false,
					'has_addons'     => true,
					'has_paid_plans' => false,
					'menu'           => array(
						'slug'       => 'analogwp_templates',
						'first-path' => 'admin.php?page=analog_onboarding&from=freemius',
						'account'    => false,
						'support'    => false,
						'contact'    => false,
						'addons'     => false,
					),
				)
			);
		}

		return $sk_fs;
	}

	// Init Freemius.
	sk_fs();
	// Signal that SDK was initiated.
	do_action( 'sk_fs_loaded' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
}

/**
 * Fire up plugin instance.
 *
 * @since 1.6.0 Add PHP version check.
 */
add_action(
	'plugins_loaded',
	static function () {
		if ( version_compare( PHP_VERSION, ANG_PHP_MINIMUM, '<' ) ) {
			wp_die(
			/* translators: %s: version number */
				esc_html( sprintf( __( 'Style Kit for Elementor requires PHP version %s', 'analogwp-templates' ), ANG_PHP_MINIMUM ) ),
				esc_html__( 'Error Activating', 'analogwp-templates' )
			);
		}

		// Begin onboarding.
		require_once ANG_PLUGIN_DIR . 'inc/class-onboarding.php';

		if ( ! did_action( 'elementor/loaded' ) ) {
			add_action( 'admin_notices', 'analog_fail_load' );
			return;
		}

		if ( ! version_compare( ELEMENTOR_VERSION, ANG_ELEMENTOR_MINIMUM, '>=' ) ) {
			// Include files temporarily, required for rollbacks to work.
			require_once ANG_PLUGIN_DIR . 'inc/class-base.php';
			require_once ANG_PLUGIN_DIR . 'inc/Core/Storage/Transients.php';
			require_once ANG_PLUGIN_DIR . 'inc/elementor/class-tools.php';
			require_once ANG_PLUGIN_DIR . 'inc/Utils.php';

			add_action( 'admin_notices', 'analog_require_minimum_elementor' );
			return;
		}

		if ( ! version_compare( get_bloginfo( 'version' ), ANG_WP_MINIMUM, '>=' ) ) {
			add_action( 'admin_notices', 'analog_fail_wp_version' );
			return;
		}

		require_once ANG_PLUGIN_DIR . 'inc/Plugin.php';

		\Analog\Plugin::load( ANG_PLUGIN_FILE );
	}
);
