<?php
/**
 * Register Flexible_FAQs blocks in the Gutenberg editor.
 *
 * @package Flexible_FAQs
 */

namespace WPGO_Plugins\Flexible_Faqs;

/**
 * Register blocks.
 */
class Register_Blocks {

	/**
	 * Common root paths/directories.
	 *
	 * @var $module_roots
	 */
	protected $module_roots;

	/**
	 * Main class constructor.
	 *
	 * @param Array $module_roots Root plugin path/dir.
	 * @param Array $custom_plugin_data Plugin data.
	 */
	public function __construct( $module_roots, $custom_plugin_data ) {

		$this->module_roots                        = $module_roots;
		$this->custom_plugin_data                  = $custom_plugin_data;
		$this->text_expand_block_default_atts      = $this->custom_plugin_data->block_text_expand_attribute_defaults;
		$this->box_expand_block_default_atts       = $this->custom_plugin_data->block_box_expand_attribute_defaults;
		$this->accordion_expand_block_default_atts = $this->custom_plugin_data->block_accordion_expand_attribute_defaults;

		$this->block_category_slug = $this->custom_plugin_data->enqueue_prefix;

		add_filter( 'block_categories_all', array( &$this, 'add_block_category' ), 10, 2 );
		add_action( 'plugins_loaded', array( &$this, 'register_dynamic_block' ) );
	}

	/**
	 * Add custom block category.
	 *
	 * @param Array  $categories An array of categories passed as parameter.
	 * @param Object $post The current post object.
	 */
	public function add_block_category( $categories, $post ) {
		return array_merge(
			$categories,
			array(
				array(
					'slug'  => $this->block_category_slug,
					// 'icon' => 'chart-line',
					'title' => $this->custom_plugin_data->main_menu_label,
				),
			)
		);
	}

	/**
	 * Register the dynamic block.
	 *
	 * @return void
	 */
	public function register_dynamic_block() {

		// register the FAQ blocks.

		register_block_type(
			'flexible-faqs/text-expand-faqs',
			array(
				'render_callback' => array( FAQ_Shortcode::get_instance(), 'render_block_wrapper' ),
				'attributes'      => $this->text_expand_block_default_atts,
			)
		);

		register_block_type(
			'flexible-faqs/box-expand-faqs',
			array(
				'render_callback' => array( FAQ_Shortcode::get_instance(), 'render_block_wrapper' ),
				'attributes'      => $this->box_expand_block_default_atts,
			)
		);

		register_block_type(
			'flexible-faqs/accordion-expand-faqs',
			array(
				'render_callback' => array( FAQ_Shortcode::get_instance(), 'render_block_wrapper' ),
				'attributes'      => $this->accordion_expand_block_default_atts,
			)
		);
	}
} /* End class definition */
