<?php
/**
 * Plugin Name:       RSM Content Widgets
 * Plugin URI:        https://rsmspark.com/rsm-content-widgets/
 * Description:       Builder-agnostic content widgets — shortcodes, Gutenberg blocks, and optional SiteOrigin Page Builder support.
 * Version:           2.39.11
 * Requires at least: 6.0
 * Tested up to:      7.0
 * Requires PHP:      8.0
 * Author:            RSMSpark
 * Author URI:        https://rsmspark.com
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       rsm-content-widgets
 * Domain Path:       /languages
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

define( 'RSMSPARK_CONTENT_WIDGETS_VERSION', '2.39.11' );
define( 'RSMSPARK_CONTENT_WIDGETS_FILE', __FILE__ );
define( 'RSMSPARK_CONTENT_WIDGETS_PATH', plugin_dir_path( __FILE__ ) );
define( 'RSMSPARK_CONTENT_WIDGETS_URL', plugin_dir_url( __FILE__ ) );
define( 'RSMSPARK_CONTENT_WIDGETS_BASENAME', plugin_basename( __FILE__ ) );
define( 'RSMSPARK_CONTENT_WIDGETS_TEXT_DOMAIN', 'rsm-content-widgets' );
define( 'RSMSPARK_CONTENT_WIDGETS_SLUG', 'rsm-content-widgets' );

require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'rsm-shared/includes/rsm-suite-submenu-compat.php';
require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'rsm-shared/bootstrap.php';

/**
 * Bootstrap RSM Content Widgets.
 */
final class Rsmspark_Content_Widgets_Plugin {

	/**
	 * @var bool
	 */
	private static $booted = false;

	/**
	 * @return void
	 */
	public static function init() {
		if ( self::$booted ) {
			return;
		}

		self::$booted = true;

		self::load_includes();
		Rsmspark_Content_Widgets_Migration::init();
		// Free WordPress.org build: no license-client registration or Licensing admin tab.

		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/buttons/class-rsmspark-buttons.php';
		Rsmspark_Buttons::init();

		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/testimonials/class-rsmspark-testimonials.php';
		Rsmspark_Testimonials::init();

		Rsmspark_Content_Widgets_Admin::init();
		Rsmspark_Content_Widgets_Assets::init();
		Rsmspark_Content_Widgets_Shortcodes::init();
		Rsmspark_Content_Widgets_Blocks::init();
		Rsmspark_Content_Widgets_Block_Layout::init();
		Rsmspark_Content_Widgets_Bootstrap::init();
		Rsmspark_Content_Widgets_SiteOrigin::init();
		Rsmspark_Content_Widgets_Media_Picker::register();

		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-widget-json.php';
		Rsmspark_Content_Widgets_Widget_Json::init();
	}

	/**
	 * @return void
	 */
	private static function load_includes() {
		$includes = array(
			'class-rsmspark-content-widgets-registry.php',
			'class-rsmspark-content-widgets-settings.php',
			'class-rsmspark-content-widgets-license.php',
			'class-rsmspark-content-widgets-kses.php',
			'class-rsmspark-content-widgets-render.php',
			'class-rsmspark-content-widgets-focal-point.php',
			'class-rsmspark-content-widgets-schema.php',
			'class-rsmspark-content-widgets-shortcodes.php',
			'class-rsmspark-content-widgets-blocks.php',
			'class-rsmspark-content-widgets-block-layout.php',
			'class-rsmspark-content-widgets-assets.php',
			'class-rsmspark-content-widgets-media-picker.php',
			'class-rsmspark-content-widgets-siteorigin.php',
			'class-rsmspark-content-widgets-migration.php',
			'class-rsmspark-content-widgets-admin.php',
			'widgets/class-rsmspark-content-widgets-bootstrap.php',
		);

		foreach ( $includes as $file ) {
			require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/' . $file;
		}

		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/rsm-editor/bootstrap.php';
	}

}

add_action( 'plugins_loaded', array( 'Rsmspark_Content_Widgets_Plugin', 'init' ), 15 );

add_action(
	'init',
	static function () {
		if ( ! class_exists( 'Rsmspark_Getting_Started_Notice', false ) ) {
			return;
		}

		Rsmspark_Getting_Started_Notice::register(
			array(
				'slug'            => 'rsmspark-content-widgets',
				'plugin_version'  => RSMSPARK_CONTENT_WIDGETS_VERSION,
				'plugin_path'     => RSMSPARK_CONTENT_WIDGETS_PATH,
				'plugin_url'      => RSMSPARK_CONTENT_WIDGETS_URL,
				'text_domain'     => RSMSPARK_CONTENT_WIDGETS_TEXT_DOMAIN,
				'message'      => __( 'Welcome to RSM Content Widgets. Explore widgets under RSM → Content Widgets, then add them in the block editor or SiteOrigin Page Builder.', 'rsm-content-widgets' ),
				'screen_slugs' => array( 'rsmspark-content-widgets', 'rsm-buttons' ),
			)
		);
	},
	10
);

register_activation_hook(
	RSMSPARK_CONTENT_WIDGETS_FILE,
	static function () {
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-registry.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-settings.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-license.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-siteorigin.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-migration.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-bootstrap.php';

		Rsmspark_Content_Widgets_Migration::run_activation();
		Rsmspark_Content_Widgets_Bootstrap::load_classes();
		Rsmspark_Content_Widgets_Settings::ensure_defaults();
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/buttons/class-rsmspark-buttons.php';
		Rsmspark_Buttons::activate();
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/testimonials/class-rsmspark-testimonials.php';
		Rsmspark_Testimonials::activate();
		Rsmspark_Content_Widgets_SiteOrigin::activate_widgets();

		if ( class_exists( 'Rsmspark_Getting_Started_Notice', false ) ) {
			Rsmspark_Getting_Started_Notice::flag_on_activation( 'rsmspark-content-widgets' );
		}
	}
);

register_deactivation_hook(
	RSMSPARK_CONTENT_WIDGETS_FILE,
	static function () {
		// Reserved for scheduled cleanup or rewrite flushes.
	}
);

/**
 * Load RSM Spark News dashboard widget (once per site).
 *
 * @return void
 */
function rsmspark_content_widgets_load_spark_news() {
	if ( function_exists( 'rsmspark_suite_load_spark_news_module' ) ) {
		rsmspark_suite_load_spark_news_module(
			RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/rsmspark-news.php',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets',
			RSMSPARK_CONTENT_WIDGETS_PATH . 'assets'
		);
	}
}
add_action( 'plugins_loaded', 'rsmspark_content_widgets_load_spark_news', 5 );

/**
 * RSMSpark theme color bridge for editor standards (wp-admin has no front-end CSS vars).
 *
 * @param array<int, array<string, string>> $colors Existing theme colors.
 * @return array<int, array<string, string>>
 */
function rsmspark_content_widgets_editor_theme_colors( $colors ) {
	if ( ! function_exists( 'get_theme_mod' ) ) {
		return $colors;
	}

	$template = get_template();
	if ( 'openrsm-start' !== $template && 'openrsm' !== $template ) {
		return $colors;
	}

	$map = array(
		array(
			'name' => __( 'Primary', 'rsm-content-widgets' ),
			'hex'  => (string) get_theme_mod( 'openrsm_primary_color', '#2563eb' ),
		),
		array(
			'name' => __( 'Secondary', 'rsm-content-widgets' ),
			'hex'  => (string) get_theme_mod( 'openrsm_secondary_color', '#0f172a' ),
		),
		array(
			'name' => __( 'Accent', 'rsm-content-widgets' ),
			'hex'  => (string) get_theme_mod( 'openrsm_accent_color', '#f97316' ),
		),
		array(
			'name' => __( 'Text', 'rsm-content-widgets' ),
			'hex'  => (string) get_theme_mod( 'openrsm_text_color', '#1f2937' ),
		),
		array(
			'name' => __( 'Background', 'rsm-content-widgets' ),
			'hex'  => (string) get_theme_mod( 'openrsm_background_color', '#ffffff' ),
		),
	);

	return array_merge( is_array( $colors ) ? $colors : array(), $map );
}
add_filter( 'rsmspark_editor_theme_colors', 'rsmspark_content_widgets_editor_theme_colors' );
