<?php

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://www.facebook.com/nikita.flo.735
 * @since      1.0.0
 *
 * @package    FLO_APP_LOADER
 * @subpackage FLO_APP_LOADER/includes
 */

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    FLO_APP_LOADER
 * @subpackage FLO_APP_LOADER/includes
 * @author     Nichita Sinitin <nikita@flosites.net>
 */
class FLO_APP_LOADER
{
	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $theme_name    The string used to uniquely identify this plugin.
	 */
	protected $theme_name;

	/**
	 * The current version of the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $version    The current version of the plugin.
	 */
	protected $version;

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 * the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct()
	{
		if (defined('FLEX_GUTENBERG_THEME_VERSION')) {
			$this->version = FLEX_GUTENBERG_THEME_VERSION;
		} else {
			$this->version = '1.0.0';
		}

		$this->loader_path = dirname(__FILE__);

		$this->init_dep();
		$this->load_option_pages();


		// call this filter with  priority 11 in  order to execute it  later than the one
		// from the  Flo Gutenberg  plugin (which also registers Flo caetegory)
		add_filter('block_categories_all', array($this, 'maybe_register_block_category'), 11, 2);

		add_action('init', array($this, 'register_gutenberg_block'));
		add_action('enqueue_block_editor_assets', array($this, 'enqueue_gutenberg_scripts'));
	}

	private function init_dep()
	{
		$loader_path = $this->loader_path;

		include $loader_path . '/configuration-files/class-flo-init-post-types.php';
		include $loader_path . '/configuration-files/class-flo-init-taxonomy.php';

		include $loader_path . '/common/class-flo-common.php';
		if (is_admin()) {
			include $loader_path . '/configuration-files/class-flo-manage-required-plugins.php';
			include $loader_path . '/configuration-files/configs/required-plugins.php';

			add_action('fgt_tgmpa_register', 'fgt_register_required_plugins');

			include $loader_path . '/configuration-files/class-flo-init-metaboxes.php';
			include $loader_path . '/configuration-files/class-flo-init-initial-theme-data.php';
			include $loader_path . '/admin/class-flo-admin.php';

			// wizard
			include $loader_path . '/admin/class-flo-app-wizard.php';

			include $loader_path . '/updater/wp-gitlab-updater.php';
		} else {
			include $loader_path . '/public/class-flo-public.php';
		}
	}

	private function load_option_pages()
	{
		if (is_admin()) {
			$loader_path = $this->loader_path;
			include $loader_path . '/configuration-files/option-pages/class-flo-options-page-loader.php';

			$option_pages_loaders = [];
			foreach (glob($loader_path . '/configuration-files/option-pages/*.php') as $option_page) {
				if (strpos($option_page, 'class-flo-options-page-loader.php') === FALSE) {
					include $option_page;
				}
			}
		}
	}


	/**
	 *
	 * If Flo Gutenberg block plugin is activated, it already regissters the 'Flo' blocks category
	 * But, if it is not activated, we need to register that category.
	 * For that  we need  to check if that category exists or not
	 * @param array - $categorues
	 * @param WP_Post
	 * @return array
	 */
	public function maybe_register_block_category($categories, $post)
	{

		// search through  the current categories  array to check if that categ  already exists
		$maybe_categ_exists = flo_search_array_key_value($array = $categories, $key = 'slug', $value = 'flo');
		if (!(is_array($maybe_categ_exists) && sizeof($maybe_categ_exists))) {
			return array_merge(
				$categories,
				array(
					array(
						'slug'	 => 'flo',
						'title'	 => 'FLO Blocks',
					),
				)
			);
		}

		return $categories;
	}

	public function register_gutenberg_block()
	{
		register_block_type(
			'gutenberg-block/child-content',
			array(
				'render_callback' => array($this, 'gutenbergchild_content'),
			)
		);
	}

	public function gutenbergchild_content($attributes)
	{
		// is_admin() returns FALSE in save_post hook with Gutenberg editor
		// therefore we need to also check for REST_REQUEST
		if (is_admin() || defined('REST_REQUEST') && REST_REQUEST) {
			return;
		}

		global $post;
		if (defined('FGT_WOO_SUPPORT') && FGT_WOO_SUPPORT && $post->post_type === 'product') {
			remove_filter('the_content', 'wpautop');
			ob_start();
			woocommerce_content();
			$content = ob_get_clean();
		} else {
			$content = $post->post_content;
		}
		if (has_filter('flo_the_content')) {
			$content = apply_filters('flo_the_content', $content, $attributes);
		} else {
			$blocks = parse_blocks($content);
			$is_gutenberg_page = (!empty($blocks) && count($blocks)==1 && (''!== $blocks[0]['blockName'] ||  $blocks[0]['blockName'] !== NULL));

			if ($is_gutenberg_page) {
				$content = pronto_formatter($content);
			} else {
				$result = '';
				foreach ($blocks as $key => $block) {
					$result .= render_block($block);
				}
				$content = $result;
			}
		}

		global $wp_embed;

		$blocks = do_shortcode($wp_embed->autoembed(do_blocks($content)));

		return  $blocks;
	}

	public function enqueue_gutenberg_scripts()
	{
		$dependencies = ['wp-blocks', 'wp-components'];
		if (strpos($_SERVER['REQUEST_URI'], 'widgets.php') === FALSE) {
			$dependencies[] = 'wp-editor';
		}

		wp_enqueue_script('child_content' . '_gutenberg', get_template_directory_uri() . '/dist/gutenberg.min.js', $dependencies);
	}
}

new FLO_APP_LOADER();
