<?php
/**
 * Plugin Name:       JOC Blockora Morph - Professional Blocks
 * Description:       Import, customize, and manage Gutenberg blocks with variants, shortcodes, and theme branding.
 * Version:           0.3.15
 * Requires at least: 6.4
 * Requires PHP:      8.0
 * Author:            JayOnCode
 * Author URI:        https://www.jayoncode.com/
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       joc-blockora-morph
 * Domain Path:       /languages
 *
 * @package JOCBM
 *
 * Copyright (C) 2026 JayOnCode
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

defined( 'ABSPATH' ) || exit;

define( 'JOCBM_VERSION', '0.3.15' );
define( 'JOCBM_DB_VERSION', '1.5.0' );
/** Bump when catalog templates or placement attr migrations need a one-time re-sync. */
define( 'JOCBM_SEED_VERSION', '2.0.74-beta' );
define( 'JOCBM_FILE', __FILE__ );
define( 'JOCBM_PATH', plugin_dir_path( __FILE__ ) );
define( 'JOCBM_URL', plugin_dir_url( __FILE__ ) );
define( 'JOCBM_BASENAME', plugin_basename( __FILE__ ) );

/** @var string Primary shortcode tag (4+ char prefix). */
define( 'JOCBM_SHORTCODE', 'jocbm_blockora_morph' );

require_once JOCBM_PATH . 'includes/Autoloader.php';

JOCBM\Autoloader::register();

/**
 * Activation with environment guard.
 */
register_activation_hook(
	__FILE__,
	static function () {
		$result = JOCBM\Compatibility::assert_can_activate();
		if ( is_wp_error( $result ) ) {
			wp_die(
				esc_html( $result->get_error_message() ),
				esc_html__( 'JOC Blockora Morph activation failed', 'joc-blockora-morph' ),
				array(
					'back_link' => true,
					'response'  => 200,
				)
			);
		}
		JOCBM\Activator::activate();
	}
);

register_deactivation_hook( __FILE__, array( 'JOCBM\\Deactivator', 'deactivate' ) );

/**
 * Boot the plugin.
 *
 * @return \JOCBM\Plugin
 */
function jocbm(): JOCBM\Plugin {
	return JOCBM\Plugin::instance();
}

/**
 * Whether a Pro extension is active.
 * Pro add-ons should return true via the `jocbm_is_pro` filter.
 *
 * @return bool
 */
function jocbm_is_pro(): bool {
	return (bool) apply_filters( 'jocbm_is_pro', false );
}

/**
 * Re-sync library templates from all registered template directories.
 *
 * Call from Pro on activation (after registering `jocbm_template_dirs`).
 */
function jocbm_resync_library(): void {
	\JOCBM\Database\Seeder::seed();
	$fp = \JOCBM\Services\TemplateLibrary::fingerprint();
	if ( $fp ) {
		update_option( 'jocbm_templates_fp', $fp );
	}

	/**
	 * Fires after a manual / Pro-triggered library resync.
	 */
	do_action( 'jocbm_library_synced' );
}

add_action(
	'plugins_loaded',
	static function () {
		if ( ! JOCBM\Compatibility::is_compatible() ) {
			JOCBM\Compatibility::register_incompatible();
			return;
		}

		JOCBM\Compatibility::register_untested();
		add_action( 'admin_init', array( 'JOCBM\\Compatibility', 'maybe_dismiss_untested' ) );

		jocbm()->boot();
	},
	20
);
