<?php
/**
 * Register Favorite movie quote block
 *
 * @link       http://example.com
 * @since      1.0.0
 *
 * @package    Movies
 * @subpackage Movies/includes
 */

/**
 * Register Favorite movie quote block.
 *
 * @package    Movies
 * @subpackage Movies/includes
 * @author     Your Name <email@example.com>
 */
class Favorite_Movie_Quote {
	public function __construct() {
		add_action( 'init', array( $this, 'register_block' ) );
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_assets' ) );
		add_action( 'enqueue_block_assets', array( $this, 'enqueue_assets' ) );
	}

	public function register_block() {
		wp_register_script(
			'favorite-movie-quote-block',
			plugin_dir_url( __FILE__ ) . 'favorite-movie-quote-block.js',
			array( 'wp-blocks', 'wp-editor', 'wp-element' ),
			filemtime( plugin_dir_path( __FILE__ ) . 'favorite-movie-quote-block.js' )
		);

		register_block_type( 'movies/favorite-movie-quote', array(
			'editor_script' => 'favorite-movie-quote-block',
			'render_callback' => array( $this, 'render_block' ),
		) );
	}

	public function enqueue_assets() {
		wp_enqueue_script(
			'favorite-movie-quote-block',
			plugin_dir_url( __FILE__ ) . 'favorite-movie-quote-block.js',
			array( 'wp-blocks', 'wp-editor', 'wp-element' ),
			filemtime( plugin_dir_path( __FILE__ ) . 'favorite-movie-quote-block.js' )
		);
	}

	public function render_block( $attributes ) {
		$quote = isset( $attributes['content'] ) ? $attributes['content'] : '';
		return '<blockquote>' . $quote . '</blockquote>';
	}

}

new Favorite_Movie_Quote();