<?php

namespace EasyCriticalCSS;

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

abstract class Compatibility {
	/**
	 * Helper to output a standardized admin warning with optional action button.
	 *
	 * @param string $message Message body (already translated).
	 * @param string|null $button_label Optional button label.
	 * @param string|null $button_url Optional button URL (escaped by caller).
	 */
	protected static function render_admin_notice( $message, $button_label = null, $button_url = null ) {
		?>
		<div class="notice notice-warning">
			<p><?php echo wp_kses_post( $message ); ?></p>
			<?php if ( $button_label && $button_url && current_user_can( 'manage_options' ) ) { ?>
				<p>
					<a href="<?php echo esc_url( $button_url ); ?>" class="button button-primary"><?php echo esc_html( $button_label ); ?></a>
				</p>
			<?php } ?>
		</div>
		<?php
	}

	/**
	 * Simple wrapper to register a notice action on admin_notices.
	 *
	 * @param callable $callback
	 */
	protected static function add_admin_notice_hook( $callback ) {
		add_action( 'admin_notices', $callback );
	}

	/**
	 * Add an action only when a condition callback returns truthy.
	 *
	 * @param callable $condition_cb Callback that returns boolean.
	 * @param string $hook WP action hook name.
	 * @param callable $callback Callback to attach when condition is true.
	 * @param int $priority Hook priority.
	 */
	protected static function add_conditional_action( $condition_cb, $hook, $callback, $priority = 10 ) {
		if ( is_callable( $condition_cb ) && call_user_func( $condition_cb ) ) {
			add_action( $hook, $callback, $priority );
		}
	}

	/**
	 * Add a filter only when a condition callback returns truthy.
	 *
	 * @param callable $condition_cb
	 * @param string $hook
	 * @param callable $callback
	 * @param int $priority
	 * @param int $accepted_args
	 */
	protected static function add_conditional_filter( $condition_cb, $hook, $callback, $priority = 10, $accepted_args = 1 ) {
		if ( is_callable( $condition_cb ) && call_user_func( $condition_cb ) ) {
			add_filter( $hook, $callback, $priority, $accepted_args );
		}
	}
}
