<?php

/**
 * Revio Gutenberg Block
 *
 * @since 1.2.0
 * @package RVIOR
 */

namespace RVIOR;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit( 1 );


if ( ! class_exists( __NAMESPACE__ . '\Blocks' ) ) {

	/**
	 * Revio Gutenberg Block
	 *
	 * @since 1.2.0
	 */
	class Blocks extends \RVIOR\Base\Controller {


		/**
		 * Register block
		 *
		 * @return void
		 */
		public function register_hooks() {
			add_filter( 'block_categories_all', [ $this, 'register_block_category' ], 10, 2 );
			add_action( 'init', [ $this, 'register_blocks' ] );
		}


		/**
		 * Register RVIOR Gutenberg Block Category
		 *
		 * @param array $categories Block categories.
		 * @return mixed
		 */
		public function register_block_category( $categories ) {

			return array_merge(
				$categories,
				[
					[
						'slug'  => 'rvior_blocks',
						'title' => esc_html__( 'Revio', 'revio-reviews' ),
						'icon'  => 'microphone',
					],
				]
			);
		}


		/**
		 * Register RVIOR Gutenberg Blocks
		 *
		 * @return void
		 */
		public function register_blocks() {
			if ( ! function_exists( 'register_block_type' ) ) {
				return;
			}

			wp_register_script(
				'rvior-editor-script',
				plugin_dir_url( __FILE__ ) . '/script/index.js',
				[ 'react', 'wp-block-editor', 'wp-components', 'wp-element', '_rvior_inline' ],
				RVIOR_VERSION,
				true
			);

			// Registers showcase block.
			register_block_type( 'revio/showcase', [ 'editor_script' => 'rvior-editor-script' ] );
		}
	}

	// Init the class.
	Blocks::init();
}
