<?php

/**
 * The plugin bootstrap file
 *
 * @link              https://www.triboomedia.it
 * @since             1.0.3
 * @package           Tbm_Tagmailer
 *
 * @wordpress-plugin
 * Plugin Name:       TBM Tagmailer
 * Plugin URI:        https://www.triboomedia.it
 * Description:       Iscrive utenti a TagMailer e permette di creare Newsletter con i post di WordPress
 * Version:           1.15.1
 * Author:            Dev Triboomedia
 * Author URI:        https://triboomedia.it
 * License:           Unlicensed
 * Text Domain:       tbm-tagmailer
 * Domain Path:       /languages
 * Requires PHP:      7.4
 * Update URI:        false
 * Requires at least: 5.8
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

DEFINE( "TBM_TAGMAILER_PLUGIN_DIR", plugin_dir_path( __FILE__ ) );
DEFINE( "TBM_TAGMAILER_PLUGIN_URL", plugin_dir_url( __FILE__ ) );
DEFINE( "TBM_TAGMAILER_VERSION", 'version:1.15.1' );
DEFINE( "TBM_TAGMAILER_V5_API", "https://api.tagdirect.it/tm/v5.1" );
DEFINE( "TBM_TAGMAILER_SHORTCODE", "tagmailer_form" );

// Whoops only for DEBUG
if ( class_exists( 'Whoops' ) && defined( 'TBM_TAGMAILER_DEBUG' ) && ! defined( 'WP_CLI' ) ) {
	$whoops = new \Whoops\Run;
	$whoops->pushHandler( new \Whoops\Handler\PrettyPageHandler );
	// Not exit in deprecated or strict error
	$whoops->silenceErrorsInPaths( '/.*/', $levels = E_STRICT | E_DEPRECATED );
	$whoops->register();
}

/**
 * Check if the required PHP extensions are loaded
 */
if ( ! extension_loaded( 'mbstring' )
     || ! extension_loaded( 'json' )
     || ! extension_loaded( 'intl' )
     || ! extension_loaded( 'bcmath' )
     || ! extension_loaded( 'iconv' ) ) {
	add_action( 'admin_init', function () {
		deactivate_plugins( plugin_basename( __FILE__ ) );
		if ( isset( $_GET['activate'] ) ) {
			unset( $_GET['activate'] );
		}
		add_action( 'admin_notices', function () {
			?>
            <div class="notice notice-error">
                <p><?php echo 'TBM Tagmailer plugin is not active because it requires the following PHP extensions: mbstring, json, intl, bcmath and iconv.'; ?></p>
            </div>
			<?php
		} );
	} );
}

/**
 * Check if ACF pro is present
 */
if ( class_exists( 'acf' ) && ! class_exists( 'acf_pro' ) ) {
	add_action( 'admin_init', function () {
		deactivate_plugins( plugin_basename( __FILE__ ) );
		if ( isset( $_GET['activate'] ) ) {
			unset( $_GET['activate'] );
		}
		add_action( 'admin_notices', function () {
			?>
            <div class="notice notice-error">
                <p><?php echo 'The TBM Tagmailer plugin requires the ACF PRO plugin, which is bundled with this plugin. It appears that you have the ACF standard plugin installed. Please deactivate the ACF standard plugin.'; ?></p>
            </div>
			<?php
		} );
	} );
}

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require TBM_TAGMAILER_PLUGIN_DIR . 'includes/class-tbm-tagmailer.php';

/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.0
 */
function run_tbm_tagmailer() {
	$plugin = new Tbm_Tagmailer();
	$plugin->run();
}

run_tbm_tagmailer();
