<?php
/**
 * Optional SiteOrigin Page Builder compatibility layer.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Content_Widgets_SiteOrigin
 *
 * Refactored in compat branch - Phase 0
 */
class Rsmspark_Content_Widgets_SiteOrigin {

	/**
	 * SiteOrigin Page Builder widget PHP class names.
	 *
	 * @return string[]
	 */
	public static function panel_widget_class_names() {
		return array(
			'Rsmspark_Content_Widgets_Hero_Widget',
			'Rsmspark_Content_Widgets_Tabs_Widget',
			'Rsmspark_Content_Widgets_Accordion_Widget',
			'Rsmspark_Content_Widgets_Icon_Box_Grid_Widget',
			'Rsmspark_Content_Widgets_Features_Widget',
			'Rsmspark_Content_Widgets_Image_Widget',
			'Rsmspark_Content_Widgets_Headline_Widget',
			'Rsmspark_Content_Widgets_Testimonials_Team_Widget',
			'Rsmspark_Content_Widgets_Buttons_Widget',
			'Rsmspark_Content_Widgets_Footer_Widget',
			'Rsmspark_Content_Widgets_Logo_Grid_Widget',
			'Rsmspark_Content_Widgets_Stats_Counter_Widget',
			'Rsmspark_Content_Widgets_Event_Countdown_Widget',
		);
	}

	/**
	 * SiteOrigin Widgets Bundle Gutenberg block name for a widget PHP class.
	 *
	 * @param string $widget_class Widget class name.
	 * @return string
	 */
	public static function sowb_gutenberg_block_name( $widget_class ) {
		$class = (string) $widget_class;
		if ( '' === $class ) {
			return '';
		}

		return 'sowb/' . strtolower( str_replace( array( '_', '\\' ), '-', $class ) );
	}

	/**
	 * SOWB block names that duplicate native rsm-content-widgets/* blocks.
	 *
	 * @return string[]
	 */
	public static function sowb_gutenberg_blocks_with_native_duplicates() {
		if ( ! class_exists( 'SiteOrigin_Widget', false ) ) {
			return array();
		}

		if ( ! Rsmspark_Content_Widgets_Settings::block_editor_ui_enabled() ) {
			return array();
		}

		$blocks = array();
		foreach ( self::panel_widget_class_names() as $class ) {
			$name = self::sowb_gutenberg_block_name( $class );
			if ( '' !== $name ) {
				$blocks[] = $name;
			}
		}

		return $blocks;
	}

	/**
	 * @return void
	 */
	public static function init() {
		if ( ! class_exists( 'SiteOrigin_Widget', false ) ) {
			add_action( 'admin_notices', array( __CLASS__, 'maybe_dependency_notice' ) );
			return;
		}

		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-legacy.php';

		if ( ! Rsmspark_Content_Widgets_Settings::siteorigin_enabled() ) {
			return;
		}

		add_filter( 'siteorigin_widgets_widget_folders', array( __CLASS__, 'register_widget_folders' ), 5 );
		add_filter( 'siteorigin_widgets_active_widgets', array( __CLASS__, 'filter_active_widgets' ), 20 );
		add_filter( 'siteorigin_panels_widgets', array( __CLASS__, 'brand_panels_widgets' ), 15 );
		add_filter( 'siteorigin_panels_widget_dialog_tabs', array( __CLASS__, 'register_panels_widget_tab' ) );
		add_filter( 'siteorigin_panels_data', array( __CLASS__, 'normalize_panels_button_widgets' ), 15, 2 );
		add_filter( 'siteorigin_panels_data', array( __CLASS__, 'normalize_panels_image_widgets' ), 15, 2 );
		add_filter( 'siteorigin_widgets_template_file_buttons', array( __CLASS__, 'resolve_buttons_template' ), 5, 3 );
		add_action( 'rsmspark_content_widgets_license_activated', array( __CLASS__, 'activate_widgets' ) );
		add_action( 'init', array( __CLASS__, 'register_widget_json_admin_hooks' ), 20 );
		add_action( 'siteorigin_widgets_enqueue_admin_scripts', array( __CLASS__, 'enqueue_widget_json_admin' ), 5, 2 );
	}

	/**
	 * Per-widget SO hooks — the global hook often passes a slug/id, not the PHP class name.
	 *
	 * @return void
	 */
	public static function register_widget_json_admin_hooks() {
		if ( ! class_exists( 'SiteOrigin_Widget', false ) || ! Rsmspark_Content_Widgets_Settings::siteorigin_enabled() ) {
			return;
		}

		if ( ! class_exists( 'Rsmspark_Content_Widgets_Widget_Json_Registry', false ) ) {
			require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-widget-json-registry.php';
		}

		if ( class_exists( 'Rsmspark_Content_Widgets_Bootstrap', false ) ) {
			Rsmspark_Content_Widgets_Bootstrap::load_classes();
		}

		foreach ( Rsmspark_Content_Widgets_Registry::widget_ids() as $registry_id ) {
			if ( in_array( $registry_id, self::widget_json_skip_form_ids(), true ) ) {
				continue;
			}

			if ( ! Rsmspark_Content_Widgets_Widget_Json_Registry::supports_widget_json( $registry_id ) ) {
				continue;
			}

			$so_id = Rsmspark_Content_Widgets_Registry::siteorigin_folder_id( $registry_id );
			add_action(
				'siteorigin_widgets_enqueue_admin_scripts_' . $so_id,
				array( __CLASS__, 'enqueue_generic_widget_json_admin_assets' )
			);
		}
	}

	/**
	 * Enqueue generic widget JSON admin assets when a widget opens its SO form.
	 *
	 * @param SiteOrigin_Widget|null $widget Widget instance.
	 * @return void
	 */
	public static function maybe_enqueue_widget_json_admin_for_widget( $widget ) {
		if ( ! $widget instanceof SiteOrigin_Widget ) {
			return;
		}

		$registry_id = '';
		if ( class_exists( 'Rsmspark_Content_Widgets_Loader', false ) ) {
			$registry_id = sanitize_key( (string) Rsmspark_Content_Widgets_Loader::registry_id( $widget->id_base ) );
		}

		if ( '' === $registry_id ) {
			$registry_id = self::resolve_registry_id_from_admin_context( (string) $widget->id_base );
		}

		if ( '' === $registry_id || in_array( $registry_id, self::widget_json_skip_form_ids(), true ) ) {
			return;
		}

		if ( ! class_exists( 'Rsmspark_Content_Widgets_Widget_Json_Registry', false ) ) {
			require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-widget-json-registry.php';
		}

		if ( ! Rsmspark_Content_Widgets_Widget_Json_Registry::supports_widget_json( $registry_id ) ) {
			return;
		}

		self::enqueue_generic_widget_json_admin_assets();
	}

	/**
	 * Shared Import / export JSON section for SiteOrigin widget forms.
	 *
	 * @param string $registry_id Widget registry id.
	 * @return array<string, mixed>
	 */
	public static function import_export_form_section( $registry_id ) {
		$registry_id = sanitize_key( (string) $registry_id );
		$defs        = Rsmspark_Content_Widgets_Registry::definitions();
		$label       = isset( $defs[ $registry_id ]['label'] ) ? (string) $defs[ $registry_id ]['label'] : $registry_id;

		return array(
			'type'   => 'section',
			'label'  => __( 'Import / export JSON', 'rsm-content-widgets' ),
			'fields' => array(
				'export_json' => array(
					'type'        => 'textarea',
					'label'       => __( 'Export JSON', 'rsm-content-widgets' ),
					'rows'        => 8,
					'description' => sprintf(
						/* translators: %s: widget label */
						__( 'Generate JSON from the current %s settings, then copy or save the text below.', 'rsm-content-widgets' ),
						$label
					)
						. '<p class="rsmspark-cw-widget-json-actions"><button type="button" class="button rsmspark-cw-widget-export-json" data-rsmspark-cw-widget="'
						. esc_attr( $registry_id )
						. '">'
						. esc_html__( 'Export JSON', 'rsm-content-widgets' )
						. '</button></p>',
				),
				'import_json' => array(
					'type'        => 'textarea',
					'label'       => __( 'Import JSON', 'rsm-content-widgets' ),
					'rows'        => 8,
					'description' => sprintf(
						/* translators: %s: widget label */
						__( 'Paste an RSM %s export (widget or block JSON), then click Apply import.', 'rsm-content-widgets' ),
						$label
					)
						. '<p class="rsmspark-cw-widget-json-actions"><button type="button" class="button button-primary rsmspark-cw-widget-import-json" data-rsmspark-cw-widget="'
						. esc_attr( $registry_id )
						. '">'
						. esc_html__( 'Apply import', 'rsm-content-widgets' )
						. '</button></p>',
				),
			),
		);
	}

	/**
	 * Prepend import/export section when the widget form does not already define one.
	 *
	 * @param string               $registry_id Widget registry id.
	 * @param array<string, mixed> $form        Widget form fields.
	 * @return array<string, mixed>
	 */
	public static function prepend_import_export_form( $registry_id, array $form ) {
		$registry_id = sanitize_key( (string) $registry_id );

		if ( isset( $form['import_export'] ) || in_array( $registry_id, self::widget_json_skip_form_ids(), true ) ) {
			return $form;
		}

		if ( class_exists( 'Rsmspark_Content_Widgets_Bootstrap', false ) ) {
			Rsmspark_Content_Widgets_Bootstrap::load_classes();
		}

		if ( ! class_exists( 'Rsmspark_Content_Widgets_Widget_Json_Registry', false ) ) {
			require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-widget-json-registry.php';
		}

		if ( ! Rsmspark_Content_Widgets_Widget_Json_Registry::supports_widget_json( $registry_id ) ) {
			return $form;
		}

		return array_merge(
			array(
				'import_export' => self::import_export_form_section( $registry_id ),
			),
			$form
		);
	}

	/**
	 * Widget ids with bespoke import/export UI (presets or dedicated admin JS).
	 *
	 * @return string[]
	 */
	public static function widget_json_skip_form_ids() {
		if ( ! class_exists( 'Rsmspark_Content_Widgets_Widget_Json_Registry', false ) ) {
			require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-widget-json-registry.php';
		}

		return Rsmspark_Content_Widgets_Widget_Json_Registry::widgets_with_import_export_form();
	}

	/**
	 * @param string|SiteOrigin_Widget $widget_class Widget PHP class, slug, or instance token.
	 * @param SiteOrigin_Widget|null   $widget       Widget instance.
	 * @return void
	 */
	public static function enqueue_widget_json_admin( $widget_class, $widget = null ) {
		$registry_id = self::resolve_registry_id_from_admin_context( (string) $widget_class );

		if ( '' === $registry_id && $widget instanceof SiteOrigin_Widget ) {
			$registry_id = self::resolve_registry_id_from_admin_context( (string) $widget->id_base );
		}

		if ( '' === $registry_id || in_array( $registry_id, self::widget_json_skip_form_ids(), true ) ) {
			return;
		}

		if ( ! class_exists( 'Rsmspark_Content_Widgets_Widget_Json_Registry', false ) ) {
			require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-widget-json-registry.php';
		}

		if ( ! Rsmspark_Content_Widgets_Widget_Json_Registry::supports_widget_json( $registry_id ) ) {
			return;
		}

		self::enqueue_generic_widget_json_admin_assets();
	}

	/**
	 * @return void
	 */
	public static function enqueue_generic_widget_json_admin_assets() {
		if ( ! class_exists( 'Rsmspark_Content_Widgets_Assets', false ) ) {
			return;
		}

		Rsmspark_Content_Widgets_Assets::enqueue_export_json_ui_assets();

		wp_enqueue_style(
			'rsmspark-cw-admin-widgets',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/css/admin-widgets.css',
			array( 'rsmspark-cw-export-json-ui' ),
			RSMSPARK_CONTENT_WIDGETS_VERSION
		);

		wp_enqueue_script(
			'rsmspark-cw-widget-admin',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-cw-widget-admin.js',
			array( 'jquery', 'rsmspark-cw-export-json-ui' ),
			RSMSPARK_CONTENT_WIDGETS_VERSION,
			true
		);

		$handle = 'rsmspark-cw-widget-admin';
		$data   = wp_scripts()->get_data( $handle, 'data' );
		if (
			! is_string( $data )
			|| (
				false === strpos( $data, 'rsmsparkCwWidgetAdmin' )
				&& false === strpos( $data, 'rsmCwWidgetAdmin' )
			)
		) {
			wp_localize_script(
				$handle,
				'rsmsparkCwWidgetAdmin',
				array(
					'ajaxUrl' => admin_url( 'admin-ajax.php' ),
					'nonce'   => wp_create_nonce( 'rsmspark_cw_widget_json' ),
					'i18n'    => array(
						'exportLabel'      => __( 'Generated export (also copied to your clipboard):', 'rsm-content-widgets' ),
						'exportPanelTitle' => __( 'JSON export ready', 'rsm-content-widgets' ),
						'importError'      => __( 'Paste valid RSM widget JSON export data (widget or block export).', 'rsm-content-widgets' ),
						'importApplied'    => __( 'Import applied. Review repeater rows if counts changed.', 'rsm-content-widgets' ),
						'confirm'          => __( 'Apply this import? Current settings will be overwritten.', 'rsm-content-widgets' ),
						'requestError'     => __( 'Could not process widget JSON. Try again.', 'rsm-content-widgets' ),
						'setupError'       => __( 'Export could not start. Reload the widget form and try again.', 'rsm-content-widgets' ),
					),
				)
			);
		}
	}

	/**
	 * @param string $token Widget PHP class name or SiteOrigin widget slug/id.
	 * @return string Registry id or empty string.
	 */
	public static function resolve_registry_id_from_admin_context( $token ) {
		$token = trim( (string) $token );
		if ( '' === $token ) {
			return '';
		}

		$from_class = self::registry_id_from_widget_class( $token );
		if ( '' !== $from_class ) {
			return $from_class;
		}

		if ( class_exists( 'Rsmspark_Content_Widgets_Loader', false ) ) {
			return sanitize_key( (string) Rsmspark_Content_Widgets_Loader::registry_id( $token ) );
		}

		return Rsmspark_Content_Widgets_Registry::registry_id_from_block_slug( $token );
	}

	/**
	 * @param string $widget_class SiteOrigin widget PHP class name.
	 * @return string Registry id or empty string.
	 */
	public static function registry_id_from_widget_class( $widget_class ) {
		$map = array(
			'Rsmspark_Content_Widgets_Hero_Widget'               => 'hero',
			'Rsmspark_Content_Widgets_Tabs_Widget'               => 'tabs',
			'Rsmspark_Content_Widgets_Accordion_Widget'          => 'accordion',
			'Rsmspark_Content_Widgets_Icon_Box_Grid_Widget'      => 'icon_box_grid',
			'Rsmspark_Content_Widgets_Features_Widget'             => 'features',
			'Rsmspark_Content_Widgets_Image_Widget'              => 'image',
			'Rsmspark_Content_Widgets_Headline_Widget'           => 'headline',
			'Rsmspark_Content_Widgets_Testimonials_Team_Widget'  => 'testimonials_team',
			'Rsmspark_Content_Widgets_Buttons_Widget'            => 'buttons',
			'Rsmspark_Content_Widgets_Footer_Widget'             => 'footer',
			'Rsmspark_Content_Widgets_Logo_Grid_Widget'          => 'logo_grid',
			'Rsmspark_Content_Widgets_Stats_Counter_Widget'      => 'stats_counter',
			'Rsmspark_Content_Widgets_Event_Countdown_Widget'    => 'event_countdown',
		);

		return $map[ (string) $widget_class ] ?? '';
	}

	/**
	 * @return bool
	 */
	public static function is_available() {
		return class_exists( 'SiteOrigin_Widget', false ) && Rsmspark_Content_Widgets_Settings::siteorigin_enabled();
	}

	/**
	 * @param string $registry_id Widget registry id.
	 * @return bool
	 */
	public static function should_register_widget( $registry_id ) {
		if ( ! self::is_available() ) {
			return false;
		}

		if ( ! Rsmspark_Content_Widgets_Settings::is_enabled() ) {
			return false;
		}

		if ( ! Rsmspark_Content_Widgets_Settings::is_widget_enabled( $registry_id ) ) {
			return false;
		}

		if ( Rsmspark_Content_Widgets_Registry::is_pro_widget( $registry_id ) ) {
			if ( ! class_exists( 'Rsmspark_Content_Widgets_License', false ) ) {
				return false;
			}

			$feature = Rsmspark_Content_Widgets_Registry::required_feature( $registry_id );
			return Rsmspark_Content_Widgets_License::has_feature( $feature );
		}

		return true;
	}

	/**
	 * @return void
	 */
	public static function activate_widgets() {
		if ( ! class_exists( 'SiteOrigin_Widgets_Bundle', false ) ) {
			return;
		}

		$bundle = SiteOrigin_Widgets_Bundle::single();
		foreach ( array_keys( Rsmspark_Content_Widgets_Registry::definitions() ) as $widget_id ) {
			if ( self::should_register_widget( $widget_id ) ) {
				$bundle->activate_widget( Rsmspark_Content_Widgets_Registry::siteorigin_folder_id( $widget_id ), true );
			}
		}

		if ( self::should_register_widget( 'buttons' ) ) {
			$bundle->activate_widget( 'buttons', true );
		}
	}

	/**
	 * Normalize legacy RSM Button widget instances stored in Page Builder layouts.
	 *
	 * @param array<string, mixed> $panels_data Panels data.
	 * @param int                  $post_id     Post ID.
	 * @return array<string, mixed>
	 */
	public static function normalize_panels_button_widgets( $panels_data, $post_id ) {
		unset( $post_id );

		if ( empty( $panels_data['widgets'] ) || ! is_array( $panels_data['widgets'] ) ) {
			return $panels_data;
		}

		foreach ( $panels_data['widgets'] as $widget_id => $widget ) {
			if ( ! is_array( $widget ) ) {
				continue;
			}

			$class      = (string) ( $widget['panels_info']['class'] ?? '' );
			$widget_ref = (string) ( $widget['panels_info']['widget_id'] ?? '' );
			$is_button  = 'Rsmspark_Content_Widgets_Buttons_Widget' === $class
				|| in_array( $widget_ref, array( 'buttons', 'rsmspark-cw-button' ), true );

			if ( ! $is_button ) {
				continue;
			}

			$panels_info = isset( $widget['panels_info'] ) && is_array( $widget['panels_info'] )
				? $widget['panels_info']
				: array();
			$panels_info['class']     = 'Rsmspark_Content_Widgets_Buttons_Widget';
			$panels_info['widget_id'] = 'buttons';

			if ( class_exists( 'Rsmspark_Content_Widgets_Widget_Buttons', false ) ) {
				$widget = Rsmspark_Content_Widgets_Widget_Buttons::normalize_fields( $widget );
			}

			$widget['panels_info'] = $panels_info;
			$panels_data['widgets'][ $widget_id ] = $widget;
		}

		return $panels_data;
	}

	/**
	 * Normalize legacy RSM Image widget instances stored with the generic SO id "image".
	 *
	 * @param array<string, mixed> $panels_data Panels data.
	 * @param int                  $post_id     Post ID.
	 * @return array<string, mixed>
	 */
	public static function normalize_panels_image_widgets( $panels_data, $post_id ) {
		unset( $post_id );

		if ( empty( $panels_data['widgets'] ) || ! is_array( $panels_data['widgets'] ) ) {
			return $panels_data;
		}

		foreach ( $panels_data['widgets'] as $widget_id => $widget ) {
			if ( ! is_array( $widget ) ) {
				continue;
			}

			$class      = (string) ( $widget['panels_info']['class'] ?? '' );
			$widget_ref = (string) ( $widget['panels_info']['widget_id'] ?? '' );
			$is_image   = 'Rsmspark_Content_Widgets_Image_Widget' === $class
				|| in_array( $widget_ref, array( 'image', 'rsm-image' ), true );

			if ( ! $is_image ) {
				continue;
			}

			$panels_info = isset( $widget['panels_info'] ) && is_array( $widget['panels_info'] )
				? $widget['panels_info']
				: array();
			$panels_info['class']     = 'Rsmspark_Content_Widgets_Image_Widget';
			$panels_info['widget_id'] = 'rsm-image';

			$widget['panels_info']                = $panels_info;
			$panels_data['widgets'][ $widget_id ] = $widget;
		}

		return $panels_data;
	}

	/**
	 * @param array<string|int, string> $folders Widget folders.
	 * @return array<string|int, string>
	 */
	public static function register_widget_folders( $folders ) {
		if ( ! is_array( $folders ) ) {
			$folders = array();
		}

		$folders[] = RSMSPARK_CONTENT_WIDGETS_PATH . 'widgets/';
		return $folders;
	}

	/**
	 * @param array<string, bool> $active_widgets Active widget map.
	 * @return array<string, bool>
	 */
	public static function filter_active_widgets( $active_widgets ) {
		if ( ! is_array( $active_widgets ) ) {
			$active_widgets = array();
		}

		foreach ( Rsmspark_Content_Widgets_Registry::definitions() as $registry_id => $widget ) {
			$so_id = Rsmspark_Content_Widgets_Registry::siteorigin_folder_id( $registry_id );
			if ( self::should_register_widget( $registry_id ) ) {
				$active_widgets[ $so_id ] = true;
			} elseif ( isset( $active_widgets[ $so_id ] ) && ! empty( $widget['is_pro'] ) ) {
				unset( $active_widgets[ $so_id ] );
			}
		}

		return $active_widgets;
	}

	/**
	 * @param array<string, mixed> $widgets Page Builder widgets.
	 * @return array<string, mixed>
	 */
	public static function brand_panels_widgets( $widgets ) {
		if ( ! is_array( $widgets ) ) {
			return $widgets;
		}

		foreach ( self::panel_widget_class_names() as $class ) {
			if ( ! isset( $widgets[ $class ] ) || ! is_array( $widgets[ $class ] ) ) {
				continue;
			}

			$icon = (string) ( $widgets[ $class ]['icon'] ?? 'dashicons dashicons-admin-generic' );
			if ( false === strpos( $icon, 'rsmspark-cw-panels-icon' ) ) {
				$widgets[ $class ]['icon'] = 'rsmspark-cw-panels-icon ' . $icon;
			}

			$groups = is_array( $widgets[ $class ]['groups'] ?? null ) ? $widgets[ $class ]['groups'] : array();
			if ( ! in_array( 'rsmspark-content-widgets', $groups, true ) ) {
				$groups[] = 'rsmspark-content-widgets';
			}
			$widgets[ $class ]['groups'] = $groups;
		}

		return $widgets;
	}

	/**
	 * @param array<string, mixed> $tabs Dialog tabs.
	 * @return array<string, mixed>
	 */
	public static function register_panels_widget_tab( $tabs ) {
		if ( ! is_array( $tabs ) ) {
			$tabs = array();
		}

		$tabs['rsmspark-content-widgets'] = array(
			'title'  => __( 'RSM Content Widgets', 'rsm-content-widgets' ),
			'filter' => array(
				'groups' => array( 'rsmspark-content-widgets' ),
			),
		);

		return $tabs;
	}

	/**
	 * Resolve RSM Button tpl path (RSM Directory pattern — SO path map can miss custom widgets).
	 *
	 * @param string               $template_file Default template path.
	 * @param SiteOrigin_Widget    $widget        Widget instance.
	 * @param array<string, mixed> $instance      Widget instance data.
	 * @return string
	 */
	public static function resolve_buttons_template( $template_file, $widget, $instance ) {
		unset( $widget, $instance );

		$path = RSMSPARK_CONTENT_WIDGETS_PATH . 'widgets/buttons/buttons.tpl.php';
		return is_readable( $path ) ? $path : $template_file;
	}

	/**
	 * Show notice when SiteOrigin output is enabled in settings but the plugin is missing.
	 *
	 * @return void
	 */
	public static function maybe_dependency_notice() {
		if ( class_exists( 'SiteOrigin_Widget', false ) ) {
			return;
		}

		if ( ! Rsmspark_Content_Widgets_Settings::siteorigin_enabled() ) {
			return;
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( ! $screen || false === strpos( (string) $screen->id, Rsmspark_Content_Widgets_Admin::PAGE_SLUG ) ) {
			return;
		}

		?>
		<?php
		rsmspark_suite_render_admin_notice(
			wp_kses_post(
				sprintf(
					/* translators: %s: SiteOrigin Widgets Bundle plugin name */
					__( '<strong>RSM Content Widgets</strong> has SiteOrigin output enabled in settings, but <strong>%s</strong> is not active. Shortcodes and blocks still work.', 'rsm-content-widgets' ),
					'SiteOrigin Widgets Bundle'
				)
			),
			array( 'html' => true )
		);
	}
}
