<?php
/**
 * Plugin Name: Headingo
 * Description: Choose which heading levels are visible in the Classic Editor toolbar.
 * Version:		1.0.0
 * Author:      Jamie Dąbrowiecki
 * Author URI:  https://www.jdab.co.uk
 * License:     GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: headingo
 */

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

function headingo_default_headings() {
	return [ 'h1','h2','h3','h4','h5','h6','pre' ];
}

add_action( 'admin_menu', function () {
	add_options_page(
		__( 'Heading Visibility', 'headingo' ),
		__( 'Heading Visibility', 'headingo' ),
		'manage_options',
		'heading-visibility',
		'headingo_render_settings_page'
	);
});

add_action( 'admin_init', function () {
	register_setting(
		'headingo_settings',
		'headingo_allowed_headings',
		[
			'type' => 'array',
			'sanitize_callback' => 'headingo_sanitize_headings',
			'default' => headingo_default_headings(),
		]
	);
});

function headingo_sanitize_headings( $input ) {
	$allowed = [ 'h1','h2','h3','h4','h5','h6','pre' ];
	return array_values( array_intersect( (array) $input, $allowed ) );
}

function headingo_get_allowed_headings() {
	return get_option( 'headingo_allowed_headings', headingo_default_headings() );
}

function headingo_render_settings_page() {
	$allowed = headingo_get_allowed_headings();
	$labels = [
		'h1'  => __( 'Heading 1', 'headingo' ),
		'h2'  => __( 'Heading 2', 'headingo' ),
		'h3'  => __( 'Heading 3', 'headingo' ),
		'h4'  => __( 'Heading 4', 'headingo' ),
		'h5'  => __( 'Heading 5', 'headingo' ),
		'h6'  => __( 'Heading 6', 'headingo' ),
		'pre' => __( 'Preformatted', 'headingo' ),
	];
	?>
	<div class="wrap">
		<h1><?php esc_html_e( 'Heading Visibility', 'headingo' ); ?></h1>

		<p>
			<?php esc_html_e(
				'This plugin applies only to the Classic Editor. The Block Editor (Gutenberg) does not allow plugins to reliably control available heading levels, as these are governed by editor and theme configuration (theme.json) by design.',
				'headingo'
			); ?>
		</p>

		<p>
			<?php esc_html_e(
				'The heading levels selected below will be available when editing content in the Classic Editor. Uncheck any heading levels you wish to remove from the Classic Editor toolbar. The Paragraph option is always available and cannot be removed.',
				'headingo'
			); ?>
		</p>

		<form method="post" action="options.php">
			<?php settings_fields( 'headingo_settings' ); ?>

			<table class="form-table">
				<tr>
					<td>
						<?php foreach ( $labels as $key => $label ) : ?>
							<label style="display:block; margin-bottom:6px;">
								<input type="checkbox"
									name="headingo_allowed_headings[]"
									value="<?php echo esc_attr( $key ); ?>"
									<?php checked( in_array( $key, $allowed, true ) ); ?> />
								<?php echo esc_html( $label ); ?>
							</label>
						<?php endforeach; ?>
					</td>
				</tr>
			</table>

			<?php submit_button(); ?>
		</form>
	</div>
	<?php
}

add_filter( 'tiny_mce_before_init', function ( $init ) {
	if ( ! current_user_can( 'edit_posts' ) ) {
		return $init;
	}

	$allowed = headingo_get_allowed_headings();

	$formats = [ 'Paragraph=p' ];

	foreach ( [1,2,3,4,5,6] as $level ) {
		if ( in_array( "h{$level}", $allowed, true ) ) {
			$formats[] = "Heading {$level}=h{$level}";
		}
	}

	if ( in_array( 'pre', $allowed, true ) ) {
		$formats[] = 'Preformatted=pre';
	}

	$init['block_formats'] = implode( ';', $formats );
	return $init;
});