<?php
/**
 * Plugin Name:       Synalysis Blog Importer for Nostr
 * Plugin URI:        https://github.com/synalysis/synalysis-blog-importer-for-nostr
 * Description:       Imports NIP-23 long-form articles from Nostr relays into WordPress with archive and single templates plus optional NIP-05 (not affiliated with the Nostr protocol or WordPress).
 * Version:           1.1.4
 * Requires at least: 6.0
 * Requires PHP:      8.1
 * Author:            Synalysis
 * Author URI:        https://github.com/synalysis
 * License:           MIT
 * License URI:       https://opensource.org/licenses/MIT
 * Text Domain:       synalysis-blog-importer-for-nostr
 * Domain Path:       /languages
 *
 * @package SynalysisBlogImporterForNostr
 */

declare(strict_types=1);

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

define( 'SYNALYSIS_BLOG_IMPORTER_FILE', __FILE__ );
define( 'SYNALYSIS_BLOG_IMPORTER_DIR', plugin_dir_path( __FILE__ ) );
define( 'SYNALYSIS_BLOG_IMPORTER_URL', plugin_dir_url( __FILE__ ) );
define( 'SYNALYSIS_BLOG_IMPORTER_VERSION', '1.1.4' );

if ( ! is_readable( SYNALYSIS_BLOG_IMPORTER_DIR . 'vendor/autoload.php' ) ) {
	add_action(
		'admin_notices',
		static function (): void {
			if ( ! current_user_can( 'activate_plugins' ) ) {
				return;
			}
			$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
			// Limit to Plugins screen (Guideline 11): avoid sitewide dashboard banners for a dev-only state.
			if ( ! $screen || $screen->id !== 'plugins' ) {
				return;
			}
			echo '<div class="notice notice-error"><p>';
			echo esc_html__( 'Synalysis Blog Importer for Nostr: run Composer in the plugin directory (composer install) so vendor/autoload.php exists.', 'synalysis-blog-importer-for-nostr' );
			echo '</p></div>';
		}
	);
	return;
}

require_once SYNALYSIS_BLOG_IMPORTER_DIR . 'vendor/autoload.php';
require_once SYNALYSIS_BLOG_IMPORTER_DIR . 'includes/functions.php';

/**
 * Plugin activation.
 */
function synalysis_blog_importer_activate(): void {
	if ( ! is_readable( SYNALYSIS_BLOG_IMPORTER_DIR . 'vendor/autoload.php' ) ) {
		return;
	}
	require_once SYNALYSIS_BLOG_IMPORTER_DIR . 'vendor/autoload.php';
	require_once SYNALYSIS_BLOG_IMPORTER_DIR . 'includes/functions.php';

	$cpt = new Synalysis_Blog_Importer_CPT();
	$cpt->do_register_post_type();
	$cpt->register_rewrite_rules();
	Synalysis_Blog_Importer_Nip05::register_rewrite_rules();
	flush_rewrite_rules( false );
	synalysis_blog_importer_schedule_cron();
}

/**
 * Plugin deactivation.
 */
function synalysis_blog_importer_deactivate(): void {
	wp_clear_scheduled_hook( 'synalysis_blog_importer_sync_cron' );
	flush_rewrite_rules( false );
}

register_activation_hook( SYNALYSIS_BLOG_IMPORTER_FILE, 'synalysis_blog_importer_activate' );
register_deactivation_hook( SYNALYSIS_BLOG_IMPORTER_FILE, 'synalysis_blog_importer_deactivate' );

$synalysis_blog_importer_plugin = new Synalysis_Blog_Importer_Plugin();
$synalysis_blog_importer_plugin->run();
