<?php

/**
 * Plugin Name: TableKit Pro
 * Description: Powerful Table Builder for Gutenberg block editor.
 * Requires at least: 6.1
 * Requires PHP: 7.4
 * Plugin URI: https://wpmet.com/plugin/table-builder-pro/
 * Author: Wpmet
 * Version: 1.0.2
 * Author URI: https://wpmet.com/
 * License: GPL-3.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 * Text Domain: table-builder-pro
 * Domain Path: /languages
 */

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


final class TableBuilderPro
{
	/**
	 * plugin version
	 *
	 * @var string
	 */
	const VERSION = '1.0.2';

	/**
	 * Instance of the class
	 *
	 * @var TableBuilderPro|null
	 */
	private static $instance = null;

	public static function get_instance(): TableBuilderPro
	{
		if (null === self::$instance) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	public function __construct()
	{
		/**
		 * Plugins helper constants
		 *
		 * @return void
		 * @since 1.0.0
		 */
		$this->define_constants();

		/**
		 * Make sure ADD AUTOLOAD is scoped/vendor/scoper-autoload.php file
		 *
		 * @return void
		 * @since 1.0.0
		 */
		require_once TABLE_BUILDER_BLOCK_PRO_PLUGIN_DIR . './scoped/vendor/scoper-autoload.php';

		if (! TableBuilderPro\Libs\ActivateTableBuilder::is_plugin_active()) {
			add_action('admin_notices', ['TableBuilderPro\Libs\ActivateTableBuilder', 'table_builder_missing_notice']);
			return;
		}

		/**
		 * Fires after initialization of the GutenKit plugin
		 *
		 * @return void
		 * @since 1.0.0
		 */
		add_action('plugins_loaded', [$this, 'on_plugins_loaded']);
	}


	private function define_constants()
	{
		define('TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION', self::VERSION);
		define('TABLE_BUILDER_BLOCK_PRO_PLUGIN_URL', trailingslashit(plugin_dir_url(__FILE__)));
		define('TABLE_BUILDER_BLOCK_PRO_PLUGIN_DIR', trailingslashit(plugin_dir_path(__FILE__)));
		define('TABLE_BUILDER_BLOCK_PRO_INC_DIR', TABLE_BUILDER_BLOCK_PRO_PLUGIN_DIR . 'includes/');
		define('TABLE_BUILDER_BLOCK_PRO_STYLE_DIR', TABLE_BUILDER_BLOCK_PRO_PLUGIN_DIR . 'build/styles/');
		define('TABLE_BUILDER_BLOCK_PRO_BLOCKS_DIR', TABLE_BUILDER_BLOCK_PRO_PLUGIN_DIR . 'build/blocks/');
	}

	public function activated_plugin()
	{

		update_option('table_builder_pro_version', TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION);

		if (!get_option('table_builder_pro_installed_time')) {
			add_option('table_builder_pro_installed_time', time());
		}

		TableBuilderPro\Libs\ActivateTableBuilder::activate_table_builder();
	}

	public function on_plugins_loaded(): void
	{
		do_action('table-builder-pro/before_init');

		(new TableBuilderPro\Admin\License\LicenseRoute);
		TableBuilderPro\Admin\Admin::instance();
		TableBuilderPro\Core\Enqueue::instance();
		TableBuilderPro\Routes\Init::instance();
		TableBuilderPro\Config\Blocks::instance();
		TableBuilderPro\Shortcode\Shortcode::instance();
		TableBuilderPro\Shortcode\EditorAssets::instance();

		add_filter('plugin_action_links', [$this, 'tableBuilderLink'], 10, 2);
	}

	public function tableBuilderLink(array $pluginActions, string $pluginFile): array
	{
		if ($pluginFile === plugin_basename(__FILE__)) {
			return $pluginActions;
		}

		return $pluginActions;
	}
}

if (class_exists('TableBuilderPro')) {
	TableBuilderPro::get_instance();
}

// Activation hook to run the activation function
register_activation_hook(__FILE__, [TableBuilderPro::get_instance(), 'activated_plugin']);
