<?php
/**
 * Plugin Name: Elementor Addon Components
 * Description: This amazing plugin includes free components, such as image, item and product grid. A header and footer builder, custom CSS, dynamic tags including ACF, element display conditions and much more.
 * Plugin URI: https://elementor-addon-components.com/
 * Update URI: https://elementor-addon-components.com
 * Author: Team EAC
 * Author URI: https://elementor-addon-components.com/
 * Version: 2.4.6
 * Requires at least: 6.5.0
 * Tested up to: 6.9
 * Requires PHP: 7.4
 * Requires Plugins: elementor
 * Elementor tested up to: 3.33.6
 * WC requires at least: 8.0.0
 * WC tested up to: 9.9.3
 * Text Domain: eac-components
 * Domain Path: /languages
 * License: GPLv3 or later License
 * URI: http://www.gnu.org/licenses/gpl-3.0.html
 * 'Elementor Addon Components' is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GPL General Public License for more details.
 */

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

define( 'EAC_DOMAIN_NAME', 'eac-components' );
define( 'EAC_PLUGIN_NAME', 'Elementor Addon Components' );
define( 'EAC_PLUGIN_SLUG', 'elementor-addon-components' );
define( 'EAC_PLUGIN_SITE', 'https://elementor-addon-components.com' );
define( 'EAC_PLUGIN_VERSION', '2.4.6' );
define( 'EAC_PLUGIN_FILE', __FILE__ );
define( 'EAC_PLUGIN_URL', plugins_url( '/', __FILE__ ) );
define( 'EAC_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'EAC_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );

define( 'EAC_ADMIN_NAMESPACE', 'EACCustomWidgets\\Admin\\Settings\\' );
define( 'EAC_INCLUDES_NAMESPACE', 'EACCustomWidgets\\Includes\\' );
define( 'EAC_ACF_PATH', EAC_PLUGIN_PATH . 'includes/acf/' );
define( 'EAC_ACF_JSON_PATH', EAC_PLUGIN_PATH . 'includes/acf/acf-json' );

define( 'EAC_SCRIPT_DEBUG', false ); // true = .js ou false = .min.js
define( 'EAC_STYLE_DEBUG', false );  // true = .css ou false = .min.css

function eac_plugin_activation() {
	update_option( 'eac_options_plugin_activated', 'yes', false );
}
register_activation_hook( EAC_PLUGIN_FILE, 'eac_plugin_activation' );

function eac_plugin_deactivation() {
	update_option( 'eac_options_plugin_activated', 'no', false );
}
register_deactivation_hook( EAC_PLUGIN_FILE, 'eac_plugin_deactivation' );

/** Vérifie les compatibilités et instancie le plugin */
function eac_load_plugin(): void {
	load_plugin_textdomain( EAC_DOMAIN_NAME, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

	if ( eac_is_compatible() ) {
		add_filter( 'plugin_action_links_' . EAC_PLUGIN_BASENAME, 'eac_add_settings_action_links' );
		add_filter( 'plugin_row_meta', 'eac_add_row_meta_links', 10, 2 );
		add_action( 'elementor/init', function () {
			require_once __DIR__ . '/eac-plugin.php';
		});
	} else {
		deactivate_plugins( EAC_PLUGIN_BASENAME );
	}
}
add_action( 'plugins_loaded', 'eac_load_plugin' );

/**
 * eac_is_compatible
 * Vérification des compatibilités pour les versions Elementor, WordPress et PHP
 *
 * @return bool
 */
function eac_is_compatible(): bool {
	$compatible        = true;
	$elementor_version = '3.28.0';
	$wp_version        = '6.5.0';
	$php_version       = '7.4';

	/** Notification Elementor n'est pas chargé */
	if ( ! did_action( 'elementor/loaded' ) ) {
		$compatible = false;
		add_action(
			'admin_notices',
			function () { ?>
				<div class='notice notice-error is-dismissible'>
					<p><?php esc_html_e( 'Elementor Addon Components is not working because you need to activate Elementor plugin !', 'eac-components' ); ?></p>
				</div>
			<?php }
		);
		/** Notification Elementor n'est pas à la bonne version */
	} elseif ( version_compare( ELEMENTOR_VERSION, $elementor_version, '<' ) ) {
		$compatible = false;
		add_action(
			'admin_notices',
			function () use ( $elementor_version ) {
				$message = sprintf(
					/* translators: 1: Elementor version minimale */
					esc_html__( 'Elementor Addon Components minimum version Elementor', 'eac-components' ) . ' %1$s',
					$elementor_version
				); ?>
				<div class='notice notice-error is-dismissible'>
					<p><?php echo esc_html( $message ); ?></p>
				</div>
			<?php } );
		/** Notification WordPress n'est pas à la bonne version */
	} elseif ( version_compare( get_bloginfo( 'version' ), $wp_version, '<' ) ) {
		$compatible = false;
		add_action(
			'admin_notices',
			function () use ( $wp_version ) {
				$message = sprintf(
					/* translators: 1: WordPress version minimale */
					esc_html__( 'Elementor Addon Components minimum version WordPress', 'eac-components' ) . ' %1$s',
					$wp_version
				); ?>
				<div class='notice notice-error is-dismissible'>
					<p><?php echo esc_html( $message ); ?></p>
				</div>
			<?php } );
		/** Notification PHP n'est pas à la bonne version */
	} elseif ( version_compare( PHP_VERSION, $php_version, '<' ) ) {
		$compatible = false;
		add_action(
			'admin_notices',
			function () use ( $php_version ) {
				$message = sprintf(
					/* translators: 1: PHP version minimale */
					esc_html__( 'Elementor Addon Components minimum version PHP', 'eac-components' ) . ' %1$s',
					$php_version
				); ?>
				<div class='notice notice-error is-dismissible'>
					<p><?php echo esc_html( $message ); ?></p>
				</div>
			<?php } );
	}
	return $compatible;
}

/** Ajout du lien vers la page de réglages du plugin */
function eac_add_settings_action_links( array $links ): array {
	$setting_link = array( '<a href="' . esc_url( admin_url( 'admin.php?page=eac-components' ) ) . '">' . esc_html__( 'Settings', 'eac-components' ) . '</a>' );
	return array_merge( $setting_link, $links );
}

/** Ajout du lien vers la page du centre d'aide et voir les détails du plugin */
function eac_add_row_meta_links( array $meta_links, string $plugin_file ): array {
	if ( EAC_PLUGIN_BASENAME === $plugin_file ) {
		// Lien view détails
		$meta_links[2] = sprintf(
			'<a href="%1$s" class="thickbox open-plugin-details-modal">%2$s</a>',
			esc_url( add_query_arg(
				array(
					'tab'       => 'plugin-information',
					'plugin'    => EAC_PLUGIN_SLUG,
					'TB_iframe' => true,
					'width'     => 600,
					'height'    => 550,
				),
				admin_url( 'plugin-install.php' )
			) ),
			esc_html__( 'View details', 'eac-components' )
		);

		// Help Center
		$setting_link = array(
			'<a href="' . EAC_PLUGIN_SITE . '/help-center/" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Help center', 'eac-components' ) . '</a>',
			/**'<a href="' . EAC_PLUGIN_SITE . '/donate.php?for=' . EAC_PLUGIN_SLUG . '" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-smiley"></span> <span>Buy me a coffee!</span></a>'*/
		);
		$meta_links = array_merge( $meta_links, $setting_link );
	}
	return $meta_links;
}
