<?php

/**
 * Plugin glomex_oEmbed
 *
 * @package   glomex_oEmbed
 * @author    glomex <info@glomex.com>
 * @copyright 2022 glomex
 * @license   Apache-2.0
 * @link      https://glomex.com
 */

namespace glomex_oEmbed\Internals;

use glomex_oEmbed\Engine\Base;

/**
 * Internal glomex oEmbed integration wrapper
 */
class OembedHandler extends Base {

	/**
	 * Initialize integration
	 *
	 * @return bool
	 */
	public function initialize() {
		parent::initialize();

		( new OembedExtender )->run_hooks();
		new OembedHooksProvider;

		\add_action(
			'init',
			function() {
				global $wp_embed;

				/**
				 * Remove our oEmbed integration provided by theme (in case it is still present for some reason…)
				 */
				\wp_embed_unregister_handler( 'glomex-embed' );

				/**
				 * Register integration
				 */
				$wp_embed->register_handler(
					'glomex-embed-plugin',
					'#https://exchange.glomex.com/*+#',
					function ( array $matches, array $attr, string $url, $rawattr ): string {
						return ( new OembedExtender )->init( $matches, $attr, $url, $rawattr );
					}
				);
			},
			20
		);

		return true;
	}

}
