<?php

/**
 * WordPress plugin - "glomex oEmbed".
 *
 * @package   Glomex\Oembed
 * @author    glomex
 * @copyright 2024 glomex
 * @license   Apache-2.0
 * @link      https://glomex.com
 * @wordpress-plugin
 * Plugin Name:     glomex oEmbed
 * Plugin URI:      https://wordpress.org/plugins/glomex-oembed/
 * Description:     glomex oEmbed WP plugin
 * Version:         0.9.4
 * Author:          glomex
 * Author URI:      https://glomex.com/
 * Text Domain:     glomex-oembed
 * License:         Apache-2.0
 * License URI:     https://oss.ninja/apache-2.0-header/glomex
 * Domain Path:     /languages
 * Requires PHP:    7.4
 * WordPress-Plugin-Boilerplate-Powered: v3.3.0
 */

// If this file is called directly, abort.
if ( !defined( 'ABSPATH' ) ) {
	// @phpstan-ignore-next-line
	die( 'We\'re sorry, but you can not directly access this file.' );
}

define( 'GLOMEX_OEMBED_VERSION', '0.9.4' );
define( 'GLOMEX_OEMBED_TEXTDOMAIN', 'glomex-oembed' );
define( 'GLOMEX_OEMBED_NAME', 'glomex oEmbed' );
define( 'GLOMEX_OEMBED_PLUGIN_ROOT', plugin_dir_path( __FILE__ ) );
define( 'GLOMEX_OEMBED_PLUGIN_ABSOLUTE', __FILE__ );
define( 'GLOMEX_OEMBED_MIN_PHP_VERSION', '7.4' );
define( 'GLOMEX_OEMBED_WP_VERSION', '5.3' );

add_action(
	'init',
	static function () {
		load_plugin_textdomain(
			'glomex-oembed',
			false,
			dirname( plugin_basename( __FILE__ ) ) . '/languages'
		);
	}
);

if (
	version_compare( PHP_VERSION, GLOMEX_OEMBED_MIN_PHP_VERSION, '<=' ) ||
	!file_exists( GLOMEX_OEMBED_PLUGIN_ROOT . 'vendor-prod/autoload.php' )
) {
	add_action(
		'admin_init',
		static function() {
			deactivate_plugins( plugin_basename( __FILE__ ) );
		}
	);
	add_action(
		'admin_notices',
		static function() {
			echo wp_kses_post(
				sprintf(
					'<div class="notice notice-error"><p>%s</p><p>%s</p></div>',
					// translators: The placeholder is for PHP minimal version defined in a constant GLOMEX_OEMBEDMIN_PHP_VERSION
					sprintf( __( '"glomex oEmbed" is deactivated because requires PHP %s or newer and installed composer.', 'glomex-oembed' ), GLOMEX_OEMBED_MIN_PHP_VERSION ),
					sprintf( __( 'This error can be triggered because `vendor-prod` directory is either incomplete or missing. Please try reinstalling the plugin.', 'glomex-oembed' ), GLOMEX_OEMBED_MIN_PHP_VERSION )
				)
			);
		}
	);

	// Return early to prevent loading the plugin.
	return;
}

$glomexoembedLibraries = require GLOMEX_OEMBED_PLUGIN_ROOT . 'vendor-prod/autoload.php'; //phpcs:ignore

if ( file_exists( GLOMEX_OEMBED_PLUGIN_ROOT . 'functions/functions.php' ) ) {
	require_once GLOMEX_OEMBED_PLUGIN_ROOT . 'functions/functions.php';
}

if ( file_exists( GLOMEX_OEMBED_PLUGIN_ROOT . 'functions/debug.php' ) ) {
	require_once GLOMEX_OEMBED_PLUGIN_ROOT . 'functions/debug.php';
}

$requirements = new \Micropackage\Requirements\Requirements(
	'glomex oEmbed',
	array(
		'php'            => GLOMEX_OEMBED_MIN_PHP_VERSION,
		'php_extensions' => array( 'mbstring', 'curl' ),
		'wp'             => GLOMEX_OEMBED_WP_VERSION,
	)
);

if ( ! $requirements->satisfied() ) {
	$requirements->print_notice( __( 'glomex oEmbed plugin cannot operate because your system does not meet minimal requirements:', 'glomex-oembed' ) );

	add_action(
		'admin_init',
		static function() {
			deactivate_plugins( plugin_basename( __FILE__ ) );
		}
	);

	return;
}

// Enables permanently dismissible notice
if ( class_exists( '\WPDesk\Notice\AjaxHandler' ) ) {
	add_action(
		'init',
		function () {
			( new \WPDesk\Notice\AjaxHandler )->hooks();
		}
	);
}

if ( ! wp_installing() ) {
	register_activation_hook( GLOMEX_OEMBED_TEXTDOMAIN . '/' . GLOMEX_OEMBED_TEXTDOMAIN . '.php', array( new \glomex_oEmbed\Backend\ActDeact, 'activate' ) );
	register_deactivation_hook( GLOMEX_OEMBED_TEXTDOMAIN . '/' . GLOMEX_OEMBED_TEXTDOMAIN . '.php', array( new \glomex_oEmbed\Backend\ActDeact, 'deactivate' ) );
	add_action(
		'plugins_loaded',
		static function () use ( $glomexoembedLibraries ) {
			new \glomex_oEmbed\Engine\Initialize( $glomexoembedLibraries );
		}
	);
}
