<?php
/**
 * Main plugin class.
 *
 * @package BricksExtras
 */

namespace BricksExtras;

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

$classes = array(
	'BricksExtrasUpdater',
	'BricksExtrasLicense',
	'SettingsPage',
	'ConditionsPage',
	'MiscPage',
	'ChangelogPage',
	'VideoProvidersPage',
);

foreach ( $classes as $class ) {
	if ( ! class_exists( $class ) ) {
		require_once __DIR__ . "/$class.php";
	}
}

/**
 * Main plugin class.
 */
class Plugin {

	const PREFIX    = 'bricksextras_';
	const TITLE     = 'BricksExtras';
	const VERSION   = '1.7.2';
	const STORE_URL = 'https://bricksextras.com';
	const ITEM_ID   = 367;

	/**
	 * Constructor - sets up plugin hooks.
	 */
	public function __construct() {

		/* needs to run early, on the plugins_loaded to be picked up by Bricks */
		add_action( 'plugins_loaded', array( __CLASS__, 'conditions_init' ), 1 );

		/* Fires after WordPress has finished loading but before any headers are sent */
		add_action( 'init', array( __CLASS__, 'bricksextras_init' ), 20 );

		/* EDD licenseing activation form */
		BricksExtrasLicense::init( self::PREFIX, self::TITLE, self::STORE_URL, self::ITEM_ID );

		/* Add admin menu item */
		add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ), 11 );

		/* Add text strings to builder */
		add_filter( 'bricks/builder/i18n', array( __CLASS__, 'x_i18n' ) );

		/* Add settings link to plugin page */
		add_filter( 'plugin_action_links_' . BRICKSEXTRAS_BASE, array( __CLASS__, 'settings_link' ) );

		/* Setup the plugin updater */
		add_action( 'init', array( __CLASS__, 'plugin_updater' ), 0 );

		/* admin setting styles */
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_settings_styles' ) );

		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'builder_styles' ) );

		if ( get_option( 'bricksextras_lottie' ) ) {
			add_filter( 'upload_mimes', array( __CLASS__, 'upload_mimes_json' ) );
		}

		/* Clean database on uninstall */
		register_uninstall_hook( BRICKSEXTRAS_BASE, array( __CLASS__, 'clean_db_on_uninstall' ) );
	}

	/**
	 * Add text strings to builder.
	 *
	 * @param array $i18n Internationalization strings.
	 * @return array Modified i18n array.
	 */
	public static function x_i18n( $i18n ) {

		// For element category 'extras'.
		$i18n['extras'] = esc_html__( 'Extras', 'bricksextras' );

		return $i18n;
	}

	/**
	 * Add admin menu item.
	 */
	public static function admin_menu() {

		if ( ! class_exists( '\Bricks\Capabilities' ) ) {
			return;
		}

		// Return: Current user has no access to Bricks.
		if ( \Bricks\Capabilities::current_user_has_no_access() ) {
			return;
		}

		global $menu;

		foreach ( $menu as $item ) {
			if ( false !== array_search( 'bricks', $item, true ) ) {
				break;
			}
		}

		add_submenu_page( 'bricks', self::TITLE, self::TITLE, 'manage_options', self::PREFIX . 'menu', array( __CLASS__, 'menu_item' ) );
	}

	/**
	 * Add settings link to plugin page.
	 *
	 * @param array $links Plugin action links.
	 * @return array Modified links array.
	 */
	public static function settings_link( $links ) {
		$url = esc_url(
			add_query_arg(
				'page',
				self::PREFIX . 'menu',
				get_admin_url() . 'admin.php'
			)
		);

		// Create the link.
		$settings_link = "<a href='$url'>" . __( 'Settings', 'bricksextras' ) . '</a>';

		// Adds the link to the beginning of the array.
		array_unshift(
			$links,
			$settings_link
		);

		return $links;
	}

	/**
	 * Setup the plugin updater.
	 */
	public static function plugin_updater() {

		// To support auto-updates, this needs to run during the wp_version_check cron job for privileged users.
		if ( ! current_user_can( 'manage_options' ) && ! wp_doing_cron() ) {
			return;
		}

		// Retrieve our license key from wp-config.php constant or DB.
		$license_key = BricksExtrasLicense::get_license_key();

		// Setup the updater.
		$edd_updater = new BricksExtrasUpdater(
			self::STORE_URL,
			__FILE__,
			array(
				'version'   => self::VERSION, // Current version number.
				'license'   => $license_key, // License key (used get_option above to retrieve from DB).
				'item_id'   => self::ITEM_ID, // ID of the product.
				'item_name' => self::TITLE,
				'author'    => 'BricksExtras', // Author of this plugin.
				'url'       => home_url(),
				'beta'      => false,
			)
		);
	}

	/**
	 * Clean database on uninstall.
	 */
	public static function clean_db_on_uninstall() {
		foreach ( wp_load_alloptions() as $option => $value ) {
			if ( strpos( $option, 'bricksextras_' ) === 0 ) {
				delete_option( $option );
			}
		}
	}

	/**
	 * Settings page styles.
	 *
	 * @param string $hook_suffix Admin page hook suffix.
	 */
	public static function admin_settings_styles( $hook_suffix ) {

		if ( strpos( $hook_suffix, 'bricksextras' ) === false ) {
			return;
		}

		wp_enqueue_style( 'x_admin_css', BRICKSEXTRAS_URL . 'includes/css/admin.css', array(), self::VERSION );
	}

	/**
	 * Enqueue builder styles and scripts.
	 */
	public static function builder_styles() {

		if ( ! function_exists( 'bricks_is_builder' ) ) {
			return;
		}

		if ( bricks_is_builder() && ! bricks_is_builder_main() ) {
			wp_enqueue_style( 'x_builder_css', BRICKSEXTRAS_URL . 'includes/css/builder.css', array(), '1.0.2' );
		}

		if ( bricks_is_builder_main() ) {
			wp_enqueue_style( 'x_builder_ui_css', BRICKSEXTRAS_URL . 'includes/css/builder-ui.css', array(), '1.0.0' );
		}

		if ( bricks_is_builder_iframe() ) {
			wp_enqueue_script( 'x-lottie', BRICKSEXTRAS_URL . 'components/assets/js/lottie.min.js', array(), '5.9.6', true );
			wp_enqueue_script( 'x-lottie-interactivity', BRICKSEXTRAS_URL . 'components/assets/js/lottieinteractivity.min.js', array(), '1.6.1', true );
			wp_enqueue_script( 'x-lottie-init', BRICKSEXTRAS_URL . 'components/assets/js/lottie-builder.js', array(), '1.0.0', true );

			wp_enqueue_script( 'x-frontend', BRICKSEXTRAS_URL . 'components/assets/js/' . \BricksExtras\Helpers::maybe_minify_scripts( 'frontend' ) . '.js', array( 'bricks-scripts' ), self::VERSION, true );
			wp_enqueue_script( 'x-iframe', BRICKSEXTRAS_URL . 'includes/js/iframe.js', array(), '1.0.2', true );
		}
	}

	/**
	 * Settings page tabs.
	 */
	public static function menu_item() {
		$tab = \BricksExtras\Helpers::get_get_value( 'tab' ) ? \BricksExtras\Helpers::get_get_value( 'tab' ) : false;
		?>
		<div class="wrap bricksextras-settings">
			<h1>BricksExtras Settings</h1>
			<br>
			<h2 class="nav-tab-wrapper">
			<a href="?page=bricksextras_menu&amp;tab=settings" class="nav-tab<?php echo ( false === $tab || 'settings' === $tab ) ? ' nav-tab-active' : ''; ?>"><?php echo esc_html__( 'Elements', 'bricksextras' ); ?></a>
			<a href="?page=bricksextras_menu&amp;tab=conditions" class="nav-tab<?php echo 'conditions' === $tab ? ' nav-tab-active' : ''; ?>"><?php echo esc_html__( 'Conditions', 'bricksextras' ); ?></a>
			<a href="?page=bricksextras_menu&amp;tab=misc" class="nav-tab<?php echo 'misc' === $tab ? ' nav-tab-active' : ''; ?>"><?php echo esc_html__( 'Misc', 'bricksextras' ); ?></a>
			<a href="?page=bricksextras_menu&amp;tab=video_providers" class="nav-tab<?php echo 'video_providers' === $tab ? ' nav-tab-active' : ''; ?>"><?php echo esc_html__( 'API keys', 'bricksextras' ); ?></a>
			<a href="?page=bricksextras_menu&amp;tab=license" class="nav-tab<?php echo 'license' === $tab ? ' nav-tab-active' : ''; ?>"><?php echo esc_html__( 'License', 'bricksextras' ); ?></a>
			<a href="?page=bricksextras_menu&amp;tab=changelog" class="nav-tab<?php echo 'changelog' === $tab ? ' nav-tab-active' : ''; ?>"><?php echo esc_html__( 'Changelog', 'bricksextras' ); ?></a>
		</h2>
			<div class="bricks-admin-wrapper">
			<?php
			if ( 'license' === $tab ) {
				BricksExtrasLicense::license_page();
			} elseif ( 'changelog' === $tab ) {
				ChangelogPage::init();
			} elseif ( 'conditions' === $tab ) {
				ConditionsPage::init( self::PREFIX, self::TITLE, self::VERSION );
			} elseif ( 'misc' === $tab ) {
				MiscPage::init( self::PREFIX, self::TITLE, self::VERSION );
			} elseif ( 'video_providers' === $tab ) {
				VideoProvidersPage::init( self::PREFIX, self::TITLE, self::VERSION );
			} else {
				SettingsPage::init( self::PREFIX, self::TITLE, self::VERSION );
			}
			?>
		</div></div>
		<?php
	}

	/**
	 * Allow JSON inside media library for lottie.
	 *
	 * @param array $mimes MIME types.
	 * @return array Modified MIME types.
	 */
	public static function upload_mimes_json( $mimes ) {
		if ( current_user_can( 'manage_options' ) ) {
			$mimes['json'] = 'application/json';
		}
		return $mimes;
	}

	/**
	 * Fires after WordPress has finished loading but before any headers are sent.
	 */
	public static function bricksextras_init() {

		if ( ! class_exists( 'BricksExtrasMain' ) ) {
			require_once __DIR__ . '/BricksExtrasMain.php';
		}

		$bricks_extras = new BricksExtrasMain( self::PREFIX, self::VERSION );
	}

	/**
	 * Initialize conditions.
	 */
	public static function conditions_init() {

		if ( ! class_exists( 'BricksExtrasConditions' ) ) {
			require_once __DIR__ . '/BricksExtrasConditions.php';
			( new BricksExtrasConditions() )->init( self::PREFIX );
		}
	}
}