<?php
/**
 * Plugin Name: WP-Admin Tabbed Settings Pages (DEMO)
 * Description: A sample implementation of tabbed settings pages within WP-Admin.
 * Author:      Steve Grunwell
 * Author URI:  https://stevegrunwell.com
 */

namespace SteveGrunwell\WPAdminTabs\Demo;

/**
 * Register the individual settings sections.
 */
add_action( 'admin_init', function () {
	// Register the setting.
	register_setting(
		'wp-admin-tabs',
		'wp-admin-tabs-demo-option'
	);

	// Register multiple sections.
	for ( $i = 1; $i <= 5; $i++ ) {
		add_settings_section(
			"wp-admin-tabs-demo-section$i",
			"Section $i",
			__NAMESPACE__ . '\render_section_heading',
			'wp-admin-tabs-demo'
		);
		add_settings_field(
			"section$i-input",
			"Section $i Title",
			__NAMESPACE__ . '\render_input',
			'wp-admin-tabs-demo',
			"wp-admin-tabs-demo-section$i",
			[
				'label_for' => "section$i-input",
			]
		);
		add_settings_field(
			"section$i-textarea",
			"Section $i Description",
			__NAMESPACE__ . '\render_textarea',
			'wp-admin-tabs-demo',
			"wp-admin-tabs-demo-section$i",
			[
				'label_for' => "section$i-textarea",
			]
		);
	}
} );

/**
 * Register the settings pages — one tabbed, one not.
 */
add_action( 'admin_menu', function () {
	add_options_page(
		__( 'Settings Page Demo (Classic)' ),
		__( 'Non-tabbed Demo' ),
		'manage_options',
		'wp-admin-tabs-demo-classic',
		__NAMESPACE__ . '\render_classic_settings_page'
	);
	add_options_page(
		__( 'Settings Page Demo (Tabbed)' ),
		__( 'Tabbed Demo' ),
		'manage_options',
		'wp-admin-tabs-demo-tabbed',
		__NAMESPACE__ . '\render_tabbed_settings_page'
	);
} );

/**
 * Render the heading at the top of each section.
 *
 * @param mixed[] $args
 */
function render_section_heading( $args = [] ) {
	echo '<p>This is ' . esc_html( strtolower( $args['title'] ) ) . '.</p>';
}

/**
 * Render a generic input.
 *
 * @param mixed[] $args Additional arguments passed to the input.
 */
function render_input( $args = [] ) {
	$id = ! empty( $args['label_for'] ) ? $args['label_for'] : false;

	if ( ! $id ) {
		esc_html_e( 'Invalid ID provided.' );
		return;
	}

	$option = get_option( 'wp-admin-tabs-demo-option', [] );

	printf(
		'<input name="wp-admin-tabs-demo-option[%1$s]" id="%1$s" type="text" class="regular-text" value="%2$s" />',
		esc_attr( $id ),
		esc_attr( ! empty( $option[ $id ] ) ? $option[ $id ] : '' )
	);
}

/**
 * Render a generic textarea.
 *
 * @param mixed[] $args Additional arguments passed to the textarea.
 */
function render_textarea( $args = [] ) {
	$id = ! empty( $args['label_for'] ) ? $args['label_for'] : false;

	if ( ! $id ) {
		esc_html_e( 'Invalid ID provided.' );
		return;
	}

	$option = get_option( 'wp-admin-tabs-demo-option', [] );

	printf(
		'<textarea name="wp-admin-tabs-demo-option[%1$s]" id="%1$s" class="regular-text">%2$s</textarea>',
		esc_attr( $id ),
		esc_attr( ! empty( $option[ $id ] ) ? $option[ $id ] : '' )
	);
}

/**
 * Render the "classic" settings page.
 */
function render_classic_settings_page() {
?>
	<div class="wrap">
		<h1><?php esc_html_e( 'Settings Page Demo (Classic)' ); ?></h1>
		<form method="POST" action="<?php echo esc_attr( admin_url( 'options.php' ) ); ?>">
			<?php do_settings_sections( 'wp-admin-tabs-demo' ); ?>
			<?php settings_fields( 'wp-admin-tabs' ); ?>
			<?php submit_button(); ?>
		</form>
	</div>
<?php
}

/**
 * Render the tabbed version of the settings page.
 *
 * This uses the do_tabbed_settings_sections() function to create a tabbed settings page.
 */
function render_tabbed_settings_page() {
?>
	<div class="wrap">
		<h1><?php esc_html_e( 'Settings Page Demo (Tabbed)' ); ?></h1>
		<form method="POST" action="<?php echo esc_attr( admin_url( 'options.php' ) ); ?>">
			<?php do_tabbed_settings_sections( 'wp-admin-tabs-demo' ); ?>
			<?php settings_fields( 'wp-admin-tabs' ); ?>
			<?php submit_button(); ?>
		</form>
	</div>
<?php
}
